2006-11-09 14:54:47 +08:00
|
|
|
//===--- Sema.h - Semantic Analysis & AST Building --------------*- C++ -*-===//
|
2006-11-03 14:42:29 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-11-09 14:54:47 +08:00
|
|
|
// This file defines the Sema class, which performs semantic analysis and
|
|
|
|
// builds ASTs.
|
2006-11-03 14:42:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-09 14:54:47 +08:00
|
|
|
#ifndef LLVM_CLANG_AST_SEMA_H
|
|
|
|
#define LLVM_CLANG_AST_SEMA_H
|
2006-11-03 14:42:29 +08:00
|
|
|
|
|
|
|
#include "clang/Parse/Action.h"
|
|
|
|
#include <vector>
|
2006-11-10 13:17:58 +08:00
|
|
|
#include <string>
|
2006-11-03 14:42:29 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace clang {
|
2006-11-10 14:20:45 +08:00
|
|
|
class ASTContext;
|
2006-11-03 14:42:29 +08:00
|
|
|
class Preprocessor;
|
|
|
|
class Decl;
|
2007-04-03 06:55:05 +08:00
|
|
|
class Expr;
|
2007-01-21 15:42:07 +08:00
|
|
|
class VarDecl;
|
2007-01-28 03:27:06 +08:00
|
|
|
class TypedefDecl;
|
|
|
|
class FunctionDecl;
|
2007-04-06 06:36:20 +08:00
|
|
|
class QualType;
|
2006-11-20 14:49:47 +08:00
|
|
|
class LangOptions;
|
2007-01-21 15:42:07 +08:00
|
|
|
class DeclaratorChunk;
|
2007-03-06 09:09:46 +08:00
|
|
|
class LexerToken;
|
2006-11-03 14:42:29 +08:00
|
|
|
|
2006-11-09 14:54:47 +08:00
|
|
|
/// Sema - This implements semantic analysis and AST building for C.
|
|
|
|
class Sema : public Action {
|
2007-02-28 09:22:02 +08:00
|
|
|
Preprocessor &PP;
|
|
|
|
|
2006-11-10 14:20:45 +08:00
|
|
|
ASTContext &Context;
|
2006-11-03 14:42:29 +08:00
|
|
|
|
2006-11-21 09:21:07 +08:00
|
|
|
/// CurFunctionDecl - If inside of a function body, this contains a pointer to
|
|
|
|
/// the function decl for the function being parsed.
|
|
|
|
FunctionDecl *CurFunctionDecl;
|
|
|
|
|
2006-11-03 14:42:29 +08:00
|
|
|
/// LastInGroupList - This vector is populated when there are multiple
|
|
|
|
/// declarators in a single decl group (e.g. "int A, B, C"). In this case,
|
|
|
|
/// all but the last decl will be entered into this. This is used by the
|
|
|
|
/// ASTStreamer.
|
|
|
|
std::vector<Decl*> &LastInGroupList;
|
|
|
|
public:
|
2007-03-01 03:32:13 +08:00
|
|
|
Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
|
2006-11-03 14:42:29 +08:00
|
|
|
|
2006-11-20 14:49:47 +08:00
|
|
|
const LangOptions &getLangOptions() const;
|
|
|
|
|
2007-03-24 06:27:02 +08:00
|
|
|
/// always returns true, which simplifies error handling (i.e. less code).
|
|
|
|
bool Diag(SourceLocation Loc, unsigned DiagID,
|
2006-11-10 13:17:58 +08:00
|
|
|
const std::string &Msg = std::string());
|
2007-03-24 06:27:02 +08:00
|
|
|
bool Diag(const LexerToken &Tok, unsigned DiagID,
|
2007-03-06 09:09:46 +08:00
|
|
|
const std::string &M = std::string());
|
2007-04-06 06:36:20 +08:00
|
|
|
bool Diag(SourceLocation Loc, unsigned DiagID, QualType t);
|
2006-11-10 13:17:58 +08:00
|
|
|
|
2006-11-12 06:59:23 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Type Analysis / Processing: SemaType.cpp.
|
|
|
|
//
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType GetTypeForDeclarator(Declarator &D, Scope *S);
|
2006-11-12 06:59:23 +08:00
|
|
|
|
2006-11-19 09:48:02 +08:00
|
|
|
virtual TypeResult ParseTypeName(Scope *S, Declarator &D);
|
2006-11-12 06:59:23 +08:00
|
|
|
|
2006-12-02 14:47:41 +08:00
|
|
|
virtual TypeResult ParseParamDeclaratorType(Scope *S, Declarator &D);
|
2007-03-22 05:08:52 +08:00
|
|
|
private:
|
2006-11-03 14:42:29 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
2006-11-10 13:29:30 +08:00
|
|
|
// Symbol table / Decl tracking callbacks: SemaDecl.cpp.
|
2006-11-03 14:42:29 +08:00
|
|
|
//
|
2006-11-20 09:29:42 +08:00
|
|
|
virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
|
|
|
|
DeclTy *LastInGroup);
|
2007-01-21 15:42:07 +08:00
|
|
|
virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
|
2006-11-21 09:21:07 +08:00
|
|
|
virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual void PopScope(SourceLocation Loc, Scope *S);
|
2007-01-28 03:27:06 +08:00
|
|
|
|
2006-11-19 10:43:37 +08:00
|
|
|
/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
|
|
|
|
/// no declarator (e.g. "struct foo;") is parsed.
|
|
|
|
virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
|
|
|
|
|
2007-01-24 04:11:08 +08:00
|
|
|
virtual DeclTy *ParseTag(Scope *S, unsigned TagType, TagKind TK,
|
2007-01-23 12:08:05 +08:00
|
|
|
SourceLocation KWLoc, IdentifierInfo *Name,
|
|
|
|
SourceLocation NameLoc);
|
2007-01-24 07:42:53 +08:00
|
|
|
virtual DeclTy *ParseField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
|
|
|
|
Declarator &D, ExprTy *BitfieldWidth);
|
|
|
|
virtual void ParseRecordBody(SourceLocation RecLoc, DeclTy *TagDecl,
|
|
|
|
DeclTy **Fields, unsigned NumFields);
|
2007-01-25 15:29:02 +08:00
|
|
|
virtual DeclTy *ParseEnumConstant(Scope *S, DeclTy *EnumDecl,
|
|
|
|
SourceLocation IdLoc, IdentifierInfo *Id,
|
|
|
|
SourceLocation EqualLoc, ExprTy *Val);
|
|
|
|
virtual void ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
|
|
|
|
DeclTy **Elements, unsigned NumElements);
|
2007-03-15 05:52:03 +08:00
|
|
|
private:
|
|
|
|
/// Subroutines of ParseDeclarator()...
|
|
|
|
TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D);
|
|
|
|
TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, Decl *Old);
|
|
|
|
FunctionDecl *MergeFunctionDecl(FunctionDecl *New, Decl *Old);
|
|
|
|
VarDecl *MergeVarDecl(VarDecl *New, Decl *Old);
|
2007-03-22 05:08:52 +08:00
|
|
|
/// AddTopLevelDecl - called after the decl has been fully processed.
|
|
|
|
/// Allows for bookkeeping and post-processing of each declaration.
|
|
|
|
void AddTopLevelDecl(Decl *current, Decl *last);
|
2007-03-16 08:33:25 +08:00
|
|
|
|
2007-03-15 05:52:03 +08:00
|
|
|
/// More parsing and symbol table subroutines...
|
|
|
|
VarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
|
|
|
|
Scope *FnBodyScope);
|
|
|
|
Decl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI, SourceLocation IdLoc,
|
|
|
|
Scope *S);
|
|
|
|
Decl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
|
|
|
|
Decl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
|
|
|
|
Scope *S);
|
2007-01-24 07:42:53 +08:00
|
|
|
|
2006-11-03 14:42:29 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
2006-11-10 13:29:30 +08:00
|
|
|
// Statement Parsing Callbacks: SemaStmt.cpp.
|
2007-03-15 05:52:03 +08:00
|
|
|
public:
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
|
|
|
|
StmtTy **Elts, unsigned NumElts);
|
|
|
|
virtual StmtResult ParseExprStmt(ExprTy *Expr) {
|
|
|
|
return Expr; // Exprs are Stmts.
|
|
|
|
}
|
2006-11-05 08:19:50 +08:00
|
|
|
virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
|
|
|
|
SourceLocation DotDotDotLoc, ExprTy *RHSVal,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt);
|
|
|
|
virtual StmtResult ParseDefaultStmt(SourceLocation DefaultLoc,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt);
|
|
|
|
virtual StmtResult ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt);
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
|
|
|
|
StmtTy *ThenVal, SourceLocation ElseLoc,
|
|
|
|
StmtTy *ElseVal);
|
2006-11-05 04:59:27 +08:00
|
|
|
virtual StmtResult ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond,
|
|
|
|
StmtTy *Body);
|
2006-11-05 04:40:44 +08:00
|
|
|
virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
|
|
|
|
StmtTy *Body);
|
|
|
|
virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
|
|
|
|
SourceLocation WhileLoc, ExprTy *Cond);
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
virtual StmtResult ParseForStmt(SourceLocation ForLoc,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
StmtTy *First, ExprTy *Second, ExprTy *Third,
|
|
|
|
SourceLocation RParenLoc, StmtTy *Body);
|
2006-11-05 09:46:01 +08:00
|
|
|
virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc,
|
|
|
|
SourceLocation LabelLoc,
|
|
|
|
IdentifierInfo *LabelII);
|
|
|
|
virtual StmtResult ParseIndirectGotoStmt(SourceLocation GotoLoc,
|
|
|
|
SourceLocation StarLoc,
|
|
|
|
ExprTy *DestExp);
|
2006-11-10 13:17:58 +08:00
|
|
|
virtual StmtResult ParseContinueStmt(SourceLocation ContinueLoc,
|
|
|
|
Scope *CurScope);
|
|
|
|
virtual StmtResult ParseBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
|
2006-11-05 04:18:38 +08:00
|
|
|
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
|
|
|
|
ExprTy *RetValExp);
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
2006-11-10 13:29:30 +08:00
|
|
|
// Expression Parsing Callbacks: SemaExpr.cpp.
|
2006-11-03 14:42:29 +08:00
|
|
|
|
|
|
|
// Primary Expressions.
|
2006-11-20 14:49:47 +08:00
|
|
|
virtual ExprResult ParseIdentifierExpr(Scope *S, SourceLocation Loc,
|
|
|
|
IdentifierInfo &II,
|
|
|
|
bool HasTrailingLParen);
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc,
|
|
|
|
tok::TokenKind Kind);
|
2007-03-06 09:09:46 +08:00
|
|
|
virtual ExprResult ParseNumericConstant(const LexerToken &);
|
2007-04-27 04:39:23 +08:00
|
|
|
virtual ExprResult ParseCharacterConstant(const LexerToken &);
|
2006-11-03 14:42:29 +08:00
|
|
|
virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
|
|
|
|
ExprTy *Val);
|
2006-11-09 14:32:27 +08:00
|
|
|
|
2007-02-22 07:46:25 +08:00
|
|
|
/// ParseStringLiteral - The specified tokens were lexed as pasted string
|
2006-11-09 14:32:27 +08:00
|
|
|
/// fragments (e.g. "foo" "bar" L"baz").
|
2007-02-22 07:46:25 +08:00
|
|
|
virtual ExprResult ParseStringLiteral(const LexerToken *Toks, unsigned NumToks);
|
2006-11-09 14:32:27 +08:00
|
|
|
|
2006-11-03 14:42:29 +08:00
|
|
|
// Binary/Unary Operators. 'Tok' is the token for the operator.
|
|
|
|
virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
|
|
|
|
ExprTy *Input);
|
|
|
|
virtual ExprResult
|
|
|
|
ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
|
|
|
|
SourceLocation LParenLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RParenLoc);
|
|
|
|
|
|
|
|
virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc,
|
|
|
|
tok::TokenKind Kind, ExprTy *Input);
|
|
|
|
|
|
|
|
virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
|
|
|
|
ExprTy *Idx, SourceLocation RLoc);
|
|
|
|
virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
|
|
|
|
tok::TokenKind OpKind,
|
|
|
|
SourceLocation MemberLoc,
|
|
|
|
IdentifierInfo &Member);
|
|
|
|
|
|
|
|
/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
|
|
|
|
/// This provides the location of the left/right parens and a list of comma
|
|
|
|
/// locations.
|
|
|
|
virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
|
|
|
|
ExprTy **Args, unsigned NumArgs,
|
|
|
|
SourceLocation *CommaLocs,
|
|
|
|
SourceLocation RParenLoc);
|
|
|
|
|
|
|
|
virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RParenLoc, ExprTy *Op);
|
|
|
|
|
|
|
|
virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
|
|
|
|
ExprTy *LHS,ExprTy *RHS);
|
|
|
|
|
|
|
|
/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
|
|
|
|
/// in the case of a the GNU conditional expr extension.
|
|
|
|
virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
|
|
|
|
SourceLocation ColonLoc,
|
|
|
|
ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
|
2006-12-05 02:06:35 +08:00
|
|
|
|
|
|
|
/// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
|
|
|
|
virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
|
|
|
|
SourceLocation LAngleBracketLoc, TypeTy *Ty,
|
|
|
|
SourceLocation RAngleBracketLoc,
|
|
|
|
SourceLocation LParenLoc, ExprTy *E,
|
|
|
|
SourceLocation RParenLoc);
|
2007-02-13 09:51:42 +08:00
|
|
|
|
|
|
|
/// ParseCXXBoolLiteral - Parse {true,false} literals.
|
|
|
|
virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc,
|
2007-02-14 04:09:46 +08:00
|
|
|
tok::TokenKind Kind);
|
2007-03-22 05:08:52 +08:00
|
|
|
private:
|
2007-04-24 08:23:05 +08:00
|
|
|
QualType UsualUnaryConversion(QualType t); // C99 6.3
|
|
|
|
QualType UsualArithmeticConversions(QualType t1, QualType t2); // C99 6.3.1.8
|
|
|
|
|
2007-03-31 07:47:58 +08:00
|
|
|
/// the following "Check" methods will either return a well formed AST node
|
|
|
|
/// or will return true if the expressions didn't type check properly.
|
2007-04-20 07:00:49 +08:00
|
|
|
|
|
|
|
/// type checking binary operators (subroutines of ParseBinOp).
|
2007-04-24 08:23:05 +08:00
|
|
|
/// The unsigned arguments are really enums (BinaryOperator::Opcode)
|
|
|
|
ExprResult CheckMultiplicativeOperands( // C99 6.5.5
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckAdditiveOperands( // C99 6.5.6
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckShiftOperands( // C99 6.5.7
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckRelationalOperands( // C99 6.5.8
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckEqualityOperands( // C99 6.5.9
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckBitwiseOperands( // C99 6.5.[10...12]
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckLogicalOperands( // C99 6.5.[13,14]
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
2007-04-27 04:39:23 +08:00
|
|
|
ExprResult CheckAssignmentOperands( // C99 6.5.16
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
|
|
|
|
ExprResult CheckCommaOperands( // C99 6.5.17
|
|
|
|
Expr *lex, Expr *rex, SourceLocation OpLoc);
|
2007-03-31 07:47:58 +08:00
|
|
|
|
2007-04-20 07:00:49 +08:00
|
|
|
/// type checking unary operators (subroutines of ParseUnaryOp).
|
2007-04-24 08:23:05 +08:00
|
|
|
/// The unsigned arguments are really enums (UnaryOperator::Opcode)
|
|
|
|
ExprResult CheckIncrementDecrementOperand( // C99 6.5.3.1
|
2007-04-26 03:01:39 +08:00
|
|
|
Expr *op, SourceLocation loc, unsigned OpCode);
|
2007-04-24 08:23:05 +08:00
|
|
|
ExprResult CheckAddressOfOperand( // C99 6.5.3.2
|
2007-04-26 03:01:39 +08:00
|
|
|
Expr *op, SourceLocation loc);
|
2007-04-24 08:23:05 +08:00
|
|
|
ExprResult CheckIndirectionOperand( // C99 6.5.3.2
|
2007-04-26 03:01:39 +08:00
|
|
|
Expr *op, SourceLocation loc);
|
2007-04-24 08:23:05 +08:00
|
|
|
ExprResult CheckArithmeticOperand( // C99 6.5.3.3
|
2007-04-26 03:01:39 +08:00
|
|
|
Expr *op, SourceLocation OpLoc, unsigned OpCode);
|
2006-11-03 14:42:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace clang
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|