blob: f411b448d5f84be073b311314edcbaa6e016a283 [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 Lattner9b6d4cb2006-08-23 05:17:46 +000054 virtual ExprTy *ParseBinOp(const LexerToken &Tok, ExprTy *LHS, ExprTy *RHS);
55
56 /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
57 /// in the case of a the GNU conditional expr extension.
58 virtual ExprTy *ParseConditionalOp(SourceLocation QuestionLoc,
59 SourceLocation ColonLoc,
60 ExprTy *Cond, ExprTy *LHS, ExprTy *RHS);
Chris Lattnerc11438c2006-08-18 05:17:52 +000061};
62} // end anonymous namespace
63
64
65//===----------------------------------------------------------------------===//
66// Symbol table tracking callbacks.
67//===----------------------------------------------------------------------===//
68
69bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const {
70 Decl *D = II.getFETokenInfo<Decl>();
71 return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef;
72}
73
74void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D,
75 ExprTy *Init) {
76 IdentifierInfo *II = D.getIdentifier();
77 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
78
79 Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl);
80
81 // If this has an identifier, add it to the scope stack.
82 if (II) {
83 // If PrevDecl includes conflicting name here, emit a diagnostic.
84 II->setFETokenInfo(New);
85 S->AddDecl(II);
86 }
87}
88
89void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
90 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
91 I != E; ++I) {
92 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
93 Decl *D = II.getFETokenInfo<Decl>();
94 assert(D && "This decl didn't get pushed??");
95
96 Decl *Next = D->getNext();
97
98 // FIXME: Push the decl on the parent function list if in a function.
99 delete D;
100
101 II.setFETokenInfo(Next);
102 }
103}
104
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000105//===--------------------------------------------------------------------===//
106// Expression Parsing Callbacks.
107//===--------------------------------------------------------------------===//
108
109ASTBuilder::ExprTy *ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) {
110 return new IntegerConstant();
111}
112ASTBuilder::ExprTy *ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) {
113 return new FloatingConstant();
114}
115
Chris Lattner1b926492006-08-23 06:42:10 +0000116ASTBuilder::ExprTy *ASTBuilder::ParseParenExpr(SourceLocation L,
117 SourceLocation R,
118 ExprTy *Val) {
119 // FIXME: This is obviously just for testing.
120 ((Expr*)Val)->dump();
121 if (!FullLocInfo) return Val;
122
123 return new ParenExpr(L, R, (Expr*)Val);
124}
125
126// Unary Operators. 'Tok' is the token for the operator.
127ASTBuilder::ExprTy *ASTBuilder::ParseUnaryOp(const LexerToken &Tok,
128 ExprTy *Input) {
129 UnaryOperator::Opcode Opc;
130 switch (Tok.getKind()) {
131 default: assert(0 && "Unknown unary op!");
132 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
133 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
134 case tok::amp: Opc = UnaryOperator::AddrOf; break;
135 case tok::star: Opc = UnaryOperator::Deref; break;
136 case tok::plus: Opc = UnaryOperator::Plus; break;
137 case tok::minus: Opc = UnaryOperator::Minus; break;
138 case tok::tilde: Opc = UnaryOperator::Not; break;
139 case tok::exclaim: Opc = UnaryOperator::LNot; break;
140 case tok::kw___real: Opc = UnaryOperator::Real; break;
141 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
142 }
143
144 if (!FullLocInfo)
145 return new UnaryOperator((Expr*)Input, Opc);
146 else
147 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
148}
149
150ASTBuilder::ExprTy *ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok,
151 ExprTy *Input) {
152 UnaryOperator::Opcode Opc;
153 switch (Tok.getKind()) {
154 default: assert(0 && "Unknown unary op!");
155 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
156 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
157 }
158
159 if (!FullLocInfo)
160 return new UnaryOperator((Expr*)Input, Opc);
161 else
162 return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc);
163}
164
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000165// Binary Operators. 'Tok' is the token for the operator.
166ASTBuilder::ExprTy *ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS,
167 ExprTy *RHS) {
168 BinaryOperator::Opcode Opc;
169 switch (Tok.getKind()) {
170 default: assert(0 && "Unknown binop!");
171 case tok::star: Opc = BinaryOperator::Mul; break;
172 case tok::slash: Opc = BinaryOperator::Div; break;
173 case tok::percent: Opc = BinaryOperator::Rem; break;
174 case tok::plus: Opc = BinaryOperator::Add; break;
175 case tok::minus: Opc = BinaryOperator::Sub; break;
176 case tok::lessless: Opc = BinaryOperator::Shl; break;
177 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
178 case tok::lessequal: Opc = BinaryOperator::LE; break;
179 case tok::less: Opc = BinaryOperator::LT; break;
180 case tok::greaterequal: Opc = BinaryOperator::GE; break;
181 case tok::greater: Opc = BinaryOperator::GT; break;
182 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
183 case tok::equalequal: Opc = BinaryOperator::EQ; break;
184 case tok::amp: Opc = BinaryOperator::And; break;
185 case tok::caret: Opc = BinaryOperator::Xor; break;
186 case tok::pipe: Opc = BinaryOperator::Or; break;
187 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
188 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
189 case tok::equal: Opc = BinaryOperator::Assign; break;
190 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
191 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
192 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
193 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
194 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
195 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
196 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
197 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
198 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
199 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
200 case tok::comma: Opc = BinaryOperator::Comma; break;
201 }
202
203 if (!FullLocInfo)
204 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
205 else
206 return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc);
207}
208
209/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
210/// in the case of a the GNU conditional expr extension.
211ASTBuilder::ExprTy *ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
212 SourceLocation ColonLoc,
213 ExprTy *Cond, ExprTy *LHS,
214 ExprTy *RHS) {
215 if (!FullLocInfo)
216 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
217 else
218 return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS,
219 ColonLoc, (Expr*)RHS);
220}
221
Chris Lattnerc11438c2006-08-18 05:17:52 +0000222
223/// Interface to the Builder.cpp file.
224///
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000225Action *CreateASTBuilderActions(bool FullLocInfo) {
226 return new ASTBuilder(FullLocInfo);
Chris Lattnerc11438c2006-08-18 05:17:52 +0000227}
228
229
230