Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 1 | //===--- Builder.cpp - AST Builder Implementation -------------------------===// |
| 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 implements the actions class which builds an AST out of a parse |
| 11 | // stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Action.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/Parse/Scope.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 19 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "llvm/Support/Compiler.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace clang; |
| 24 | |
| 25 | /// ASTBuilder |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN ASTBuilder : public Action { |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 28 | Preprocessor &PP; |
| 29 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 30 | /// FullLocInfo - If this is true, the ASTBuilder constructs AST Nodes that |
| 31 | /// capture maximal location information for each source-language construct. |
| 32 | bool FullLocInfo; |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 33 | |
| 34 | /// LastInGroupList - This vector is populated when there are multiple |
| 35 | /// declarators in a single decl group (e.g. "int A, B, C"). In this case, |
| 36 | /// all but the last decl will be entered into this. This is used by the |
| 37 | /// ASTStreamer. |
| 38 | std::vector<Decl*> &LastInGroupList; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 39 | public: |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 40 | ASTBuilder(Preprocessor &pp, bool fullLocInfo, |
| 41 | std::vector<Decl*> &prevInGroup) |
| 42 | : PP(pp), FullLocInfo(fullLocInfo), LastInGroupList(prevInGroup) {} |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 43 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 44 | //===--------------------------------------------------------------------===// |
| 45 | // Symbol table tracking callbacks. |
| 46 | // |
| 47 | virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const; |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 48 | virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 49 | DeclTy *LastInGroup); |
| 50 | virtual DeclTy *ParseFunctionDefinition(Scope *S, Declarator &D, |
| 51 | StmtTy *Body); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 52 | virtual void PopScope(SourceLocation Loc, Scope *S); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 53 | |
| 54 | //===--------------------------------------------------------------------===// |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 55 | // Statement Parsing Callbacks. |
| 56 | |
| 57 | virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 58 | StmtTy **Elts, unsigned NumElts); |
| 59 | virtual StmtResult ParseExprStmt(ExprTy *Expr) { |
| 60 | // TODO: Full info should track this with a node. |
| 61 | return Expr; // Exprs are Stmts. |
| 62 | } |
| 63 | |
Chris Lattner | 5f84a06 | 2006-10-25 05:55:20 +0000 | [diff] [blame^] | 64 | virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 65 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 66 | StmtTy *ElseVal); |
| 67 | |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 68 | virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc, |
| 69 | ExprTy *RetValExp); |
| 70 | |
| 71 | //===--------------------------------------------------------------------===// |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 72 | // Expression Parsing Callbacks. |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 73 | |
| 74 | // Primary Expressions. |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 75 | virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc, |
| 76 | tok::TokenKind Kind); |
| 77 | virtual ExprResult ParseIntegerConstant(SourceLocation Loc); |
| 78 | virtual ExprResult ParseFloatingConstant(SourceLocation Loc); |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 79 | virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R, |
| 80 | ExprTy *Val); |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 81 | virtual ExprResult ParseStringExpr(const char *StrData, unsigned StrLen, |
| 82 | bool isWide, |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 83 | SourceLocation *TokLocs, unsigned NumToks); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 85 | // Binary/Unary Operators. 'Tok' is the token for the operator. |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 86 | virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
| 87 | ExprTy *Input); |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 88 | virtual ExprResult |
| 89 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 90 | SourceLocation LParenLoc, TypeTy *Ty, |
| 91 | SourceLocation RParenLoc); |
| 92 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 93 | virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 94 | tok::TokenKind Kind, ExprTy *Input); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 96 | virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 97 | ExprTy *Idx, SourceLocation RLoc); |
| 98 | virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc, |
| 99 | tok::TokenKind OpKind, |
| 100 | SourceLocation MemberLoc, |
| 101 | IdentifierInfo &Member); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 102 | |
| 103 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 104 | /// This provides the location of the left/right parens and a list of comma |
| 105 | /// locations. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 106 | virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
| 107 | ExprTy **Args, unsigned NumArgs, |
| 108 | SourceLocation *CommaLocs, |
| 109 | SourceLocation RParenLoc); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 110 | |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 111 | virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 112 | SourceLocation RParenLoc, ExprTy *Op); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 113 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 114 | virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 115 | ExprTy *LHS,ExprTy *RHS); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 116 | |
| 117 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 118 | /// in the case of a the GNU conditional expr extension. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 119 | virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc, |
| 120 | SourceLocation ColonLoc, |
| 121 | ExprTy *Cond, ExprTy *LHS, ExprTy *RHS); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 122 | }; |
| 123 | } // end anonymous namespace |
| 124 | |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Symbol table tracking callbacks. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const { |
| 131 | Decl *D = II.getFETokenInfo<Decl>(); |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 132 | return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 135 | Action::DeclTy * |
| 136 | ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 137 | DeclTy *LastInGroup) { |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 138 | IdentifierInfo *II = D.getIdentifier(); |
| 139 | Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0; |
| 140 | |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 141 | Decl *New; |
| 142 | if (D.isFunctionDeclarator()) |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 143 | New = new FunctionDecl(II, D, PrevDecl); |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 144 | else |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 145 | New = new VarDecl(II, D, PrevDecl); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 146 | |
| 147 | // If this has an identifier, add it to the scope stack. |
| 148 | if (II) { |
| 149 | // If PrevDecl includes conflicting name here, emit a diagnostic. |
| 150 | II->setFETokenInfo(New); |
| 151 | S->AddDecl(II); |
| 152 | } |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 0535ebb | 2006-10-25 05:28:22 +0000 | [diff] [blame] | 154 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 155 | // remember this in the LastInGroupList list. |
| 156 | if (LastInGroup && S->getParent() == 0) |
| 157 | LastInGroupList.push_back((Decl*)LastInGroup); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 158 | |
| 159 | return New; |
| 160 | } |
| 161 | |
| 162 | Action::DeclTy * |
| 163 | ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) { |
| 164 | FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 165 | |
| 166 | FD->setBody((Stmt*)Body); |
| 167 | |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 168 | return FD; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) { |
| 172 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 173 | I != E; ++I) { |
| 174 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 175 | Decl *D = II.getFETokenInfo<Decl>(); |
| 176 | assert(D && "This decl didn't get pushed??"); |
| 177 | |
| 178 | Decl *Next = D->getNext(); |
| 179 | |
| 180 | // FIXME: Push the decl on the parent function list if in a function. |
| 181 | delete D; |
| 182 | |
| 183 | II.setFETokenInfo(Next); |
| 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 187 | //===--------------------------------------------------------------------===// |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 188 | // Statement Parsing Callbacks. |
| 189 | //===--------------------------------------------------------------------===// |
| 190 | |
| 191 | Action::StmtResult |
| 192 | ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 193 | StmtTy **Elts, unsigned NumElts) { |
| 194 | if (FullLocInfo || NumElts > 1) |
| 195 | return new CompoundStmt((Stmt**)Elts, NumElts); |
| 196 | else if (NumElts == 1) |
| 197 | return Elts[0]; // {stmt} -> stmt |
| 198 | else |
| 199 | return 0; // {} -> ; |
| 200 | } |
| 201 | |
Chris Lattner | 5f84a06 | 2006-10-25 05:55:20 +0000 | [diff] [blame^] | 202 | Action::StmtResult |
| 203 | ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 204 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 205 | StmtTy *ElseVal) { |
| 206 | return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal); |
| 207 | } |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 208 | |
| 209 | Action::StmtResult |
| 210 | ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc, |
| 211 | ExprTy *RetValExp) { |
Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 212 | return new ReturnStmt((Expr*)RetValExp); |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | //===--------------------------------------------------------------------===// |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 216 | // Expression Parsing Callbacks. |
| 217 | //===--------------------------------------------------------------------===// |
| 218 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 219 | Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc, |
| 220 | tok::TokenKind Kind) { |
| 221 | switch (Kind) { |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 222 | default: |
| 223 | assert(0 && "Unknown simple primary expr!"); |
| 224 | case tok::identifier: { |
| 225 | // Could be enum-constant or decl. |
| 226 | //Tok.getIdentifierInfo() |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 227 | return new DeclRefExpr(*(Decl*)0); |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | case tok::char_constant: // constant: character-constant |
| 231 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 232 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 233 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | 94b4ce3 | 2006-10-06 05:51:35 +0000 | [diff] [blame] | 234 | //assert(0 && "FIXME: Unimp so far!"); |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 235 | return new DeclRefExpr(*(Decl*)0); |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 239 | Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 240 | return new IntegerConstant(); |
| 241 | } |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 242 | Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 243 | return new FloatingConstant(); |
| 244 | } |
| 245 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 246 | Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L, |
| 247 | SourceLocation R, |
| 248 | ExprTy *Val) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 249 | if (!FullLocInfo) return Val; |
| 250 | |
| 251 | return new ParenExpr(L, R, (Expr*)Val); |
| 252 | } |
| 253 | |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 254 | /// ParseStringExpr - This accepts a string after semantic analysis. This string |
| 255 | /// may be the result of string concatenation ([C99 5.1.1.2, translation phase |
| 256 | /// #6]), so it may come from multiple tokens. |
| 257 | /// |
| 258 | Action::ExprResult ASTBuilder:: |
| 259 | ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide, |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 260 | SourceLocation *TokLocs, unsigned NumToks) { |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 261 | assert(NumToks && "Must have at least one string!"); |
| 262 | |
| 263 | if (!FullLocInfo) |
| 264 | return new StringExpr(StrData, StrLen, isWide); |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 265 | else |
| 266 | return new StringExprLOC(StrData, StrLen, isWide, TokLocs, NumToks); |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 270 | // Unary Operators. 'Tok' is the token for the operator. |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 271 | Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc, |
| 272 | tok::TokenKind Op, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 273 | ExprTy *Input) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 274 | UnaryOperator::Opcode Opc; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 275 | switch (Op) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 276 | default: assert(0 && "Unknown unary op!"); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 277 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 278 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 279 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 280 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 281 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 282 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 283 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 284 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 285 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 286 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 287 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 288 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 289 | case tok::ampamp: Opc = UnaryOperator::AddrLabel; break; |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 290 | case tok::kw___extension__: |
| 291 | if (!FullLocInfo) return Input; |
| 292 | Opc = UnaryOperator::Extension; |
| 293 | break; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (!FullLocInfo) |
| 297 | return new UnaryOperator((Expr*)Input, Opc); |
| 298 | else |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 299 | return new UnaryOperatorLOC(OpLoc, (Expr*)Input, Opc); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 302 | Action::ExprResult ASTBuilder:: |
| 303 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 304 | SourceLocation LParenLoc, TypeTy *Ty, |
| 305 | SourceLocation RParenLoc) { |
| 306 | if (!FullLocInfo) |
| 307 | return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty); |
| 308 | else |
| 309 | return new SizeOfAlignOfTypeExprLOC(OpLoc, isSizeof, LParenLoc, (Type*)Ty, |
| 310 | RParenLoc); |
| 311 | } |
| 312 | |
| 313 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 314 | Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 315 | tok::TokenKind Kind, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 316 | ExprTy *Input) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 317 | UnaryOperator::Opcode Opc; |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 318 | switch (Kind) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 319 | default: assert(0 && "Unknown unary op!"); |
| 320 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 321 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 322 | } |
| 323 | |
| 324 | if (!FullLocInfo) |
| 325 | return new UnaryOperator((Expr*)Input, Opc); |
| 326 | else |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 327 | return new UnaryOperatorLOC(OpLoc, (Expr*)Input, Opc); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 330 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 331 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 332 | ExprTy *Idx, SourceLocation RLoc) { |
| 333 | if (!FullLocInfo) |
| 334 | return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx); |
| 335 | else |
| 336 | return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc); |
| 337 | } |
| 338 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 339 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 340 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 341 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 342 | IdentifierInfo &Member) { |
Chris Lattner | 6f3a117 | 2006-08-24 05:19:28 +0000 | [diff] [blame] | 343 | Decl *MemberDecl = 0; |
| 344 | // TODO: Look up MemberDecl. |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 345 | if (!FullLocInfo) |
Chris Lattner | 6f3a117 | 2006-08-24 05:19:28 +0000 | [diff] [blame] | 346 | return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 347 | else |
| 348 | return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow, |
Chris Lattner | 6f3a117 | 2006-08-24 05:19:28 +0000 | [diff] [blame] | 349 | MemberLoc, MemberDecl); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 353 | /// This provides the location of the left/right parens and a list of comma |
| 354 | /// locations. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 355 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 356 | ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
| 357 | ExprTy **Args, unsigned NumArgs, |
| 358 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
| 359 | if (!FullLocInfo) |
| 360 | return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs); |
| 361 | else |
| 362 | return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs, |
| 363 | CommaLocs, RParenLoc); |
| 364 | } |
| 365 | |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 366 | Action::ExprResult ASTBuilder:: |
| 367 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 368 | SourceLocation RParenLoc, ExprTy *Op) { |
| 369 | if (!FullLocInfo) |
| 370 | return new CastExpr((Type*)Ty, (Expr*)Op); |
| 371 | else |
| 372 | return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op); |
| 373 | } |
| 374 | |
| 375 | |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 377 | // Binary Operators. 'Tok' is the token for the operator. |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 378 | Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc, |
| 379 | tok::TokenKind Kind, ExprTy *LHS, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 380 | ExprTy *RHS) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 381 | BinaryOperator::Opcode Opc; |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 382 | switch (Kind) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 383 | default: assert(0 && "Unknown binop!"); |
| 384 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 385 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 386 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 387 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 388 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 389 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 390 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 391 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 392 | case tok::less: Opc = BinaryOperator::LT; break; |
| 393 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 394 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 395 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 396 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 397 | case tok::amp: Opc = BinaryOperator::And; break; |
| 398 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 399 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 400 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 401 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 402 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 403 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 404 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 405 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 406 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 407 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 408 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 409 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 410 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 411 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 412 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 413 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 414 | } |
| 415 | |
| 416 | if (!FullLocInfo) |
| 417 | return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc); |
| 418 | else |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 419 | return new BinaryOperatorLOC((Expr*)LHS, TokLoc, (Expr*)RHS, Opc); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 423 | /// in the case of a the GNU conditional expr extension. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 424 | Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc, |
| 425 | SourceLocation ColonLoc, |
| 426 | ExprTy *Cond, ExprTy *LHS, |
| 427 | ExprTy *RHS) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 428 | if (!FullLocInfo) |
| 429 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS); |
| 430 | else |
| 431 | return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS, |
| 432 | ColonLoc, (Expr*)RHS); |
| 433 | } |
| 434 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 435 | |
| 436 | /// Interface to the Builder.cpp file. |
| 437 | /// |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 438 | Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo, |
| 439 | std::vector<Decl*> &LastInGroupList) { |
| 440 | return new ASTBuilder(PP, FullLocInfo, LastInGroupList); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 441 | } |
| 442 | |