blob: 4d3d437404ff8aea9f6eaf991be8f08e8390118d [file] [log] [blame]
Chris Lattner7cee11f2006-11-03 06:42:29 +00001//===--- ASTBuilder.cpp - AST Builder Implementation ----------------------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
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 Lattner7cee11f2006-11-03 06:42:29 +000015#include "clang/AST/ASTBuilder.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000016#include "clang/Parse/Action.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000017#include "clang/AST/Decl.h"
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000018#include "clang/AST/Expr.h"
19#include "clang/Parse/Scope.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000020#include "clang/Lex/IdentifierTable.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000021#include "clang/Lex/Preprocessor.h"
22#include "llvm/Support/Compiler.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000023using namespace llvm;
24using namespace clang;
25
Chris Lattnerc11438c2006-08-18 05:17:52 +000026//===----------------------------------------------------------------------===//
27// Symbol table tracking callbacks.
28//===----------------------------------------------------------------------===//
29
Chris Lattner2abeb122006-10-28 19:51:26 +000030bool ASTBuilder::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattnerc11438c2006-08-18 05:17:52 +000031 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +000032 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +000033}
34
Chris Lattner2dacc3f2006-10-16 00:33:54 +000035Action::DeclTy *
36ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
37 DeclTy *LastInGroup) {
Chris Lattnerc11438c2006-08-18 05:17:52 +000038 IdentifierInfo *II = D.getIdentifier();
39 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
40
Chris Lattnera11999d2006-10-15 22:34:45 +000041 Decl *New;
42 if (D.isFunctionDeclarator())
Chris Lattner2dacc3f2006-10-16 00:33:54 +000043 New = new FunctionDecl(II, D, PrevDecl);
Chris Lattnera11999d2006-10-15 22:34:45 +000044 else
Chris Lattner2dacc3f2006-10-16 00:33:54 +000045 New = new VarDecl(II, D, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +000046
47 // If this has an identifier, add it to the scope stack.
48 if (II) {
49 // If PrevDecl includes conflicting name here, emit a diagnostic.
50 II->setFETokenInfo(New);
51 S->AddDecl(II);
52 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000053
Chris Lattner0535ebb2006-10-25 05:28:22 +000054 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
55 // remember this in the LastInGroupList list.
56 if (LastInGroup && S->getParent() == 0)
57 LastInGroupList.push_back((Decl*)LastInGroup);
Chris Lattner2dacc3f2006-10-16 00:33:54 +000058
59 return New;
60}
61
62Action::DeclTy *
63ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
64 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
Chris Lattner30f910e2006-10-16 05:52:41 +000065
66 FD->setBody((Stmt*)Body);
67
Chris Lattner2dacc3f2006-10-16 00:33:54 +000068 return FD;
Chris Lattnerc11438c2006-08-18 05:17:52 +000069}
70
71void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
72 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
73 I != E; ++I) {
74 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
75 Decl *D = II.getFETokenInfo<Decl>();
76 assert(D && "This decl didn't get pushed??");
77
78 Decl *Next = D->getNext();
79
80 // FIXME: Push the decl on the parent function list if in a function.
81 delete D;
82
83 II.setFETokenInfo(Next);
84 }
85}
86
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000087//===--------------------------------------------------------------------===//
Chris Lattnere5cca062006-10-25 04:29:46 +000088// Statement Parsing Callbacks.
89//===--------------------------------------------------------------------===//
90
91Action::StmtResult
92ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R,
93 StmtTy **Elts, unsigned NumElts) {
Chris Lattner72b7d392006-11-04 06:37:16 +000094 if (NumElts > 1)
Chris Lattnere5cca062006-10-25 04:29:46 +000095 return new CompoundStmt((Stmt**)Elts, NumElts);
96 else if (NumElts == 1)
97 return Elts[0]; // {stmt} -> stmt
98 else
99 return 0; // {} -> ;
100}
101
Chris Lattner6c0ff132006-11-05 00:19:50 +0000102Action::StmtResult
103ASTBuilder::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
104 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
105 SourceLocation ColonLoc, StmtTy *SubStmt) {
106 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
107}
108
109Action::StmtResult
110ASTBuilder::ParseDefaultStmt(SourceLocation DefaultLoc,
111 SourceLocation ColonLoc, StmtTy *SubStmt) {
112 return new DefaultStmt((Stmt*)SubStmt);
113}
114
115Action::StmtResult
116ASTBuilder::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
117 SourceLocation ColonLoc, StmtTy *SubStmt) {
118 return new LabelStmt(II, (Stmt*)SubStmt);
119}
120
Chris Lattner5f84a062006-10-25 05:55:20 +0000121Action::StmtResult
122ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
123 StmtTy *ThenVal, SourceLocation ElseLoc,
124 StmtTy *ElseVal) {
125 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
126}
Chris Lattnerf2174b62006-11-04 20:59:27 +0000127Action::StmtResult
128ASTBuilder::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond,
129 StmtTy *Body) {
130 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
131}
Chris Lattnere5cca062006-10-25 04:29:46 +0000132
Chris Lattner71e23ce2006-11-04 20:18:38 +0000133Action::StmtResult
134ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
135 StmtTy *First, ExprTy *Second, ExprTy *Third,
136 SourceLocation RParenLoc, StmtTy *Body) {
137 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
138}
139
Chris Lattnere5cca062006-10-25 04:29:46 +0000140Action::StmtResult
Chris Lattner85ed8732006-11-04 20:40:44 +0000141ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
142 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
143}
144
145Action::StmtResult
146ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
147 SourceLocation WhileLoc, ExprTy *Cond) {
148 return new DoStmt((Stmt*)Body, (Expr*)Cond);
149}
150
151Action::StmtResult
Chris Lattnere5cca062006-10-25 04:29:46 +0000152ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
153 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000154 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000155}
156
157//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000158// Expression Parsing Callbacks.
159//===--------------------------------------------------------------------===//
160
Chris Lattnerae319692006-10-25 03:49:28 +0000161Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
162 tok::TokenKind Kind) {
163 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000164 default:
165 assert(0 && "Unknown simple primary expr!");
166 case tok::identifier: {
167 // Could be enum-constant or decl.
168 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000169 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000170 }
171
172 case tok::char_constant: // constant: character-constant
173 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
174 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
175 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000176 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000177 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000178 }
179}
180
Chris Lattnerae319692006-10-25 03:49:28 +0000181Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000182 return new IntegerConstant();
183}
Chris Lattnerae319692006-10-25 03:49:28 +0000184Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000185 return new FloatingConstant();
186}
187
Chris Lattner98286a42006-08-24 05:02:11 +0000188Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
189 SourceLocation R,
190 ExprTy *Val) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000191 return Val;
Chris Lattner1b926492006-08-23 06:42:10 +0000192}
193
Chris Lattnerd3e98952006-10-06 05:22:26 +0000194/// ParseStringExpr - This accepts a string after semantic analysis. This string
195/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
196/// #6]), so it may come from multiple tokens.
197///
198Action::ExprResult ASTBuilder::
199ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +0000200 SourceLocation *TokLocs, unsigned NumToks) {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000201 assert(NumToks && "Must have at least one string!");
Chris Lattner72b7d392006-11-04 06:37:16 +0000202 return new StringExpr(StrData, StrLen, isWide);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000203}
204
205
Chris Lattner1b926492006-08-23 06:42:10 +0000206// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000207Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
208 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000209 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000210 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000211 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000212 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000213 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
214 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
215 case tok::amp: Opc = UnaryOperator::AddrOf; break;
216 case tok::star: Opc = UnaryOperator::Deref; break;
217 case tok::plus: Opc = UnaryOperator::Plus; break;
218 case tok::minus: Opc = UnaryOperator::Minus; break;
219 case tok::tilde: Opc = UnaryOperator::Not; break;
220 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000221 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
222 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000223 case tok::kw___real: Opc = UnaryOperator::Real; break;
224 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
225 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattnerc52b1182006-10-25 05:45:55 +0000226 case tok::kw___extension__:
Chris Lattner72b7d392006-11-04 06:37:16 +0000227 return Input;
228 //Opc = UnaryOperator::Extension;
229 //break;
Chris Lattner1b926492006-08-23 06:42:10 +0000230 }
231
Chris Lattner72b7d392006-11-04 06:37:16 +0000232 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000233}
234
Chris Lattner26da7302006-08-24 06:49:19 +0000235Action::ExprResult ASTBuilder::
236ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
237 SourceLocation LParenLoc, TypeTy *Ty,
238 SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000239 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
Chris Lattner26da7302006-08-24 06:49:19 +0000240}
241
242
Chris Lattnerae319692006-10-25 03:49:28 +0000243Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
244 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000245 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000246 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000247 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000248 default: assert(0 && "Unknown unary op!");
249 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
250 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
251 }
252
Chris Lattner72b7d392006-11-04 06:37:16 +0000253 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000254}
255
Chris Lattner98286a42006-08-24 05:02:11 +0000256Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000257ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
258 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000259 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
Chris Lattnere165d942006-08-24 04:40:38 +0000260}
261
Chris Lattner98286a42006-08-24 05:02:11 +0000262Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000263ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
264 tok::TokenKind OpKind, SourceLocation MemberLoc,
265 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000266 Decl *MemberDecl = 0;
267 // TODO: Look up MemberDecl.
Chris Lattner72b7d392006-11-04 06:37:16 +0000268 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000269}
270
271/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
272/// This provides the location of the left/right parens and a list of comma
273/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000274Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000275ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
276 ExprTy **Args, unsigned NumArgs,
277 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000278 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
Chris Lattnere165d942006-08-24 04:40:38 +0000279}
280
Chris Lattnere550a4e2006-08-24 06:37:51 +0000281Action::ExprResult ASTBuilder::
282ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
283 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000284 return new CastExpr((Type*)Ty, (Expr*)Op);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000285}
286
287
Chris Lattnere165d942006-08-24 04:40:38 +0000288
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000289// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000290Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
291 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000292 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000293 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000294 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000295 default: assert(0 && "Unknown binop!");
296 case tok::star: Opc = BinaryOperator::Mul; break;
297 case tok::slash: Opc = BinaryOperator::Div; break;
298 case tok::percent: Opc = BinaryOperator::Rem; break;
299 case tok::plus: Opc = BinaryOperator::Add; break;
300 case tok::minus: Opc = BinaryOperator::Sub; break;
301 case tok::lessless: Opc = BinaryOperator::Shl; break;
302 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
303 case tok::lessequal: Opc = BinaryOperator::LE; break;
304 case tok::less: Opc = BinaryOperator::LT; break;
305 case tok::greaterequal: Opc = BinaryOperator::GE; break;
306 case tok::greater: Opc = BinaryOperator::GT; break;
307 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
308 case tok::equalequal: Opc = BinaryOperator::EQ; break;
309 case tok::amp: Opc = BinaryOperator::And; break;
310 case tok::caret: Opc = BinaryOperator::Xor; break;
311 case tok::pipe: Opc = BinaryOperator::Or; break;
312 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
313 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
314 case tok::equal: Opc = BinaryOperator::Assign; break;
315 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
316 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
317 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
318 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
319 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
320 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
321 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
322 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
323 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
324 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
325 case tok::comma: Opc = BinaryOperator::Comma; break;
326 }
327
Chris Lattner72b7d392006-11-04 06:37:16 +0000328 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000329}
330
331/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
332/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000333Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
334 SourceLocation ColonLoc,
335 ExprTy *Cond, ExprTy *LHS,
336 ExprTy *RHS) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000337 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000338}
339