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