blob: 7711d183281fb5479866bc6e2c4ad6fd8958223a [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
70
Chris Lattner98286a42006-08-24 05:02:11 +000071 virtual ExprResult ParseBinOp(const LexerToken &Tok, ExprTy *LHS,ExprTy *RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000072
73 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
74 /// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +000075 virtual ExprResult ParseConditionalOp(SourceLocation QuestionLoc,
76 SourceLocation ColonLoc,
77 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000078};
79} // end anonymous namespace
80
81
82//===----------------------------------------------------------------------===//
83// Symbol table tracking callbacks.
84//===----------------------------------------------------------------------===//
85
86bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
87 Decl *D = II.getFETokenInfo<Decl>();
88 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
89}
90
91void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
92 ExprTy *Init) {
93 IdentifierInfo *II = D.getIdentifier();
94 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
95
96 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
97
98 // If this has an identifier, add it to the scope stack.
99 if (II) {
100 // If PrevDecl includes conflicting name here, emit a diagnostic.
101 II->setFETokenInfo(New);
102 S->AddDecl(II);
103 }
104}
105
106void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
107 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
108 I != E; ++I) {
109 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
110 Decl *D = II.getFETokenInfo<Decl>();
111 assert(D && "This decl didn't get pushed??");
112
113 Decl *Next = D->getNext();
114
115 // FIXME: Push the decl on the parent function list if in a function.
116 delete D;
117
118 II.setFETokenInfo(Next);
119 }
120}
121
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000122//===--------------------------------------------------------------------===//
123// Expression Parsing Callbacks.
124//===--------------------------------------------------------------------===//
125
Chris Lattner98286a42006-08-24 05:02:11 +0000126Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(const LexerToken &Tok) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000127 switch (Tok.getKind()) {
128 default:
129 assert(0 && "Unknown simple primary expr!");
130 case tok::identifier: {
131 // Could be enum-constant or decl.
132 //Tok.getIdentifierInfo()
133 return new DeclExpr(*(Decl*)0);
134 }
135
136 case tok::char_constant: // constant: character-constant
137 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
138 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
139 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
140 assert(0 && "Unimp so far!");
141 return 0;
142 }
143}
144
Chris Lattner98286a42006-08-24 05:02:11 +0000145Action::ExprResult ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000146 return new IntegerConstant();
147}
Chris Lattner98286a42006-08-24 05:02:11 +0000148Action::ExprResult ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000149 return new FloatingConstant();
150}
151
Chris Lattner98286a42006-08-24 05:02:11 +0000152Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
153 SourceLocation R,
154 ExprTy *Val) {
Chris Lattner1b926492006-08-23 06:42:10 +0000155 // FIXME: This is obviously just for testing.
156 ((Expr*)Val)->dump();
157 if (!FullLocInfo) return Val;
158
159 return new ParenExpr(L, R, (Expr*)Val);
160}
161
162// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000163Action::ExprResult ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
164 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000165 UnaryOperator::Opcode Opc;
166 switch (Tok.getKind()) {
167 default: assert(0 && "Unknown unary op!");
168 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
169 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
170 case tok::amp: Opc = UnaryOperator::AddrOf; break;
171 case tok::star: Opc = UnaryOperator::Deref; break;
172 case tok::plus: Opc = UnaryOperator::Plus; break;
173 case tok::minus: Opc = UnaryOperator::Minus; break;
174 case tok::tilde: Opc = UnaryOperator::Not; break;
175 case tok::exclaim: Opc = UnaryOperator::LNot; break;
176 case tok::kw___real: Opc = UnaryOperator::Real; break;
177 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
178 }
179
180 if (!FullLocInfo)
181 return new UnaryOperator((Expr*)Input, Opc);
182 else
183 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
184}
185
Chris Lattner98286a42006-08-24 05:02:11 +0000186Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
187 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000188 UnaryOperator::Opcode Opc;
189 switch (Tok.getKind()) {
190 default: assert(0 && "Unknown unary op!");
191 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
192 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
193 }
194
195 if (!FullLocInfo)
196 return new UnaryOperator((Expr*)Input, Opc);
197 else
198 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
199}
200
Chris Lattner98286a42006-08-24 05:02:11 +0000201Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000202ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
203 ExprTy *Idx, SourceLocation RLoc) {
204 if (!FullLocInfo)
205 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
206 else
207 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
208}
209
Chris Lattner98286a42006-08-24 05:02:11 +0000210Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000211ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
212 tok::TokenKind OpKind, SourceLocation MemberLoc,
213 IdentifierInfo &Member) {
214 if (!FullLocInfo)
215 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, Member);
216 else
217 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
218 MemberLoc, Member);
219}
220
221/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
222/// This provides the location of the left/right parens and a list of comma
223/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000224Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000225ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
226 ExprTy **Args, unsigned NumArgs,
227 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
228 if (!FullLocInfo)
229 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
230 else
231 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
232 CommaLocs, RParenLoc);
233}
234
235
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000236// Binary Operators. 'Tok' is the token for the operator.
Chris Lattner98286a42006-08-24 05:02:11 +0000237Action::ExprResult ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
238 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000239 BinaryOperator::Opcode Opc;
240 switch (Tok.getKind()) {
241 default: assert(0 && "Unknown binop!");
242 case tok::star: Opc = BinaryOperator::Mul; break;
243 case tok::slash: Opc = BinaryOperator::Div; break;
244 case tok::percent: Opc = BinaryOperator::Rem; break;
245 case tok::plus: Opc = BinaryOperator::Add; break;
246 case tok::minus: Opc = BinaryOperator::Sub; break;
247 case tok::lessless: Opc = BinaryOperator::Shl; break;
248 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
249 case tok::lessequal: Opc = BinaryOperator::LE; break;
250 case tok::less: Opc = BinaryOperator::LT; break;
251 case tok::greaterequal: Opc = BinaryOperator::GE; break;
252 case tok::greater: Opc = BinaryOperator::GT; break;
253 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
254 case tok::equalequal: Opc = BinaryOperator::EQ; break;
255 case tok::amp: Opc = BinaryOperator::And; break;
256 case tok::caret: Opc = BinaryOperator::Xor; break;
257 case tok::pipe: Opc = BinaryOperator::Or; break;
258 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
259 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
260 case tok::equal: Opc = BinaryOperator::Assign; break;
261 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
262 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
263 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
264 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
265 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
266 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
267 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
268 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
269 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
270 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
271 case tok::comma: Opc = BinaryOperator::Comma; break;
272 }
273
274 if (!FullLocInfo)
275 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
276 else
277 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
278}
279
280/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
281/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000282Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
283 SourceLocation ColonLoc,
284 ExprTy *Cond, ExprTy *LHS,
285 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000286 if (!FullLocInfo)
287 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
288 else
289 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
290 ColonLoc, (Expr*)RHS);
291}
292
Chris Lattnerc11438c2006-08-18 05:17:52 +0000293
294/// Interface to the Builder.cpp file.
295///
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000296Action *CreateASTBuilderActions(bool FullLocInfo) {
297 return new ASTBuilder(FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000298}
299
300
301