blob: 6788496cb98dd7b79407fc5dab607a251578c1b9 [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 Lattnerc11438c2006-08-18 05:17:52 +000034public:
Chris Lattnerd3e98952006-10-06 05:22:26 +000035 ASTBuilder(Preprocessor &pp, bool fullLocInfo)
36 : PP(pp), FullLocInfo(fullLocInfo) {}
37
Chris Lattnerc11438c2006-08-18 05:17:52 +000038 //===--------------------------------------------------------------------===//
39 // Symbol table tracking callbacks.
40 //
41 virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const;
42 virtual void ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
43 ExprTy *Init);
44 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000045
46 //===--------------------------------------------------------------------===//
47 // Expression Parsing Callbacks.
Chris Lattner1b926492006-08-23 06:42:10 +000048
49 // Primary Expressions.
Chris Lattner98286a42006-08-24 05:02:11 +000050 virtual ExprResult ParseSimplePrimaryExpr(const LexerToken &Tok);
51 virtual ExprResult ParseIntegerConstant(const LexerToken &Tok);
52 virtual ExprResult ParseFloatingConstant(const LexerToken &Tok);
53 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
54 ExprTy *Val);
Chris Lattnerd3e98952006-10-06 05:22:26 +000055 virtual ExprResult ParseStringExpr(const char *StrData, unsigned StrLen,
56 bool isWide,
57 const LexerToken *Toks, unsigned NumToks);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000058
Chris Lattner1b926492006-08-23 06:42:10 +000059 // Binary/Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +000060 virtual ExprResult ParseUnaryOp(const LexerToken &Tok, ExprTy *Input);
Chris Lattner26da7302006-08-24 06:49:19 +000061 virtual ExprResult
62 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
63 SourceLocation LParenLoc, TypeTy *Ty,
64 SourceLocation RParenLoc);
65
Chris Lattner98286a42006-08-24 05:02:11 +000066 virtual ExprResult ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input);
Chris Lattner1b926492006-08-23 06:42:10 +000067
Chris Lattner98286a42006-08-24 05:02:11 +000068 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
69 ExprTy *Idx, SourceLocation RLoc);
70 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
71 tok::TokenKind OpKind,
72 SourceLocation MemberLoc,
73 IdentifierInfo &Member);
Chris Lattnere165d942006-08-24 04:40:38 +000074
75 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
76 /// This provides the location of the left/right parens and a list of comma
77 /// locations.
Chris Lattner98286a42006-08-24 05:02:11 +000078 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
79 ExprTy **Args, unsigned NumArgs,
80 SourceLocation *CommaLocs,
81 SourceLocation RParenLoc);
Chris Lattnere165d942006-08-24 04:40:38 +000082
Chris Lattnere550a4e2006-08-24 06:37:51 +000083 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
84 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattnere165d942006-08-24 04:40:38 +000085
Chris Lattner98286a42006-08-24 05:02:11 +000086 virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000087
88 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
89 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +000090 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
91 SourceLocation ColonLoc,
92 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000093};
94} // end anonymous namespace
95
96
97//===----------------------------------------------------------------------===//
98// Symbol table tracking callbacks.
99//===----------------------------------------------------------------------===//
100
101bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
102 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +0000103 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +0000104}
105
106void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
107 ExprTy *Init) {
108 IdentifierInfo *II = D.getIdentifier();
109 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
110
Chris Lattnera11999d2006-10-15 22:34:45 +0000111 Decl *New;
112 if (D.isFunctionDeclarator())
113 New = new FunctionDecl(II, D, Loc, PrevDecl);
114 else
115 New = new VarDecl(II, D, Loc, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000116
117 // If this has an identifier, add it to the scope stack.
118 if (II) {
119 // If PrevDecl includes conflicting name here, emit a diagnostic.
120 II->setFETokenInfo(New);
121 S->AddDecl(II);
122 }
123}
124
125void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
126 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
127 I != E; ++I) {
128 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
129 Decl *D = II.getFETokenInfo<Decl>();
130 assert(D && "This decl didn't get pushed??");
131
132 Decl *Next = D->getNext();
133
134 // FIXME: Push the decl on the parent function list if in a function.
135 delete D;
136
137 II.setFETokenInfo(Next);
138 }
139}
140
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000141//===--------------------------------------------------------------------===//
142// Expression Parsing Callbacks.
143//===--------------------------------------------------------------------===//
144
Chris Lattner98286a42006-08-24 05:02:11 +0000145Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000146 switch (Tok.getKind()) {
147 default:
148 assert(0 && "Unknown simple primary expr!");
149 case tok::identifier: {
150 // Could be enum-constant or decl.
151 //Tok.getIdentifierInfo()
152 return new DeclExpr(*(Decl*)0);
153 }
154
155 case tok::char_constant: // constant: character-constant
156 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
157 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
158 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000159 //assert(0 && "FIXME: Unimp so far!");
160 return new DeclExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000161 }
162}
163
Chris Lattner98286a42006-08-24 05:02:11 +0000164Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000165 return new IntegerConstant();
166}
Chris Lattner98286a42006-08-24 05:02:11 +0000167Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000168 return new FloatingConstant();
169}
170
Chris Lattner98286a42006-08-24 05:02:11 +0000171Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
172 SourceLocation R,
173 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000174 // FIXME: This is obviously just for testing.
175 ((Expr*)Val)->dump();
176 if (!FullLocInfo) return Val;
177
178 return new ParenExpr(L, R, (Expr*)Val);
179}
180
Chris Lattnerd3e98952006-10-06 05:22:26 +0000181/// ParseStringExpr - This accepts a string after semantic analysis. This string
182/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
183/// #6]), so it may come from multiple tokens.
184///
185Action::ExprResult ASTBuilder::
186ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
187 const LexerToken *Toks, unsigned NumToks) {
188 assert(NumToks && "Must have at least one string!");
189
190 if (!FullLocInfo)
191 return new StringExpr(StrData, StrLen, isWide);
192 else {
193 SmallVector<SourceLocation, 4> Locs;
194 for (unsigned i = 0; i != NumToks; ++i)
195 Locs.push_back(Toks[i].getLocation());
196 return new StringExprLOC(StrData, StrLen, isWide, &Locs[0], Locs.size());
197 }
198}
199
200
Chris Lattner1b926492006-08-23 06:42:10 +0000201// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000202Action::ExprResult ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
203 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000204 UnaryOperator::Opcode Opc;
205 switch (Tok.getKind()) {
206 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000207 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
208 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
209 case tok::amp: Opc = UnaryOperator::AddrOf; break;
210 case tok::star: Opc = UnaryOperator::Deref; break;
211 case tok::plus: Opc = UnaryOperator::Plus; break;
212 case tok::minus: Opc = UnaryOperator::Minus; break;
213 case tok::tilde: Opc = UnaryOperator::Not; break;
214 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000215 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
216 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000217 case tok::kw___real: Opc = UnaryOperator::Real; break;
218 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
219 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattner1b926492006-08-23 06:42:10 +0000220 }
221
222 if (!FullLocInfo)
223 return new UnaryOperator((Expr*)Input, Opc);
224 else
225 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
226}
227
Chris Lattner26da7302006-08-24 06:49:19 +0000228Action::ExprResult ASTBuilder::
229ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
230 SourceLocation LParenLoc, TypeTy *Ty,
231 SourceLocation RParenLoc) {
232 if (!FullLocInfo)
233 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
234 else
235 return new SizeOfAlignOfTypeExprLOC(OpLoc, isSizeof, LParenLoc, (Type*)Ty,
236 RParenLoc);
237}
238
239
Chris Lattner98286a42006-08-24 05:02:11 +0000240Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
241 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000242 UnaryOperator::Opcode Opc;
243 switch (Tok.getKind()) {
244 default: assert(0 && "Unknown unary op!");
245 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
246 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
247 }
248
249 if (!FullLocInfo)
250 return new UnaryOperator((Expr*)Input, Opc);
251 else
252 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
253}
254
Chris Lattner98286a42006-08-24 05:02:11 +0000255Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000256ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
257 ExprTy *Idx, SourceLocation RLoc) {
258 if (!FullLocInfo)
259 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
260 else
261 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
262}
263
Chris Lattner98286a42006-08-24 05:02:11 +0000264Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000265ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
266 tok::TokenKind OpKind, SourceLocation MemberLoc,
267 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000268 Decl *MemberDecl = 0;
269 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000270 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000271 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000272 else
273 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000274 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000275}
276
277/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
278/// This provides the location of the left/right parens and a list of comma
279/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000280Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000281ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
282 ExprTy **Args, unsigned NumArgs,
283 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
284 if (!FullLocInfo)
285 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
286 else
287 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
288 CommaLocs, RParenLoc);
289}
290
Chris Lattnere550a4e2006-08-24 06:37:51 +0000291Action::ExprResult ASTBuilder::
292ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
293 SourceLocation RParenLoc, ExprTy *Op) {
294 if (!FullLocInfo)
295 return new CastExpr((Type*)Ty, (Expr*)Op);
296 else
297 return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
298}
299
300
Chris Lattnere165d942006-08-24 04:40:38 +0000301
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000302// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000303Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
304 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000305 BinaryOperator::Opcode Opc;
306 switch (Tok.getKind()) {
307 default: assert(0 && "Unknown binop!");
308 case tok::star: Opc = BinaryOperator::Mul; break;
309 case tok::slash: Opc = BinaryOperator::Div; break;
310 case tok::percent: Opc = BinaryOperator::Rem; break;
311 case tok::plus: Opc = BinaryOperator::Add; break;
312 case tok::minus: Opc = BinaryOperator::Sub; break;
313 case tok::lessless: Opc = BinaryOperator::Shl; break;
314 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
315 case tok::lessequal: Opc = BinaryOperator::LE; break;
316 case tok::less: Opc = BinaryOperator::LT; break;
317 case tok::greaterequal: Opc = BinaryOperator::GE; break;
318 case tok::greater: Opc = BinaryOperator::GT; break;
319 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
320 case tok::equalequal: Opc = BinaryOperator::EQ; break;
321 case tok::amp: Opc = BinaryOperator::And; break;
322 case tok::caret: Opc = BinaryOperator::Xor; break;
323 case tok::pipe: Opc = BinaryOperator::Or; break;
324 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
325 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
326 case tok::equal: Opc = BinaryOperator::Assign; break;
327 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
328 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
329 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
330 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
331 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
332 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
333 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
334 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
335 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
336 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
337 case tok::comma: Opc = BinaryOperator::Comma; break;
338 }
339
340 if (!FullLocInfo)
341 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
342 else
343 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
344}
345
346/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
347/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000348Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
349 SourceLocation ColonLoc,
350 ExprTy *Cond, ExprTy *LHS,
351 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000352 if (!FullLocInfo)
353 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
354 else
355 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
356 ColonLoc, (Expr*)RHS);
357}
358
Chris Lattnerc11438c2006-08-18 05:17:52 +0000359
360/// Interface to the Builder.cpp file.
361///
Chris Lattnerd3e98952006-10-06 05:22:26 +0000362Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo) {
363 return new ASTBuilder(PP, FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000364}
365
366
367