blob: ea724ab44b2e05c406032c99cfd6735acfe4c489 [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); // "({..})"
283
284 /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
285 virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
286 SourceLocation LAngleBracketLoc, TypeTy *Ty,
287 SourceLocation RAngleBracketLoc,
288 SourceLocation LParenLoc, ExprTy *E,
289 SourceLocation RParenLoc);
290
291 /// ParseCXXBoolLiteral - Parse {true,false} literals.
292 virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc,
293 tok::TokenKind Kind);
294private:
295 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
296 // functions and arrays to their respective pointers (C99 6.3.2.1).
297 void UsualUnaryConversions(Expr *&expr);
298
299 // DefaultFunctionArrayConversion - converts functions and arrays
300 // to their respective pointers (C99 6.3.2.1).
301 void DefaultFunctionArrayConversion(Expr *&expr);
302
303 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
304 // operands and then handles various conversions that are common to binary
305 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
306 // routine returns the first non-arithmetic type found. The client is
307 // responsible for emitting appropriate error diagnostics.
308 void UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr);
309
310 enum AssignmentCheckResult {
311 Compatible,
312 Incompatible,
313 PointerFromInt,
314 IntFromPointer,
315 IncompatiblePointer,
316 CompatiblePointerDiscardsQualifiers
317 };
318 // CheckAssignmentConstraints - Perform type checking for assignment,
319 // argument passing, variable initialization, and function return values.
320 // This routine is only used by the following two methods. C99 6.5.16.
321 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
322
323 // CheckSingleAssignmentConstraints - Currently used by ParseCallExpr,
324 // CheckAssignmentOperands, and ParseReturnStmt. Prior to type checking,
325 // this routine performs the default function/array converions.
326 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
327 Expr *&rExpr);
328 // CheckCompoundAssignmentConstraints - Type check without performing any
329 // conversions. For compound assignments, the "Check...Operands" methods
330 // perform the necessary conversions.
331 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
332 QualType rhs);
333
334 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
335 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
336 QualType rhsType);
337
338 /// the following "Check" methods will return a valid/converted QualType
339 /// or a null QualType (indicating an error diagnostic was issued).
340
341 /// type checking binary operators (subroutines of ParseBinOp).
342 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
343 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
344 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
345 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
346 inline QualType CheckRemainderOperands( // C99 6.5.5
347 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
348 inline QualType CheckAdditionOperands( // C99 6.5.6
349 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
350 inline QualType CheckSubtractionOperands( // C99 6.5.6
351 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
352 inline QualType CheckShiftOperands( // C99 6.5.7
353 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
354 inline QualType CheckRelationalOperands( // C99 6.5.8
355 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
356 inline QualType CheckEqualityOperands( // C99 6.5.9
357 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
358 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
359 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
360 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
361 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
362 // CheckAssignmentOperands is used for both simple and compound assignment.
363 // For simple assignment, pass both expressions and a null converted type.
364 // For compound assignment, pass both expressions and the converted type.
365 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
366 Expr *lex, Expr *rex, SourceLocation OpLoc, QualType convertedType);
367 inline QualType CheckCommaOperands( // C99 6.5.17
368 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
369 inline QualType CheckConditionalOperands( // C99 6.5.15
370 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
371
372 /// type checking unary operators (subroutines of ParseUnaryOp).
373 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
374 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
375 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
376 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
377 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
378 bool isSizeof);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000379
380 /// type checking primary expressions.
381 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
382 IdentifierInfo &Comp, SourceLocation CmpLoc);
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384 /// C99: 6.7.5p3: Used by ParseDeclarator/ParseField to make sure we have
385 /// a constant expression of type int with a value greater than zero. If the
386 /// array has an incomplete type or a valid constant size, return false,
387 /// otherwise emit a diagnostic and return true.
388 bool VerifyConstantArrayType(const ArrayType *ary, SourceLocation loc);
389};
390
391
392} // end namespace clang
393
394#endif