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