blob: 6e87331b966e43a4d8a7c2ea2a814e87b431ebf2 [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;
Fariborz Jahanianf7cf3a62007-09-29 17:14:55 +000051 class ObjcImplementationDecl;
Fariborz Jahaniana91aa322007-10-02 16:38:50 +000052 class ObjcCategoryImplDecl;
53 class ObjcCategoryDecl;
Steve Naroff89529b12007-10-02 21:43:37 +000054 class ObjcIvarDecl;
Steve Naroff82113e32007-07-29 16:33:31 +000055
Chris Lattner4b009652007-07-25 00:24:17 +000056/// Sema - This implements semantic analysis and AST building for C.
57class Sema : public Action {
58 Preprocessor &PP;
59
60 ASTContext &Context;
61
62 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
63 /// the function decl for the function being parsed.
64 FunctionDecl *CurFunctionDecl;
65
66 /// LastInGroupList - This vector is populated when there are multiple
67 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
68 /// all but the last decl will be entered into this. This is used by the
69 /// ASTStreamer.
70 std::vector<Decl*> &LastInGroupList;
71
72 /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
73 /// it (which acts like the label decl in some ways). Forward referenced
74 /// labels have a LabelStmt created for them with a null location & SubStmt.
75 llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
76
77 llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
Steve Naroff82113e32007-07-29 16:33:31 +000078
79 /// OCUVectorDecls - This is a list all the OCU vector types. This allows
80 /// us to associate a raw vector type with one of the OCU type names.
81 /// This is only necessary for issuing pretty diagnostics.
82 llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls;
Chris Lattner2e64c072007-08-10 20:18:51 +000083
84 // Enum values used by KnownFunctionIDs (see below).
85 enum {
86 id_printf,
87 id_fprintf,
88 id_sprintf,
89 id_snprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000090 id_asprintf,
Ted Kremenek2d7e9532007-08-10 21:13:51 +000091 id_vsnprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000092 id_vasprintf,
93 id_vfprintf,
94 id_vsprintf,
95 id_vprintf,
96 id_num_known_functions
97 };
98
99 /// KnownFunctionIDs - This is a list of IdentifierInfo objects to a set
100 /// of known functions used by the semantic analysis to do various
101 /// kinds of checking (e.g. checking format string errors in printf calls).
102 /// This list is populated upon the creation of a Sema object.
103 IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ];
104
Chris Lattner4b009652007-07-25 00:24:17 +0000105public:
106 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
107
108 const LangOptions &getLangOptions() const;
109
110 /// The primitive diagnostic helpers - always returns true, which simplifies
111 /// error handling (i.e. less code).
112 bool Diag(SourceLocation Loc, unsigned DiagID);
113 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
114 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
115 const std::string &Msg2);
116
117 /// More expressive diagnostic helpers for expressions (say that 6 times:-)
118 bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1);
119 bool Diag(SourceLocation Loc, unsigned DiagID,
120 SourceRange R1, SourceRange R2);
121 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
122 SourceRange R1);
123 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
124 SourceRange R1, SourceRange R2);
125 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
126 const std::string &Msg2, SourceRange R1);
127 bool Diag(SourceLocation Loc, unsigned DiagID,
128 const std::string &Msg1, const std::string &Msg2,
129 SourceRange R1, SourceRange R2);
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000130
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000131 virtual void DeleteExpr(ExprTy *E);
132 virtual void DeleteStmt(StmtTy *S);
133
Chris Lattner4b009652007-07-25 00:24:17 +0000134 //===--------------------------------------------------------------------===//
135 // Type Analysis / Processing: SemaType.cpp.
136 //
137 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
138
Steve Naroff0acc9c92007-09-15 18:49:24 +0000139 virtual TypeResult ActOnTypeName(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000140
Steve Naroff0acc9c92007-09-15 18:49:24 +0000141 virtual TypeResult ActOnParamDeclaratorType(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000142private:
143 //===--------------------------------------------------------------------===//
144 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
145 //
146 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000147 virtual DeclTy *ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000148 void AddInitializerToDecl(DeclTy *dcl, ExprTy *init);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
150
151 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
152 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
153 virtual void PopScope(SourceLocation Loc, Scope *S);
154
155 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
156 /// no declarator (e.g. "struct foo;") is parsed.
157 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
158
Steve Naroff0acc9c92007-09-15 18:49:24 +0000159 virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattner4b009652007-07-25 00:24:17 +0000160 SourceLocation KWLoc, IdentifierInfo *Name,
161 SourceLocation NameLoc, AttributeList *Attr);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000162 virtual DeclTy *ActOnField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
Chris Lattner4b009652007-07-25 00:24:17 +0000163 Declarator &D, ExprTy *BitfieldWidth);
Steve Naroffffeaa552007-09-14 23:09:53 +0000164
165 // This is used for both record definitions and ObjC interface declarations.
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000166 virtual void ActOnFields(Scope* S,
167 SourceLocation RecLoc, DeclTy *TagDecl,
Steve Naroffffeaa552007-09-14 23:09:53 +0000168 DeclTy **Fields, unsigned NumFields,
169 tok::ObjCKeywordKind *visibility = 0);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000170 virtual DeclTy *ActOnEnumConstant(Scope *S, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000171 DeclTy *LastEnumConstant,
172 SourceLocation IdLoc, IdentifierInfo *Id,
173 SourceLocation EqualLoc, ExprTy *Val);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000174 virtual void ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000175 DeclTy **Elements, unsigned NumElements);
176private:
Steve Naroff0acc9c92007-09-15 18:49:24 +0000177 /// Subroutines of ActOnDeclarator()...
Steve Naroff2591e1b2007-09-13 23:52:58 +0000178 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, ScopedDecl *LastDecl);
Steve Naroffcb597472007-09-13 21:41:19 +0000179 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *Old);
180 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, ScopedDecl *Old);
181 VarDecl *MergeVarDecl(VarDecl *New, ScopedDecl *Old);
Chris Lattner4b009652007-07-25 00:24:17 +0000182 /// AddTopLevelDecl - called after the decl has been fully processed.
183 /// Allows for bookkeeping and post-processing of each declaration.
184 void AddTopLevelDecl(Decl *current, Decl *last);
185
186 /// More parsing and symbol table subroutines...
187 ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
188 Scope *FnBodyScope);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000189 ScopedDecl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
190 SourceLocation IdLoc, Scope *S);
Steve Narofffa465d12007-10-02 20:01:56 +0000191 ObjcInterfaceDecl *getObjCInterfaceDecl(IdentifierInfo *Id);
Fariborz Jahaniandd243ef2007-09-29 17:04:06 +0000192 ObjcProtocolDecl *getObjCProtocolDecl(Scope *S,
193 IdentifierInfo *Id, SourceLocation IdLoc);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000194 ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
Steve Narofff0c31dd2007-09-16 16:16:00 +0000195 ScopedDecl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
Chris Lattner4b009652007-07-25 00:24:17 +0000196 Scope *S);
197 // Decl attributes - this routine is the top level dispatcher.
198 void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
199 AttributeList *declarator_postfix);
200 void HandleDeclAttribute(Decl *New, AttributeList *rawAttr);
201
202 // HandleVectorTypeAttribute - this attribute is only applicable to
203 // integral and float scalars, although arrays, pointers, and function
204 // return values are allowed in conjunction with this construct. Aggregates
205 // with this attribute are invalid, even if they are of the same size as a
206 // corresponding scalar.
207 // The raw attribute should contain precisely 1 argument, the vector size
208 // for the variable, measured in bytes. If curType and rawAttr are well
209 // formed, this routine will return a new vector type.
210 QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr);
Steve Naroff82113e32007-07-29 16:33:31 +0000211 void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000212
Fariborz Jahanianf7cf3a62007-09-29 17:14:55 +0000213 /// CheckProtocolMethodDefs - This routine checks unimpletented methods
214 /// Declared in protocol, and those referenced by it.
215 void CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl,
Fariborz Jahaniand3952f32007-10-02 20:06:01 +0000216 bool& IncompleteImpl,
Fariborz Jahanianf7cf3a62007-09-29 17:14:55 +0000217 const llvm::DenseMap<void *, char>& InsMap,
218 const llvm::DenseMap<void *, char>& ClsMap);
219
Steve Naroff89529b12007-10-02 21:43:37 +0000220 /// CheckImplementationIvars - This routine checks if the instance variables
221 /// listed in the implelementation match those listed in the interface.
222 void CheckImplementationIvars(ObjcImplementationDecl *ImpDecl,
223 ObjcIvarDecl **Fields, unsigned nIvars);
224
Fariborz Jahanianf7cf3a62007-09-29 17:14:55 +0000225 /// ImplMethodsVsClassMethods - This is main routine to warn if any method
226 /// remains unimplemented in the @implementation class.
227 void ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl,
228 ObjcInterfaceDecl* IDecl);
229
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000230 /// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
231 /// category interface is implemented in the category @implementation.
232 void ImplCategoryMethodsVsIntfMethods(ObjcCategoryImplDecl *CatImplDecl,
Fariborz Jahaniand3952f32007-10-02 20:06:01 +0000233 ObjcCategoryDecl *CatClassDecl);
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000234
Chris Lattner4b009652007-07-25 00:24:17 +0000235 //===--------------------------------------------------------------------===//
236 // Statement Parsing Callbacks: SemaStmt.cpp.
237public:
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000238 virtual StmtResult ActOnExprStmt(ExprTy *Expr);
Chris Lattner4b009652007-07-25 00:24:17 +0000239
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000240 virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc);
241 virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +0000242 StmtTy **Elts, unsigned NumElts,
243 bool isStmtExpr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000244 virtual StmtResult ActOnDeclStmt(DeclTy *Decl);
245 virtual StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000246 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
247 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000248 virtual StmtResult ActOnDefaultStmt(SourceLocation DefaultLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000249 SourceLocation ColonLoc, StmtTy *SubStmt,
250 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000251 virtual StmtResult ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000252 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000253 virtual StmtResult ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000254 StmtTy *ThenVal, SourceLocation ElseLoc,
255 StmtTy *ElseVal);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000256 virtual StmtResult ActOnStartOfSwitchStmt(ExprTy *Cond);
257 virtual StmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
Chris Lattner4b009652007-07-25 00:24:17 +0000258 ExprTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000259 virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
Chris Lattner4b009652007-07-25 00:24:17 +0000260 StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000261 virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000262 SourceLocation WhileLoc, ExprTy *Cond);
263
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000264 virtual StmtResult ActOnForStmt(SourceLocation ForLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000265 SourceLocation LParenLoc,
266 StmtTy *First, ExprTy *Second, ExprTy *Third,
267 SourceLocation RParenLoc, StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000268 virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000269 SourceLocation LabelLoc,
270 IdentifierInfo *LabelII);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000271 virtual StmtResult ActOnIndirectGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000272 SourceLocation StarLoc,
273 ExprTy *DestExp);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000274 virtual StmtResult ActOnContinueStmt(SourceLocation ContinueLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000275 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000276 virtual StmtResult ActOnBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000277
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000278 virtual StmtResult ActOnReturnStmt(SourceLocation ReturnLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000279 ExprTy *RetValExp);
280
281 //===--------------------------------------------------------------------===//
282 // Expression Parsing Callbacks: SemaExpr.cpp.
283
284 // Primary Expressions.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000285 virtual ExprResult ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000286 IdentifierInfo &II,
287 bool HasTrailingLParen);
Steve Naroff87d58b42007-09-16 03:34:24 +0000288 virtual ExprResult ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000289 tok::TokenKind Kind);
Steve Naroff87d58b42007-09-16 03:34:24 +0000290 virtual ExprResult ActOnNumericConstant(const Token &);
291 virtual ExprResult ActOnCharacterConstant(const Token &);
292 virtual ExprResult ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000293 ExprTy *Val);
294
Steve Naroff87d58b42007-09-16 03:34:24 +0000295 /// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000296 /// fragments (e.g. "foo" "bar" L"baz").
Steve Naroff87d58b42007-09-16 03:34:24 +0000297 virtual ExprResult ActOnStringLiteral(const Token *Toks, unsigned NumToks);
Chris Lattner4b009652007-07-25 00:24:17 +0000298
299 // Binary/Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +0000300 virtual ExprResult ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +0000301 ExprTy *Input);
302 virtual ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000303 ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000304 SourceLocation LParenLoc, TypeTy *Ty,
305 SourceLocation RParenLoc);
306
Steve Naroff87d58b42007-09-16 03:34:24 +0000307 virtual ExprResult ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000308 tok::TokenKind Kind, ExprTy *Input);
309
Steve Naroff87d58b42007-09-16 03:34:24 +0000310 virtual ExprResult ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000311 ExprTy *Idx, SourceLocation RLoc);
Steve Naroff87d58b42007-09-16 03:34:24 +0000312 virtual ExprResult ActOnMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000313 tok::TokenKind OpKind,
314 SourceLocation MemberLoc,
315 IdentifierInfo &Member);
316
Steve Naroff87d58b42007-09-16 03:34:24 +0000317 /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000318 /// This provides the location of the left/right parens and a list of comma
319 /// locations.
Steve Naroff87d58b42007-09-16 03:34:24 +0000320 virtual ExprResult ActOnCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000321 ExprTy **Args, unsigned NumArgs,
322 SourceLocation *CommaLocs,
323 SourceLocation RParenLoc);
324
Steve Naroff87d58b42007-09-16 03:34:24 +0000325 virtual ExprResult ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000326 SourceLocation RParenLoc, ExprTy *Op);
327
Steve Naroff87d58b42007-09-16 03:34:24 +0000328 virtual ExprResult ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000329 SourceLocation RParenLoc, ExprTy *Op);
330
Steve Naroff87d58b42007-09-16 03:34:24 +0000331 virtual ExprResult ActOnInitList(SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000332 ExprTy **InitList, unsigned NumInit,
333 SourceLocation RParenLoc);
334
Steve Naroff87d58b42007-09-16 03:34:24 +0000335 virtual ExprResult ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000336 ExprTy *LHS,ExprTy *RHS);
337
Steve Naroff87d58b42007-09-16 03:34:24 +0000338 /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000339 /// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000340 virtual ExprResult ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000341 SourceLocation ColonLoc,
342 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
343
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000344 /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
345 virtual ExprResult ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000346 IdentifierInfo *LabelII);
347
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000348 virtual ExprResult ActOnStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt,
Chris Lattner4b009652007-07-25 00:24:17 +0000349 SourceLocation RPLoc); // "({..})"
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000350
351 /// __builtin_offsetof(type, a.b[123][456].c)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000352 virtual ExprResult ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000353 SourceLocation TypeLoc, TypeTy *Arg1,
354 OffsetOfComponent *CompPtr,
355 unsigned NumComponents,
356 SourceLocation RParenLoc);
357
Steve Naroff63bad2d2007-08-01 22:05:33 +0000358 // __builtin_types_compatible_p(type1, type2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000359 virtual ExprResult ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +0000360 TypeTy *arg1, TypeTy *arg2,
361 SourceLocation RPLoc);
Steve Naroff93c53012007-08-03 21:21:27 +0000362
363 // __builtin_choose_expr(constExpr, expr1, expr2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000364 virtual ExprResult ActOnChooseExpr(SourceLocation BuiltinLoc,
Steve Naroff93c53012007-08-03 21:21:27 +0000365 ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
366 SourceLocation RPLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000367
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000368 /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
369 virtual ExprResult ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000370 SourceLocation LAngleBracketLoc, TypeTy *Ty,
371 SourceLocation RAngleBracketLoc,
372 SourceLocation LParenLoc, ExprTy *E,
373 SourceLocation RParenLoc);
374
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000375 /// ActOnCXXBoolLiteral - Parse {true,false} literals.
376 virtual ExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000377 tok::TokenKind Kind);
Anders Carlssona66cad42007-08-21 17:43:55 +0000378
379 // ParseObjCStringLiteral - Parse Objective-C string literals.
380 virtual ExprResult ParseObjCStringLiteral(ExprTy *string);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000381 virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc,
382 SourceLocation LParenLoc,
383 TypeTy *Ty,
384 SourceLocation RParenLoc);
385
Steve Naroff81f1bba2007-09-06 21:24:23 +0000386 // Objective-C declarations.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000387 virtual DeclTy *ObjcStartClassInterface(Scope* S,
388 SourceLocation AtInterafceLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000389 IdentifierInfo *ClassName, SourceLocation ClassLoc,
390 IdentifierInfo *SuperName, SourceLocation SuperLoc,
391 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
392 AttributeList *AttrList);
393
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000394 virtual DeclTy *ObjcStartProtoInterface(Scope* S,
395 SourceLocation AtProtoInterfaceLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000396 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
397 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
398
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000399 virtual DeclTy *ObjcStartCatInterface(Scope* S,
400 SourceLocation AtInterfaceLoc,
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000401 IdentifierInfo *ClassName, SourceLocation ClassLoc,
402 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
403 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
404
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000405 virtual DeclTy *ObjcStartClassImplementation(Scope* S,
406 SourceLocation AtClassImplLoc,
407 IdentifierInfo *ClassName, SourceLocation ClassLoc,
408 IdentifierInfo *SuperClassname,
409 SourceLocation SuperClassLoc);
410
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000411 virtual DeclTy *ObjcStartCategoryImplementation(Scope* S,
412 SourceLocation AtCatImplLoc,
413 IdentifierInfo *ClassName,
414 SourceLocation ClassLoc,
415 IdentifierInfo *CatName,
416 SourceLocation CatLoc);
417
Steve Naroffb4dfe362007-10-02 22:39:18 +0000418 virtual DeclTy *ActOnForwardClassDeclaration(Scope *S, SourceLocation Loc,
419 IdentifierInfo **IdentList,
420 unsigned NumElts);
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000421
Steve Naroffb4dfe362007-10-02 22:39:18 +0000422 virtual DeclTy *ActOnForwardProtocolDeclaration(Scope *S,
423 SourceLocation AtProtocolLoc,
424 IdentifierInfo **IdentList,
425 unsigned NumElts);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000426
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000427 virtual void ObjcAddMethodsToClass(Scope* S, DeclTy *ClassDecl,
Steve Naroff75494892007-09-11 21:17:26 +0000428 DeclTy **allMethods, unsigned allNum);
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000429
Steve Naroffb4dfe362007-10-02 22:39:18 +0000430 virtual DeclTy *ActOnMethodDeclaration(SourceLocation MethodLoc,
Steve Naroff6cb1d362007-09-28 22:22:11 +0000431 tok::TokenKind MethodType, TypeTy *ReturnType, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000432 // optional arguments. The number of types/arguments is obtained
433 // from the Sel.getNumArgs().
434 TypeTy **ArgTypes, IdentifierInfo **ArgNames,
435 AttributeList *AttrList, tok::ObjCKeywordKind MethodImplKind);
Steve Naroffd3f5ee42007-09-17 21:01:15 +0000436
Steve Naroff4ed9d662007-09-27 14:38:14 +0000437 // ActOnClassMessage - used for both unary and keyword messages.
438 // ArgExprs is optional - if it is present, the number of expressions
439 // is obtained from Sel.getNumArgs().
440 virtual ExprResult ActOnClassMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000441 IdentifierInfo *receivingClassName, Selector Sel,
Steve Narofffa465d12007-10-02 20:01:56 +0000442 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000443
444 // ActOnInstanceMessage - used for both unary and keyword messages.
445 // ArgExprs is optional - if it is present, the number of expressions
446 // is obtained from Sel.getNumArgs().
447 virtual ExprResult ActOnInstanceMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000448 ExprTy *receiver, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000449 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
Chris Lattner4b009652007-07-25 00:24:17 +0000450private:
451 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
452 // functions and arrays to their respective pointers (C99 6.3.2.1).
453 void UsualUnaryConversions(Expr *&expr);
454
455 // DefaultFunctionArrayConversion - converts functions and arrays
456 // to their respective pointers (C99 6.3.2.1).
457 void DefaultFunctionArrayConversion(Expr *&expr);
458
Steve Naroffdb65e052007-08-28 23:30:39 +0000459 // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
460 // do not have a prototype. Integer promotions are performed on each
461 // argument, and arguments that have type float are promoted to double.
462 void DefaultArgumentPromotion(Expr *&expr);
463
Chris Lattner4b009652007-07-25 00:24:17 +0000464 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
465 // operands and then handles various conversions that are common to binary
466 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
467 // routine returns the first non-arithmetic type found. The client is
468 // responsible for emitting appropriate error diagnostics.
Steve Naroff8f708362007-08-24 19:07:16 +0000469 QualType UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr,
470 bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000471 enum AssignmentCheckResult {
472 Compatible,
473 Incompatible,
474 PointerFromInt,
475 IntFromPointer,
476 IncompatiblePointer,
477 CompatiblePointerDiscardsQualifiers
478 };
479 // CheckAssignmentConstraints - Perform type checking for assignment,
480 // argument passing, variable initialization, and function return values.
481 // This routine is only used by the following two methods. C99 6.5.16.
482 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
483
Steve Naroff87d58b42007-09-16 03:34:24 +0000484 // CheckSingleAssignmentConstraints - Currently used by ActOnCallExpr,
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000485 // CheckAssignmentOperands, and ActOnReturnStmt. Prior to type checking,
Chris Lattner4b009652007-07-25 00:24:17 +0000486 // this routine performs the default function/array converions.
487 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
488 Expr *&rExpr);
489 // CheckCompoundAssignmentConstraints - Type check without performing any
490 // conversions. For compound assignments, the "Check...Operands" methods
491 // perform the necessary conversions.
492 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
493 QualType rhs);
494
495 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
496 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
497 QualType rhsType);
498
499 /// the following "Check" methods will return a valid/converted QualType
500 /// or a null QualType (indicating an error diagnostic was issued).
501
Steve Naroff87d58b42007-09-16 03:34:24 +0000502 /// type checking binary operators (subroutines of ActOnBinOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000503 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
504 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
505 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
Steve Naroff8f708362007-08-24 19:07:16 +0000506 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000507 inline QualType CheckRemainderOperands( // C99 6.5.5
Steve Naroff8f708362007-08-24 19:07:16 +0000508 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000509 inline QualType CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000510 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000511 inline QualType CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000512 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000513 inline QualType CheckShiftOperands( // C99 6.5.7
Steve Naroff8f708362007-08-24 19:07:16 +0000514 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner254f3bc2007-08-26 01:18:55 +0000515 inline QualType CheckCompareOperands( // C99 6.5.8/9
516 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isRelational);
Chris Lattner4b009652007-07-25 00:24:17 +0000517 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
Steve Naroff8f708362007-08-24 19:07:16 +0000518 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000519 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
520 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
521 // CheckAssignmentOperands is used for both simple and compound assignment.
522 // For simple assignment, pass both expressions and a null converted type.
523 // For compound assignment, pass both expressions and the converted type.
524 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
Steve Naroff0f32f432007-08-24 22:33:52 +0000525 Expr *lex, Expr *&rex, SourceLocation OpLoc, QualType convertedType);
Chris Lattner4b009652007-07-25 00:24:17 +0000526 inline QualType CheckCommaOperands( // C99 6.5.17
527 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
528 inline QualType CheckConditionalOperands( // C99 6.5.15
529 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
530
Steve Naroff87d58b42007-09-16 03:34:24 +0000531 /// type checking unary operators (subroutines of ActOnUnaryOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000532 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
533 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
534 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
535 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
536 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
537 bool isSizeof);
Chris Lattner5110ad52007-08-24 21:41:10 +0000538 QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000539
540 /// type checking primary expressions.
541 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
542 IdentifierInfo &Comp, SourceLocation CmpLoc);
543
Steve Naroffe14e5542007-09-02 02:04:30 +0000544 /// type checking declaration initializers (C99 6.7.8)
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000545 bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
Steve Naroff1c9de712007-09-03 01:24:23 +0000546 bool isStatic);
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000547 bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
548 bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
549 bool isStatic, QualType ElementType);
Steve Naroff509d0b52007-09-04 02:20:04 +0000550 void CheckVariableInitList(QualType DeclType, InitListExpr *IList,
551 QualType ElementType, bool isStatic,
552 int &nInitializers, bool &hadError);
553 void CheckConstantInitList(QualType DeclType, InitListExpr *IList,
554 QualType ElementType, bool isStatic,
555 int &nInitializers, bool &hadError);
556
Chris Lattner3429a812007-08-23 05:46:52 +0000557 /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
558 /// the specified width and sign. If an overflow occurs, detect it and emit
559 /// the specified diagnostic.
560 void ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &OldVal,
561 unsigned NewWidth, bool NewSign,
562 SourceLocation Loc, unsigned DiagID);
563
Chris Lattner2e64c072007-08-10 20:18:51 +0000564 //===--------------------------------------------------------------------===//
565 // Extra semantic analysis beyond the C type system
566 private:
567
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000568 bool CheckFunctionCall(Expr *Fn,
Ted Kremenek081ed872007-08-14 17:39:48 +0000569 SourceLocation LParenLoc, SourceLocation RParenLoc,
570 FunctionDecl *FDecl,
Chris Lattner2e64c072007-08-10 20:18:51 +0000571 Expr** Args, unsigned NumArgsInCall);
572
Ted Kremenek081ed872007-08-14 17:39:48 +0000573 void CheckPrintfArguments(Expr *Fn,
574 SourceLocation LParenLoc, SourceLocation RParenLoc,
575 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek30596542007-08-10 21:21:05 +0000576 unsigned format_idx, Expr** Args,
577 unsigned NumArgsInCall);
Ted Kremenek45925ab2007-08-17 16:46:58 +0000578
579 void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
580 SourceLocation ReturnLoc);
581
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000582
583 bool CheckBuiltinCFStringArgument(Expr* Arg);
Chris Lattner4b009652007-07-25 00:24:17 +0000584};
585
586
587} // end namespace clang
588
589#endif