blob: 116ec09dd90f058cae8c4afa7cebd4eb3206f4de [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 Lattner9b6d4cb2006-08-23 05:17:46 +000020#include "clang/Lex/LexerToken.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
26/// ASTBuilder
27namespace {
28class VISIBILITY_HIDDEN ASTBuilder : public Action {
Chris Lattnerd3e98952006-10-06 05:22:26 +000029 Preprocessor &PP;
30
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000031 /// 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 Lattner2dacc3f2006-10-16 00:33:54 +000034
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 Lattnerc11438c2006-08-18 05:17:52 +000040public:
Chris Lattner2dacc3f2006-10-16 00:33:54 +000041 ASTBuilder(Preprocessor &pp, bool fullLocInfo,
42 std::vector<Decl*> &prevInGroup)
43 : PP(pp), FullLocInfo(fullLocInfo), LastInGroupList(prevInGroup) {}
Chris Lattnerd3e98952006-10-06 05:22:26 +000044
Chris Lattnerc11438c2006-08-18 05:17:52 +000045 //===--------------------------------------------------------------------===//
46 // Symbol table tracking callbacks.
47 //
48 virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const;
Chris Lattner2dacc3f2006-10-16 00:33:54 +000049 virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
50 DeclTy *LastInGroup);
51 virtual DeclTy *ParseFunctionDefinition(Scope *S, Declarator &D,
52 StmtTy *Body);
Chris Lattnerc11438c2006-08-18 05:17:52 +000053 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000054
55 //===--------------------------------------------------------------------===//
56 // Expression Parsing Callbacks.
Chris Lattner1b926492006-08-23 06:42:10 +000057
58 // Primary Expressions.
Chris Lattner98286a42006-08-24 05:02:11 +000059 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 Lattnerd3e98952006-10-06 05:22:26 +000064 virtual ExprResult ParseStringExpr(const char *StrData, unsigned StrLen,
65 bool isWide,
66 const LexerToken *Toks, unsigned NumToks);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000067
Chris Lattner1b926492006-08-23 06:42:10 +000068 // Binary/Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +000069 virtual ExprResult ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
70 ExprTy *Input);
Chris Lattner26da7302006-08-24 06:49:19 +000071 virtual ExprResult
72 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
73 SourceLocation LParenLoc, TypeTy *Ty,
74 SourceLocation RParenLoc);
75
Chris Lattner98286a42006-08-24 05:02:11 +000076 virtual ExprResult ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input);
Chris Lattner1b926492006-08-23 06:42:10 +000077
Chris Lattner98286a42006-08-24 05:02:11 +000078 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
79 ExprTy *Idx, SourceLocation RLoc);
80 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
81 tok::TokenKind OpKind,
82 SourceLocation MemberLoc,
83 IdentifierInfo &Member);
Chris Lattnere165d942006-08-24 04:40:38 +000084
85 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
86 /// This provides the location of the left/right parens and a list of comma
87 /// locations.
Chris Lattner98286a42006-08-24 05:02:11 +000088 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
89 ExprTy **Args, unsigned NumArgs,
90 SourceLocation *CommaLocs,
91 SourceLocation RParenLoc);
Chris Lattnere165d942006-08-24 04:40:38 +000092
Chris Lattnere550a4e2006-08-24 06:37:51 +000093 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
94 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattnere165d942006-08-24 04:40:38 +000095
Chris Lattner98286a42006-08-24 05:02:11 +000096 virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000097
98 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
99 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000100 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
101 SourceLocation ColonLoc,
102 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000103};
104} // end anonymous namespace
105
106
107//===----------------------------------------------------------------------===//
108// Symbol table tracking callbacks.
109//===----------------------------------------------------------------------===//
110
111bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
112 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +0000113 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +0000114}
115
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000116Action::DeclTy *
117ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
118 DeclTy *LastInGroup) {
Chris Lattnerc11438c2006-08-18 05:17:52 +0000119 IdentifierInfo *II = D.getIdentifier();
120 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
121
Chris Lattnera11999d2006-10-15 22:34:45 +0000122 Decl *New;
123 if (D.isFunctionDeclarator())
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000124 New = new FunctionDecl(II, D, PrevDecl);
Chris Lattnera11999d2006-10-15 22:34:45 +0000125 else
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000126 New = new VarDecl(II, D, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000127
128 // If this has an identifier, add it to the scope stack.
129 if (II) {
130 // If PrevDecl includes conflicting name here, emit a diagnostic.
131 II->setFETokenInfo(New);
132 S->AddDecl(II);
133 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000134
135 if (LastInGroup) LastInGroupList.push_back((Decl*)LastInGroup);
136
137 return New;
138}
139
140Action::DeclTy *
141ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
142 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
Chris Lattner30f910e2006-10-16 05:52:41 +0000143
144 FD->setBody((Stmt*)Body);
145
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000146 return FD;
Chris Lattnerc11438c2006-08-18 05:17:52 +0000147}
148
149void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
150 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
151 I != E; ++I) {
152 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
153 Decl *D = II.getFETokenInfo<Decl>();
154 assert(D && "This decl didn't get pushed??");
155
156 Decl *Next = D->getNext();
157
158 // FIXME: Push the decl on the parent function list if in a function.
159 delete D;
160
161 II.setFETokenInfo(Next);
162 }
163}
164
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000165//===--------------------------------------------------------------------===//
166// Expression Parsing Callbacks.
167//===--------------------------------------------------------------------===//
168
Chris Lattner98286a42006-08-24 05:02:11 +0000169Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000170 switch (Tok.getKind()) {
171 default:
172 assert(0 && "Unknown simple primary expr!");
173 case tok::identifier: {
174 // Could be enum-constant or decl.
175 //Tok.getIdentifierInfo()
176 return new DeclExpr(*(Decl*)0);
177 }
178
179 case tok::char_constant: // constant: character-constant
180 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
181 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
182 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000183 //assert(0 && "FIXME: Unimp so far!");
184 return new DeclExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000185 }
186}
187
Chris Lattner98286a42006-08-24 05:02:11 +0000188Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000189 return new IntegerConstant();
190}
Chris Lattner98286a42006-08-24 05:02:11 +0000191Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000192 return new FloatingConstant();
193}
194
Chris Lattner98286a42006-08-24 05:02:11 +0000195Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
196 SourceLocation R,
197 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000198 if (!FullLocInfo) return Val;
199
200 return new ParenExpr(L, R, (Expr*)Val);
201}
202
Chris Lattnerd3e98952006-10-06 05:22:26 +0000203/// ParseStringExpr - This accepts a string after semantic analysis. This string
204/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
205/// #6]), so it may come from multiple tokens.
206///
207Action::ExprResult ASTBuilder::
208ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
209 const LexerToken *Toks, unsigned NumToks) {
210 assert(NumToks && "Must have at least one string!");
211
212 if (!FullLocInfo)
213 return new StringExpr(StrData, StrLen, isWide);
214 else {
215 SmallVector<SourceLocation, 4> Locs;
216 for (unsigned i = 0; i != NumToks; ++i)
217 Locs.push_back(Toks[i].getLocation());
218 return new StringExprLOC(StrData, StrLen, isWide, &Locs[0], Locs.size());
219 }
220}
221
222
Chris Lattner1b926492006-08-23 06:42:10 +0000223// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000224Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
225 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000226 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000227 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000228 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000229 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000230 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
231 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
232 case tok::amp: Opc = UnaryOperator::AddrOf; break;
233 case tok::star: Opc = UnaryOperator::Deref; break;
234 case tok::plus: Opc = UnaryOperator::Plus; break;
235 case tok::minus: Opc = UnaryOperator::Minus; break;
236 case tok::tilde: Opc = UnaryOperator::Not; break;
237 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000238 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
239 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000240 case tok::kw___real: Opc = UnaryOperator::Real; break;
241 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
242 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattner1b926492006-08-23 06:42:10 +0000243 }
244
245 if (!FullLocInfo)
246 return new UnaryOperator((Expr*)Input, Opc);
247 else
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000248 return new UnaryOperatorLOC(OpLoc, (Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000249}
250
Chris Lattner26da7302006-08-24 06:49:19 +0000251Action::ExprResult ASTBuilder::
252ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
253 SourceLocation LParenLoc, TypeTy *Ty,
254 SourceLocation RParenLoc) {
255 if (!FullLocInfo)
256 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
257 else
258 return new SizeOfAlignOfTypeExprLOC(OpLoc, isSizeof, LParenLoc, (Type*)Ty,
259 RParenLoc);
260}
261
262
Chris Lattner98286a42006-08-24 05:02:11 +0000263Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
264 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000265 UnaryOperator::Opcode Opc;
266 switch (Tok.getKind()) {
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
275 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
276}
277
Chris Lattner98286a42006-08-24 05:02:11 +0000278Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000279ParseArraySubscriptExpr(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 Lattner98286a42006-08-24 05:02:11 +0000287Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000288ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
289 tok::TokenKind OpKind, SourceLocation MemberLoc,
290 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000291 Decl *MemberDecl = 0;
292 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000293 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000294 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000295 else
296 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000297 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000298}
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 Lattner98286a42006-08-24 05:02:11 +0000303Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000304ParseCallExpr(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 Lattnere550a4e2006-08-24 06:37:51 +0000314Action::ExprResult ASTBuilder::
315ParseCastExpr(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 Lattnere165d942006-08-24 04:40:38 +0000324
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000325// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000326Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
327 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000328 BinaryOperator::Opcode Opc;
329 switch (Tok.getKind()) {
330 default: assert(0 && "Unknown binop!");
331 case tok::star: Opc = BinaryOperator::Mul; break;
332 case tok::slash: Opc = BinaryOperator::Div; break;
333 case tok::percent: Opc = BinaryOperator::Rem; break;
334 case tok::plus: Opc = BinaryOperator::Add; break;
335 case tok::minus: Opc = BinaryOperator::Sub; break;
336 case tok::lessless: Opc = BinaryOperator::Shl; break;
337 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
338 case tok::lessequal: Opc = BinaryOperator::LE; break;
339 case tok::less: Opc = BinaryOperator::LT; break;
340 case tok::greaterequal: Opc = BinaryOperator::GE; break;
341 case tok::greater: Opc = BinaryOperator::GT; break;
342 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
343 case tok::equalequal: Opc = BinaryOperator::EQ; break;
344 case tok::amp: Opc = BinaryOperator::And; break;
345 case tok::caret: Opc = BinaryOperator::Xor; break;
346 case tok::pipe: Opc = BinaryOperator::Or; break;
347 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
348 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
349 case tok::equal: Opc = BinaryOperator::Assign; break;
350 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
351 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
352 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
353 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
354 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
355 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
356 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
357 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
358 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
359 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
360 case tok::comma: Opc = BinaryOperator::Comma; break;
361 }
362
363 if (!FullLocInfo)
364 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
365 else
366 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
367}
368
369/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
370/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000371Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
372 SourceLocation ColonLoc,
373 ExprTy *Cond, ExprTy *LHS,
374 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000375 if (!FullLocInfo)
376 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
377 else
378 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
379 ColonLoc, (Expr*)RHS);
380}
381
Chris Lattnerc11438c2006-08-18 05:17:52 +0000382
383/// Interface to the Builder.cpp file.
384///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000385Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo,
386 std::vector<Decl*> &LastInGroupList) {
387 return new ASTBuilder(PP, FullLocInfo, LastInGroupList);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000388}
389