blob: 06eed86d4423a162849f43f5e6bc6c642f42428b [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);
Chris Lattner26da7302006-08-24 06:49:19 +000053 virtual ExprResult
54 ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
55 SourceLocation LParenLoc, TypeTy *Ty,
56 SourceLocation RParenLoc);
57
Chris Lattner98286a42006-08-24 05:02:11 +000058 virtual ExprResult ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input);
Chris Lattner1b926492006-08-23 06:42:10 +000059
Chris Lattner98286a42006-08-24 05:02:11 +000060 virtual ExprResult ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
61 ExprTy *Idx, SourceLocation RLoc);
62 virtual ExprResult ParseMemberReferenceExpr(ExprTy *Base,SourceLocation OpLoc,
63 tok::TokenKind OpKind,
64 SourceLocation MemberLoc,
65 IdentifierInfo &Member);
Chris Lattnere165d942006-08-24 04:40:38 +000066
67 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
68 /// This provides the location of the left/right parens and a list of comma
69 /// locations.
Chris Lattner98286a42006-08-24 05:02:11 +000070 virtual ExprResult ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
71 ExprTy **Args, unsigned NumArgs,
72 SourceLocation *CommaLocs,
73 SourceLocation RParenLoc);
Chris Lattnere165d942006-08-24 04:40:38 +000074
Chris Lattnere550a4e2006-08-24 06:37:51 +000075 virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
76 SourceLocation RParenLoc, ExprTy *Op);
Chris Lattnere165d942006-08-24 04:40:38 +000077
Chris Lattner98286a42006-08-24 05:02:11 +000078 virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000079
80 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
81 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +000082 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
83 SourceLocation ColonLoc,
84 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000085};
86} // end anonymous namespace
87
88
89//===----------------------------------------------------------------------===//
90// Symbol table tracking callbacks.
91//===----------------------------------------------------------------------===//
92
93bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
94 Decl *D = II.getFETokenInfo<Decl>();
95 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
96}
97
98void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
99 ExprTy *Init) {
100 IdentifierInfo *II = D.getIdentifier();
101 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
102
103 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
104
105 // If this has an identifier, add it to the scope stack.
106 if (II) {
107 // If PrevDecl includes conflicting name here, emit a diagnostic.
108 II->setFETokenInfo(New);
109 S->AddDecl(II);
110 }
111}
112
113void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
114 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
115 I != E; ++I) {
116 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
117 Decl *D = II.getFETokenInfo<Decl>();
118 assert(D && "This decl didn't get pushed??");
119
120 Decl *Next = D->getNext();
121
122 // FIXME: Push the decl on the parent function list if in a function.
123 delete D;
124
125 II.setFETokenInfo(Next);
126 }
127}
128
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000129//===--------------------------------------------------------------------===//
130// Expression Parsing Callbacks.
131//===--------------------------------------------------------------------===//
132
Chris Lattner98286a42006-08-24 05:02:11 +0000133Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000134 switch (Tok.getKind()) {
135 default:
136 assert(0 && "Unknown simple primary expr!");
137 case tok::identifier: {
138 // Could be enum-constant or decl.
139 //Tok.getIdentifierInfo()
140 return new DeclExpr(*(Decl*)0);
141 }
142
143 case tok::char_constant: // constant: character-constant
144 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
145 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
146 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
147 assert(0 && "Unimp so far!");
148 return 0;
149 }
150}
151
Chris Lattner98286a42006-08-24 05:02:11 +0000152Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000153 return new IntegerConstant();
154}
Chris Lattner98286a42006-08-24 05:02:11 +0000155Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000156 return new FloatingConstant();
157}
158
Chris Lattner98286a42006-08-24 05:02:11 +0000159Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
160 SourceLocation R,
161 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000162 // FIXME: This is obviously just for testing.
163 ((Expr*)Val)->dump();
164 if (!FullLocInfo) return Val;
165
166 return new ParenExpr(L, R, (Expr*)Val);
167}
168
169// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000170Action::ExprResult ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
171 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000172 UnaryOperator::Opcode Opc;
173 switch (Tok.getKind()) {
174 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000175 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
176 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
177 case tok::amp: Opc = UnaryOperator::AddrOf; break;
178 case tok::star: Opc = UnaryOperator::Deref; break;
179 case tok::plus: Opc = UnaryOperator::Plus; break;
180 case tok::minus: Opc = UnaryOperator::Minus; break;
181 case tok::tilde: Opc = UnaryOperator::Not; break;
182 case tok::exclaim: Opc = UnaryOperator::LNot; break;
183 case tok::kw___real: Opc = UnaryOperator::Real; break;
184 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
185 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
186 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattner1b926492006-08-23 06:42:10 +0000187 }
188
189 if (!FullLocInfo)
190 return new UnaryOperator((Expr*)Input, Opc);
191 else
192 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
193}
194
Chris Lattner26da7302006-08-24 06:49:19 +0000195Action::ExprResult ASTBuilder::
196ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
197 SourceLocation LParenLoc, TypeTy *Ty,
198 SourceLocation RParenLoc) {
199 if (!FullLocInfo)
200 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
201 else
202 return new SizeOfAlignOfTypeExprLOC(OpLoc, isSizeof, LParenLoc, (Type*)Ty,
203 RParenLoc);
204}
205
206
Chris Lattner98286a42006-08-24 05:02:11 +0000207Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
208 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000209 UnaryOperator::Opcode Opc;
210 switch (Tok.getKind()) {
211 default: assert(0 && "Unknown unary op!");
212 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
213 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
214 }
215
216 if (!FullLocInfo)
217 return new UnaryOperator((Expr*)Input, Opc);
218 else
219 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
220}
221
Chris Lattner98286a42006-08-24 05:02:11 +0000222Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000223ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
224 ExprTy *Idx, SourceLocation RLoc) {
225 if (!FullLocInfo)
226 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
227 else
228 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
229}
230
Chris Lattner98286a42006-08-24 05:02:11 +0000231Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000232ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
233 tok::TokenKind OpKind, SourceLocation MemberLoc,
234 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000235 Decl *MemberDecl = 0;
236 // TODO: Look up MemberDecl.
Chris Lattnere165d942006-08-24 04:40:38 +0000237 if (!FullLocInfo)
Chris Lattner6f3a1172006-08-24 05:19:28 +0000238 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000239 else
240 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
Chris Lattner6f3a1172006-08-24 05:19:28 +0000241 MemberLoc, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000242}
243
244/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
245/// This provides the location of the left/right parens and a list of comma
246/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000247Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000248ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
249 ExprTy **Args, unsigned NumArgs,
250 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
251 if (!FullLocInfo)
252 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
253 else
254 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
255 CommaLocs, RParenLoc);
256}
257
Chris Lattnere550a4e2006-08-24 06:37:51 +0000258Action::ExprResult ASTBuilder::
259ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
260 SourceLocation RParenLoc, ExprTy *Op) {
261 if (!FullLocInfo)
262 return new CastExpr((Type*)Ty, (Expr*)Op);
263 else
264 return new CastExprLOC(LParenLoc, (Type*)Ty, RParenLoc, (Expr*)Op);
265}
266
267
Chris Lattnere165d942006-08-24 04:40:38 +0000268
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000269// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000270Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
271 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000272 BinaryOperator::Opcode Opc;
273 switch (Tok.getKind()) {
274 default: assert(0 && "Unknown binop!");
275 case tok::star: Opc = BinaryOperator::Mul; break;
276 case tok::slash: Opc = BinaryOperator::Div; break;
277 case tok::percent: Opc = BinaryOperator::Rem; break;
278 case tok::plus: Opc = BinaryOperator::Add; break;
279 case tok::minus: Opc = BinaryOperator::Sub; break;
280 case tok::lessless: Opc = BinaryOperator::Shl; break;
281 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
282 case tok::lessequal: Opc = BinaryOperator::LE; break;
283 case tok::less: Opc = BinaryOperator::LT; break;
284 case tok::greaterequal: Opc = BinaryOperator::GE; break;
285 case tok::greater: Opc = BinaryOperator::GT; break;
286 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
287 case tok::equalequal: Opc = BinaryOperator::EQ; break;
288 case tok::amp: Opc = BinaryOperator::And; break;
289 case tok::caret: Opc = BinaryOperator::Xor; break;
290 case tok::pipe: Opc = BinaryOperator::Or; break;
291 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
292 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
293 case tok::equal: Opc = BinaryOperator::Assign; break;
294 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
295 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
296 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
297 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
298 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
299 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
300 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
301 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
302 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
303 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
304 case tok::comma: Opc = BinaryOperator::Comma; break;
305 }
306
307 if (!FullLocInfo)
308 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
309 else
310 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
311}
312
313/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
314/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000315Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
316 SourceLocation ColonLoc,
317 ExprTy *Cond, ExprTy *LHS,
318 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000319 if (!FullLocInfo)
320 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
321 else
322 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
323 ColonLoc, (Expr*)RHS);
324}
325
Chris Lattnerc11438c2006-08-18 05:17:52 +0000326
327/// Interface to the Builder.cpp file.
328///
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000329Action *CreateASTBuilderActions(bool FullLocInfo) {
330 return new ASTBuilder(FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000331}
332
333
334