blob: 5b169d6a52f423bd9ebb3b1d2937c2ea8a764d3b [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 Lattner9b6d4cb2006-08-23 05:17:46 +000045 virtual ExprTy *ParseIntegerConstant(const LexerToken &Tok);
46 virtual ExprTy *ParseFloatingConstant(const LexerToken &Tok);
Chris Lattner1b926492006-08-23 06:42:10 +000047 virtual ExprTy *ParseParenExpr(SourceLocation L, SourceLocation R,
48 ExprTy *Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000049
Chris Lattner1b926492006-08-23 06:42:10 +000050 // Binary/Unary Operators. 'Tok' is the token for the operator.
51 virtual ExprTy *ParseUnaryOp(const LexerToken &Tok, ExprTy *Input);
52 virtual ExprTy *ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input);
53
Chris Lattnere165d942006-08-24 04:40:38 +000054 virtual ExprTy *ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
55 ExprTy *Idx, SourceLocation RLoc);
56 virtual ExprTy *ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
57 tok::TokenKind OpKind,
58 SourceLocation MemberLoc,
59 IdentifierInfo &Member);
60
61 /// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
62 /// This provides the location of the left/right parens and a list of comma
63 /// locations.
64 virtual ExprTy *ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
65 ExprTy **Args, unsigned NumArgs,
66 SourceLocation *CommaLocs,
67 SourceLocation RParenLoc);
68
69
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000070 virtual ExprTy *ParseBinOp(const LexerToken &Tok, ExprTy *LHS, ExprTy *RHS);
71
72 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
73 /// in the case of a the GNU conditional expr extension.
74 virtual ExprTy *ParseConditionalOp(SourceLocation QuestionLoc,
75 SourceLocation ColonLoc,
76 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000077};
78} // end anonymous namespace
79
80
81//===----------------------------------------------------------------------===//
82// Symbol table tracking callbacks.
83//===----------------------------------------------------------------------===//
84
85bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
86 Decl *D = II.getFETokenInfo<Decl>();
87 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
88}
89
90void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
91 ExprTy *Init) {
92 IdentifierInfo *II = D.getIdentifier();
93 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
94
95 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
96
97 // If this has an identifier, add it to the scope stack.
98 if (II) {
99 // If PrevDecl includes conflicting name here, emit a diagnostic.
100 II->setFETokenInfo(New);
101 S->AddDecl(II);
102 }
103}
104
105void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
106 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
107 I != E; ++I) {
108 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
109 Decl *D = II.getFETokenInfo<Decl>();
110 assert(D && "This decl didn't get pushed??");
111
112 Decl *Next = D->getNext();
113
114 // FIXME: Push the decl on the parent function list if in a function.
115 delete D;
116
117 II.setFETokenInfo(Next);
118 }
119}
120
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000121//===--------------------------------------------------------------------===//
122// Expression Parsing Callbacks.
123//===--------------------------------------------------------------------===//
124
125ASTBuilder::ExprTy *ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
126 return new IntegerConstant();
127}
128ASTBuilder::ExprTy *ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
129 return new FloatingConstant();
130}
131
Chris Lattner1b926492006-08-23 06:42:10 +0000132ASTBuilder::ExprTy *ASTBuilder::ParseParenExpr(SourceLocation L,
133 SourceLocation R,
134 ExprTy *Val) {
135 // FIXME: This is obviously just for testing.
136 ((Expr*)Val)->dump();
137 if (!FullLocInfo) return Val;
138
139 return new ParenExpr(L, R, (Expr*)Val);
140}
141
142// Unary Operators. 'Tok' is the token for the operator.
143ASTBuilder::ExprTy *ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
144 ExprTy *Input) {
145 UnaryOperator::Opcode Opc;
146 switch (Tok.getKind()) {
147 default: assert(0 && "Unknown unary op!");
148 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
149 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
150 case tok::amp: Opc = UnaryOperator::AddrOf; break;
151 case tok::star: Opc = UnaryOperator::Deref; break;
152 case tok::plus: Opc = UnaryOperator::Plus; break;
153 case tok::minus: Opc = UnaryOperator::Minus; break;
154 case tok::tilde: Opc = UnaryOperator::Not; break;
155 case tok::exclaim: Opc = UnaryOperator::LNot; break;
156 case tok::kw___real: Opc = UnaryOperator::Real; break;
157 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
158 }
159
160 if (!FullLocInfo)
161 return new UnaryOperator((Expr*)Input, Opc);
162 else
163 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
164}
165
166ASTBuilder::ExprTy *ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
167 ExprTy *Input) {
168 UnaryOperator::Opcode Opc;
169 switch (Tok.getKind()) {
170 default: assert(0 && "Unknown unary op!");
171 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
172 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
173 }
174
175 if (!FullLocInfo)
176 return new UnaryOperator((Expr*)Input, Opc);
177 else
178 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
179}
180
Chris Lattnere165d942006-08-24 04:40:38 +0000181ASTBuilder::ExprTy *ASTBuilder::
182ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
183 ExprTy *Idx, SourceLocation RLoc) {
184 if (!FullLocInfo)
185 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
186 else
187 return new ArraySubscriptExprLOC((Expr*)Base, LLoc, (Expr*)Idx, RLoc);
188}
189
190ASTBuilder::ExprTy *ASTBuilder::
191ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
192 tok::TokenKind OpKind, SourceLocation MemberLoc,
193 IdentifierInfo &Member) {
194 if (!FullLocInfo)
195 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, Member);
196 else
197 return new MemberExprLOC((Expr*)Base, OpLoc, OpKind == tok::arrow,
198 MemberLoc, Member);
199}
200
201/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
202/// This provides the location of the left/right parens and a list of comma
203/// locations.
204ASTBuilder::ExprTy *ASTBuilder::
205ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
206 ExprTy **Args, unsigned NumArgs,
207 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
208 if (!FullLocInfo)
209 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
210 else
211 return new CallExprLOC((Expr*)Fn, LParenLoc, (Expr**)Args, NumArgs,
212 CommaLocs, RParenLoc);
213}
214
215
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000216// Binary Operators. 'Tok' is the token for the operator.
217ASTBuilder::ExprTy *ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
218 ExprTy *RHS) {
219 BinaryOperator::Opcode Opc;
220 switch (Tok.getKind()) {
221 default: assert(0 && "Unknown binop!");
222 case tok::star: Opc = BinaryOperator::Mul; break;
223 case tok::slash: Opc = BinaryOperator::Div; break;
224 case tok::percent: Opc = BinaryOperator::Rem; break;
225 case tok::plus: Opc = BinaryOperator::Add; break;
226 case tok::minus: Opc = BinaryOperator::Sub; break;
227 case tok::lessless: Opc = BinaryOperator::Shl; break;
228 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
229 case tok::lessequal: Opc = BinaryOperator::LE; break;
230 case tok::less: Opc = BinaryOperator::LT; break;
231 case tok::greaterequal: Opc = BinaryOperator::GE; break;
232 case tok::greater: Opc = BinaryOperator::GT; break;
233 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
234 case tok::equalequal: Opc = BinaryOperator::EQ; break;
235 case tok::amp: Opc = BinaryOperator::And; break;
236 case tok::caret: Opc = BinaryOperator::Xor; break;
237 case tok::pipe: Opc = BinaryOperator::Or; break;
238 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
239 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
240 case tok::equal: Opc = BinaryOperator::Assign; break;
241 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
242 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
243 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
244 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
245 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
246 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
247 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
248 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
249 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
250 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
251 case tok::comma: Opc = BinaryOperator::Comma; break;
252 }
253
254 if (!FullLocInfo)
255 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
256 else
257 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
258}
259
260/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
261/// in the case of a the GNU conditional expr extension.
262ASTBuilder::ExprTy *ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
263 SourceLocation ColonLoc,
264 ExprTy *Cond, ExprTy *LHS,
265 ExprTy *RHS) {
266 if (!FullLocInfo)
267 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
268 else
269 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
270 ColonLoc, (Expr*)RHS);
271}
272
Chris Lattnerc11438c2006-08-18 05:17:52 +0000273
274/// Interface to the Builder.cpp file.
275///
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000276Action *CreateASTBuilderActions(bool FullLocInfo) {
277 return new ASTBuilder(FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000278}
279
280
281