blob: 02696ac82e292041104c6e9ed5a5b66121f43bb3 [file] [log] [blame]
Chris Lattnercc67ec12006-11-09 06:54:47 +00001//===--- Sema.h - Semantic Analysis & AST Building --------------*- C++ -*-===//
Chris Lattner7cee11f2006-11-03 06:42:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercc67ec12006-11-09 06:54:47 +000010// This file defines the Sema class, which performs semantic analysis and
11// builds ASTs.
Chris Lattner7cee11f2006-11-03 06:42:29 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnercc67ec12006-11-09 06:54:47 +000015#ifndef LLVM_CLANG_AST_SEMA_H
16#define LLVM_CLANG_AST_SEMA_H
Chris Lattner7cee11f2006-11-03 06:42:29 +000017
18#include "clang/Parse/Action.h"
19#include <vector>
Chris Lattnereaafe1222006-11-10 05:17:58 +000020#include <string>
Chris Lattner7cee11f2006-11-03 06:42:29 +000021
22namespace llvm {
23namespace clang {
Chris Lattnercb6a3822006-11-10 06:20:45 +000024 class ASTContext;
Chris Lattner7cee11f2006-11-03 06:42:29 +000025 class Preprocessor;
26 class Decl;
Steve Naroff78403362007-04-02 22:55:05 +000027 class Expr;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +000028 class VarDecl;
Chris Lattner01564d92007-01-27 19:27:06 +000029 class TypedefDecl;
30 class FunctionDecl;
Steve Naroffe5aa9be2007-04-05 22:36:20 +000031 class QualType;
Chris Lattnerac18be92006-11-20 06:49:47 +000032 class LangOptions;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +000033 class DeclaratorChunk;
Steve Naroff8160ea22007-03-06 01:09:46 +000034 class LexerToken;
Steve Naroff35d85152007-05-07 00:24:15 +000035 class IntegerLiteral;
Steve Naroff8eeeb132007-05-08 21:09:37 +000036 class ArrayType;
Chris Lattner7cee11f2006-11-03 06:42:29 +000037
Chris Lattnercc67ec12006-11-09 06:54:47 +000038/// Sema - This implements semantic analysis and AST building for C.
39class Sema : public Action {
Steve Naroff38d31b42007-02-28 01:22:02 +000040 Preprocessor &PP;
41
Chris Lattnercb6a3822006-11-10 06:20:45 +000042 ASTContext &Context;
Chris Lattner7cee11f2006-11-03 06:42:29 +000043
Chris Lattner229ce602006-11-21 01:21:07 +000044 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
45 /// the function decl for the function being parsed.
46 FunctionDecl *CurFunctionDecl;
47
Chris Lattner7cee11f2006-11-03 06:42:29 +000048 /// LastInGroupList - This vector is populated when there are multiple
49 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
50 /// all but the last decl will be entered into this. This is used by the
51 /// ASTStreamer.
52 std::vector<Decl*> &LastInGroupList;
Steve Naroff35d85152007-05-07 00:24:15 +000053
54 /// Constant for "1"
55 IntegerLiteral *constantOne;
Chris Lattner7cee11f2006-11-03 06:42:29 +000056public:
Steve Naroff2c055d22007-02-28 19:32:13 +000057 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
Chris Lattner7cee11f2006-11-03 06:42:29 +000058
Chris Lattnerac18be92006-11-20 06:49:47 +000059 const LangOptions &getLangOptions() const;
60
Steve Narofff1e53692007-03-23 22:27:02 +000061 /// always returns true, which simplifies error handling (i.e. less code).
62 bool Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattnereaafe1222006-11-10 05:17:58 +000063 const std::string &Msg = std::string());
Steve Narofff1e53692007-03-23 22:27:02 +000064 bool Diag(const LexerToken &Tok, unsigned DiagID,
Steve Naroff8160ea22007-03-06 01:09:46 +000065 const std::string &M = std::string());
Steve Naroffe5aa9be2007-04-05 22:36:20 +000066 bool Diag(SourceLocation Loc, unsigned DiagID, QualType t);
Chris Lattnereaafe1222006-11-10 05:17:58 +000067
Chris Lattner7cee11f2006-11-03 06:42:29 +000068 //===--------------------------------------------------------------------===//
Chris Lattnerf84a79c2006-11-11 22:59:23 +000069 // Type Analysis / Processing: SemaType.cpp.
70 //
Steve Naroffe5aa9be2007-04-05 22:36:20 +000071 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
Chris Lattnerf84a79c2006-11-11 22:59:23 +000072
Chris Lattner33e8a552006-11-19 01:48:02 +000073 virtual TypeResult ParseTypeName(Scope *S, Declarator &D);
Chris Lattnerf84a79c2006-11-11 22:59:23 +000074
Chris Lattner216d8652006-12-02 06:47:41 +000075 virtual TypeResult ParseParamDeclaratorType(Scope *S, Declarator &D);
Steve Naroff26c8ea52007-03-21 21:08:52 +000076private:
Chris Lattnerf84a79c2006-11-11 22:59:23 +000077 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +000078 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
Chris Lattner7cee11f2006-11-03 06:42:29 +000079 //
Chris Lattner2ebe4bb2006-11-20 01:29:42 +000080 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
Chris Lattner7cee11f2006-11-03 06:42:29 +000081 virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
82 DeclTy *LastInGroup);
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +000083 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
Chris Lattner229ce602006-11-21 01:21:07 +000084 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
Chris Lattner7cee11f2006-11-03 06:42:29 +000085 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner01564d92007-01-27 19:27:06 +000086
Chris Lattner200bdc32006-11-19 02:43:37 +000087 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
88 /// no declarator (e.g. "struct foo;") is parsed.
89 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
90
Chris Lattner7b9ace62007-01-23 20:11:08 +000091 virtual DeclTy *ParseTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +000092 SourceLocation KWLoc, IdentifierInfo *Name,
93 SourceLocation NameLoc);
Chris Lattner1300fb92007-01-23 23:42:53 +000094 virtual DeclTy *ParseField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
95 Declarator &D, ExprTy *BitfieldWidth);
96 virtual void ParseRecordBody(SourceLocation RecLoc, DeclTy *TagDecl,
97 DeclTy **Fields, unsigned NumFields);
Chris Lattnerc1915e22007-01-25 07:29:02 +000098 virtual DeclTy *ParseEnumConstant(Scope *S, DeclTy *EnumDecl,
99 SourceLocation IdLoc, IdentifierInfo *Id,
100 SourceLocation EqualLoc, ExprTy *Val);
101 virtual void ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
102 DeclTy **Elements, unsigned NumElements);
Steve Naroff3273c222007-03-14 21:52:03 +0000103private:
104 /// Subroutines of ParseDeclarator()...
105 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D);
106 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, Decl *Old);
107 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, Decl *Old);
108 VarDecl *MergeVarDecl(VarDecl *New, Decl *Old);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000109 /// AddTopLevelDecl - called after the decl has been fully processed.
110 /// Allows for bookkeeping and post-processing of each declaration.
111 void AddTopLevelDecl(Decl *current, Decl *last);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000112
Steve Naroff3273c222007-03-14 21:52:03 +0000113 /// More parsing and symbol table subroutines...
114 VarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
115 Scope *FnBodyScope);
116 Decl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI, SourceLocation IdLoc,
117 Scope *S);
118 Decl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
119 Decl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
120 Scope *S);
Chris Lattner1300fb92007-01-23 23:42:53 +0000121
Chris Lattner7cee11f2006-11-03 06:42:29 +0000122 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +0000123 // Statement Parsing Callbacks: SemaStmt.cpp.
Steve Naroff3273c222007-03-14 21:52:03 +0000124public:
Chris Lattner7cee11f2006-11-03 06:42:29 +0000125 virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
126 StmtTy **Elts, unsigned NumElts);
127 virtual StmtResult ParseExprStmt(ExprTy *Expr) {
Chris Lattner7cee11f2006-11-03 06:42:29 +0000128 return Expr; // Exprs are Stmts.
129 }
Chris Lattner6c0ff132006-11-05 00:19:50 +0000130 virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
131 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
132 SourceLocation ColonLoc, StmtTy *SubStmt);
133 virtual StmtResult ParseDefaultStmt(SourceLocation DefaultLoc,
134 SourceLocation ColonLoc, StmtTy *SubStmt);
135 virtual StmtResult ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
136 SourceLocation ColonLoc, StmtTy *SubStmt);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000137 virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
138 StmtTy *ThenVal, SourceLocation ElseLoc,
139 StmtTy *ElseVal);
Chris Lattnerf2174b62006-11-04 20:59:27 +0000140 virtual StmtResult ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond,
141 StmtTy *Body);
Chris Lattner85ed8732006-11-04 20:40:44 +0000142 virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
143 StmtTy *Body);
144 virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
145 SourceLocation WhileLoc, ExprTy *Cond);
146
Chris Lattner71e23ce2006-11-04 20:18:38 +0000147 virtual StmtResult ParseForStmt(SourceLocation ForLoc,
148 SourceLocation LParenLoc,
149 StmtTy *First, ExprTy *Second, ExprTy *Third,
150 SourceLocation RParenLoc, StmtTy *Body);
Chris Lattner16976d32006-11-05 01:46:01 +0000151 virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc,
152 SourceLocation LabelLoc,
153 IdentifierInfo *LabelII);
154 virtual StmtResult ParseIndirectGotoStmt(SourceLocation GotoLoc,
155 SourceLocation StarLoc,
156 ExprTy *DestExp);
Chris Lattnereaafe1222006-11-10 05:17:58 +0000157 virtual StmtResult ParseContinueStmt(SourceLocation ContinueLoc,
158 Scope *CurScope);
159 virtual StmtResult ParseBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
Chris Lattner71e23ce2006-11-04 20:18:38 +0000160
Chris Lattner7cee11f2006-11-03 06:42:29 +0000161 virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
162 ExprTy *RetValExp);
163
164 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +0000165 // Expression Parsing Callbacks: SemaExpr.cpp.
Chris Lattner7cee11f2006-11-03 06:42:29 +0000166
167 // Primary Expressions.
Chris Lattnerac18be92006-11-20 06:49:47 +0000168 virtual ExprResult ParseIdentifierExpr(Scope *S, SourceLocation Loc,
169 IdentifierInfo &II,
170 bool HasTrailingLParen);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000171 virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc,
172 tok::TokenKind Kind);
Steve Naroff8160ea22007-03-06 01:09:46 +0000173 virtual ExprResult ParseNumericConstant(const LexerToken &);
Steve Naroffae4143e2007-04-26 20:39:23 +0000174 virtual ExprResult ParseCharacterConstant(const LexerToken &);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000175 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
176 ExprTy *Val);
Chris Lattner697e5d62006-11-09 06:32:27 +0000177
Steve Naroffdf7855b2007-02-21 23:46:25 +0000178 /// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner697e5d62006-11-09 06:32:27 +0000179 /// fragments (e.g. "foo" "bar" L"baz").
Steve Naroffdf7855b2007-02-21 23:46:25 +0000180 virtual ExprResult ParseStringLiteral(const LexerToken *Toks, unsigned NumToks);
Chris Lattner697e5d62006-11-09 06:32:27 +0000181
Chris Lattner7cee11f2006-11-03 06:42:29 +0000182 // Binary/Unary Operators. 'Tok' is the token for the operator.
183 virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
184 ExprTy *Input);
185 virtual ExprResult
186 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
187 SourceLocation LParenLoc, TypeTy *Ty,
188 SourceLocation RParenLoc);
189
190 virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc,
191 tok::TokenKind Kind, ExprTy *Input);
192
193 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
194 ExprTy *Idx, SourceLocation RLoc);
195 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
196 tok::TokenKind OpKind,
197 SourceLocation MemberLoc,
198 IdentifierInfo &Member);
199
200 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
201 /// This provides the location of the left/right parens and a list of comma
202 /// locations.
203 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
204 ExprTy **Args, unsigned NumArgs,
205 SourceLocation *CommaLocs,
206 SourceLocation RParenLoc);
207
208 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
209 SourceLocation RParenLoc, ExprTy *Op);
210
211 virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
212 ExprTy *LHS,ExprTy *RHS);
213
214 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
215 /// in the case of a the GNU conditional expr extension.
216 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
217 SourceLocation ColonLoc,
218 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattner29375652006-12-04 18:06:35 +0000219
220 /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
221 virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
222 SourceLocation LAngleBracketLoc, TypeTy *Ty,
223 SourceLocation RAngleBracketLoc,
224 SourceLocation LParenLoc, ExprTy *E,
225 SourceLocation RParenLoc);
Bill Wendling4073ed52007-02-13 01:51:42 +0000226
227 /// ParseCXXBoolLiteral - Parse {true,false} literals.
228 virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc,
Bill Wendlingbf313b02007-02-13 20:09:46 +0000229 tok::TokenKind Kind);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000230private:
Steve Naroff1926c832007-04-24 00:23:05 +0000231 QualType UsualUnaryConversion(QualType t); // C99 6.3
232 QualType UsualArithmeticConversions(QualType t1, QualType t2); // C99 6.3.1.8
233
Steve Naroff17f76e02007-05-03 21:03:48 +0000234 enum AssignmentConversionResult {
235 Compatible,
236 Incompatible,
237 PointerFromInt,
238 IntFromPointer,
239 IncompatiblePointer
240 };
Steve Naroff218bc2b2007-05-04 21:54:46 +0000241 // Conversions for assignment, argument passing, initialization, and
242 // function return values. UsualAssignmentConversions is currently used by
243 // CheckSimpleAssignment, CheckCompoundAssignment and ParseCallExpr.
244 QualType UsualAssignmentConversions(QualType lhs, QualType rhs, // C99 6.5.16
Steve Naroff17f76e02007-05-03 21:03:48 +0000245 AssignmentConversionResult &r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000246
Steve Naroff8eeeb132007-05-08 21:09:37 +0000247 /// the following "Check" methods will return a valid/converted QualType
248 /// or a null QualType (indicating an error diagnostic was issued).
249
Steve Naroff47500512007-04-19 23:00:49 +0000250 /// type checking binary operators (subroutines of ParseBinOp).
Steve Naroff218bc2b2007-05-04 21:54:46 +0000251 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
252 Expr *lex, Expr *rex, SourceLocation OpLoc);
253 inline QualType CheckRemainderOperands( // C99 6.5.5
254 Expr *lex, Expr *rex, SourceLocation OpLoc);
255 inline QualType CheckAdditionOperands( // C99 6.5.6
256 Expr *lex, Expr *rex, SourceLocation OpLoc);
257 inline QualType CheckSubtractionOperands( // C99 6.5.6
258 Expr *lex, Expr *rex, SourceLocation OpLoc);
259 inline QualType CheckShiftOperands( // C99 6.5.7
260 Expr *lex, Expr *rex, SourceLocation OpLoc);
261 inline QualType CheckRelationalOperands( // C99 6.5.8
262 Expr *lex, Expr *rex, SourceLocation OpLoc);
263 inline QualType CheckEqualityOperands( // C99 6.5.9
264 Expr *lex, Expr *rex, SourceLocation OpLoc);
265 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
266 Expr *lex, Expr *rex, SourceLocation OpLoc);
267 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
268 Expr *lex, Expr *rex, SourceLocation OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000269 // CheckAssignmentOperands is used for both simple and compound assignment.
270 // For simple assignment, pass both expressions and a null converted type.
271 // For compound assignment, pass both expressions and the converted type.
272 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
273 Expr *lex, Expr *rex, SourceLocation OpLoc, QualType convertedType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000274 inline QualType CheckCommaOperands( // C99 6.5.17
Steve Naroffae4143e2007-04-26 20:39:23 +0000275 Expr *lex, Expr *rex, SourceLocation OpLoc);
Steve Naroff95af0132007-03-30 23:47:58 +0000276
Steve Naroff47500512007-04-19 23:00:49 +0000277 /// type checking unary operators (subroutines of ParseUnaryOp).
Steve Naroff35d85152007-05-07 00:24:15 +0000278 QualType CheckIncrementDecrementOperand( // C99 6.5.3.1
Steve Narofff633d092007-04-25 19:01:39 +0000279 Expr *op, SourceLocation loc);
Steve Naroff35d85152007-05-07 00:24:15 +0000280 QualType CheckAddressOfOperand( // C99 6.5.3.2
281 Expr *op, SourceLocation loc);
282 QualType CheckIndirectionOperand( // C99 6.5.3.2
Steve Narofff633d092007-04-25 19:01:39 +0000283 Expr *op, SourceLocation loc);
Steve Naroff8eeeb132007-05-08 21:09:37 +0000284
285 // C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
286 // a constant expression of type int with a value greater than zero.
287 bool isConstantArrayType(ArrayType *ary, SourceLocation loc);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000288};
289
290
291} // end namespace clang
292} // end namespace llvm
293
294#endif