blob: cae97fc2bd605484455a0e6d7eb83680da5d21b1 [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>();
103 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
104}
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
111 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
112
113 // If this has an identifier, add it to the scope stack.
114 if (II) {
115 // If PrevDecl includes conflicting name here, emit a diagnostic.
116 II->setFETokenInfo(New);
117 S->AddDecl(II);
118 }
119}
120
121void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
122 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
123 I != E; ++I) {
124 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
125 Decl *D = II.getFETokenInfo<Decl>();
126 assert(D && "This decl didn't get pushed??");
127
128 Decl *Next = D->getNext();
129
130 // FIXME: Push the decl on the parent function list if in a function.
131 delete D;
132
133 II.setFETokenInfo(Next);
134 }
135}
136
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000137//===--------------------------------------------------------------------===//
138// Expression Parsing Callbacks.
139//===--------------------------------------------------------------------===//
140
Chris Lattner98286a42006-08-24 05:02:11 +0000141Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000142 switch (Tok.getKind()) {
143 default:
144 assert(0 && "Unknown simple primary expr!");
145 case tok::identifier: {
146 // Could be enum-constant or decl.
147 //Tok.getIdentifierInfo()
148 return new DeclExpr(*(Decl*)0);
149 }
150
151 case tok::char_constant: // constant: character-constant
152 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
153 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
154 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
155 assert(0 && "Unimp so far!");
156 return 0;
157 }
158}
159
Chris Lattner98286a42006-08-24 05:02:11 +0000160Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000161 return new IntegerConstant();
162}
Chris Lattner98286a42006-08-24 05:02:11 +0000163Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000164 return new FloatingConstant();
165}
166
Chris Lattner98286a42006-08-24 05:02:11 +0000167Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
168 SourceLocation R,
169 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000170 // FIXME: This is obviously just for testing.
171 ((Expr*)Val)->dump();
172 if (!FullLocInfo) return Val;
173
174 return new ParenExpr(L, R, (Expr*)Val);
175}
176
Chris Lattnerd3e98952006-10-06 05:22:26 +0000177/// ParseStringExpr - This accepts a string after semantic analysis. This string
178/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
179/// #6]), so it may come from multiple tokens.
180///
181Action::ExprResult ASTBuilder::
182ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
183 const LexerToken *Toks, unsigned NumToks) {
184 assert(NumToks && "Must have at least one string!");
185
186 if (!FullLocInfo)
187 return new StringExpr(StrData, StrLen, isWide);
188 else {
189 SmallVector<SourceLocation, 4> Locs;
190 for (unsigned i = 0; i != NumToks; ++i)
191 Locs.push_back(Toks[i].getLocation());
192 return new StringExprLOC(StrData, StrLen, isWide, &Locs[0], Locs.size());
193 }
194}
195
196
Chris Lattner1b926492006-08-23 06:42:10 +0000197// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000198Action::ExprResult ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
199 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000200 UnaryOperator::Opcode Opc;
201 switch (Tok.getKind()) {
202 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000203 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
204 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
205 case tok::amp: Opc = UnaryOperator::AddrOf; break;
206 case tok::star: Opc = UnaryOperator::Deref; break;
207 case tok::plus: Opc = UnaryOperator::Plus; break;
208 case tok::minus: Opc = UnaryOperator::Minus; break;
209 case tok::tilde: Opc = UnaryOperator::Not; break;
210 case tok::exclaim: Opc = UnaryOperator::LNot; break;
211 case tok::kw___real: Opc = UnaryOperator::Real; break;
212 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
213 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
214 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattner1b926492006-08-23 06:42:10 +0000215 }
216
217 if (!FullLocInfo)
218 return new UnaryOperator((Expr*)Input, Opc);
219 else
220 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
221}
222
Chris Lattner26da7302006-08-24 06:49:19 +0000223Action::ExprResult ASTBuilder::
224ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
225 SourceLocation LParenLoc, TypeTy *Ty,
226 SourceLocation RParenLoc) {
227 if (!FullLocInfo)
228 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
229 else
230 return new SizeOfAlignOfTypeExprLOC(OpLoc, isSizeof, LParenLoc, (Type*)Ty,
231 RParenLoc);
232}
233
234
Chris Lattner98286a42006-08-24 05:02:11 +0000235Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
236 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000237 UnaryOperator::Opcode Opc;
238 switch (Tok.getKind()) {
239 default: assert(0 && "Unknown unary op!");
240 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
241 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
242 }
243
244 if (!FullLocInfo)
245 return new UnaryOperator((Expr*)Input, Opc);
246 else
247 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
248}
249
Chris Lattner98286a42006-08-24 05:02:11 +0000250Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000251ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
252 ExprTy *Idx, SourceLocation RLoc) {
253 if (!FullLocInfo)
254 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
255 else
256 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
257}
258
Chris Lattner98286a42006-08-24 05:02:11 +0000259Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000260ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
261 tok::TokenKind OpKind, SourceLocation MemberLoc,
262 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000263 Decl *MemberDecl = 0;
264 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000265 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000266 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000267 else
268 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000269 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000270}
271
272/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
273/// This provides the location of the left/right parens and a list of comma
274/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000275Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000276ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
277 ExprTy **Args, unsigned NumArgs,
278 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
279 if (!FullLocInfo)
280 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
281 else
282 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
283 CommaLocs, RParenLoc);
284}
285
Chris Lattnere550a4e2006-08-24 06:37:51 +0000286Action::ExprResult ASTBuilder::
287ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
288 SourceLocation RParenLoc, ExprTy *Op) {
289 if (!FullLocInfo)
290 return new CastExpr((Type*)Ty, (Expr*)Op);
291 else
292 return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
293}
294
295
Chris Lattnere165d942006-08-24 04:40:38 +0000296
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000297// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000298Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
299 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000300 BinaryOperator::Opcode Opc;
301 switch (Tok.getKind()) {
302 default: assert(0 && "Unknown binop!");
303 case tok::star: Opc = BinaryOperator::Mul; break;
304 case tok::slash: Opc = BinaryOperator::Div; break;
305 case tok::percent: Opc = BinaryOperator::Rem; break;
306 case tok::plus: Opc = BinaryOperator::Add; break;
307 case tok::minus: Opc = BinaryOperator::Sub; break;
308 case tok::lessless: Opc = BinaryOperator::Shl; break;
309 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
310 case tok::lessequal: Opc = BinaryOperator::LE; break;
311 case tok::less: Opc = BinaryOperator::LT; break;
312 case tok::greaterequal: Opc = BinaryOperator::GE; break;
313 case tok::greater: Opc = BinaryOperator::GT; break;
314 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
315 case tok::equalequal: Opc = BinaryOperator::EQ; break;
316 case tok::amp: Opc = BinaryOperator::And; break;
317 case tok::caret: Opc = BinaryOperator::Xor; break;
318 case tok::pipe: Opc = BinaryOperator::Or; break;
319 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
320 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
321 case tok::equal: Opc = BinaryOperator::Assign; break;
322 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
323 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
324 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
325 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
326 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
327 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
328 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
329 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
330 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
331 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
332 case tok::comma: Opc = BinaryOperator::Comma; break;
333 }
334
335 if (!FullLocInfo)
336 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
337 else
338 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
339}
340
341/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
342/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000343Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
344 SourceLocation ColonLoc,
345 ExprTy *Cond, ExprTy *LHS,
346 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000347 if (!FullLocInfo)
348 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
349 else
350 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
351 ColonLoc, (Expr*)RHS);
352}
353
Chris Lattnerc11438c2006-08-18 05:17:52 +0000354
355/// Interface to the Builder.cpp file.
356///
Chris Lattnerd3e98952006-10-06 05:22:26 +0000357Action *CreateASTBuilderActions(Preprocessor &PP, bool FullLocInfo) {
358 return new ASTBuilder(PP, FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000359}
360
361
362