blob: 465e3fb73a6ab3f5b3e6f9b1cbb017a8465b6625 [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
Chris Lattner3429a812007-08-23 05:46:52 +000024namespace llvm {
25 class APSInt;
26}
27
Chris Lattner4b009652007-07-25 00:24:17 +000028namespace clang {
29 class ASTContext;
30 class Preprocessor;
31 class Decl;
Steve Naroffd21bc0d2007-09-13 18:10:37 +000032 class ScopedDecl;
Chris Lattner4b009652007-07-25 00:24:17 +000033 class Expr;
Steve Naroff9091f3f2007-09-02 15:34:30 +000034 class InitListExpr;
Chris Lattner4b009652007-07-25 00:24:17 +000035 class VarDecl;
36 class ParmVarDecl;
37 class TypedefDecl;
38 class FunctionDecl;
39 class QualType;
Chris Lattner3496d522007-09-04 02:45:27 +000040 struct LangOptions;
41 struct DeclaratorChunk;
Chris Lattner4b009652007-07-25 00:24:17 +000042 class Token;
43 class IntegerLiteral;
44 class ArrayType;
45 class LabelStmt;
46 class SwitchStmt;
Steve Naroff1b8a46c2007-07-27 22:15:19 +000047 class OCUVectorType;
Steve Naroff82113e32007-07-29 16:33:31 +000048 class TypedefDecl;
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +000049 class ObjcInterfaceDecl;
Fariborz Jahaniandd243ef2007-09-29 17:04:06 +000050 class ObjcProtocolDecl;
Steve Naroff82113e32007-07-29 16:33:31 +000051
Chris Lattner4b009652007-07-25 00:24:17 +000052/// Sema - This implements semantic analysis and AST building for C.
53class Sema : public Action {
54 Preprocessor &PP;
55
56 ASTContext &Context;
57
58 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
59 /// the function decl for the function being parsed.
60 FunctionDecl *CurFunctionDecl;
61
62 /// LastInGroupList - This vector is populated when there are multiple
63 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
64 /// all but the last decl will be entered into this. This is used by the
65 /// ASTStreamer.
66 std::vector<Decl*> &LastInGroupList;
67
68 /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
69 /// it (which acts like the label decl in some ways). Forward referenced
70 /// labels have a LabelStmt created for them with a null location & SubStmt.
71 llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
72
73 llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
Steve Naroff82113e32007-07-29 16:33:31 +000074
75 /// OCUVectorDecls - This is a list all the OCU vector types. This allows
76 /// us to associate a raw vector type with one of the OCU type names.
77 /// This is only necessary for issuing pretty diagnostics.
78 llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls;
Chris Lattner2e64c072007-08-10 20:18:51 +000079
80 // Enum values used by KnownFunctionIDs (see below).
81 enum {
82 id_printf,
83 id_fprintf,
84 id_sprintf,
85 id_snprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000086 id_asprintf,
Ted Kremenek2d7e9532007-08-10 21:13:51 +000087 id_vsnprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000088 id_vasprintf,
89 id_vfprintf,
90 id_vsprintf,
91 id_vprintf,
92 id_num_known_functions
93 };
94
95 /// KnownFunctionIDs - This is a list of IdentifierInfo objects to a set
96 /// of known functions used by the semantic analysis to do various
97 /// kinds of checking (e.g. checking format string errors in printf calls).
98 /// This list is populated upon the creation of a Sema object.
99 IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ];
100
Chris Lattner4b009652007-07-25 00:24:17 +0000101public:
102 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
103
104 const LangOptions &getLangOptions() const;
105
106 /// The primitive diagnostic helpers - always returns true, which simplifies
107 /// error handling (i.e. less code).
108 bool Diag(SourceLocation Loc, unsigned DiagID);
109 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
110 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
111 const std::string &Msg2);
112
113 /// More expressive diagnostic helpers for expressions (say that 6 times:-)
114 bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1);
115 bool Diag(SourceLocation Loc, unsigned DiagID,
116 SourceRange R1, SourceRange R2);
117 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
118 SourceRange R1);
119 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
120 SourceRange R1, SourceRange R2);
121 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
122 const std::string &Msg2, SourceRange R1);
123 bool Diag(SourceLocation Loc, unsigned DiagID,
124 const std::string &Msg1, const std::string &Msg2,
125 SourceRange R1, SourceRange R2);
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000126
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000127 virtual void DeleteExpr(ExprTy *E);
128 virtual void DeleteStmt(StmtTy *S);
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130 //===--------------------------------------------------------------------===//
131 // Type Analysis / Processing: SemaType.cpp.
132 //
133 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
134
Steve Naroff0acc9c92007-09-15 18:49:24 +0000135 virtual TypeResult ActOnTypeName(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000136
Steve Naroff0acc9c92007-09-15 18:49:24 +0000137 virtual TypeResult ActOnParamDeclaratorType(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000138private:
139 //===--------------------------------------------------------------------===//
140 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
141 //
142 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000143 virtual DeclTy *ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000144 void AddInitializerToDecl(DeclTy *dcl, ExprTy *init);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
146
147 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
148 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
149 virtual void PopScope(SourceLocation Loc, Scope *S);
150
151 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
152 /// no declarator (e.g. "struct foo;") is parsed.
153 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
154
Steve Naroff0acc9c92007-09-15 18:49:24 +0000155 virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattner4b009652007-07-25 00:24:17 +0000156 SourceLocation KWLoc, IdentifierInfo *Name,
157 SourceLocation NameLoc, AttributeList *Attr);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000158 virtual DeclTy *ActOnField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
Chris Lattner4b009652007-07-25 00:24:17 +0000159 Declarator &D, ExprTy *BitfieldWidth);
Steve Naroffffeaa552007-09-14 23:09:53 +0000160
161 // This is used for both record definitions and ObjC interface declarations.
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000162 virtual void ActOnFields(Scope* S,
163 SourceLocation RecLoc, DeclTy *TagDecl,
Steve Naroffffeaa552007-09-14 23:09:53 +0000164 DeclTy **Fields, unsigned NumFields,
165 tok::ObjCKeywordKind *visibility = 0);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000166 virtual DeclTy *ActOnEnumConstant(Scope *S, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000167 DeclTy *LastEnumConstant,
168 SourceLocation IdLoc, IdentifierInfo *Id,
169 SourceLocation EqualLoc, ExprTy *Val);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000170 virtual void ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000171 DeclTy **Elements, unsigned NumElements);
172private:
Steve Naroff0acc9c92007-09-15 18:49:24 +0000173 /// Subroutines of ActOnDeclarator()...
Steve Naroff2591e1b2007-09-13 23:52:58 +0000174 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, ScopedDecl *LastDecl);
Steve Naroffcb597472007-09-13 21:41:19 +0000175 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *Old);
176 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, ScopedDecl *Old);
177 VarDecl *MergeVarDecl(VarDecl *New, ScopedDecl *Old);
Chris Lattner4b009652007-07-25 00:24:17 +0000178 /// AddTopLevelDecl - called after the decl has been fully processed.
179 /// Allows for bookkeeping and post-processing of each declaration.
180 void AddTopLevelDecl(Decl *current, Decl *last);
181
182 /// More parsing and symbol table subroutines...
183 ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
184 Scope *FnBodyScope);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000185 ScopedDecl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
186 SourceLocation IdLoc, Scope *S);
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000187 ObjcInterfaceDecl *getObjCInterfaceDecl(Scope *S,
188 IdentifierInfo *Id, SourceLocation IdLoc);
Fariborz Jahaniandd243ef2007-09-29 17:04:06 +0000189 ObjcProtocolDecl *getObjCProtocolDecl(Scope *S,
190 IdentifierInfo *Id, SourceLocation IdLoc);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000191 ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
Steve Narofff0c31dd2007-09-16 16:16:00 +0000192 ScopedDecl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
Chris Lattner4b009652007-07-25 00:24:17 +0000193 Scope *S);
194 // Decl attributes - this routine is the top level dispatcher.
195 void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
196 AttributeList *declarator_postfix);
197 void HandleDeclAttribute(Decl *New, AttributeList *rawAttr);
198
199 // HandleVectorTypeAttribute - this attribute is only applicable to
200 // integral and float scalars, although arrays, pointers, and function
201 // return values are allowed in conjunction with this construct. Aggregates
202 // with this attribute are invalid, even if they are of the same size as a
203 // corresponding scalar.
204 // The raw attribute should contain precisely 1 argument, the vector size
205 // for the variable, measured in bytes. If curType and rawAttr are well
206 // formed, this routine will return a new vector type.
207 QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr);
Steve Naroff82113e32007-07-29 16:33:31 +0000208 void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000209
210 //===--------------------------------------------------------------------===//
211 // Statement Parsing Callbacks: SemaStmt.cpp.
212public:
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000213 virtual StmtResult ActOnExprStmt(ExprTy *Expr);
Chris Lattner4b009652007-07-25 00:24:17 +0000214
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000215 virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc);
216 virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +0000217 StmtTy **Elts, unsigned NumElts,
218 bool isStmtExpr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000219 virtual StmtResult ActOnDeclStmt(DeclTy *Decl);
220 virtual StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000221 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
222 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000223 virtual StmtResult ActOnDefaultStmt(SourceLocation DefaultLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000224 SourceLocation ColonLoc, StmtTy *SubStmt,
225 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000226 virtual StmtResult ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000227 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000228 virtual StmtResult ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000229 StmtTy *ThenVal, SourceLocation ElseLoc,
230 StmtTy *ElseVal);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000231 virtual StmtResult ActOnStartOfSwitchStmt(ExprTy *Cond);
232 virtual StmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
Chris Lattner4b009652007-07-25 00:24:17 +0000233 ExprTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000234 virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
Chris Lattner4b009652007-07-25 00:24:17 +0000235 StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000236 virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000237 SourceLocation WhileLoc, ExprTy *Cond);
238
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000239 virtual StmtResult ActOnForStmt(SourceLocation ForLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000240 SourceLocation LParenLoc,
241 StmtTy *First, ExprTy *Second, ExprTy *Third,
242 SourceLocation RParenLoc, StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000243 virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000244 SourceLocation LabelLoc,
245 IdentifierInfo *LabelII);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000246 virtual StmtResult ActOnIndirectGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000247 SourceLocation StarLoc,
248 ExprTy *DestExp);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000249 virtual StmtResult ActOnContinueStmt(SourceLocation ContinueLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000250 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000251 virtual StmtResult ActOnBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000252
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000253 virtual StmtResult ActOnReturnStmt(SourceLocation ReturnLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000254 ExprTy *RetValExp);
255
256 //===--------------------------------------------------------------------===//
257 // Expression Parsing Callbacks: SemaExpr.cpp.
258
259 // Primary Expressions.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000260 virtual ExprResult ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000261 IdentifierInfo &II,
262 bool HasTrailingLParen);
Steve Naroff87d58b42007-09-16 03:34:24 +0000263 virtual ExprResult ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000264 tok::TokenKind Kind);
Steve Naroff87d58b42007-09-16 03:34:24 +0000265 virtual ExprResult ActOnNumericConstant(const Token &);
266 virtual ExprResult ActOnCharacterConstant(const Token &);
267 virtual ExprResult ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000268 ExprTy *Val);
269
Steve Naroff87d58b42007-09-16 03:34:24 +0000270 /// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000271 /// fragments (e.g. "foo" "bar" L"baz").
Steve Naroff87d58b42007-09-16 03:34:24 +0000272 virtual ExprResult ActOnStringLiteral(const Token *Toks, unsigned NumToks);
Chris Lattner4b009652007-07-25 00:24:17 +0000273
274 // Binary/Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +0000275 virtual ExprResult ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +0000276 ExprTy *Input);
277 virtual ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000278 ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000279 SourceLocation LParenLoc, TypeTy *Ty,
280 SourceLocation RParenLoc);
281
Steve Naroff87d58b42007-09-16 03:34:24 +0000282 virtual ExprResult ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000283 tok::TokenKind Kind, ExprTy *Input);
284
Steve Naroff87d58b42007-09-16 03:34:24 +0000285 virtual ExprResult ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000286 ExprTy *Idx, SourceLocation RLoc);
Steve Naroff87d58b42007-09-16 03:34:24 +0000287 virtual ExprResult ActOnMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000288 tok::TokenKind OpKind,
289 SourceLocation MemberLoc,
290 IdentifierInfo &Member);
291
Steve Naroff87d58b42007-09-16 03:34:24 +0000292 /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000293 /// This provides the location of the left/right parens and a list of comma
294 /// locations.
Steve Naroff87d58b42007-09-16 03:34:24 +0000295 virtual ExprResult ActOnCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000296 ExprTy **Args, unsigned NumArgs,
297 SourceLocation *CommaLocs,
298 SourceLocation RParenLoc);
299
Steve Naroff87d58b42007-09-16 03:34:24 +0000300 virtual ExprResult ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000301 SourceLocation RParenLoc, ExprTy *Op);
302
Steve Naroff87d58b42007-09-16 03:34:24 +0000303 virtual ExprResult ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000304 SourceLocation RParenLoc, ExprTy *Op);
305
Steve Naroff87d58b42007-09-16 03:34:24 +0000306 virtual ExprResult ActOnInitList(SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000307 ExprTy **InitList, unsigned NumInit,
308 SourceLocation RParenLoc);
309
Steve Naroff87d58b42007-09-16 03:34:24 +0000310 virtual ExprResult ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000311 ExprTy *LHS,ExprTy *RHS);
312
Steve Naroff87d58b42007-09-16 03:34:24 +0000313 /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000314 /// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000315 virtual ExprResult ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000316 SourceLocation ColonLoc,
317 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
318
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000319 /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
320 virtual ExprResult ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000321 IdentifierInfo *LabelII);
322
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000323 virtual ExprResult ActOnStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt,
Chris Lattner4b009652007-07-25 00:24:17 +0000324 SourceLocation RPLoc); // "({..})"
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000325
326 /// __builtin_offsetof(type, a.b[123][456].c)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000327 virtual ExprResult ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000328 SourceLocation TypeLoc, TypeTy *Arg1,
329 OffsetOfComponent *CompPtr,
330 unsigned NumComponents,
331 SourceLocation RParenLoc);
332
Steve Naroff63bad2d2007-08-01 22:05:33 +0000333 // __builtin_types_compatible_p(type1, type2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000334 virtual ExprResult ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +0000335 TypeTy *arg1, TypeTy *arg2,
336 SourceLocation RPLoc);
Steve Naroff93c53012007-08-03 21:21:27 +0000337
338 // __builtin_choose_expr(constExpr, expr1, expr2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000339 virtual ExprResult ActOnChooseExpr(SourceLocation BuiltinLoc,
Steve Naroff93c53012007-08-03 21:21:27 +0000340 ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
341 SourceLocation RPLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000342
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000343 /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
344 virtual ExprResult ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000345 SourceLocation LAngleBracketLoc, TypeTy *Ty,
346 SourceLocation RAngleBracketLoc,
347 SourceLocation LParenLoc, ExprTy *E,
348 SourceLocation RParenLoc);
349
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000350 /// ActOnCXXBoolLiteral - Parse {true,false} literals.
351 virtual ExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000352 tok::TokenKind Kind);
Anders Carlssona66cad42007-08-21 17:43:55 +0000353
354 // ParseObjCStringLiteral - Parse Objective-C string literals.
355 virtual ExprResult ParseObjCStringLiteral(ExprTy *string);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000356 virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc,
357 SourceLocation LParenLoc,
358 TypeTy *Ty,
359 SourceLocation RParenLoc);
360
Steve Naroff81f1bba2007-09-06 21:24:23 +0000361 // Objective-C declarations.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000362 virtual DeclTy *ObjcStartClassInterface(Scope* S,
363 SourceLocation AtInterafceLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000364 IdentifierInfo *ClassName, SourceLocation ClassLoc,
365 IdentifierInfo *SuperName, SourceLocation SuperLoc,
366 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
367 AttributeList *AttrList);
368
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000369 virtual DeclTy *ObjcStartProtoInterface(Scope* S,
370 SourceLocation AtProtoInterfaceLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000371 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
372 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
373
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000374 virtual DeclTy *ObjcStartCatInterface(Scope* S,
375 SourceLocation AtInterfaceLoc,
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000376 IdentifierInfo *ClassName, SourceLocation ClassLoc,
377 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
378 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
379
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000380 virtual DeclTy *ObjcStartClassImplementation(Scope* S,
381 SourceLocation AtClassImplLoc,
382 IdentifierInfo *ClassName, SourceLocation ClassLoc,
383 IdentifierInfo *SuperClassname,
384 SourceLocation SuperClassLoc);
385
Steve Naroff81f1bba2007-09-06 21:24:23 +0000386 virtual DeclTy *ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
387 IdentifierInfo **IdentList,
388 unsigned NumElts);
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000389
390 virtual DeclTy *ObjcForwardProtocolDeclaration(Scope *S,
391 SourceLocation AtProtocolLoc,
392 IdentifierInfo **IdentList,
393 unsigned NumElts);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000394
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000395 virtual void ObjcAddMethodsToClass(Scope* S, DeclTy *ClassDecl,
Steve Naroff75494892007-09-11 21:17:26 +0000396 DeclTy **allMethods, unsigned allNum);
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000397
398 virtual void ActOnImpleIvarVsClassIvars(DeclTy *ClassDecl,
399 DeclTy **Fields, unsigned NumFields);
400
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000401 virtual DeclTy *ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
Steve Naroff6cb1d362007-09-28 22:22:11 +0000402 tok::TokenKind MethodType, TypeTy *ReturnType, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000403 // optional arguments. The number of types/arguments is obtained
404 // from the Sel.getNumArgs().
405 TypeTy **ArgTypes, IdentifierInfo **ArgNames,
406 AttributeList *AttrList, tok::ObjCKeywordKind MethodImplKind);
Steve Naroffd3f5ee42007-09-17 21:01:15 +0000407
Steve Naroff4ed9d662007-09-27 14:38:14 +0000408 // ActOnClassMessage - used for both unary and keyword messages.
409 // ArgExprs is optional - if it is present, the number of expressions
410 // is obtained from Sel.getNumArgs().
411 virtual ExprResult ActOnClassMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000412 IdentifierInfo *receivingClassName, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000413 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
414
415 // ActOnInstanceMessage - used for both unary and keyword messages.
416 // ArgExprs is optional - if it is present, the number of expressions
417 // is obtained from Sel.getNumArgs().
418 virtual ExprResult ActOnInstanceMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000419 ExprTy *receiver, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000420 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
Chris Lattner4b009652007-07-25 00:24:17 +0000421private:
422 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
423 // functions and arrays to their respective pointers (C99 6.3.2.1).
424 void UsualUnaryConversions(Expr *&expr);
425
426 // DefaultFunctionArrayConversion - converts functions and arrays
427 // to their respective pointers (C99 6.3.2.1).
428 void DefaultFunctionArrayConversion(Expr *&expr);
429
Steve Naroffdb65e052007-08-28 23:30:39 +0000430 // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
431 // do not have a prototype. Integer promotions are performed on each
432 // argument, and arguments that have type float are promoted to double.
433 void DefaultArgumentPromotion(Expr *&expr);
434
Chris Lattner4b009652007-07-25 00:24:17 +0000435 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
436 // operands and then handles various conversions that are common to binary
437 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
438 // routine returns the first non-arithmetic type found. The client is
439 // responsible for emitting appropriate error diagnostics.
Steve Naroff8f708362007-08-24 19:07:16 +0000440 QualType UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr,
441 bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000442 enum AssignmentCheckResult {
443 Compatible,
444 Incompatible,
445 PointerFromInt,
446 IntFromPointer,
447 IncompatiblePointer,
448 CompatiblePointerDiscardsQualifiers
449 };
450 // CheckAssignmentConstraints - Perform type checking for assignment,
451 // argument passing, variable initialization, and function return values.
452 // This routine is only used by the following two methods. C99 6.5.16.
453 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
454
Steve Naroff87d58b42007-09-16 03:34:24 +0000455 // CheckSingleAssignmentConstraints - Currently used by ActOnCallExpr,
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000456 // CheckAssignmentOperands, and ActOnReturnStmt. Prior to type checking,
Chris Lattner4b009652007-07-25 00:24:17 +0000457 // this routine performs the default function/array converions.
458 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
459 Expr *&rExpr);
460 // CheckCompoundAssignmentConstraints - Type check without performing any
461 // conversions. For compound assignments, the "Check...Operands" methods
462 // perform the necessary conversions.
463 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
464 QualType rhs);
465
466 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
467 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
468 QualType rhsType);
469
470 /// the following "Check" methods will return a valid/converted QualType
471 /// or a null QualType (indicating an error diagnostic was issued).
472
Steve Naroff87d58b42007-09-16 03:34:24 +0000473 /// type checking binary operators (subroutines of ActOnBinOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000474 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
475 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
476 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
Steve Naroff8f708362007-08-24 19:07:16 +0000477 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000478 inline QualType CheckRemainderOperands( // C99 6.5.5
Steve Naroff8f708362007-08-24 19:07:16 +0000479 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000480 inline QualType CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000481 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000482 inline QualType CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000483 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000484 inline QualType CheckShiftOperands( // C99 6.5.7
Steve Naroff8f708362007-08-24 19:07:16 +0000485 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner254f3bc2007-08-26 01:18:55 +0000486 inline QualType CheckCompareOperands( // C99 6.5.8/9
487 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isRelational);
Chris Lattner4b009652007-07-25 00:24:17 +0000488 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
Steve Naroff8f708362007-08-24 19:07:16 +0000489 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000490 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
491 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
492 // CheckAssignmentOperands is used for both simple and compound assignment.
493 // For simple assignment, pass both expressions and a null converted type.
494 // For compound assignment, pass both expressions and the converted type.
495 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
Steve Naroff0f32f432007-08-24 22:33:52 +0000496 Expr *lex, Expr *&rex, SourceLocation OpLoc, QualType convertedType);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 inline QualType CheckCommaOperands( // C99 6.5.17
498 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
499 inline QualType CheckConditionalOperands( // C99 6.5.15
500 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
501
Steve Naroff87d58b42007-09-16 03:34:24 +0000502 /// type checking unary operators (subroutines of ActOnUnaryOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000503 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
504 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
505 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
506 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
507 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
508 bool isSizeof);
Chris Lattner5110ad52007-08-24 21:41:10 +0000509 QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000510
511 /// type checking primary expressions.
512 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
513 IdentifierInfo &Comp, SourceLocation CmpLoc);
514
Steve Naroffe14e5542007-09-02 02:04:30 +0000515 /// type checking declaration initializers (C99 6.7.8)
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000516 bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
Steve Naroff1c9de712007-09-03 01:24:23 +0000517 bool isStatic);
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000518 bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
519 bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
520 bool isStatic, QualType ElementType);
Steve Naroff509d0b52007-09-04 02:20:04 +0000521 void CheckVariableInitList(QualType DeclType, InitListExpr *IList,
522 QualType ElementType, bool isStatic,
523 int &nInitializers, bool &hadError);
524 void CheckConstantInitList(QualType DeclType, InitListExpr *IList,
525 QualType ElementType, bool isStatic,
526 int &nInitializers, bool &hadError);
527
Chris Lattner3429a812007-08-23 05:46:52 +0000528 /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
529 /// the specified width and sign. If an overflow occurs, detect it and emit
530 /// the specified diagnostic.
531 void ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &OldVal,
532 unsigned NewWidth, bool NewSign,
533 SourceLocation Loc, unsigned DiagID);
534
Chris Lattner2e64c072007-08-10 20:18:51 +0000535 //===--------------------------------------------------------------------===//
536 // Extra semantic analysis beyond the C type system
537 private:
538
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000539 bool CheckFunctionCall(Expr *Fn,
Ted Kremenek081ed872007-08-14 17:39:48 +0000540 SourceLocation LParenLoc, SourceLocation RParenLoc,
541 FunctionDecl *FDecl,
Chris Lattner2e64c072007-08-10 20:18:51 +0000542 Expr** Args, unsigned NumArgsInCall);
543
Ted Kremenek081ed872007-08-14 17:39:48 +0000544 void CheckPrintfArguments(Expr *Fn,
545 SourceLocation LParenLoc, SourceLocation RParenLoc,
546 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek30596542007-08-10 21:21:05 +0000547 unsigned format_idx, Expr** Args,
548 unsigned NumArgsInCall);
Ted Kremenek45925ab2007-08-17 16:46:58 +0000549
550 void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
551 SourceLocation ReturnLoc);
552
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000553
554 bool CheckBuiltinCFStringArgument(Expr* Arg);
Chris Lattner4b009652007-07-25 00:24:17 +0000555};
556
557
558} // end namespace clang
559
560#endif