blob: 0ff8c034b23bd84776fd8559759ade53b0bad5cd [file] [log] [blame]
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00001//===--- 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 Lattnerc11438c2006-08-18 05:17:52 +000015#include "clang/Parse/Action.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000016#include "clang/AST/Decl.h"
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000017#include "clang/AST/Expr.h"
18#include "clang/Parse/Scope.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000019#include "clang/Lex/IdentifierTable.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000020#include "clang/Lex/Preprocessor.h"
21#include "llvm/Support/Compiler.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000022using namespace llvm;
23using namespace clang;
24
25/// ASTBuilder
26namespace {
27class VISIBILITY_HIDDEN ASTBuilder : public Action {
Chris Lattnerd3e98952006-10-06 05:22:26 +000028 Preprocessor &PP;
29
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000030 /// 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 Lattner2dacc3f2006-10-16 00:33:54 +000033
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 Lattnerc11438c2006-08-18 05:17:52 +000039public:
Chris Lattner2dacc3f2006-10-16 00:33:54 +000040 ASTBuilder(Preprocessor &pp, bool fullLocInfo,
41 std::vector<Decl*> &prevInGroup)
42 : PP(pp), FullLocInfo(fullLocInfo), LastInGroupList(prevInGroup) {}
Chris Lattnerd3e98952006-10-06 05:22:26 +000043
Chris Lattnerc11438c2006-08-18 05:17:52 +000044 //===--------------------------------------------------------------------===//
45 // Symbol table tracking callbacks.
46 //
47 virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000048 virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
49 DeclTy *LastInGroup);
50 virtual DeclTy *ParseFunctionDefinition(Scope *S, Declarator &D,
51 StmtTy *Body);
Chris Lattnerc11438c2006-08-18 05:17:52 +000052 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000053
54 //===--------------------------------------------------------------------===//
Chris Lattnere5cca062006-10-25 04:29:46 +000055 // 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 Lattner5f84a062006-10-25 05:55:20 +000064 virtual StmtResult ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
65 StmtTy *ThenVal, SourceLocation ElseLoc,
66 StmtTy *ElseVal);
67
Chris Lattnere5cca062006-10-25 04:29:46 +000068 virtual StmtResult ParseReturnStmt(SourceLocation ReturnLoc,
69 ExprTy *RetValExp);
70
71 //===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000072 // Expression Parsing Callbacks.
Chris Lattner1b926492006-08-23 06:42:10 +000073
74 // Primary Expressions.
Chris Lattnerae319692006-10-25 03:49:28 +000075 virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc,
76 tok::TokenKind Kind);
77 virtual ExprResult ParseIntegerConstant(SourceLocation Loc);
78 virtual ExprResult ParseFloatingConstant(SourceLocation Loc);
Chris Lattner98286a42006-08-24 05:02:11 +000079 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
80 ExprTy *Val);
Chris Lattnerd3e98952006-10-06 05:22:26 +000081 virtual ExprResult ParseStringExpr(const char *StrData, unsigned StrLen,
82 bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +000083 SourceLocation *TokLocs, unsigned NumToks);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000084
Chris Lattner1b926492006-08-23 06:42:10 +000085 // Binary/Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +000086 virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
87 ExprTy *Input);
Chris Lattner26da7302006-08-24 06:49:19 +000088 virtual ExprResult
89 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
90 SourceLocation LParenLoc, TypeTy *Ty,
91 SourceLocation RParenLoc);
92
Chris Lattnerae319692006-10-25 03:49:28 +000093 virtual ExprResult ParsePostfixUnaryOp(SourceLocation OpLoc,
94 tok::TokenKind Kind, ExprTy *Input);
Chris Lattner1b926492006-08-23 06:42:10 +000095
Chris Lattner98286a42006-08-24 05:02:11 +000096 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 Lattnere165d942006-08-24 04:40:38 +0000102
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 Lattner98286a42006-08-24 05:02:11 +0000106 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
107 ExprTy **Args, unsigned NumArgs,
108 SourceLocation *CommaLocs,
109 SourceLocation RParenLoc);
Chris Lattnere165d942006-08-24 04:40:38 +0000110
Chris Lattnere550a4e2006-08-24 06:37:51 +0000111 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
112 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattnere165d942006-08-24 04:40:38 +0000113
Chris Lattnerae319692006-10-25 03:49:28 +0000114 virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
115 ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000116
117 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
118 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000119 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
120 SourceLocation ColonLoc,
121 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000122};
123} // end anonymous namespace
124
125
126//===----------------------------------------------------------------------===//
127// Symbol table tracking callbacks.
128//===----------------------------------------------------------------------===//
129
130bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
131 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +0000132 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +0000133}
134
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000135Action::DeclTy *
136ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
137 DeclTy *LastInGroup) {
Chris Lattnerc11438c2006-08-18 05:17:52 +0000138 IdentifierInfo *II = D.getIdentifier();
139 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
140
Chris Lattnera11999d2006-10-15 22:34:45 +0000141 Decl *New;
142 if (D.isFunctionDeclarator())
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000143 New = new FunctionDecl(II, D, PrevDecl);
Chris Lattnera11999d2006-10-15 22:34:45 +0000144 else
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000145 New = new VarDecl(II, D, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000146
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 Lattner2dacc3f2006-10-16 00:33:54 +0000153
Chris Lattner0535ebb2006-10-25 05:28:22 +0000154 // 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 Lattner2dacc3f2006-10-16 00:33:54 +0000158
159 return New;
160}
161
162Action::DeclTy *
163ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
164 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
Chris Lattner30f910e2006-10-16 05:52:41 +0000165
166 FD->setBody((Stmt*)Body);
167
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000168 return FD;
Chris Lattnerc11438c2006-08-18 05:17:52 +0000169}
170
171void 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 Lattner9b6d4cb2006-08-23 05:17:46 +0000187//===--------------------------------------------------------------------===//
Chris Lattnere5cca062006-10-25 04:29:46 +0000188// Statement Parsing Callbacks.
189//===--------------------------------------------------------------------===//
190
191Action::StmtResult
192ASTBuilder::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 Lattner5f84a062006-10-25 05:55:20 +0000202Action::StmtResult
203ASTBuilder::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 Lattnere5cca062006-10-25 04:29:46 +0000208
209Action::StmtResult
210ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
211 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000212 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000213}
214
215//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000216// Expression Parsing Callbacks.
217//===--------------------------------------------------------------------===//
218
Chris Lattnerae319692006-10-25 03:49:28 +0000219Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
220 tok::TokenKind Kind) {
221 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000222 default:
223 assert(0 && "Unknown simple primary expr!");
224 case tok::identifier: {
225 // Could be enum-constant or decl.
226 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000227 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000228 }
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 Lattner94b4ce32006-10-06 05:51:35 +0000234 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000235 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000236 }
237}
238
Chris Lattnerae319692006-10-25 03:49:28 +0000239Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000240 return new IntegerConstant();
241}
Chris Lattnerae319692006-10-25 03:49:28 +0000242Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000243 return new FloatingConstant();
244}
245
Chris Lattner98286a42006-08-24 05:02:11 +0000246Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
247 SourceLocation R,
248 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000249 if (!FullLocInfo) return Val;
250
251 return new ParenExpr(L, R, (Expr*)Val);
252}
253
Chris Lattnerd3e98952006-10-06 05:22:26 +0000254/// 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///
258Action::ExprResult ASTBuilder::
259ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +0000260 SourceLocation *TokLocs, unsigned NumToks) {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000261 assert(NumToks && "Must have at least one string!");
262
263 if (!FullLocInfo)
264 return new StringExpr(StrData, StrLen, isWide);
Chris Lattnerae319692006-10-25 03:49:28 +0000265 else
266 return new StringExprLOC(StrData, StrLen, isWide, TokLocs, NumToks);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000267}
268
269
Chris Lattner1b926492006-08-23 06:42:10 +0000270// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000271Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
272 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000273 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000274 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000275 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000276 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000277 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 Lattner26115ac2006-08-24 06:10:04 +0000285 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
286 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000287 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 Lattnerc52b1182006-10-25 05:45:55 +0000290 case tok::kw___extension__:
291 if (!FullLocInfo) return Input;
292 Opc = UnaryOperator::Extension;
293 break;
Chris Lattner1b926492006-08-23 06:42:10 +0000294 }
295
296 if (!FullLocInfo)
297 return new UnaryOperator((Expr*)Input, Opc);
298 else
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000299 return new UnaryOperatorLOC(OpLoc, (Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000300}
301
Chris Lattner26da7302006-08-24 06:49:19 +0000302Action::ExprResult ASTBuilder::
303ParseSizeOfAlignOfTypeExpr(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 Lattnerae319692006-10-25 03:49:28 +0000314Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
315 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000316 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000317 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000318 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000319 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 Lattnerae319692006-10-25 03:49:28 +0000327 return new UnaryOperatorLOC(OpLoc, (Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000328}
329
Chris Lattner98286a42006-08-24 05:02:11 +0000330Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000331ParseArraySubscriptExpr(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 Lattner98286a42006-08-24 05:02:11 +0000339Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000340ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
341 tok::TokenKind OpKind, SourceLocation MemberLoc,
342 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000343 Decl *MemberDecl = 0;
344 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000345 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000346 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000347 else
348 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000349 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000350}
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 Lattner98286a42006-08-24 05:02:11 +0000355Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000356ParseCallExpr(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 Lattnere550a4e2006-08-24 06:37:51 +0000366Action::ExprResult ASTBuilder::
367ParseCastExpr(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 Lattnere165d942006-08-24 04:40:38 +0000376
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000377// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000378Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
379 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000380 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000381 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000382 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000383 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 Lattnerae319692006-10-25 03:49:28 +0000419 return new BinaryOperatorLOC((Expr*)LHS, TokLoc, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000420}
421
422/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
423/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000424Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
425 SourceLocation ColonLoc,
426 ExprTy *Cond, ExprTy *LHS,
427 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000428 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 Lattnerc11438c2006-08-18 05:17:52 +0000435
436/// Interface to the Builder.cpp file.
437///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000438Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo,
439 std::vector<Decl*> &LastInGroupList) {
440 return new ASTBuilder(PP, FullLocInfo, LastInGroupList);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000441}
442