Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- 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" |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | #include <string> |
| 23 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | class APSInt; |
| 26 | } |
| 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | namespace clang { |
| 29 | class ASTContext; |
| 30 | class Preprocessor; |
| 31 | class Decl; |
| 32 | class Expr; |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 33 | class InitListExpr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | class VarDecl; |
| 35 | class ParmVarDecl; |
| 36 | class TypedefDecl; |
| 37 | class FunctionDecl; |
| 38 | class QualType; |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 39 | struct LangOptions; |
| 40 | struct DeclaratorChunk; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 41 | class Token; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | class IntegerLiteral; |
| 43 | class ArrayType; |
| 44 | class LabelStmt; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 45 | class SwitchStmt; |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 46 | class OCUVectorType; |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 47 | class TypedefDecl; |
| 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | /// Sema - This implements semantic analysis and AST building for C. |
| 50 | class Sema : public Action { |
| 51 | Preprocessor &PP; |
| 52 | |
| 53 | ASTContext &Context; |
| 54 | |
| 55 | /// CurFunctionDecl - If inside of a function body, this contains a pointer to |
| 56 | /// the function decl for the function being parsed. |
| 57 | FunctionDecl *CurFunctionDecl; |
| 58 | |
| 59 | /// LastInGroupList - This vector is populated when there are multiple |
| 60 | /// declarators in a single decl group (e.g. "int A, B, C"). In this case, |
| 61 | /// all but the last decl will be entered into this. This is used by the |
| 62 | /// ASTStreamer. |
| 63 | std::vector<Decl*> &LastInGroupList; |
| 64 | |
| 65 | /// LabelMap - This is a mapping from label identifiers to the LabelStmt for |
| 66 | /// it (which acts like the label decl in some ways). Forward referenced |
| 67 | /// labels have a LabelStmt created for them with a null location & SubStmt. |
| 68 | llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap; |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 69 | |
| 70 | llvm::SmallVector<SwitchStmt*, 8> SwitchStack; |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 71 | |
| 72 | /// OCUVectorDecls - This is a list all the OCU vector types. This allows |
| 73 | /// us to associate a raw vector type with one of the OCU type names. |
| 74 | /// This is only necessary for issuing pretty diagnostics. |
| 75 | llvm::SmallVector<TypedefDecl*, 24> OCUVectorDecls; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 76 | |
| 77 | // Enum values used by KnownFunctionIDs (see below). |
| 78 | enum { |
| 79 | id_printf, |
| 80 | id_fprintf, |
| 81 | id_sprintf, |
| 82 | id_snprintf, |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 83 | id_asprintf, |
Ted Kremenek | e0eb80a | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 84 | id_vsnprintf, |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 85 | id_vasprintf, |
| 86 | id_vfprintf, |
| 87 | id_vsprintf, |
| 88 | id_vprintf, |
| 89 | id_num_known_functions |
| 90 | }; |
| 91 | |
| 92 | /// KnownFunctionIDs - This is a list of IdentifierInfo objects to a set |
| 93 | /// of known functions used by the semantic analysis to do various |
| 94 | /// kinds of checking (e.g. checking format string errors in printf calls). |
| 95 | /// This list is populated upon the creation of a Sema object. |
| 96 | IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ]; |
| 97 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | public: |
| 99 | Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup); |
| 100 | |
| 101 | const LangOptions &getLangOptions() const; |
| 102 | |
| 103 | /// The primitive diagnostic helpers - always returns true, which simplifies |
| 104 | /// error handling (i.e. less code). |
| 105 | bool Diag(SourceLocation Loc, unsigned DiagID); |
| 106 | bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg); |
| 107 | bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 108 | const std::string &Msg2); |
| 109 | |
| 110 | /// More expressive diagnostic helpers for expressions (say that 6 times:-) |
| 111 | bool Diag(SourceLocation Loc, unsigned DiagID, SourceRange R1); |
| 112 | bool Diag(SourceLocation Loc, unsigned DiagID, |
| 113 | SourceRange R1, SourceRange R2); |
| 114 | bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 115 | SourceRange R1); |
| 116 | bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 117 | SourceRange R1, SourceRange R2); |
| 118 | bool Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 119 | const std::string &Msg2, SourceRange R1); |
| 120 | bool Diag(SourceLocation Loc, unsigned DiagID, |
| 121 | const std::string &Msg1, const std::string &Msg2, |
| 122 | SourceRange R1, SourceRange R2); |
| 123 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 124 | virtual void DeleteExpr(ExprTy *E); |
| 125 | virtual void DeleteStmt(StmtTy *S); |
| 126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | //===--------------------------------------------------------------------===// |
| 128 | // Type Analysis / Processing: SemaType.cpp. |
| 129 | // |
| 130 | QualType GetTypeForDeclarator(Declarator &D, Scope *S); |
| 131 | |
| 132 | virtual TypeResult ParseTypeName(Scope *S, Declarator &D); |
| 133 | |
| 134 | virtual TypeResult ParseParamDeclaratorType(Scope *S, Declarator &D); |
| 135 | private: |
| 136 | //===--------------------------------------------------------------------===// |
| 137 | // Symbol table / Decl tracking callbacks: SemaDecl.cpp. |
| 138 | // |
| 139 | virtual DeclTy *isTypeName(const IdentifierInfo &II, Scope *S) const; |
| 140 | virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 141 | DeclTy *LastInGroup); |
| 142 | virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group); |
| 143 | |
| 144 | virtual DeclTy *ParseStartOfFunctionDef(Scope *S, Declarator &D); |
| 145 | virtual DeclTy *ParseFunctionDefBody(DeclTy *Decl, StmtTy *Body); |
| 146 | virtual void PopScope(SourceLocation Loc, Scope *S); |
| 147 | |
| 148 | /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with |
| 149 | /// no declarator (e.g. "struct foo;") is parsed. |
| 150 | virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS); |
| 151 | |
| 152 | virtual DeclTy *ParseTag(Scope *S, unsigned TagType, TagKind TK, |
| 153 | SourceLocation KWLoc, IdentifierInfo *Name, |
| 154 | SourceLocation NameLoc, AttributeList *Attr); |
| 155 | virtual DeclTy *ParseField(Scope *S, DeclTy *TagDecl,SourceLocation DeclStart, |
| 156 | Declarator &D, ExprTy *BitfieldWidth); |
| 157 | virtual void ParseRecordBody(SourceLocation RecLoc, DeclTy *TagDecl, |
| 158 | DeclTy **Fields, unsigned NumFields); |
| 159 | virtual DeclTy *ParseEnumConstant(Scope *S, DeclTy *EnumDecl, |
| 160 | DeclTy *LastEnumConstant, |
| 161 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 162 | SourceLocation EqualLoc, ExprTy *Val); |
| 163 | virtual void ParseEnumBody(SourceLocation EnumLoc, DeclTy *EnumDecl, |
| 164 | DeclTy **Elements, unsigned NumElements); |
| 165 | private: |
| 166 | /// Subroutines of ParseDeclarator()... |
| 167 | TypedefDecl *ParseTypedefDecl(Scope *S, Declarator &D, Decl *LastDeclarator); |
| 168 | TypedefDecl *MergeTypeDefDecl(TypedefDecl *New, Decl *Old); |
| 169 | FunctionDecl *MergeFunctionDecl(FunctionDecl *New, Decl *Old); |
| 170 | VarDecl *MergeVarDecl(VarDecl *New, Decl *Old); |
| 171 | /// AddTopLevelDecl - called after the decl has been fully processed. |
| 172 | /// Allows for bookkeeping and post-processing of each declaration. |
| 173 | void AddTopLevelDecl(Decl *current, Decl *last); |
| 174 | |
| 175 | /// More parsing and symbol table subroutines... |
| 176 | ParmVarDecl *ParseParamDeclarator(DeclaratorChunk &FI, unsigned ArgNo, |
| 177 | Scope *FnBodyScope); |
| 178 | Decl *LookupScopedDecl(IdentifierInfo *II, unsigned NSI, SourceLocation IdLoc, |
| 179 | Scope *S); |
| 180 | Decl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S); |
| 181 | Decl *ImplicitlyDefineFunction(SourceLocation Loc, IdentifierInfo &II, |
| 182 | Scope *S); |
| 183 | // Decl attributes - this routine is the top level dispatcher. |
| 184 | void HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix, |
| 185 | AttributeList *declarator_postfix); |
| 186 | void HandleDeclAttribute(Decl *New, AttributeList *rawAttr); |
| 187 | |
| 188 | // HandleVectorTypeAttribute - this attribute is only applicable to |
| 189 | // integral and float scalars, although arrays, pointers, and function |
| 190 | // return values are allowed in conjunction with this construct. Aggregates |
| 191 | // with this attribute are invalid, even if they are of the same size as a |
| 192 | // corresponding scalar. |
| 193 | // The raw attribute should contain precisely 1 argument, the vector size |
| 194 | // for the variable, measured in bytes. If curType and rawAttr are well |
| 195 | // formed, this routine will return a new vector type. |
| 196 | QualType HandleVectorTypeAttribute(QualType curType, AttributeList *rawAttr); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 197 | void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | |
| 199 | //===--------------------------------------------------------------------===// |
| 200 | // Statement Parsing Callbacks: SemaStmt.cpp. |
| 201 | public: |
| 202 | virtual StmtResult ParseExprStmt(ExprTy *Expr); |
| 203 | |
| 204 | virtual StmtResult ParseNullStmt(SourceLocation SemiLoc); |
| 205 | virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R, |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 206 | StmtTy **Elts, unsigned NumElts, |
| 207 | bool isStmtExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | virtual StmtResult ParseDeclStmt(DeclTy *Decl); |
| 209 | virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, |
| 210 | SourceLocation DotDotDotLoc, ExprTy *RHSVal, |
| 211 | SourceLocation ColonLoc, StmtTy *SubStmt); |
| 212 | virtual StmtResult ParseDefaultStmt(SourceLocation DefaultLoc, |
Chris Lattner | 6c36be5 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 213 | SourceLocation ColonLoc, StmtTy *SubStmt, |
| 214 | Scope *CurScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 215 | virtual StmtResult ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 216 | SourceLocation ColonLoc, StmtTy *SubStmt); |
| 217 | virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 218 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 219 | StmtTy *ElseVal); |
Anders Carlsson | c1fcb77 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 220 | virtual StmtResult StartSwitchStmt(ExprTy *Cond); |
| 221 | virtual StmtResult FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, |
| 222 | ExprTy *Body); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | virtual StmtResult ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, |
| 224 | StmtTy *Body); |
| 225 | virtual StmtResult ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 226 | SourceLocation WhileLoc, ExprTy *Cond); |
| 227 | |
| 228 | virtual StmtResult ParseForStmt(SourceLocation ForLoc, |
| 229 | SourceLocation LParenLoc, |
| 230 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 231 | SourceLocation RParenLoc, StmtTy *Body); |
| 232 | virtual StmtResult ParseGotoStmt(SourceLocation GotoLoc, |
| 233 | SourceLocation LabelLoc, |
| 234 | IdentifierInfo *LabelII); |
| 235 | virtual StmtResult ParseIndirectGotoStmt(SourceLocation GotoLoc, |
| 236 | SourceLocation StarLoc, |
| 237 | ExprTy *DestExp); |
| 238 | virtual StmtResult ParseContinueStmt(SourceLocation ContinueLoc, |
| 239 | Scope *CurScope); |
| 240 | virtual StmtResult ParseBreakStmt(SourceLocation GotoLoc, Scope *CurScope); |
| 241 | |
| 242 | virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc, |
| 243 | ExprTy *RetValExp); |
| 244 | |
| 245 | //===--------------------------------------------------------------------===// |
| 246 | // Expression Parsing Callbacks: SemaExpr.cpp. |
| 247 | |
| 248 | // Primary Expressions. |
| 249 | virtual ExprResult ParseIdentifierExpr(Scope *S, SourceLocation Loc, |
| 250 | IdentifierInfo &II, |
| 251 | bool HasTrailingLParen); |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 252 | virtual ExprResult ParsePreDefinedExpr(SourceLocation Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | tok::TokenKind Kind); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 254 | virtual ExprResult ParseNumericConstant(const Token &); |
| 255 | virtual ExprResult ParseCharacterConstant(const Token &); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R, |
| 257 | ExprTy *Val); |
| 258 | |
| 259 | /// ParseStringLiteral - The specified tokens were lexed as pasted string |
| 260 | /// fragments (e.g. "foo" "bar" L"baz"). |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 261 | virtual ExprResult ParseStringLiteral(const Token *Toks, unsigned NumToks); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | |
| 263 | // Binary/Unary Operators. 'Tok' is the token for the operator. |
| 264 | virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
| 265 | ExprTy *Input); |
| 266 | virtual ExprResult |
| 267 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 268 | SourceLocation LParenLoc, TypeTy *Ty, |
| 269 | SourceLocation RParenLoc); |
| 270 | |
| 271 | virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 272 | tok::TokenKind Kind, ExprTy *Input); |
| 273 | |
| 274 | virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 275 | ExprTy *Idx, SourceLocation RLoc); |
| 276 | virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc, |
| 277 | tok::TokenKind OpKind, |
| 278 | SourceLocation MemberLoc, |
| 279 | IdentifierInfo &Member); |
| 280 | |
| 281 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 282 | /// This provides the location of the left/right parens and a list of comma |
| 283 | /// locations. |
| 284 | virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
| 285 | ExprTy **Args, unsigned NumArgs, |
| 286 | SourceLocation *CommaLocs, |
| 287 | SourceLocation RParenLoc); |
| 288 | |
| 289 | virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 290 | SourceLocation RParenLoc, ExprTy *Op); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 291 | |
| 292 | virtual ExprResult ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 293 | SourceLocation RParenLoc, ExprTy *Op); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 295 | virtual ExprResult ParseInitList(SourceLocation LParenLoc, |
| 296 | ExprTy **InitList, unsigned NumInit, |
| 297 | SourceLocation RParenLoc); |
| 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 300 | ExprTy *LHS,ExprTy *RHS); |
| 301 | |
| 302 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 303 | /// in the case of a the GNU conditional expr extension. |
| 304 | virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc, |
| 305 | SourceLocation ColonLoc, |
| 306 | ExprTy *Cond, ExprTy *LHS, ExprTy *RHS); |
| 307 | |
| 308 | /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 309 | virtual ExprResult ParseAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
| 310 | IdentifierInfo *LabelII); |
| 311 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 312 | virtual ExprResult ParseStmtExpr(SourceLocation LPLoc, StmtTy *SubStmt, |
| 313 | SourceLocation RPLoc); // "({..})" |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 314 | |
| 315 | /// __builtin_offsetof(type, a.b[123][456].c) |
| 316 | virtual ExprResult ParseBuiltinOffsetOf(SourceLocation BuiltinLoc, |
| 317 | SourceLocation TypeLoc, TypeTy *Arg1, |
| 318 | OffsetOfComponent *CompPtr, |
| 319 | unsigned NumComponents, |
| 320 | SourceLocation RParenLoc); |
| 321 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 322 | // __builtin_types_compatible_p(type1, type2) |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 323 | virtual ExprResult ParseTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 324 | TypeTy *arg1, TypeTy *arg2, |
| 325 | SourceLocation RPLoc); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 326 | |
| 327 | // __builtin_choose_expr(constExpr, expr1, expr2) |
| 328 | virtual ExprResult ParseChooseExpr(SourceLocation BuiltinLoc, |
| 329 | ExprTy *cond, ExprTy *expr1, ExprTy *expr2, |
| 330 | SourceLocation RPLoc); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | /// ParseCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's. |
| 333 | virtual ExprResult ParseCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind, |
| 334 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 335 | SourceLocation RAngleBracketLoc, |
| 336 | SourceLocation LParenLoc, ExprTy *E, |
| 337 | SourceLocation RParenLoc); |
| 338 | |
| 339 | /// ParseCXXBoolLiteral - Parse {true,false} literals. |
| 340 | virtual ExprResult ParseCXXBoolLiteral(SourceLocation OpLoc, |
| 341 | tok::TokenKind Kind); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 342 | |
| 343 | // ParseObjCStringLiteral - Parse Objective-C string literals. |
| 344 | virtual ExprResult ParseObjCStringLiteral(ExprTy *string); |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 345 | virtual ExprResult ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 346 | SourceLocation LParenLoc, |
| 347 | TypeTy *Ty, |
| 348 | SourceLocation RParenLoc); |
| 349 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | private: |
| 351 | // UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 352 | // functions and arrays to their respective pointers (C99 6.3.2.1). |
| 353 | void UsualUnaryConversions(Expr *&expr); |
| 354 | |
| 355 | // DefaultFunctionArrayConversion - converts functions and arrays |
| 356 | // to their respective pointers (C99 6.3.2.1). |
| 357 | void DefaultFunctionArrayConversion(Expr *&expr); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 358 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 359 | // DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 360 | // do not have a prototype. Integer promotions are performed on each |
| 361 | // argument, and arguments that have type float are promoted to double. |
| 362 | void DefaultArgumentPromotion(Expr *&expr); |
| 363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | // UsualArithmeticConversions - performs the UsualUnaryConversions on it's |
| 365 | // operands and then handles various conversions that are common to binary |
| 366 | // operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 367 | // routine returns the first non-arithmetic type found. The client is |
| 368 | // responsible for emitting appropriate error diagnostics. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 369 | QualType UsualArithmeticConversions(Expr *&lExpr, Expr *&rExpr, |
| 370 | bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | enum AssignmentCheckResult { |
| 372 | Compatible, |
| 373 | Incompatible, |
| 374 | PointerFromInt, |
| 375 | IntFromPointer, |
| 376 | IncompatiblePointer, |
| 377 | CompatiblePointerDiscardsQualifiers |
| 378 | }; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 379 | // CheckAssignmentConstraints - Perform type checking for assignment, |
| 380 | // argument passing, variable initialization, and function return values. |
| 381 | // This routine is only used by the following two methods. C99 6.5.16. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | AssignmentCheckResult CheckAssignmentConstraints(QualType lhs, QualType rhs); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 383 | |
| 384 | // CheckSingleAssignmentConstraints - Currently used by ParseCallExpr, |
| 385 | // CheckAssignmentOperands, and ParseReturnStmt. Prior to type checking, |
| 386 | // this routine performs the default function/array converions. |
| 387 | AssignmentCheckResult CheckSingleAssignmentConstraints(QualType lhs, |
| 388 | Expr *&rExpr); |
| 389 | // CheckCompoundAssignmentConstraints - Type check without performing any |
| 390 | // conversions. For compound assignments, the "Check...Operands" methods |
| 391 | // perform the necessary conversions. |
| 392 | AssignmentCheckResult CheckCompoundAssignmentConstraints(QualType lhs, |
| 393 | QualType rhs); |
| 394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | // Helper function for CheckAssignmentConstraints (C99 6.5.16.1p1) |
| 396 | AssignmentCheckResult CheckPointerTypesForAssignment(QualType lhsType, |
| 397 | QualType rhsType); |
| 398 | |
| 399 | /// the following "Check" methods will return a valid/converted QualType |
| 400 | /// or a null QualType (indicating an error diagnostic was issued). |
| 401 | |
| 402 | /// type checking binary operators (subroutines of ParseBinOp). |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 403 | inline void InvalidOperands(SourceLocation l, Expr *&lex, Expr *&rex); |
| 404 | inline QualType CheckVectorOperands(SourceLocation l, Expr *&lex, Expr *&rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | inline QualType CheckMultiplyDivideOperands( // C99 6.5.5 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 406 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | inline QualType CheckRemainderOperands( // C99 6.5.5 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 408 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | inline QualType CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 410 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | inline QualType CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 412 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | inline QualType CheckShiftOperands( // C99 6.5.7 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 414 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 415 | inline QualType CheckCompareOperands( // C99 6.5.8/9 |
| 416 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isRelational); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | inline QualType CheckBitwiseOperands( // C99 6.5.[10...12] |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 418 | Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | inline QualType CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 420 | Expr *&lex, Expr *&rex, SourceLocation OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | // CheckAssignmentOperands is used for both simple and compound assignment. |
| 422 | // For simple assignment, pass both expressions and a null converted type. |
| 423 | // For compound assignment, pass both expressions and the converted type. |
| 424 | inline QualType CheckAssignmentOperands( // C99 6.5.16.[1,2] |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 425 | Expr *lex, Expr *&rex, SourceLocation OpLoc, QualType convertedType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 426 | inline QualType CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 427 | Expr *&lex, Expr *&rex, SourceLocation OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 428 | inline QualType CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 429 | Expr *&cond, Expr *&lhs, Expr *&rhs, SourceLocation questionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | |
| 431 | /// type checking unary operators (subroutines of ParseUnaryOp). |
| 432 | /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4 |
| 433 | QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc); |
| 434 | QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc); |
| 435 | QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc); |
| 436 | QualType CheckSizeOfAlignOfOperand(QualType type, SourceLocation loc, |
| 437 | bool isSizeof); |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 438 | QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 439 | |
| 440 | /// type checking primary expressions. |
| 441 | QualType CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc, |
| 442 | IdentifierInfo &Comp, SourceLocation CmpLoc); |
| 443 | |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 444 | /// type checking declaration initializers (C99 6.7.8) |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame^] | 445 | bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType, |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 446 | bool isStatic); |
Steve Naroff | 9e8925e | 2007-09-04 14:36:54 +0000 | [diff] [blame^] | 447 | bool CheckSingleInitializer(Expr *&simpleInit, QualType declType); |
| 448 | bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot, |
| 449 | bool isStatic, QualType ElementType); |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 450 | void CheckVariableInitList(QualType DeclType, InitListExpr *IList, |
| 451 | QualType ElementType, bool isStatic, |
| 452 | int &nInitializers, bool &hadError); |
| 453 | void CheckConstantInitList(QualType DeclType, InitListExpr *IList, |
| 454 | QualType ElementType, bool isStatic, |
| 455 | int &nInitializers, bool &hadError); |
| 456 | |
Chris Lattner | f4021e7 | 2007-08-23 05:46:52 +0000 | [diff] [blame] | 457 | /// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have |
| 458 | /// the specified width and sign. If an overflow occurs, detect it and emit |
| 459 | /// the specified diagnostic. |
| 460 | void ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &OldVal, |
| 461 | unsigned NewWidth, bool NewSign, |
| 462 | SourceLocation Loc, unsigned DiagID); |
| 463 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 464 | //===--------------------------------------------------------------------===// |
| 465 | // Extra semantic analysis beyond the C type system |
| 466 | private: |
| 467 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 468 | bool CheckFunctionCall(Expr *Fn, |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 469 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 470 | FunctionDecl *FDecl, |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 471 | Expr** Args, unsigned NumArgsInCall); |
| 472 | |
Ted Kremenek | 71895b9 | 2007-08-14 17:39:48 +0000 | [diff] [blame] | 473 | void CheckPrintfArguments(Expr *Fn, |
| 474 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 475 | bool HasVAListArg, FunctionDecl *FDecl, |
Ted Kremenek | 8207710 | 2007-08-10 21:21:05 +0000 | [diff] [blame] | 476 | unsigned format_idx, Expr** Args, |
| 477 | unsigned NumArgsInCall); |
Ted Kremenek | 06de276 | 2007-08-17 16:46:58 +0000 | [diff] [blame] | 478 | |
| 479 | void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType, |
| 480 | SourceLocation ReturnLoc); |
| 481 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 482 | |
| 483 | bool CheckBuiltinCFStringArgument(Expr* Arg); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | |
| 487 | } // end namespace clang |
| 488 | |
| 489 | #endif |