blob: 56eb4d15fd15a70ab0d083f4b344848da9dce491 [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
Chris Lattner71e23ce2006-11-04 20:18:38 +0000109Action::StmtResult
110ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
111 StmtTy *First, ExprTy *Second, ExprTy *Third,
112 SourceLocation RParenLoc, StmtTy *Body) {
113 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
114}
115
Chris Lattnere5cca062006-10-25 04:29:46 +0000116Action::StmtResult
Chris Lattner85ed8732006-11-04 20:40:44 +0000117ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
118 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
119}
120
121Action::StmtResult
122ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
123 SourceLocation WhileLoc, ExprTy *Cond) {
124 return new DoStmt((Stmt*)Body, (Expr*)Cond);
125}
126
127Action::StmtResult
Chris Lattnere5cca062006-10-25 04:29:46 +0000128ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
129 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000130 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000131}
132
133//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000134// Expression Parsing Callbacks.
135//===--------------------------------------------------------------------===//
136
Chris Lattnerae319692006-10-25 03:49:28 +0000137Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
138 tok::TokenKind Kind) {
139 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000140 default:
141 assert(0 && "Unknown simple primary expr!");
142 case tok::identifier: {
143 // Could be enum-constant or decl.
144 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000145 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000146 }
147
148 case tok::char_constant: // constant: character-constant
149 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
150 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
151 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000152 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000153 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000154 }
155}
156
Chris Lattnerae319692006-10-25 03:49:28 +0000157Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000158 return new IntegerConstant();
159}
Chris Lattnerae319692006-10-25 03:49:28 +0000160Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000161 return new FloatingConstant();
162}
163
Chris Lattner98286a42006-08-24 05:02:11 +0000164Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
165 SourceLocation R,
166 ExprTy *Val) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000167 return Val;
Chris Lattner1b926492006-08-23 06:42:10 +0000168}
169
Chris Lattnerd3e98952006-10-06 05:22:26 +0000170/// ParseStringExpr - This accepts a string after semantic analysis. This string
171/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
172/// #6]), so it may come from multiple tokens.
173///
174Action::ExprResult ASTBuilder::
175ParseStringExpr(const char *StrData, unsigned StrLen, bool isWide,
Chris Lattnerae319692006-10-25 03:49:28 +0000176 SourceLocation *TokLocs, unsigned NumToks) {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000177 assert(NumToks && "Must have at least one string!");
Chris Lattner72b7d392006-11-04 06:37:16 +0000178 return new StringExpr(StrData, StrLen, isWide);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000179}
180
181
Chris Lattner1b926492006-08-23 06:42:10 +0000182// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000183Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
184 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000185 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000186 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000187 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000188 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000189 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
190 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
191 case tok::amp: Opc = UnaryOperator::AddrOf; break;
192 case tok::star: Opc = UnaryOperator::Deref; break;
193 case tok::plus: Opc = UnaryOperator::Plus; break;
194 case tok::minus: Opc = UnaryOperator::Minus; break;
195 case tok::tilde: Opc = UnaryOperator::Not; break;
196 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000197 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
198 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000199 case tok::kw___real: Opc = UnaryOperator::Real; break;
200 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
201 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattnerc52b1182006-10-25 05:45:55 +0000202 case tok::kw___extension__:
Chris Lattner72b7d392006-11-04 06:37:16 +0000203 return Input;
204 //Opc = UnaryOperator::Extension;
205 //break;
Chris Lattner1b926492006-08-23 06:42:10 +0000206 }
207
Chris Lattner72b7d392006-11-04 06:37:16 +0000208 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000209}
210
Chris Lattner26da7302006-08-24 06:49:19 +0000211Action::ExprResult ASTBuilder::
212ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
213 SourceLocation LParenLoc, TypeTy *Ty,
214 SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000215 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
Chris Lattner26da7302006-08-24 06:49:19 +0000216}
217
218
Chris Lattnerae319692006-10-25 03:49:28 +0000219Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
220 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000221 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000222 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000223 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000224 default: assert(0 && "Unknown unary op!");
225 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
226 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
227 }
228
Chris Lattner72b7d392006-11-04 06:37:16 +0000229 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000230}
231
Chris Lattner98286a42006-08-24 05:02:11 +0000232Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000233ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
234 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000235 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
Chris Lattnere165d942006-08-24 04:40:38 +0000236}
237
Chris Lattner98286a42006-08-24 05:02:11 +0000238Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000239ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
240 tok::TokenKind OpKind, SourceLocation MemberLoc,
241 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000242 Decl *MemberDecl = 0;
243 // TODO: Look up MemberDecl.
Chris Lattner72b7d392006-11-04 06:37:16 +0000244 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000245}
246
247/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
248/// This provides the location of the left/right parens and a list of comma
249/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000250Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000251ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
252 ExprTy **Args, unsigned NumArgs,
253 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000254 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
Chris Lattnere165d942006-08-24 04:40:38 +0000255}
256
Chris Lattnere550a4e2006-08-24 06:37:51 +0000257Action::ExprResult ASTBuilder::
258ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
259 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000260 return new CastExpr((Type*)Ty, (Expr*)Op);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000261}
262
263
Chris Lattnere165d942006-08-24 04:40:38 +0000264
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000265// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000266Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
267 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000268 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000269 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000270 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000271 default: assert(0 && "Unknown binop!");
272 case tok::star: Opc = BinaryOperator::Mul; break;
273 case tok::slash: Opc = BinaryOperator::Div; break;
274 case tok::percent: Opc = BinaryOperator::Rem; break;
275 case tok::plus: Opc = BinaryOperator::Add; break;
276 case tok::minus: Opc = BinaryOperator::Sub; break;
277 case tok::lessless: Opc = BinaryOperator::Shl; break;
278 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
279 case tok::lessequal: Opc = BinaryOperator::LE; break;
280 case tok::less: Opc = BinaryOperator::LT; break;
281 case tok::greaterequal: Opc = BinaryOperator::GE; break;
282 case tok::greater: Opc = BinaryOperator::GT; break;
283 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
284 case tok::equalequal: Opc = BinaryOperator::EQ; break;
285 case tok::amp: Opc = BinaryOperator::And; break;
286 case tok::caret: Opc = BinaryOperator::Xor; break;
287 case tok::pipe: Opc = BinaryOperator::Or; break;
288 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
289 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
290 case tok::equal: Opc = BinaryOperator::Assign; break;
291 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
292 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
293 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
294 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
295 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
296 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
297 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
298 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
299 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
300 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
301 case tok::comma: Opc = BinaryOperator::Comma; break;
302 }
303
Chris Lattner72b7d392006-11-04 06:37:16 +0000304 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000305}
306
307/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
308/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000309Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
310 SourceLocation ColonLoc,
311 ExprTy *Cond, ExprTy *LHS,
312 ExprTy *RHS) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000313 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000314}
315