blob: b6d47971f0b5baa2de0039c88835b06a26c1d3d3 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.h - Semantic Analysis & AST Building --------------*- C++ -*-===//
2//
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//
10// This file defines the Sema class, which performs semantic analysis and
11// builds ASTs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_SEMA_H
16#define LLVM_CLANG_AST_SEMA_H
17
18#include "clang/Parse/Action.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include <vector>
22#include <string>
23
24namespace clang {
25 class ASTContext;
26 class Preprocessor;
27 class Decl;
28 class Expr;
29 class VarDecl;
30 class ParmVarDecl;
31 class TypedefDecl;
32 class FunctionDecl;
33 class QualType;
34 class LangOptions;
35 class DeclaratorChunk;
36 class Token;
37 class IntegerLiteral;
38 class ArrayType;
39 class LabelStmt;
40 class SwitchStmt;
Steve Naroff1b8a46c2007-07-27 22:15:19 +000041 class OCUVectorType;
Steve Naroff82113e32007-07-29 16:33:31 +000042 class TypedefDecl;
43
Chris Lattner4b009652007-07-25 00:24:17 +000044/// Sema - This implements semantic analysis and AST building for C.
45class Sema : public Action {
46 Preprocessor &PP;
47
48 ASTContext &Context;
49
50 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
51 /// the function decl for the function being parsed.
52 FunctionDecl *CurFunctionDecl;
53
54 /// LastInGroupList - This vector is populated when there are multiple
55 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
56 /// all but the last decl will be entered into this. This is used by the
57 /// ASTStreamer.
58 std::vector<Decl*> &LastInGroupList;
59
60 /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
61 /// it (which acts like the label decl in some ways). Forward referenced
62 /// labels have a LabelStmt created for them with a null location & SubStmt.
63 llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
64
65 llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
Steve Naroff82113e32007-07-29 16:33:31 +000066
67 /// OCUVectorDecls - This is a list all the OCU vector types. This allows
68 /// us to associate a raw vector type with one of the OCU type names.
69 /// This is only necessary for issuing pretty diagnostics.
70 llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls;
Chris Lattner4b009652007-07-25 00:24:17 +000071public:
72 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
73
74 const LangOptions &getLangOptions() const;
75
76 /// The primitive diagnostic helpers - always returns true, which simplifies
77 /// error handling (i.e. less code).
78 bool Diag(SourceLocation Loc, unsigned DiagID);
79 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
80 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
81 const std::string &Msg2);
82
83 /// More expressive diagnostic helpers for expressions (say that 6 times:-)
84 bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1);
85 bool Diag(SourceLocation Loc, unsigned DiagID,
86 SourceRange R1, SourceRange R2);
87 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
88 SourceRange R1);
89 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
90 SourceRange R1, SourceRange R2);
91 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
92 const std::string &Msg2, SourceRange R1);
93 bool Diag(SourceLocation Loc, unsigned DiagID,
94 const std::string &Msg1, const std::string &Msg2,
95 SourceRange R1, SourceRange R2);
96
97 //===--------------------------------------------------------------------===//
98 // Type Analysis / Processing: SemaType.cpp.
99 //
100 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
101
102 virtual TypeResult ParseTypeName(Scope *S, Declarator &D);
103
104 virtual TypeResult ParseParamDeclaratorType(Scope *S, Declarator &D);
105private:
106 //===--------------------------------------------------------------------===//
107 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
108 //
109 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
110 virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
111 DeclTy *LastInGroup);
112 virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
113
114 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
115 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
116 virtual void PopScope(SourceLocation Loc, Scope *S);
117
118 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
119 /// no declarator (e.g. "struct foo;") is parsed.
120 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
121
122 virtual DeclTy *ParseTag(Scope *S, unsigned TagType, TagKind TK,
123 SourceLocation KWLoc, IdentifierInfo *Name,
124 SourceLocation NameLoc, AttributeList *Attr);
125 virtual DeclTy *ParseField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
126 Declarator &D, ExprTy *BitfieldWidth);
127 virtual void ParseRecordBody(SourceLocation RecLoc, DeclTy *TagDecl,
128 DeclTy **Fields, unsigned NumFields);
129 virtual DeclTy *ParseEnumConstant(Scope *S, DeclTy *EnumDecl,
130 DeclTy *LastEnumConstant,
131 SourceLocation IdLoc, IdentifierInfo *Id,
132 SourceLocation EqualLoc, ExprTy *Val);
133 virtual void ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
134 DeclTy **Elements, unsigned NumElements);
135private:
136 /// Subroutines of ParseDeclarator()...
137 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, Decl *LastDeclarator);
138 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, Decl *Old);
139 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, Decl *Old);
140 VarDecl *MergeVarDecl(VarDecl *New, Decl *Old);
141 /// AddTopLevelDecl - called after the decl has been fully processed.
142 /// Allows for bookkeeping and post-processing of each declaration.
143 void AddTopLevelDecl(Decl *current, Decl *last);
144
145 /// More parsing and symbol table subroutines...
146 ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
147 Scope *FnBodyScope);
148 Decl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI, SourceLocation IdLoc,
149 Scope *S);
150 Decl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
151 Decl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
152 Scope *S);
153 // Decl attributes - this routine is the top level dispatcher.
154 void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
155 AttributeList *declarator_postfix);
156 void HandleDeclAttribute(Decl *New, AttributeList *rawAttr);
157
158 // HandleVectorTypeAttribute - this attribute is only applicable to
159 // integral and float scalars, although arrays, pointers, and function
160 // return values are allowed in conjunction with this construct. Aggregates
161 // with this attribute are invalid, even if they are of the same size as a
162 // corresponding scalar.
163 // The raw attribute should contain precisely 1 argument, the vector size
164 // for the variable, measured in bytes. If curType and rawAttr are well
165 // formed, this routine will return a new vector type.
166 QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr);
Steve Naroff82113e32007-07-29 16:33:31 +0000167 void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000168
169 //===--------------------------------------------------------------------===//
170 // Statement Parsing Callbacks: SemaStmt.cpp.
171public:
172 virtual StmtResult ParseExprStmt(ExprTy *Expr);
173
174 virtual StmtResult ParseNullStmt(SourceLocation SemiLoc);
175 virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
176 StmtTy **Elts, unsigned NumElts);
177 virtual StmtResult ParseDeclStmt(DeclTy *Decl);
178 virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
179 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
180 SourceLocation ColonLoc, StmtTy *SubStmt);
181 virtual StmtResult ParseDefaultStmt(SourceLocation DefaultLoc,
182 SourceLocation ColonLoc, StmtTy *SubStmt,
183 Scope *CurScope);
184 virtual StmtResult ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
185 SourceLocation ColonLoc, StmtTy *SubStmt);
186 virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
187 StmtTy *ThenVal, SourceLocation ElseLoc,
188 StmtTy *ElseVal);
189 virtual StmtResult StartSwitchStmt(ExprTy *Cond);
190 virtual StmtResult FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
191 ExprTy *Body);
192 virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
193 StmtTy *Body);
194 virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
195 SourceLocation WhileLoc, ExprTy *Cond);
196
197 virtual StmtResult ParseForStmt(SourceLocation ForLoc,
198 SourceLocation LParenLoc,
199 StmtTy *First, ExprTy *Second, ExprTy *Third,
200 SourceLocation RParenLoc, StmtTy *Body);
201 virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc,
202 SourceLocation LabelLoc,
203 IdentifierInfo *LabelII);
204 virtual StmtResult ParseIndirectGotoStmt(SourceLocation GotoLoc,
205 SourceLocation StarLoc,
206 ExprTy *DestExp);
207 virtual StmtResult ParseContinueStmt(SourceLocation ContinueLoc,
208 Scope *CurScope);
209 virtual StmtResult ParseBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
210
211 virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
212 ExprTy *RetValExp);
213
214 //===--------------------------------------------------------------------===//
215 // Expression Parsing Callbacks: SemaExpr.cpp.
216
217 // Primary Expressions.
218 virtual ExprResult ParseIdentifierExpr(Scope *S, SourceLocation Loc,
219 IdentifierInfo &II,
220 bool HasTrailingLParen);
221 virtual ExprResult ParsePreDefinedExpr(SourceLocation Loc,
222 tok::TokenKind Kind);
223 virtual ExprResult ParseNumericConstant(const Token &);
224 virtual ExprResult ParseCharacterConstant(const Token &);
225 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
226 ExprTy *Val);
227
228 /// ParseStringLiteral - The specified tokens were lexed as pasted string
229 /// fragments (e.g. "foo" "bar" L"baz").
230 virtual ExprResult ParseStringLiteral(const Token *Toks, unsigned NumToks);
231
232 // Binary/Unary Operators. 'Tok' is the token for the operator.
233 virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
234 ExprTy *Input);
235 virtual ExprResult
236 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
237 SourceLocation LParenLoc, TypeTy *Ty,
238 SourceLocation RParenLoc);
239
240 virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc,
241 tok::TokenKind Kind, ExprTy *Input);
242
243 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
244 ExprTy *Idx, SourceLocation RLoc);
245 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
246 tok::TokenKind OpKind,
247 SourceLocation MemberLoc,
248 IdentifierInfo &Member);
249
250 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
251 /// This provides the location of the left/right parens and a list of comma
252 /// locations.
253 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
254 ExprTy **Args, unsigned NumArgs,
255 SourceLocation *CommaLocs,
256 SourceLocation RParenLoc);
257
258 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
259 SourceLocation RParenLoc, ExprTy *Op);
260
261 virtual ExprResult ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
262 SourceLocation RParenLoc, ExprTy *Op);
263
264 virtual ExprResult ParseInitList(SourceLocation LParenLoc,
265 ExprTy **InitList, unsigned NumInit,
266 SourceLocation RParenLoc);
267
268 virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
269 ExprTy *LHS,ExprTy *RHS);
270
271 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
272 /// in the case of a the GNU conditional expr extension.
273 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
274 SourceLocation ColonLoc,
275 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
276
277 /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
278 virtual ExprResult ParseAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc,
279 IdentifierInfo *LabelII);
280
281 virtual ExprResult ParseStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt,
282 SourceLocation RPLoc); // "({..})"
Steve Naroff63bad2d2007-08-01 22:05:33 +0000283
284 // __builtin_types_compatible_p(type1, type2)
Steve Naroff5b528922007-08-01 23:45:51 +0000285 virtual ExprResult ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +0000286 TypeTy *arg1, TypeTy *arg2,
287 SourceLocation RPLoc);
Steve Naroff93c53012007-08-03 21:21:27 +0000288
289 // __builtin_choose_expr(constExpr, expr1, expr2)
290 virtual ExprResult ParseChooseExpr(SourceLocation BuiltinLoc,
291 ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
292 SourceLocation RPLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000293
294 /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
295 virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
296 SourceLocation LAngleBracketLoc, TypeTy *Ty,
297 SourceLocation RAngleBracketLoc,
298 SourceLocation LParenLoc, ExprTy *E,
299 SourceLocation RParenLoc);
300
301 /// ParseCXXBoolLiteral - Parse {true,false} literals.
302 virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc,
303 tok::TokenKind Kind);
304private:
305 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
306 // functions and arrays to their respective pointers (C99 6.3.2.1).
307 void UsualUnaryConversions(Expr *&expr);
308
309 // DefaultFunctionArrayConversion - converts functions and arrays
310 // to their respective pointers (C99 6.3.2.1).
311 void DefaultFunctionArrayConversion(Expr *&expr);
312
313 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
314 // operands and then handles various conversions that are common to binary
315 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
316 // routine returns the first non-arithmetic type found. The client is
317 // responsible for emitting appropriate error diagnostics.
318 void UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr);
319
320 enum AssignmentCheckResult {
321 Compatible,
322 Incompatible,
323 PointerFromInt,
324 IntFromPointer,
325 IncompatiblePointer,
326 CompatiblePointerDiscardsQualifiers
327 };
328 // CheckAssignmentConstraints - Perform type checking for assignment,
329 // argument passing, variable initialization, and function return values.
330 // This routine is only used by the following two methods. C99 6.5.16.
331 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
332
333 // CheckSingleAssignmentConstraints - Currently used by ParseCallExpr,
334 // CheckAssignmentOperands, and ParseReturnStmt. Prior to type checking,
335 // this routine performs the default function/array converions.
336 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
337 Expr *&rExpr);
338 // CheckCompoundAssignmentConstraints - Type check without performing any
339 // conversions. For compound assignments, the "Check...Operands" methods
340 // perform the necessary conversions.
341 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
342 QualType rhs);
343
344 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
345 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
346 QualType rhsType);
347
348 /// the following "Check" methods will return a valid/converted QualType
349 /// or a null QualType (indicating an error diagnostic was issued).
350
351 /// type checking binary operators (subroutines of ParseBinOp).
352 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
353 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
354 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
355 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
356 inline QualType CheckRemainderOperands( // C99 6.5.5
357 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
358 inline QualType CheckAdditionOperands( // C99 6.5.6
359 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
360 inline QualType CheckSubtractionOperands( // C99 6.5.6
361 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
362 inline QualType CheckShiftOperands( // C99 6.5.7
363 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
364 inline QualType CheckRelationalOperands( // C99 6.5.8
365 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
366 inline QualType CheckEqualityOperands( // C99 6.5.9
367 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
368 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
369 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
370 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
371 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
372 // CheckAssignmentOperands is used for both simple and compound assignment.
373 // For simple assignment, pass both expressions and a null converted type.
374 // For compound assignment, pass both expressions and the converted type.
375 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
376 Expr *lex, Expr *rex, SourceLocation OpLoc, QualType convertedType);
377 inline QualType CheckCommaOperands( // C99 6.5.17
378 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
379 inline QualType CheckConditionalOperands( // C99 6.5.15
380 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
381
382 /// type checking unary operators (subroutines of ParseUnaryOp).
383 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
384 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
385 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
386 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
387 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
388 bool isSizeof);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000389
390 /// type checking primary expressions.
391 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
392 IdentifierInfo &Comp, SourceLocation CmpLoc);
393
Chris Lattner4b009652007-07-25 00:24:17 +0000394 /// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
395 /// a constant expression of type int with a value greater than zero. If the
396 /// array has an incomplete type or a valid constant size, return false,
397 /// otherwise emit a diagnostic and return true.
398 bool VerifyConstantArrayType(const ArrayType *ary, SourceLocation loc);
399};
400
401
402} // end namespace clang
403
404#endif