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