blob: 35102b934ba094a9537184c2dd4a9a9538a1530d [file] [log] [blame]
Chris Lattner7cee11f2006-11-03 06:42:29 +00001//===--- ASTBuilder.cpp - AST Builder Implementation ----------------------===//
Chris Lattner3e7bd4e2006-08-17 05:51:27 +00002//
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 Lattner7cee11f2006-11-03 06:42:29 +000015#include "clang/AST/ASTBuilder.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000016#include "clang/Parse/Action.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000017#include "clang/AST/Decl.h"
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000018#include "clang/AST/Expr.h"
19#include "clang/Parse/Scope.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000020#include "clang/Lex/IdentifierTable.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000021#include "clang/Lex/Preprocessor.h"
22#include "llvm/Support/Compiler.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000023using namespace llvm;
24using namespace clang;
25
Chris Lattnerc11438c2006-08-18 05:17:52 +000026//===----------------------------------------------------------------------===//
27// Symbol table tracking callbacks.
28//===----------------------------------------------------------------------===//
29
Chris Lattner2abeb122006-10-28 19:51:26 +000030bool ASTBuilder::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattnerc11438c2006-08-18 05:17:52 +000031 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +000032 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +000033}
34
Chris Lattner2dacc3f2006-10-16 00:33:54 +000035Action::DeclTy *
36ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
37 DeclTy *LastInGroup) {
Chris Lattnerc11438c2006-08-18 05:17:52 +000038 IdentifierInfo *II = D.getIdentifier();
39 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
40
Chris Lattnera11999d2006-10-15 22:34:45 +000041 Decl *New;
42 if (D.isFunctionDeclarator())
Chris Lattner2dacc3f2006-10-16 00:33:54 +000043 New = new FunctionDecl(II, D, PrevDecl);
Chris Lattnera11999d2006-10-15 22:34:45 +000044 else
Chris Lattner2dacc3f2006-10-16 00:33:54 +000045 New = new VarDecl(II, D, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +000046
47 // If this has an identifier, add it to the scope stack.
48 if (II) {
49 // If PrevDecl includes conflicting name here, emit a diagnostic.
50 II->setFETokenInfo(New);
51 S->AddDecl(II);
52 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000053
Chris Lattner0535ebb2006-10-25 05:28:22 +000054 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
55 // remember this in the LastInGroupList list.
56 if (LastInGroup && S->getParent() == 0)
57 LastInGroupList.push_back((Decl*)LastInGroup);
Chris Lattner2dacc3f2006-10-16 00:33:54 +000058
59 return New;
60}
61
62Action::DeclTy *
63ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
64 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
Chris Lattner30f910e2006-10-16 05:52:41 +000065
66 FD->setBody((Stmt*)Body);
67
Chris Lattner2dacc3f2006-10-16 00:33:54 +000068 return FD;
Chris Lattnerc11438c2006-08-18 05:17:52 +000069}
70
71void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
72 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
73 I != E; ++I) {
74 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
75 Decl *D = II.getFETokenInfo<Decl>();
76 assert(D && "This decl didn't get pushed??");
77
78 Decl *Next = D->getNext();
79
80 // FIXME: Push the decl on the parent function list if in a function.
81 delete D;
82
83 II.setFETokenInfo(Next);
84 }
85}
86
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000087//===--------------------------------------------------------------------===//
Chris Lattnere5cca062006-10-25 04:29:46 +000088// Statement Parsing Callbacks.
89//===--------------------------------------------------------------------===//
90
91Action::StmtResult
92ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R,
93 StmtTy **Elts, unsigned NumElts) {
Chris Lattner72b7d392006-11-04 06:37:16 +000094 if (NumElts > 1)
Chris Lattnere5cca062006-10-25 04:29:46 +000095 return new CompoundStmt((Stmt**)Elts, NumElts);
96 else if (NumElts == 1)
97 return Elts[0]; // {stmt} -> stmt
98 else
99 return 0; // {} -> ;
100}
101
Chris Lattner5f84a062006-10-25 05:55:20 +0000102Action::StmtResult
103ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
104 StmtTy *ThenVal, SourceLocation ElseLoc,
105 StmtTy *ElseVal) {
106 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
107}
Chris Lattnere5cca062006-10-25 04:29:46 +0000108
109Action::StmtResult
110ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
111 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000112 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000113}
114
115//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000116// Expression Parsing Callbacks.
117//===--------------------------------------------------------------------===//
118
Chris Lattnerae319692006-10-25 03:49:28 +0000119Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
120 tok::TokenKind Kind) {
121 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000122 default:
123 assert(0 && "Unknown simple primary expr!");
124 case tok::identifier: {
125 // Could be enum-constant or decl.
126 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000127 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000128 }
129
130 case tok::char_constant: // constant: character-constant
131 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
132 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
133 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000134 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000135 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000136 }
137}
138
Chris Lattnerae319692006-10-25 03:49:28 +0000139Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000140 return new IntegerConstant();
141}
Chris Lattnerae319692006-10-25 03:49:28 +0000142Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000143 return new FloatingConstant();
144}
145
Chris Lattner98286a42006-08-24 05:02:11 +0000146Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
147 SourceLocation R,
148 ExprTy *Val) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000149 return Val;
Chris Lattner1b926492006-08-23 06:42:10 +0000150}
151
Chris Lattnerd3e98952006-10-06 05:22:26 +0000152/// ParseStringExpr - This accepts a string after semantic analysis. This string
153/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
154/// #6]), so it may come from multiple tokens.
155///
156Action::ExprResult ASTBuilder::
157ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +0000158 SourceLocation *TokLocs, unsigned NumToks) {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000159 assert(NumToks && "Must have at least one string!");
Chris Lattner72b7d392006-11-04 06:37:16 +0000160 return new StringExpr(StrData, StrLen, isWide);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000161}
162
163
Chris Lattner1b926492006-08-23 06:42:10 +0000164// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000165Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
166 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000167 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000168 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000169 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000170 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000171 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
172 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
173 case tok::amp: Opc = UnaryOperator::AddrOf; break;
174 case tok::star: Opc = UnaryOperator::Deref; break;
175 case tok::plus: Opc = UnaryOperator::Plus; break;
176 case tok::minus: Opc = UnaryOperator::Minus; break;
177 case tok::tilde: Opc = UnaryOperator::Not; break;
178 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000179 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
180 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000181 case tok::kw___real: Opc = UnaryOperator::Real; break;
182 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
183 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattnerc52b1182006-10-25 05:45:55 +0000184 case tok::kw___extension__:
Chris Lattner72b7d392006-11-04 06:37:16 +0000185 return Input;
186 //Opc = UnaryOperator::Extension;
187 //break;
Chris Lattner1b926492006-08-23 06:42:10 +0000188 }
189
Chris Lattner72b7d392006-11-04 06:37:16 +0000190 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000191}
192
Chris Lattner26da7302006-08-24 06:49:19 +0000193Action::ExprResult ASTBuilder::
194ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
195 SourceLocation LParenLoc, TypeTy *Ty,
196 SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000197 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
Chris Lattner26da7302006-08-24 06:49:19 +0000198}
199
200
Chris Lattnerae319692006-10-25 03:49:28 +0000201Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
202 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000203 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000204 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000205 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000206 default: assert(0 && "Unknown unary op!");
207 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
208 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
209 }
210
Chris Lattner72b7d392006-11-04 06:37:16 +0000211 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000212}
213
Chris Lattner98286a42006-08-24 05:02:11 +0000214Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000215ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
216 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000217 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
Chris Lattnere165d942006-08-24 04:40:38 +0000218}
219
Chris Lattner98286a42006-08-24 05:02:11 +0000220Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000221ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
222 tok::TokenKind OpKind, SourceLocation MemberLoc,
223 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000224 Decl *MemberDecl = 0;
225 // TODO: Look up MemberDecl.
Chris Lattner72b7d392006-11-04 06:37:16 +0000226 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000227}
228
229/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
230/// This provides the location of the left/right parens and a list of comma
231/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000232Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000233ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
234 ExprTy **Args, unsigned NumArgs,
235 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000236 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
Chris Lattnere165d942006-08-24 04:40:38 +0000237}
238
Chris Lattnere550a4e2006-08-24 06:37:51 +0000239Action::ExprResult ASTBuilder::
240ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
241 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000242 return new CastExpr((Type*)Ty, (Expr*)Op);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000243}
244
245
Chris Lattnere165d942006-08-24 04:40:38 +0000246
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000247// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000248Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
249 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000250 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000251 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000252 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000253 default: assert(0 && "Unknown binop!");
254 case tok::star: Opc = BinaryOperator::Mul; break;
255 case tok::slash: Opc = BinaryOperator::Div; break;
256 case tok::percent: Opc = BinaryOperator::Rem; break;
257 case tok::plus: Opc = BinaryOperator::Add; break;
258 case tok::minus: Opc = BinaryOperator::Sub; break;
259 case tok::lessless: Opc = BinaryOperator::Shl; break;
260 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
261 case tok::lessequal: Opc = BinaryOperator::LE; break;
262 case tok::less: Opc = BinaryOperator::LT; break;
263 case tok::greaterequal: Opc = BinaryOperator::GE; break;
264 case tok::greater: Opc = BinaryOperator::GT; break;
265 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
266 case tok::equalequal: Opc = BinaryOperator::EQ; break;
267 case tok::amp: Opc = BinaryOperator::And; break;
268 case tok::caret: Opc = BinaryOperator::Xor; break;
269 case tok::pipe: Opc = BinaryOperator::Or; break;
270 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
271 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
272 case tok::equal: Opc = BinaryOperator::Assign; break;
273 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
274 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
275 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
276 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
277 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
278 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
279 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
280 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
281 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
282 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
283 case tok::comma: Opc = BinaryOperator::Comma; break;
284 }
285
Chris Lattner72b7d392006-11-04 06:37:16 +0000286 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000287}
288
289/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
290/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000291Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
292 SourceLocation ColonLoc,
293 ExprTy *Cond, ExprTy *LHS,
294 ExprTy *RHS) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000295 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000296}
297