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