blob: 78d372513d07865bba86656c8a55ba5719648931 [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 Lattner5f84a062006-10-25 05:55:20 +0000102Action::StmtResult
103ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
104 StmtTy *ThenVal, SourceLocation ElseLoc,
105 StmtTy *ElseVal) {
106 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
107}
Chris Lattnere5cca062006-10-25 04:29:46 +0000108
Chris Lattner71e23ce2006-11-04 20:18:38 +0000109Action::StmtResult
110ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
111 StmtTy *First, ExprTy *Second, ExprTy *Third,
112 SourceLocation RParenLoc, StmtTy *Body) {
113 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
114}
115
Chris Lattnere5cca062006-10-25 04:29:46 +0000116Action::StmtResult
117ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
118 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000119 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000120}
121
122//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000123// Expression Parsing Callbacks.
124//===--------------------------------------------------------------------===//
125
Chris Lattnerae319692006-10-25 03:49:28 +0000126Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
127 tok::TokenKind Kind) {
128 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000129 default:
130 assert(0 && "Unknown simple primary expr!");
131 case tok::identifier: {
132 // Could be enum-constant or decl.
133 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000134 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000135 }
136
137 case tok::char_constant: // constant: character-constant
138 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
139 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
140 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000141 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000142 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000143 }
144}
145
Chris Lattnerae319692006-10-25 03:49:28 +0000146Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000147 return new IntegerConstant();
148}
Chris Lattnerae319692006-10-25 03:49:28 +0000149Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000150 return new FloatingConstant();
151}
152
Chris Lattner98286a42006-08-24 05:02:11 +0000153Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
154 SourceLocation R,
155 ExprTy *Val) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000156 return Val;
Chris Lattner1b926492006-08-23 06:42:10 +0000157}
158
Chris Lattnerd3e98952006-10-06 05:22:26 +0000159/// ParseStringExpr - This accepts a string after semantic analysis. This string
160/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
161/// #6]), so it may come from multiple tokens.
162///
163Action::ExprResult ASTBuilder::
164ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +0000165 SourceLocation *TokLocs, unsigned NumToks) {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000166 assert(NumToks && "Must have at least one string!");
Chris Lattner72b7d392006-11-04 06:37:16 +0000167 return new StringExpr(StrData, StrLen, isWide);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000168}
169
170
Chris Lattner1b926492006-08-23 06:42:10 +0000171// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000172Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
173 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000174 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000175 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000176 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000177 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000178 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
179 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
180 case tok::amp: Opc = UnaryOperator::AddrOf; break;
181 case tok::star: Opc = UnaryOperator::Deref; break;
182 case tok::plus: Opc = UnaryOperator::Plus; break;
183 case tok::minus: Opc = UnaryOperator::Minus; break;
184 case tok::tilde: Opc = UnaryOperator::Not; break;
185 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000186 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
187 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000188 case tok::kw___real: Opc = UnaryOperator::Real; break;
189 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
190 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattnerc52b1182006-10-25 05:45:55 +0000191 case tok::kw___extension__:
Chris Lattner72b7d392006-11-04 06:37:16 +0000192 return Input;
193 //Opc = UnaryOperator::Extension;
194 //break;
Chris Lattner1b926492006-08-23 06:42:10 +0000195 }
196
Chris Lattner72b7d392006-11-04 06:37:16 +0000197 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000198}
199
Chris Lattner26da7302006-08-24 06:49:19 +0000200Action::ExprResult ASTBuilder::
201ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
202 SourceLocation LParenLoc, TypeTy *Ty,
203 SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000204 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
Chris Lattner26da7302006-08-24 06:49:19 +0000205}
206
207
Chris Lattnerae319692006-10-25 03:49:28 +0000208Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
209 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000210 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000211 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000212 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000213 default: assert(0 && "Unknown unary op!");
214 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
215 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
216 }
217
Chris Lattner72b7d392006-11-04 06:37:16 +0000218 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000219}
220
Chris Lattner98286a42006-08-24 05:02:11 +0000221Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000222ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
223 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000224 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
Chris Lattnere165d942006-08-24 04:40:38 +0000225}
226
Chris Lattner98286a42006-08-24 05:02:11 +0000227Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000228ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
229 tok::TokenKind OpKind, SourceLocation MemberLoc,
230 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000231 Decl *MemberDecl = 0;
232 // TODO: Look up MemberDecl.
Chris Lattner72b7d392006-11-04 06:37:16 +0000233 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000234}
235
236/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
237/// This provides the location of the left/right parens and a list of comma
238/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000239Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000240ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
241 ExprTy **Args, unsigned NumArgs,
242 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000243 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
Chris Lattnere165d942006-08-24 04:40:38 +0000244}
245
Chris Lattnere550a4e2006-08-24 06:37:51 +0000246Action::ExprResult ASTBuilder::
247ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
248 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000249 return new CastExpr((Type*)Ty, (Expr*)Op);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000250}
251
252
Chris Lattnere165d942006-08-24 04:40:38 +0000253
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000254// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000255Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
256 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000257 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000258 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000259 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000260 default: assert(0 && "Unknown binop!");
261 case tok::star: Opc = BinaryOperator::Mul; break;
262 case tok::slash: Opc = BinaryOperator::Div; break;
263 case tok::percent: Opc = BinaryOperator::Rem; break;
264 case tok::plus: Opc = BinaryOperator::Add; break;
265 case tok::minus: Opc = BinaryOperator::Sub; break;
266 case tok::lessless: Opc = BinaryOperator::Shl; break;
267 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
268 case tok::lessequal: Opc = BinaryOperator::LE; break;
269 case tok::less: Opc = BinaryOperator::LT; break;
270 case tok::greaterequal: Opc = BinaryOperator::GE; break;
271 case tok::greater: Opc = BinaryOperator::GT; break;
272 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
273 case tok::equalequal: Opc = BinaryOperator::EQ; break;
274 case tok::amp: Opc = BinaryOperator::And; break;
275 case tok::caret: Opc = BinaryOperator::Xor; break;
276 case tok::pipe: Opc = BinaryOperator::Or; break;
277 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
278 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
279 case tok::equal: Opc = BinaryOperator::Assign; break;
280 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
281 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
282 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
283 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
284 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
285 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
286 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
287 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
288 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
289 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
290 case tok::comma: Opc = BinaryOperator::Comma; break;
291 }
292
Chris Lattner72b7d392006-11-04 06:37:16 +0000293 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000294}
295
296/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
297/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000298Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
299 SourceLocation ColonLoc,
300 ExprTy *Cond, ExprTy *LHS,
301 ExprTy *RHS) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000302 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000303}
304