blob: e304f07eeaebb0081d885c4f0893dc65147f62c8 [file] [log] [blame]
Chris Lattnercc67ec12006-11-09 06:54:47 +00001//===--- Sema.h - Semantic Analysis & AST Building --------------*- C++ -*-===//
Chris Lattner7cee11f2006-11-03 06:42:29 +00002//
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//
Chris Lattnercc67ec12006-11-09 06:54:47 +000010// This file defines the Sema class, which performs semantic analysis and
11// builds ASTs.
Chris Lattner7cee11f2006-11-03 06:42:29 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnercc67ec12006-11-09 06:54:47 +000015#ifndef LLVM_CLANG_AST_SEMA_H
16#define LLVM_CLANG_AST_SEMA_H
Chris Lattner7cee11f2006-11-03 06:42:29 +000017
18#include "clang/Parse/Action.h"
Chris Lattnere2473062007-05-28 06:28:18 +000019#include "llvm/ADT/DenseMap.h"
Anders Carlsson51873c22007-07-22 07:07:56 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattner7cee11f2006-11-03 06:42:29 +000021#include <vector>
Chris Lattnereaafe1222006-11-10 05:17:58 +000022#include <string>
Chris Lattner7cee11f2006-11-03 06:42:29 +000023
Chris Lattnerfc1c44a2007-08-23 05:46:52 +000024namespace llvm {
25 class APSInt;
26}
27
Chris Lattner7cee11f2006-11-03 06:42:29 +000028namespace clang {
Chris Lattnercb6a3822006-11-10 06:20:45 +000029 class ASTContext;
Chris Lattner7cee11f2006-11-03 06:42:29 +000030 class Preprocessor;
31 class Decl;
Steve Naroff9324db12007-09-13 18:10:37 +000032 class ScopedDecl;
Steve Naroff78403362007-04-02 22:55:05 +000033 class Expr;
Steve Narofff33527a2007-09-02 15:34:30 +000034 class InitListExpr;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +000035 class VarDecl;
Chris Lattner53621a52007-06-13 20:44:40 +000036 class ParmVarDecl;
Chris Lattner01564d92007-01-27 19:27:06 +000037 class TypedefDecl;
38 class FunctionDecl;
Steve Naroffe5aa9be2007-04-05 22:36:20 +000039 class QualType;
Chris Lattner9cf21c52007-09-04 02:45:27 +000040 struct LangOptions;
41 struct DeclaratorChunk;
Chris Lattner146762e2007-07-20 16:59:19 +000042 class Token;
Steve Naroff35d85152007-05-07 00:24:15 +000043 class IntegerLiteral;
Steve Naroff8eeeb132007-05-08 21:09:37 +000044 class ArrayType;
Chris Lattnere2473062007-05-28 06:28:18 +000045 class LabelStmt;
Anders Carlsson51873c22007-07-22 07:07:56 +000046 class SwitchStmt;
Steve Narofff8fd09e2007-07-27 22:15:19 +000047 class OCUVectorType;
Steve Naroffddf5a1d2007-07-29 16:33:31 +000048 class TypedefDecl;
49
Chris Lattnercc67ec12006-11-09 06:54:47 +000050/// Sema - This implements semantic analysis and AST building for C.
51class Sema : public Action {
Steve Naroff38d31b42007-02-28 01:22:02 +000052 Preprocessor &PP;
53
Chris Lattnercb6a3822006-11-10 06:20:45 +000054 ASTContext &Context;
Chris Lattner7cee11f2006-11-03 06:42:29 +000055
Chris Lattner229ce602006-11-21 01:21:07 +000056 /// CurFunctionDecl - If inside of a function body, this contains a pointer to
57 /// the function decl for the function being parsed.
58 FunctionDecl *CurFunctionDecl;
59
Chris Lattner7cee11f2006-11-03 06:42:29 +000060 /// LastInGroupList - This vector is populated when there are multiple
61 /// declarators in a single decl group (e.g. "int A, B, C"). In this case,
62 /// all but the last decl will be entered into this. This is used by the
63 /// ASTStreamer.
64 std::vector<Decl*> &LastInGroupList;
Chris Lattnere2473062007-05-28 06:28:18 +000065
66 /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
67 /// it (which acts like the label decl in some ways). Forward referenced
68 /// labels have a LabelStmt created for them with a null location & SubStmt.
Chris Lattner23b7eb62007-06-15 23:05:46 +000069 llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
Anders Carlsson51873c22007-07-22 07:07:56 +000070
71 llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
Steve Naroffddf5a1d2007-07-29 16:33:31 +000072
73 /// OCUVectorDecls - This is a list all the OCU vector types. This allows
74 /// us to associate a raw vector type with one of the OCU type names.
75 /// This is only necessary for issuing pretty diagnostics.
76 llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls;
Chris Lattnerb87b1b32007-08-10 20:18:51 +000077
78 // Enum values used by KnownFunctionIDs (see below).
79 enum {
80 id_printf,
81 id_fprintf,
82 id_sprintf,
83 id_snprintf,
Chris Lattnerb87b1b32007-08-10 20:18:51 +000084 id_asprintf,
Ted Kremenekcfc94192007-08-10 21:13:51 +000085 id_vsnprintf,
Chris Lattnerb87b1b32007-08-10 20:18:51 +000086 id_vasprintf,
87 id_vfprintf,
88 id_vsprintf,
89 id_vprintf,
90 id_num_known_functions
91 };
92
93 /// KnownFunctionIDs - This is a list of IdentifierInfo objects to a set
94 /// of known functions used by the semantic analysis to do various
95 /// kinds of checking (e.g. checking format string errors in printf calls).
96 /// This list is populated upon the creation of a Sema object.
97 IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ];
98
Chris Lattner7cee11f2006-11-03 06:42:29 +000099public:
Steve Naroff2c055d22007-02-28 19:32:13 +0000100 Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000101
Chris Lattnerac18be92006-11-20 06:49:47 +0000102 const LangOptions &getLangOptions() const;
103
Steve Naroff71ce2e02007-05-18 22:53:50 +0000104 /// The primitive diagnostic helpers - always returns true, which simplifies
105 /// error handling (i.e. less code).
Chris Lattnerd6647d32007-05-16 17:56:50 +0000106 bool Diag(SourceLocation Loc, unsigned DiagID);
107 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
108 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
109 const std::string &Msg2);
Steve Naroff71ce2e02007-05-18 22:53:50 +0000110
111 /// More expressive diagnostic helpers for expressions (say that 6 times:-)
112 bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1);
113 bool Diag(SourceLocation Loc, unsigned DiagID,
114 SourceRange R1, SourceRange R2);
115 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
116 SourceRange R1);
117 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
118 SourceRange R1, SourceRange R2);
119 bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
120 const std::string &Msg2, SourceRange R1);
121 bool Diag(SourceLocation Loc, unsigned DiagID,
122 const std::string &Msg1, const std::string &Msg2,
123 SourceRange R1, SourceRange R2);
124
Chris Lattner57c523f2007-08-31 04:53:24 +0000125 virtual void DeleteExpr(ExprTy *E);
126 virtual void DeleteStmt(StmtTy *S);
127
Chris Lattner7cee11f2006-11-03 06:42:29 +0000128 //===--------------------------------------------------------------------===//
Chris Lattnerf84a79c2006-11-11 22:59:23 +0000129 // Type Analysis / Processing: SemaType.cpp.
130 //
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000131 QualType GetTypeForDeclarator(Declarator &D, Scope *S);
Chris Lattnerf84a79c2006-11-11 22:59:23 +0000132
Steve Naroff30d242c2007-09-15 18:49:24 +0000133 virtual TypeResult ActOnTypeName(Scope *S, Declarator &D);
Chris Lattnerf84a79c2006-11-11 22:59:23 +0000134
Steve Naroff30d242c2007-09-15 18:49:24 +0000135 virtual TypeResult ActOnParamDeclaratorType(Scope *S, Declarator &D);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000136private:
Chris Lattnerf84a79c2006-11-11 22:59:23 +0000137 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +0000138 // Symbol table / Decl tracking callbacks: SemaDecl.cpp.
Chris Lattner7cee11f2006-11-03 06:42:29 +0000139 //
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000140 virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const;
Steve Naroff30d242c2007-09-15 18:49:24 +0000141 virtual DeclTy *ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000142 void AddInitializerToDecl(DeclTy *dcl, ExprTy *init);
Chris Lattner776fac82007-06-09 00:53:06 +0000143 virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
144
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +0000145 virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D);
Chris Lattner229ce602006-11-21 01:21:07 +0000146 virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000147 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner01564d92007-01-27 19:27:06 +0000148
Chris Lattner200bdc32006-11-19 02:43:37 +0000149 /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
150 /// no declarator (e.g. "struct foo;") is parsed.
151 virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS);
152
Steve Naroff30d242c2007-09-15 18:49:24 +0000153 virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK,
Chris Lattnerf34c4da2007-01-23 04:08:05 +0000154 SourceLocation KWLoc, IdentifierInfo *Name,
Steve Naroffb3096442007-06-09 03:47:53 +0000155 SourceLocation NameLoc, AttributeList *Attr);
Steve Naroff30d242c2007-09-15 18:49:24 +0000156 virtual DeclTy *ActOnField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart,
Chris Lattner1300fb92007-01-23 23:42:53 +0000157 Declarator &D, ExprTy *BitfieldWidth);
Steve Naroff2e688fd2007-09-14 23:09:53 +0000158
159 // This is used for both record definitions and ObjC interface declarations.
Steve Naroff30d242c2007-09-15 18:49:24 +0000160 virtual void ActOnFields(SourceLocation RecLoc, DeclTy *TagDecl,
Steve Naroff2e688fd2007-09-14 23:09:53 +0000161 DeclTy **Fields, unsigned NumFields,
162 tok::ObjCKeywordKind *visibility = 0);
Steve Naroff30d242c2007-09-15 18:49:24 +0000163 virtual DeclTy *ActOnEnumConstant(Scope *S, DeclTy *EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000164 DeclTy *LastEnumConstant,
Chris Lattnerc1915e22007-01-25 07:29:02 +0000165 SourceLocation IdLoc, IdentifierInfo *Id,
166 SourceLocation EqualLoc, ExprTy *Val);
Steve Naroff30d242c2007-09-15 18:49:24 +0000167 virtual void ActOnEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl,
Chris Lattnerc1915e22007-01-25 07:29:02 +0000168 DeclTy **Elements, unsigned NumElements);
Steve Naroff3273c222007-03-14 21:52:03 +0000169private:
Steve Naroff30d242c2007-09-15 18:49:24 +0000170 /// Subroutines of ActOnDeclarator()...
Steve Naroffa23cc792007-09-13 23:52:58 +0000171 TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, ScopedDecl *LastDecl);
Steve Naroff9def2b12007-09-13 21:41:19 +0000172 TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, ScopedDecl *Old);
173 FunctionDecl *MergeFunctionDecl(FunctionDecl *New, ScopedDecl *Old);
174 VarDecl *MergeVarDecl(VarDecl *New, ScopedDecl *Old);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000175 /// AddTopLevelDecl - called after the decl has been fully processed.
176 /// Allows for bookkeeping and post-processing of each declaration.
177 void AddTopLevelDecl(Decl *current, Decl *last);
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000178
Steve Naroff3273c222007-03-14 21:52:03 +0000179 /// More parsing and symbol table subroutines...
Chris Lattner53621a52007-06-13 20:44:40 +0000180 ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo,
181 Scope *FnBodyScope);
Steve Naroff9324db12007-09-13 18:10:37 +0000182 ScopedDecl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI,
183 SourceLocation IdLoc, Scope *S);
184 ScopedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S);
Steve Naroff3273c222007-03-14 21:52:03 +0000185 Decl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II,
186 Scope *S);
Steve Naroffa8fd9732007-06-11 00:35:03 +0000187 // Decl attributes - this routine is the top level dispatcher.
188 void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
189 AttributeList *declarator_postfix);
190 void HandleDeclAttribute(Decl *New, AttributeList *rawAttr);
191
192 // HandleVectorTypeAttribute - this attribute is only applicable to
193 // integral and float scalars, although arrays, pointers, and function
194 // return values are allowed in conjunction with this construct. Aggregates
195 // with this attribute are invalid, even if they are of the same size as a
196 // corresponding scalar.
197 // The raw attribute should contain precisely 1 argument, the vector size
198 // for the variable, measured in bytes. If curType and rawAttr are well
199 // formed, this routine will return a new vector type.
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000200 QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr);
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000201 void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr);
Chris Lattner1300fb92007-01-23 23:42:53 +0000202
Chris Lattner7cee11f2006-11-03 06:42:29 +0000203 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +0000204 // Statement Parsing Callbacks: SemaStmt.cpp.
Steve Naroff3273c222007-03-14 21:52:03 +0000205public:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000206 virtual StmtResult ParseExprStmt(ExprTy *Expr);
207
Chris Lattner0f203a72007-05-28 01:45:28 +0000208 virtual StmtResult ParseNullStmt(SourceLocation SemiLoc);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000209 virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
Chris Lattnercac27a52007-08-31 21:49:55 +0000210 StmtTy **Elts, unsigned NumElts,
211 bool isStmtExpr);
Steve Naroff2a8ad182007-05-29 22:59:26 +0000212 virtual StmtResult ParseDeclStmt(DeclTy *Decl);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000213 virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
214 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
215 SourceLocation ColonLoc, StmtTy *SubStmt);
216 virtual StmtResult ParseDefaultStmt(SourceLocation DefaultLoc,
Chris Lattner46eeb222007-07-18 02:28:47 +0000217 SourceLocation ColonLoc, StmtTy *SubStmt,
218 Scope *CurScope);
Chris Lattner6c0ff132006-11-05 00:19:50 +0000219 virtual StmtResult ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
220 SourceLocation ColonLoc, StmtTy *SubStmt);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000221 virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
222 StmtTy *ThenVal, SourceLocation ElseLoc,
223 StmtTy *ElseVal);
Anders Carlsson51873c22007-07-22 07:07:56 +0000224 virtual StmtResult StartSwitchStmt(ExprTy *Cond);
225 virtual StmtResult FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
226 ExprTy *Body);
Chris Lattner85ed8732006-11-04 20:40:44 +0000227 virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond,
228 StmtTy *Body);
229 virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
230 SourceLocation WhileLoc, ExprTy *Cond);
231
Chris Lattner71e23ce2006-11-04 20:18:38 +0000232 virtual StmtResult ParseForStmt(SourceLocation ForLoc,
233 SourceLocation LParenLoc,
234 StmtTy *First, ExprTy *Second, ExprTy *Third,
235 SourceLocation RParenLoc, StmtTy *Body);
Chris Lattner16976d32006-11-05 01:46:01 +0000236 virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc,
237 SourceLocation LabelLoc,
238 IdentifierInfo *LabelII);
239 virtual StmtResult ParseIndirectGotoStmt(SourceLocation GotoLoc,
240 SourceLocation StarLoc,
241 ExprTy *DestExp);
Chris Lattnereaafe1222006-11-10 05:17:58 +0000242 virtual StmtResult ParseContinueStmt(SourceLocation ContinueLoc,
243 Scope *CurScope);
244 virtual StmtResult ParseBreakStmt(SourceLocation GotoLoc, Scope *CurScope);
Chris Lattner71e23ce2006-11-04 20:18:38 +0000245
Chris Lattner7cee11f2006-11-03 06:42:29 +0000246 virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
247 ExprTy *RetValExp);
248
249 //===--------------------------------------------------------------------===//
Chris Lattnere168f762006-11-10 05:29:30 +0000250 // Expression Parsing Callbacks: SemaExpr.cpp.
Chris Lattner7cee11f2006-11-03 06:42:29 +0000251
252 // Primary Expressions.
Steve Naroff30d242c2007-09-15 18:49:24 +0000253 virtual ExprResult ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +0000254 IdentifierInfo &II,
255 bool HasTrailingLParen);
Anders Carlsson625bfc82007-07-21 05:21:51 +0000256 virtual ExprResult ParsePreDefinedExpr(SourceLocation Loc,
Chris Lattner7cee11f2006-11-03 06:42:29 +0000257 tok::TokenKind Kind);
Chris Lattner146762e2007-07-20 16:59:19 +0000258 virtual ExprResult ParseNumericConstant(const Token &);
259 virtual ExprResult ParseCharacterConstant(const Token &);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000260 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
261 ExprTy *Val);
Chris Lattner697e5d62006-11-09 06:32:27 +0000262
Steve Naroffdf7855b2007-02-21 23:46:25 +0000263 /// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner697e5d62006-11-09 06:32:27 +0000264 /// fragments (e.g. "foo" "bar" L"baz").
Chris Lattner146762e2007-07-20 16:59:19 +0000265 virtual ExprResult ParseStringLiteral(const Token *Toks, unsigned NumToks);
Chris Lattner697e5d62006-11-09 06:32:27 +0000266
Chris Lattner7cee11f2006-11-03 06:42:29 +0000267 // Binary/Unary Operators. 'Tok' is the token for the operator.
268 virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
269 ExprTy *Input);
270 virtual ExprResult
271 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
272 SourceLocation LParenLoc, TypeTy *Ty,
273 SourceLocation RParenLoc);
274
275 virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc,
276 tok::TokenKind Kind, ExprTy *Input);
277
278 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
279 ExprTy *Idx, SourceLocation RLoc);
280 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
281 tok::TokenKind OpKind,
282 SourceLocation MemberLoc,
283 IdentifierInfo &Member);
284
285 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
286 /// This provides the location of the left/right parens and a list of comma
287 /// locations.
288 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
289 ExprTy **Args, unsigned NumArgs,
290 SourceLocation *CommaLocs,
291 SourceLocation RParenLoc);
292
293 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
294 SourceLocation RParenLoc, ExprTy *Op);
Steve Narofffbd09832007-07-19 01:06:55 +0000295
296 virtual ExprResult ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
297 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000298
Steve Narofffbd09832007-07-19 01:06:55 +0000299 virtual ExprResult ParseInitList(SourceLocation LParenLoc,
300 ExprTy **InitList, unsigned NumInit,
301 SourceLocation RParenLoc);
302
Chris Lattner7cee11f2006-11-03 06:42:29 +0000303 virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
304 ExprTy *LHS,ExprTy *RHS);
305
306 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
307 /// in the case of a the GNU conditional expr extension.
308 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
309 SourceLocation ColonLoc,
310 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattner29375652006-12-04 18:06:35 +0000311
Chris Lattnereefa10e2007-05-28 06:56:27 +0000312 /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo".
313 virtual ExprResult ParseAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc,
314 IdentifierInfo *LabelII);
315
Chris Lattner366727f2007-07-24 16:58:17 +0000316 virtual ExprResult ParseStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt,
317 SourceLocation RPLoc); // "({..})"
Chris Lattnerf17bd422007-08-30 17:45:32 +0000318
319 /// __builtin_offsetof(type, a.b[123][456].c)
320 virtual ExprResult ParseBuiltinOffsetOf(SourceLocation BuiltinLoc,
321 SourceLocation TypeLoc, TypeTy *Arg1,
322 OffsetOfComponent *CompPtr,
323 unsigned NumComponents,
324 SourceLocation RParenLoc);
325
Steve Naroff78864672007-08-01 22:05:33 +0000326 // __builtin_types_compatible_p(type1, type2)
Steve Naroff788d8642007-08-01 23:45:51 +0000327 virtual ExprResult ParseTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +0000328 TypeTy *arg1, TypeTy *arg2,
329 SourceLocation RPLoc);
Steve Naroff9efdabc2007-08-03 21:21:27 +0000330
331 // __builtin_choose_expr(constExpr, expr1, expr2)
332 virtual ExprResult ParseChooseExpr(SourceLocation BuiltinLoc,
333 ExprTy *cond, ExprTy *expr1, ExprTy *expr2,
334 SourceLocation RPLoc);
Chris Lattner366727f2007-07-24 16:58:17 +0000335
Chris Lattner29375652006-12-04 18:06:35 +0000336 /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
337 virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
338 SourceLocation LAngleBracketLoc, TypeTy *Ty,
339 SourceLocation RAngleBracketLoc,
340 SourceLocation LParenLoc, ExprTy *E,
341 SourceLocation RParenLoc);
Bill Wendling4073ed52007-02-13 01:51:42 +0000342
343 /// ParseCXXBoolLiteral - Parse {true,false} literals.
344 virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc,
Bill Wendlingbf313b02007-02-13 20:09:46 +0000345 tok::TokenKind Kind);
Anders Carlsson76f4a902007-08-21 17:43:55 +0000346
347 // ParseObjCStringLiteral - Parse Objective-C string literals.
348 virtual ExprResult ParseObjCStringLiteral(ExprTy *string);
Anders Carlssonc5a81eb2007-08-22 15:14:15 +0000349 virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc,
350 SourceLocation LParenLoc,
351 TypeTy *Ty,
352 SourceLocation RParenLoc);
353
Steve Naroff09bf8152007-09-06 21:24:23 +0000354 // Objective-C declarations.
355 virtual DeclTy *ObjcStartClassInterface(SourceLocation AtInterafceLoc,
356 IdentifierInfo *ClassName, SourceLocation ClassLoc,
357 IdentifierInfo *SuperName, SourceLocation SuperLoc,
358 IdentifierInfo **ProtocolNames, unsigned NumProtocols,
359 AttributeList *AttrList);
360
361 virtual DeclTy *ObjcClassDeclaration(Scope *S, SourceLocation AtClassLoc,
362 IdentifierInfo **IdentList,
363 unsigned NumElts);
364
Steve Narofff2fb4ad2007-09-11 21:17:26 +0000365 virtual void ObjcAddMethodsToClass(DeclTy *ClassDecl,
366 DeclTy **allMethods, unsigned allNum);
Fariborz Jahaniancfb31fa2007-09-12 18:23:47 +0000367 virtual DeclTy *ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
368 tok::TokenKind MethodType, TypeTy *ReturnType,
369 ObjcKeywordInfo *Keywords, unsigned NumKeywords,
370 AttributeList *AttrList);
371 virtual DeclTy *ObjcBuildMethodDeclaration(SourceLocation MethodLoc,
372 tok::TokenKind MethodType, TypeTy *ReturnType,
373 IdentifierInfo *SelectorName, AttributeList *AttrList);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000374private:
Steve Naroff71b59a92007-06-04 22:22:31 +0000375 // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
Steve Naroff31090012007-07-16 21:54:35 +0000376 // functions and arrays to their respective pointers (C99 6.3.2.1).
377 void UsualUnaryConversions(Expr *&expr);
378
379 // DefaultFunctionArrayConversion - converts functions and arrays
380 // to their respective pointers (C99 6.3.2.1).
381 void DefaultFunctionArrayConversion(Expr *&expr);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000382
Steve Naroff0b661582007-08-28 23:30:39 +0000383 // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
384 // do not have a prototype. Integer promotions are performed on each
385 // argument, and arguments that have type float are promoted to double.
386 void DefaultArgumentPromotion(Expr *&expr);
387
Steve Naroff71b59a92007-06-04 22:22:31 +0000388 // UsualArithmeticConversions - performs the UsualUnaryConversions on it's
389 // operands and then handles various conversions that are common to binary
390 // operators (C99 6.3.1.8). If both operands aren't arithmetic, this
391 // routine returns the first non-arithmetic type found. The client is
392 // responsible for emitting appropriate error diagnostics.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000393 QualType UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr,
394 bool isCompAssign = false);
Steve Naroff98cf3e92007-06-06 18:38:38 +0000395 enum AssignmentCheckResult {
Steve Naroff17f76e02007-05-03 21:03:48 +0000396 Compatible,
397 Incompatible,
398 PointerFromInt,
399 IntFromPointer,
Steve Naroff1f4d7272007-05-11 04:00:31 +0000400 IncompatiblePointer,
401 CompatiblePointerDiscardsQualifiers
Steve Naroff17f76e02007-05-03 21:03:48 +0000402 };
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000403 // CheckAssignmentConstraints - Perform type checking for assignment,
404 // argument passing, variable initialization, and function return values.
405 // This routine is only used by the following two methods. C99 6.5.16.
Steve Naroff98cf3e92007-06-06 18:38:38 +0000406 AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000407
408 // CheckSingleAssignmentConstraints - Currently used by ParseCallExpr,
409 // CheckAssignmentOperands, and ParseReturnStmt. Prior to type checking,
410 // this routine performs the default function/array converions.
411 AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs,
412 Expr *&rExpr);
413 // CheckCompoundAssignmentConstraints - Type check without performing any
414 // conversions. For compound assignments, the "Check...Operands" methods
415 // perform the necessary conversions.
416 AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs,
417 QualType rhs);
418
Steve Naroff98cf3e92007-06-06 18:38:38 +0000419 // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1)
420 AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType,
421 QualType rhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000422
Steve Naroff8eeeb132007-05-08 21:09:37 +0000423 /// the following "Check" methods will return a valid/converted QualType
424 /// or a null QualType (indicating an error diagnostic was issued).
425
Steve Naroff47500512007-04-19 23:00:49 +0000426 /// type checking binary operators (subroutines of ParseBinOp).
Steve Naroff7a5af782007-07-13 16:58:59 +0000427 inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex);
428 inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000429 inline QualType CheckMultiplyDivideOperands( // C99 6.5.5
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000430 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000431 inline QualType CheckRemainderOperands( // C99 6.5.5
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000432 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000433 inline QualType CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000434 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000435 inline QualType CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000436 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000437 inline QualType CheckShiftOperands( // C99 6.5.7
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000438 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Chris Lattnerb620c342007-08-26 01:18:55 +0000439 inline QualType CheckCompareOperands( // C99 6.5.8/9
440 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isRelational);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000441 inline QualType CheckBitwiseOperands( // C99 6.5.[10...12]
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000442 Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000443 inline QualType CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +0000444 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000445 // CheckAssignmentOperands is used for both simple and compound assignment.
446 // For simple assignment, pass both expressions and a null converted type.
447 // For compound assignment, pass both expressions and the converted type.
448 inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2]
Steve Naroff0c1c7ed2007-08-24 22:33:52 +0000449 Expr *lex, Expr *&rex, SourceLocation OpLoc, QualType convertedType);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000450 inline QualType CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +0000451 Expr *&lex, Expr *&rex, SourceLocation OpLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000452 inline QualType CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000453 Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc);
Steve Naroff95af0132007-03-30 23:47:58 +0000454
Steve Naroff47500512007-04-19 23:00:49 +0000455 /// type checking unary operators (subroutines of ParseUnaryOp).
Steve Naroff9358c712007-05-27 23:58:33 +0000456 /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4
Steve Naroff71ce2e02007-05-18 22:53:50 +0000457 QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc);
458 QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc);
459 QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc);
460 QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc,
461 bool isSizeof);
Chris Lattner74ed76b2007-08-24 21:41:10 +0000462 QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc);
Steve Narofff8fd09e2007-07-27 22:15:19 +0000463
464 /// type checking primary expressions.
465 QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
466 IdentifierInfo &Comp, SourceLocation CmpLoc);
467
Steve Naroff2fea1392007-09-02 02:04:30 +0000468 /// type checking declaration initializers (C99 6.7.8)
Steve Naroff77b97002007-09-04 14:36:54 +0000469 bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000470 bool isStatic);
Steve Naroff77b97002007-09-04 14:36:54 +0000471 bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
472 bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
473 bool isStatic, QualType ElementType);
Steve Naroffac074b42007-09-04 02:20:04 +0000474 void CheckVariableInitList(QualType DeclType, InitListExpr *IList,
475 QualType ElementType, bool isStatic,
476 int &nInitializers, bool &hadError);
477 void CheckConstantInitList(QualType DeclType, InitListExpr *IList,
478 QualType ElementType, bool isStatic,
479 int &nInitializers, bool &hadError);
480
Chris Lattnerfc1c44a2007-08-23 05:46:52 +0000481 /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
482 /// the specified width and sign. If an overflow occurs, detect it and emit
483 /// the specified diagnostic.
484 void ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &OldVal,
485 unsigned NewWidth, bool NewSign,
486 SourceLocation Loc, unsigned DiagID);
487
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000488 //===--------------------------------------------------------------------===//
489 // Extra semantic analysis beyond the C type system
490 private:
491
Anders Carlsson98f07902007-08-17 05:31:46 +0000492 bool CheckFunctionCall(Expr *Fn,
Ted Kremeneke68f1aa2007-08-14 17:39:48 +0000493 SourceLocation LParenLoc, SourceLocation RParenLoc,
494 FunctionDecl *FDecl,
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000495 Expr** Args, unsigned NumArgsInCall);
496
Ted Kremeneke68f1aa2007-08-14 17:39:48 +0000497 void CheckPrintfArguments(Expr *Fn,
498 SourceLocation LParenLoc, SourceLocation RParenLoc,
499 bool HasVAListArg, FunctionDecl *FDecl,
Ted Kremenek56c864e2007-08-10 21:21:05 +0000500 unsigned format_idx, Expr** Args,
501 unsigned NumArgsInCall);
Ted Kremenekcff94fa2007-08-17 16:46:58 +0000502
503 void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
504 SourceLocation ReturnLoc);
505
Anders Carlsson98f07902007-08-17 05:31:46 +0000506
507 bool CheckBuiltinCFStringArgument(Expr* Arg);
Chris Lattner7cee11f2006-11-03 06:42:29 +0000508};
509
510
511} // end namespace clang
Chris Lattner7cee11f2006-11-03 06:42:29 +0000512
513#endif