blob: f32170dcd131ee710c2cecca6a9542066a74fd40 [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 Lattnerc11438c2006-08-18 05:17:52 +000021#include "llvm/Support/Visibility.h"
22using namespace llvm;
23using namespace clang;
24
25/// ASTBuilder
26namespace {
27class VISIBILITY_HIDDEN ASTBuilder : public Action {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000028 /// FullLocInfo - If this is true, the ASTBuilder constructs AST Nodes that
29 /// capture maximal location information for each source-language construct.
30 bool FullLocInfo;
Chris Lattnerc11438c2006-08-18 05:17:52 +000031public:
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000032 ASTBuilder(bool fullLocInfo) : FullLocInfo(fullLocInfo) {}
Chris Lattnerc11438c2006-08-18 05:17:52 +000033 //===--------------------------------------------------------------------===//
34 // Symbol table tracking callbacks.
35 //
36 virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const;
37 virtual void ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
38 ExprTy *Init);
39 virtual void PopScope(SourceLocation Loc, Scope *S);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000040
41 //===--------------------------------------------------------------------===//
42 // Expression Parsing Callbacks.
Chris Lattner1b926492006-08-23 06:42:10 +000043
44 // Primary Expressions.
Chris Lattner98286a42006-08-24 05:02:11 +000045 virtual ExprResult ParseSimplePrimaryExpr(const LexerToken &Tok);
46 virtual ExprResult ParseIntegerConstant(const LexerToken &Tok);
47 virtual ExprResult ParseFloatingConstant(const LexerToken &Tok);
48 virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
49 ExprTy *Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000050
Chris Lattner1b926492006-08-23 06:42:10 +000051 // Binary/Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +000052 virtual ExprResult ParseUnaryOp(const LexerToken &Tok, ExprTy *Input);
53 virtual ExprResult ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input);
Chris Lattner1b926492006-08-23 06:42:10 +000054
Chris Lattner98286a42006-08-24 05:02:11 +000055 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
56 ExprTy *Idx, SourceLocation RLoc);
57 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
58 tok::TokenKind OpKind,
59 SourceLocation MemberLoc,
60 IdentifierInfo &Member);
Chris Lattnere165d942006-08-24 04:40:38 +000061
62 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
63 /// This provides the location of the left/right parens and a list of comma
64 /// locations.
Chris Lattner98286a42006-08-24 05:02:11 +000065 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
66 ExprTy **Args, unsigned NumArgs,
67 SourceLocation *CommaLocs,
68 SourceLocation RParenLoc);
Chris Lattnere165d942006-08-24 04:40:38 +000069
Chris Lattnere550a4e2006-08-24 06:37:51 +000070 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
71 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattnere165d942006-08-24 04:40:38 +000072
Chris Lattner98286a42006-08-24 05:02:11 +000073 virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000074
75 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
76 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +000077 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
78 SourceLocation ColonLoc,
79 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000080};
81} // end anonymous namespace
82
83
84//===----------------------------------------------------------------------===//
85// Symbol table tracking callbacks.
86//===----------------------------------------------------------------------===//
87
88bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
89 Decl *D = II.getFETokenInfo<Decl>();
90 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
91}
92
93void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
94 ExprTy *Init) {
95 IdentifierInfo *II = D.getIdentifier();
96 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
97
98 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
99
100 // If this has an identifier, add it to the scope stack.
101 if (II) {
102 // If PrevDecl includes conflicting name here, emit a diagnostic.
103 II->setFETokenInfo(New);
104 S->AddDecl(II);
105 }
106}
107
108void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
109 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
110 I != E; ++I) {
111 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
112 Decl *D = II.getFETokenInfo<Decl>();
113 assert(D && "This decl didn't get pushed??");
114
115 Decl *Next = D->getNext();
116
117 // FIXME: Push the decl on the parent function list if in a function.
118 delete D;
119
120 II.setFETokenInfo(Next);
121 }
122}
123
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000124//===--------------------------------------------------------------------===//
125// Expression Parsing Callbacks.
126//===--------------------------------------------------------------------===//
127
Chris Lattner98286a42006-08-24 05:02:11 +0000128Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000129 switch (Tok.getKind()) {
130 default:
131 assert(0 && "Unknown simple primary expr!");
132 case tok::identifier: {
133 // Could be enum-constant or decl.
134 //Tok.getIdentifierInfo()
135 return new DeclExpr(*(Decl*)0);
136 }
137
138 case tok::char_constant: // constant: character-constant
139 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
140 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
141 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
142 assert(0 && "Unimp so far!");
143 return 0;
144 }
145}
146
Chris Lattner98286a42006-08-24 05:02:11 +0000147Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000148 return new IntegerConstant();
149}
Chris Lattner98286a42006-08-24 05:02:11 +0000150Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000151 return new FloatingConstant();
152}
153
Chris Lattner98286a42006-08-24 05:02:11 +0000154Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
155 SourceLocation R,
156 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000157 // FIXME: This is obviously just for testing.
158 ((Expr*)Val)->dump();
159 if (!FullLocInfo) return Val;
160
161 return new ParenExpr(L, R, (Expr*)Val);
162}
163
164// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000165Action::ExprResult ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
166 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000167 UnaryOperator::Opcode Opc;
168 switch (Tok.getKind()) {
169 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000170 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
171 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
172 case tok::amp: Opc = UnaryOperator::AddrOf; break;
173 case tok::star: Opc = UnaryOperator::Deref; break;
174 case tok::plus: Opc = UnaryOperator::Plus; break;
175 case tok::minus: Opc = UnaryOperator::Minus; break;
176 case tok::tilde: Opc = UnaryOperator::Not; break;
177 case tok::exclaim: Opc = UnaryOperator::LNot; break;
178 case tok::kw___real: Opc = UnaryOperator::Real; break;
179 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
180 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
181 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattner1b926492006-08-23 06:42:10 +0000182 }
183
184 if (!FullLocInfo)
185 return new UnaryOperator((Expr*)Input, Opc);
186 else
187 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
188}
189
Chris Lattner98286a42006-08-24 05:02:11 +0000190Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
191 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000192 UnaryOperator::Opcode Opc;
193 switch (Tok.getKind()) {
194 default: assert(0 && "Unknown unary op!");
195 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
196 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
197 }
198
199 if (!FullLocInfo)
200 return new UnaryOperator((Expr*)Input, Opc);
201 else
202 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
203}
204
Chris Lattner98286a42006-08-24 05:02:11 +0000205Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000206ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
207 ExprTy *Idx, SourceLocation RLoc) {
208 if (!FullLocInfo)
209 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
210 else
211 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
212}
213
Chris Lattner98286a42006-08-24 05:02:11 +0000214Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000215ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
216 tok::TokenKind OpKind, SourceLocation MemberLoc,
217 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000218 Decl *MemberDecl = 0;
219 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000220 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000221 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000222 else
223 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000224 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000225}
226
227/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
228/// This provides the location of the left/right parens and a list of comma
229/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000230Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000231ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
232 ExprTy **Args, unsigned NumArgs,
233 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
234 if (!FullLocInfo)
235 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
236 else
237 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
238 CommaLocs, RParenLoc);
239}
240
Chris Lattnere550a4e2006-08-24 06:37:51 +0000241Action::ExprResult ASTBuilder::
242ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
243 SourceLocation RParenLoc, ExprTy *Op) {
244 if (!FullLocInfo)
245 return new CastExpr((Type*)Ty, (Expr*)Op);
246 else
247 return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
248}
249
250
Chris Lattnere165d942006-08-24 04:40:38 +0000251
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000252// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000253Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
254 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000255 BinaryOperator::Opcode Opc;
256 switch (Tok.getKind()) {
257 default: assert(0 && "Unknown binop!");
258 case tok::star: Opc = BinaryOperator::Mul; break;
259 case tok::slash: Opc = BinaryOperator::Div; break;
260 case tok::percent: Opc = BinaryOperator::Rem; break;
261 case tok::plus: Opc = BinaryOperator::Add; break;
262 case tok::minus: Opc = BinaryOperator::Sub; break;
263 case tok::lessless: Opc = BinaryOperator::Shl; break;
264 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
265 case tok::lessequal: Opc = BinaryOperator::LE; break;
266 case tok::less: Opc = BinaryOperator::LT; break;
267 case tok::greaterequal: Opc = BinaryOperator::GE; break;
268 case tok::greater: Opc = BinaryOperator::GT; break;
269 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
270 case tok::equalequal: Opc = BinaryOperator::EQ; break;
271 case tok::amp: Opc = BinaryOperator::And; break;
272 case tok::caret: Opc = BinaryOperator::Xor; break;
273 case tok::pipe: Opc = BinaryOperator::Or; break;
274 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
275 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
276 case tok::equal: Opc = BinaryOperator::Assign; break;
277 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
278 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
279 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
280 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
281 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
282 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
283 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
284 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
285 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
286 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
287 case tok::comma: Opc = BinaryOperator::Comma; break;
288 }
289
290 if (!FullLocInfo)
291 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
292 else
293 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
294}
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 Lattner9b6d4cb2006-08-23 05:17:46 +0000302 if (!FullLocInfo)
303 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
304 else
305 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
306 ColonLoc, (Expr*)RHS);
307}
308
Chris Lattnerc11438c2006-08-18 05:17:52 +0000309
310/// Interface to the Builder.cpp file.
311///
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000312Action *CreateASTBuilderActions(bool FullLocInfo) {
313 return new ASTBuilder(FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000314}
315
316
317