blob: 488d105ded5ea7f9e30ec2df2240c4aa8f3c8106 [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;
Steve Naroff82113e32007-07-29 16:33:31 +000052
Chris Lattner4b009652007-07-25 00:24:17 +000053/// Sema - This implements semantic analysis and AST building for C.
54class Sema : public Action {
55 Preprocessor &PP;
56
57 ASTContext &Context;
58
59 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
60 /// the function decl for the function being parsed.
61 FunctionDecl *CurFunctionDecl;
62
63 /// LastInGroupList - This vector is populated when there are multiple
64 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
65 /// all but the last decl will be entered into this. This is used by the
66 /// ASTStreamer.
67 std::vector<Decl*> &LastInGroupList;
68
69 /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
70 /// it (which acts like the label decl in some ways). Forward referenced
71 /// labels have a LabelStmt created for them with a null location & SubStmt.
72 llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
73
74 llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
Steve Naroff82113e32007-07-29 16:33:31 +000075
76 /// OCUVectorDecls - This is a list all the OCU vector types. This allows
77 /// us to associate a raw vector type with one of the OCU type names.
78 /// This is only necessary for issuing pretty diagnostics.
79 llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls;
Chris Lattner2e64c072007-08-10 20:18:51 +000080
81 // Enum values used by KnownFunctionIDs (see below).
82 enum {
83 id_printf,
84 id_fprintf,
85 id_sprintf,
86 id_snprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000087 id_asprintf,
Ted Kremenek2d7e9532007-08-10 21:13:51 +000088 id_vsnprintf,
Chris Lattner2e64c072007-08-10 20:18:51 +000089 id_vasprintf,
90 id_vfprintf,
91 id_vsprintf,
92 id_vprintf,
93 id_num_known_functions
94 };
95
96 /// KnownFunctionIDs - This is a list of IdentifierInfo objects to a set
97 /// of known functions used by the semantic analysis to do various
98 /// kinds of checking (e.g. checking format string errors in printf calls).
99 /// This list is populated upon the creation of a Sema object.
100 IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ];
101
Chris Lattner4b009652007-07-25 00:24:17 +0000102public:
103 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
104
105 const LangOptions &getLangOptions() const;
106
107 /// The primitive diagnostic helpers - always returns true, which simplifies
108 /// error handling (i.e. less code).
109 bool Diag(SourceLocation Loc, unsigned DiagID);
110 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
111 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
112 const std::string &Msg2);
113
114 /// More expressive diagnostic helpers for expressions (say that 6 times:-)
115 bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1);
116 bool Diag(SourceLocation Loc, unsigned DiagID,
117 SourceRange R1, SourceRange R2);
118 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
119 SourceRange R1);
120 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
121 SourceRange R1, SourceRange R2);
122 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
123 const std::string &Msg2, SourceRange R1);
124 bool Diag(SourceLocation Loc, unsigned DiagID,
125 const std::string &Msg1, const std::string &Msg2,
126 SourceRange R1, SourceRange R2);
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000127
Chris Lattnerb26b7ad2007-08-31 04:53:24 +0000128 virtual void DeleteExpr(ExprTy *E);
129 virtual void DeleteStmt(StmtTy *S);
130
Chris Lattner4b009652007-07-25 00:24:17 +0000131 //===--------------------------------------------------------------------===//
132 // Type Analysis / Processing: SemaType.cpp.
133 //
134 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
135
Steve Naroff0acc9c92007-09-15 18:49:24 +0000136 virtual TypeResult ActOnTypeName(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000137
Steve Naroff0acc9c92007-09-15 18:49:24 +0000138 virtual TypeResult ActOnParamDeclaratorType(Scope *S, Declarator &D);
Chris Lattner4b009652007-07-25 00:24:17 +0000139private:
140 //===--------------------------------------------------------------------===//
141 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
142 //
143 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000144 virtual DeclTy *ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000145 void AddInitializerToDecl(DeclTy *dcl, ExprTy *init);
Chris Lattner4b009652007-07-25 00:24:17 +0000146 virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
147
148 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
149 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
150 virtual void PopScope(SourceLocation Loc, Scope *S);
151
152 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
153 /// no declarator (e.g. "struct foo;") is parsed.
154 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
155
Steve Naroff0acc9c92007-09-15 18:49:24 +0000156 virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattner4b009652007-07-25 00:24:17 +0000157 SourceLocation KWLoc, IdentifierInfo *Name,
158 SourceLocation NameLoc, AttributeList *Attr);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000159 virtual DeclTy *ActOnField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
Chris Lattner4b009652007-07-25 00:24:17 +0000160 Declarator &D, ExprTy *BitfieldWidth);
Steve Naroffffeaa552007-09-14 23:09:53 +0000161
162 // This is used for both record definitions and ObjC interface declarations.
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000163 virtual void ActOnFields(Scope* S,
164 SourceLocation RecLoc, DeclTy *TagDecl,
Steve Naroffffeaa552007-09-14 23:09:53 +0000165 DeclTy **Fields, unsigned NumFields,
166 tok::ObjCKeywordKind *visibility = 0);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000167 virtual DeclTy *ActOnEnumConstant(Scope *S, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000168 DeclTy *LastEnumConstant,
169 SourceLocation IdLoc, IdentifierInfo *Id,
170 SourceLocation EqualLoc, ExprTy *Val);
Steve Naroff0acc9c92007-09-15 18:49:24 +0000171 virtual void ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000172 DeclTy **Elements, unsigned NumElements);
173private:
Steve Naroff0acc9c92007-09-15 18:49:24 +0000174 /// Subroutines of ActOnDeclarator()...
Steve Naroff2591e1b2007-09-13 23:52:58 +0000175 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, ScopedDecl *LastDecl);
Steve Naroffcb597472007-09-13 21:41:19 +0000176 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *Old);
177 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, ScopedDecl *Old);
178 VarDecl *MergeVarDecl(VarDecl *New, ScopedDecl *Old);
Chris Lattner4b009652007-07-25 00:24:17 +0000179 /// AddTopLevelDecl - called after the decl has been fully processed.
180 /// Allows for bookkeeping and post-processing of each declaration.
181 void AddTopLevelDecl(Decl *current, Decl *last);
182
183 /// More parsing and symbol table subroutines...
184 ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
185 Scope *FnBodyScope);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000186 ScopedDecl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
187 SourceLocation IdLoc, Scope *S);
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000188 ObjcInterfaceDecl *getObjCInterfaceDecl(Scope *S,
189 IdentifierInfo *Id, SourceLocation IdLoc);
Fariborz Jahaniandd243ef2007-09-29 17:04:06 +0000190 ObjcProtocolDecl *getObjCProtocolDecl(Scope *S,
191 IdentifierInfo *Id, SourceLocation IdLoc);
Steve Naroffd21bc0d2007-09-13 18:10:37 +0000192 ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
Steve Narofff0c31dd2007-09-16 16:16:00 +0000193 ScopedDecl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
Chris Lattner4b009652007-07-25 00:24:17 +0000194 Scope *S);
195 // Decl attributes - this routine is the top level dispatcher.
196 void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
197 AttributeList *declarator_postfix);
198 void HandleDeclAttribute(Decl *New, AttributeList *rawAttr);
199
200 // HandleVectorTypeAttribute - this attribute is only applicable to
201 // integral and float scalars, although arrays, pointers, and function
202 // return values are allowed in conjunction with this construct. Aggregates
203 // with this attribute are invalid, even if they are of the same size as a
204 // corresponding scalar.
205 // The raw attribute should contain precisely 1 argument, the vector size
206 // for the variable, measured in bytes. If curType and rawAttr are well
207 // formed, this routine will return a new vector type.
208 QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr);
Steve Naroff82113e32007-07-29 16:33:31 +0000209 void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000210
Fariborz Jahanianf7cf3a62007-09-29 17:14:55 +0000211 /// CheckProtocolMethodDefs - This routine checks unimpletented methods
212 /// Declared in protocol, and those referenced by it.
213 void CheckProtocolMethodDefs(ObjcProtocolDecl *PDecl,
214 const llvm::DenseMap<void *, char>& InsMap,
215 const llvm::DenseMap<void *, char>& ClsMap);
216
217 /// ImplMethodsVsClassMethods - This is main routine to warn if any method
218 /// remains unimplemented in the @implementation class.
219 void ImplMethodsVsClassMethods(ObjcImplementationDecl* IMPDecl,
220 ObjcInterfaceDecl* IDecl);
221
Chris Lattner4b009652007-07-25 00:24:17 +0000222 //===--------------------------------------------------------------------===//
223 // Statement Parsing Callbacks: SemaStmt.cpp.
224public:
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000225 virtual StmtResult ActOnExprStmt(ExprTy *Expr);
Chris Lattner4b009652007-07-25 00:24:17 +0000226
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000227 virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc);
228 virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnerf2b07572007-08-31 21:49:55 +0000229 StmtTy **Elts, unsigned NumElts,
230 bool isStmtExpr);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000231 virtual StmtResult ActOnDeclStmt(DeclTy *Decl);
232 virtual StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000233 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
234 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000235 virtual StmtResult ActOnDefaultStmt(SourceLocation DefaultLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000236 SourceLocation ColonLoc, StmtTy *SubStmt,
237 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000238 virtual StmtResult ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
Chris Lattner4b009652007-07-25 00:24:17 +0000239 SourceLocation ColonLoc, StmtTy *SubStmt);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000240 virtual StmtResult ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
Chris Lattner4b009652007-07-25 00:24:17 +0000241 StmtTy *ThenVal, SourceLocation ElseLoc,
242 StmtTy *ElseVal);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000243 virtual StmtResult ActOnStartOfSwitchStmt(ExprTy *Cond);
244 virtual StmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
Chris Lattner4b009652007-07-25 00:24:17 +0000245 ExprTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000246 virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
Chris Lattner4b009652007-07-25 00:24:17 +0000247 StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000248 virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
Chris Lattner4b009652007-07-25 00:24:17 +0000249 SourceLocation WhileLoc, ExprTy *Cond);
250
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000251 virtual StmtResult ActOnForStmt(SourceLocation ForLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000252 SourceLocation LParenLoc,
253 StmtTy *First, ExprTy *Second, ExprTy *Third,
254 SourceLocation RParenLoc, StmtTy *Body);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000255 virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000256 SourceLocation LabelLoc,
257 IdentifierInfo *LabelII);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000258 virtual StmtResult ActOnIndirectGotoStmt(SourceLocation GotoLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000259 SourceLocation StarLoc,
260 ExprTy *DestExp);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000261 virtual StmtResult ActOnContinueStmt(SourceLocation ContinueLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000262 Scope *CurScope);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000263 virtual StmtResult ActOnBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000264
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000265 virtual StmtResult ActOnReturnStmt(SourceLocation ReturnLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000266 ExprTy *RetValExp);
267
268 //===--------------------------------------------------------------------===//
269 // Expression Parsing Callbacks: SemaExpr.cpp.
270
271 // Primary Expressions.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000272 virtual ExprResult ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000273 IdentifierInfo &II,
274 bool HasTrailingLParen);
Steve Naroff87d58b42007-09-16 03:34:24 +0000275 virtual ExprResult ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000276 tok::TokenKind Kind);
Steve Naroff87d58b42007-09-16 03:34:24 +0000277 virtual ExprResult ActOnNumericConstant(const Token &);
278 virtual ExprResult ActOnCharacterConstant(const Token &);
279 virtual ExprResult ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000280 ExprTy *Val);
281
Steve Naroff87d58b42007-09-16 03:34:24 +0000282 /// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +0000283 /// fragments (e.g. "foo" "bar" L"baz").
Steve Naroff87d58b42007-09-16 03:34:24 +0000284 virtual ExprResult ActOnStringLiteral(const Token *Toks, unsigned NumToks);
Chris Lattner4b009652007-07-25 00:24:17 +0000285
286 // Binary/Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +0000287 virtual ExprResult ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +0000288 ExprTy *Input);
289 virtual ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +0000290 ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000291 SourceLocation LParenLoc, TypeTy *Ty,
292 SourceLocation RParenLoc);
293
Steve Naroff87d58b42007-09-16 03:34:24 +0000294 virtual ExprResult ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000295 tok::TokenKind Kind, ExprTy *Input);
296
Steve Naroff87d58b42007-09-16 03:34:24 +0000297 virtual ExprResult ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000298 ExprTy *Idx, SourceLocation RLoc);
Steve Naroff87d58b42007-09-16 03:34:24 +0000299 virtual ExprResult ActOnMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000300 tok::TokenKind OpKind,
301 SourceLocation MemberLoc,
302 IdentifierInfo &Member);
303
Steve Naroff87d58b42007-09-16 03:34:24 +0000304 /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000305 /// This provides the location of the left/right parens and a list of comma
306 /// locations.
Steve Naroff87d58b42007-09-16 03:34:24 +0000307 virtual ExprResult ActOnCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000308 ExprTy **Args, unsigned NumArgs,
309 SourceLocation *CommaLocs,
310 SourceLocation RParenLoc);
311
Steve Naroff87d58b42007-09-16 03:34:24 +0000312 virtual ExprResult ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000313 SourceLocation RParenLoc, ExprTy *Op);
314
Steve Naroff87d58b42007-09-16 03:34:24 +0000315 virtual ExprResult ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000316 SourceLocation RParenLoc, ExprTy *Op);
317
Steve Naroff87d58b42007-09-16 03:34:24 +0000318 virtual ExprResult ActOnInitList(SourceLocation LParenLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000319 ExprTy **InitList, unsigned NumInit,
320 SourceLocation RParenLoc);
321
Steve Naroff87d58b42007-09-16 03:34:24 +0000322 virtual ExprResult ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000323 ExprTy *LHS,ExprTy *RHS);
324
Steve Naroff87d58b42007-09-16 03:34:24 +0000325 /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000326 /// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000327 virtual ExprResult ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000328 SourceLocation ColonLoc,
329 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
330
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000331 /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
332 virtual ExprResult ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000333 IdentifierInfo *LabelII);
334
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000335 virtual ExprResult ActOnStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt,
Chris Lattner4b009652007-07-25 00:24:17 +0000336 SourceLocation RPLoc); // "({..})"
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000337
338 /// __builtin_offsetof(type, a.b[123][456].c)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000339 virtual ExprResult ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000340 SourceLocation TypeLoc, TypeTy *Arg1,
341 OffsetOfComponent *CompPtr,
342 unsigned NumComponents,
343 SourceLocation RParenLoc);
344
Steve Naroff63bad2d2007-08-01 22:05:33 +0000345 // __builtin_types_compatible_p(type1, type2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000346 virtual ExprResult ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +0000347 TypeTy *arg1, TypeTy *arg2,
348 SourceLocation RPLoc);
Steve Naroff93c53012007-08-03 21:21:27 +0000349
350 // __builtin_choose_expr(constExpr, expr1, expr2)
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000351 virtual ExprResult ActOnChooseExpr(SourceLocation BuiltinLoc,
Steve Naroff93c53012007-08-03 21:21:27 +0000352 ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
353 SourceLocation RPLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000354
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000355 /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
356 virtual ExprResult ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +0000357 SourceLocation LAngleBracketLoc, TypeTy *Ty,
358 SourceLocation RAngleBracketLoc,
359 SourceLocation LParenLoc, ExprTy *E,
360 SourceLocation RParenLoc);
361
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000362 /// ActOnCXXBoolLiteral - Parse {true,false} literals.
363 virtual ExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000364 tok::TokenKind Kind);
Anders Carlssona66cad42007-08-21 17:43:55 +0000365
366 // ParseObjCStringLiteral - Parse Objective-C string literals.
367 virtual ExprResult ParseObjCStringLiteral(ExprTy *string);
Anders Carlsson8be1d402007-08-22 15:14:15 +0000368 virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc,
369 SourceLocation LParenLoc,
370 TypeTy *Ty,
371 SourceLocation RParenLoc);
372
Steve Naroff81f1bba2007-09-06 21:24:23 +0000373 // Objective-C declarations.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000374 virtual DeclTy *ObjcStartClassInterface(Scope* S,
375 SourceLocation AtInterafceLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +0000376 IdentifierInfo *ClassName, SourceLocation ClassLoc,
377 IdentifierInfo *SuperName, SourceLocation SuperLoc,
378 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
379 AttributeList *AttrList);
380
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000381 virtual DeclTy *ObjcStartProtoInterface(Scope* S,
382 SourceLocation AtProtoInterfaceLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000383 IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
384 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
385
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000386 virtual DeclTy *ObjcStartCatInterface(Scope* S,
387 SourceLocation AtInterfaceLoc,
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000388 IdentifierInfo *ClassName, SourceLocation ClassLoc,
389 IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
390 IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs);
391
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000392 virtual DeclTy *ObjcStartClassImplementation(Scope* S,
393 SourceLocation AtClassImplLoc,
394 IdentifierInfo *ClassName, SourceLocation ClassLoc,
395 IdentifierInfo *SuperClassname,
396 SourceLocation SuperClassLoc);
397
Steve Naroff81f1bba2007-09-06 21:24:23 +0000398 virtual DeclTy *ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
399 IdentifierInfo **IdentList,
400 unsigned NumElts);
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000401
402 virtual DeclTy *ObjcForwardProtocolDeclaration(Scope *S,
403 SourceLocation AtProtocolLoc,
404 IdentifierInfo **IdentList,
405 unsigned NumElts);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000406
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000407 virtual void ObjcAddMethodsToClass(Scope* S, DeclTy *ClassDecl,
Steve Naroff75494892007-09-11 21:17:26 +0000408 DeclTy **allMethods, unsigned allNum);
Fariborz Jahaniand34caf92007-09-26 18:27:25 +0000409
410 virtual void ActOnImpleIvarVsClassIvars(DeclTy *ClassDecl,
411 DeclTy **Fields, unsigned NumFields);
412
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000413 virtual DeclTy *ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
Steve Naroff6cb1d362007-09-28 22:22:11 +0000414 tok::TokenKind MethodType, TypeTy *ReturnType, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000415 // optional arguments. The number of types/arguments is obtained
416 // from the Sel.getNumArgs().
417 TypeTy **ArgTypes, IdentifierInfo **ArgNames,
418 AttributeList *AttrList, tok::ObjCKeywordKind MethodImplKind);
Steve Naroffd3f5ee42007-09-17 21:01:15 +0000419
Steve Naroff4ed9d662007-09-27 14:38:14 +0000420 // ActOnClassMessage - used for both unary and keyword messages.
421 // ArgExprs is optional - if it is present, the number of expressions
422 // is obtained from Sel.getNumArgs().
423 virtual ExprResult ActOnClassMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000424 IdentifierInfo *receivingClassName, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000425 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
426
427 // ActOnInstanceMessage - used for both unary and keyword messages.
428 // ArgExprs is optional - if it is present, the number of expressions
429 // is obtained from Sel.getNumArgs().
430 virtual ExprResult ActOnInstanceMessage(
Steve Naroff6cb1d362007-09-28 22:22:11 +0000431 ExprTy *receiver, Selector Sel,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000432 SourceLocation lbrac, SourceLocation rbrac, ExprTy **ArgExprs);
Chris Lattner4b009652007-07-25 00:24:17 +0000433private:
434 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
435 // functions and arrays to their respective pointers (C99 6.3.2.1).
436 void UsualUnaryConversions(Expr *&expr);
437
438 // DefaultFunctionArrayConversion - converts functions and arrays
439 // to their respective pointers (C99 6.3.2.1).
440 void DefaultFunctionArrayConversion(Expr *&expr);
441
Steve Naroffdb65e052007-08-28 23:30:39 +0000442 // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
443 // do not have a prototype. Integer promotions are performed on each
444 // argument, and arguments that have type float are promoted to double.
445 void DefaultArgumentPromotion(Expr *&expr);
446
Chris Lattner4b009652007-07-25 00:24:17 +0000447 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
448 // operands and then handles various conversions that are common to binary
449 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
450 // routine returns the first non-arithmetic type found. The client is
451 // responsible for emitting appropriate error diagnostics.
Steve Naroff8f708362007-08-24 19:07:16 +0000452 QualType UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr,
453 bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000454 enum AssignmentCheckResult {
455 Compatible,
456 Incompatible,
457 PointerFromInt,
458 IntFromPointer,
459 IncompatiblePointer,
460 CompatiblePointerDiscardsQualifiers
461 };
462 // CheckAssignmentConstraints - Perform type checking for assignment,
463 // argument passing, variable initialization, and function return values.
464 // This routine is only used by the following two methods. C99 6.5.16.
465 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
466
Steve Naroff87d58b42007-09-16 03:34:24 +0000467 // CheckSingleAssignmentConstraints - Currently used by ActOnCallExpr,
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000468 // CheckAssignmentOperands, and ActOnReturnStmt. Prior to type checking,
Chris Lattner4b009652007-07-25 00:24:17 +0000469 // this routine performs the default function/array converions.
470 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
471 Expr *&rExpr);
472 // CheckCompoundAssignmentConstraints - Type check without performing any
473 // conversions. For compound assignments, the "Check...Operands" methods
474 // perform the necessary conversions.
475 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
476 QualType rhs);
477
478 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
479 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
480 QualType rhsType);
481
482 /// the following "Check" methods will return a valid/converted QualType
483 /// or a null QualType (indicating an error diagnostic was issued).
484
Steve Naroff87d58b42007-09-16 03:34:24 +0000485 /// type checking binary operators (subroutines of ActOnBinOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000486 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
487 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
488 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
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 CheckRemainderOperands( // C99 6.5.5
Steve Naroff8f708362007-08-24 19:07:16 +0000491 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000492 inline QualType CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000493 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000494 inline QualType CheckSubtractionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +0000495 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000496 inline QualType CheckShiftOperands( // C99 6.5.7
Steve Naroff8f708362007-08-24 19:07:16 +0000497 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner254f3bc2007-08-26 01:18:55 +0000498 inline QualType CheckCompareOperands( // C99 6.5.8/9
499 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isRelational);
Chris Lattner4b009652007-07-25 00:24:17 +0000500 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
Steve Naroff8f708362007-08-24 19:07:16 +0000501 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattner4b009652007-07-25 00:24:17 +0000502 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
503 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
504 // CheckAssignmentOperands is used for both simple and compound assignment.
505 // For simple assignment, pass both expressions and a null converted type.
506 // For compound assignment, pass both expressions and the converted type.
507 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
Steve Naroff0f32f432007-08-24 22:33:52 +0000508 Expr *lex, Expr *&rex, SourceLocation OpLoc, QualType convertedType);
Chris Lattner4b009652007-07-25 00:24:17 +0000509 inline QualType CheckCommaOperands( // C99 6.5.17
510 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
511 inline QualType CheckConditionalOperands( // C99 6.5.15
512 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
513
Steve Naroff87d58b42007-09-16 03:34:24 +0000514 /// type checking unary operators (subroutines of ActOnUnaryOp).
Chris Lattner4b009652007-07-25 00:24:17 +0000515 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
516 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
517 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
518 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
519 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
520 bool isSizeof);
Chris Lattner5110ad52007-08-24 21:41:10 +0000521 QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000522
523 /// type checking primary expressions.
524 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
525 IdentifierInfo &Comp, SourceLocation CmpLoc);
526
Steve Naroffe14e5542007-09-02 02:04:30 +0000527 /// type checking declaration initializers (C99 6.7.8)
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000528 bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
Steve Naroff1c9de712007-09-03 01:24:23 +0000529 bool isStatic);
Steve Naroffe6a8c9b2007-09-04 14:36:54 +0000530 bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
531 bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
532 bool isStatic, QualType ElementType);
Steve Naroff509d0b52007-09-04 02:20:04 +0000533 void CheckVariableInitList(QualType DeclType, InitListExpr *IList,
534 QualType ElementType, bool isStatic,
535 int &nInitializers, bool &hadError);
536 void CheckConstantInitList(QualType DeclType, InitListExpr *IList,
537 QualType ElementType, bool isStatic,
538 int &nInitializers, bool &hadError);
539
Chris Lattner3429a812007-08-23 05:46:52 +0000540 /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
541 /// the specified width and sign. If an overflow occurs, detect it and emit
542 /// the specified diagnostic.
543 void ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &OldVal,
544 unsigned NewWidth, bool NewSign,
545 SourceLocation Loc, unsigned DiagID);
546
Chris Lattner2e64c072007-08-10 20:18:51 +0000547 //===--------------------------------------------------------------------===//
548 // Extra semantic analysis beyond the C type system
549 private:
550
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000551 bool CheckFunctionCall(Expr *Fn,
Ted Kremenek081ed872007-08-14 17:39:48 +0000552 SourceLocation LParenLoc, SourceLocation RParenLoc,
553 FunctionDecl *FDecl,
Chris Lattner2e64c072007-08-10 20:18:51 +0000554 Expr** Args, unsigned NumArgsInCall);
555
Ted Kremenek081ed872007-08-14 17:39:48 +0000556 void CheckPrintfArguments(Expr *Fn,
557 SourceLocation LParenLoc, SourceLocation RParenLoc,
558 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek30596542007-08-10 21:21:05 +0000559 unsigned format_idx, Expr** Args,
560 unsigned NumArgsInCall);
Ted Kremenek45925ab2007-08-17 16:46:58 +0000561
562 void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
563 SourceLocation ReturnLoc);
564
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000565
566 bool CheckBuiltinCFStringArgument(Expr* Arg);
Chris Lattner4b009652007-07-25 00:24:17 +0000567};
568
569
570} // end namespace clang
571
572#endif