blob: 4e2d8814808e48a7ccf60e15d6961c095832a2ad [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/AST/Decl.h"
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000017#include "clang/AST/Expr.h"
Chris Lattner697e5d62006-11-09 06:32:27 +000018#include "clang/Parse/Action.h"
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000019#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"
Chris Lattner697e5d62006-11-09 06:32:27 +000022#include "clang/Basic/TargetInfo.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/StringExtras.h"
Chris Lattnerc11438c2006-08-18 05:17:52 +000025using namespace llvm;
26using namespace clang;
27
Chris Lattnerc11438c2006-08-18 05:17:52 +000028//===----------------------------------------------------------------------===//
29// Symbol table tracking callbacks.
30//===----------------------------------------------------------------------===//
31
Chris Lattner2abeb122006-10-28 19:51:26 +000032bool ASTBuilder::isTypeName(const IdentifierInfo &II, Scope *S) const {
Chris Lattnerc11438c2006-08-18 05:17:52 +000033 Decl *D = II.getFETokenInfo<Decl>();
Chris Lattnera11999d2006-10-15 22:34:45 +000034 return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef;
Chris Lattnerc11438c2006-08-18 05:17:52 +000035}
36
Chris Lattner2dacc3f2006-10-16 00:33:54 +000037Action::DeclTy *
38ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
39 DeclTy *LastInGroup) {
Chris Lattnerc11438c2006-08-18 05:17:52 +000040 IdentifierInfo *II = D.getIdentifier();
41 Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0;
42
Chris Lattnera11999d2006-10-15 22:34:45 +000043 Decl *New;
44 if (D.isFunctionDeclarator())
Chris Lattner2dacc3f2006-10-16 00:33:54 +000045 New = new FunctionDecl(II, D, PrevDecl);
Chris Lattnera11999d2006-10-15 22:34:45 +000046 else
Chris Lattner2dacc3f2006-10-16 00:33:54 +000047 New = new VarDecl(II, D, PrevDecl);
Chris Lattnerc11438c2006-08-18 05:17:52 +000048
49 // If this has an identifier, add it to the scope stack.
50 if (II) {
51 // If PrevDecl includes conflicting name here, emit a diagnostic.
52 II->setFETokenInfo(New);
53 S->AddDecl(II);
54 }
Chris Lattner2dacc3f2006-10-16 00:33:54 +000055
Chris Lattner0535ebb2006-10-25 05:28:22 +000056 // If this is a top-level decl that is chained to some other (e.g. int A,B,C;)
57 // remember this in the LastInGroupList list.
58 if (LastInGroup && S->getParent() == 0)
59 LastInGroupList.push_back((Decl*)LastInGroup);
Chris Lattner2dacc3f2006-10-16 00:33:54 +000060
61 return New;
62}
63
64Action::DeclTy *
65ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) {
66 FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0);
Chris Lattner30f910e2006-10-16 05:52:41 +000067
68 FD->setBody((Stmt*)Body);
69
Chris Lattner2dacc3f2006-10-16 00:33:54 +000070 return FD;
Chris Lattnerc11438c2006-08-18 05:17:52 +000071}
72
73void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) {
74 for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
75 I != E; ++I) {
76 IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I);
77 Decl *D = II.getFETokenInfo<Decl>();
78 assert(D && "This decl didn't get pushed??");
79
80 Decl *Next = D->getNext();
81
82 // FIXME: Push the decl on the parent function list if in a function.
83 delete D;
84
85 II.setFETokenInfo(Next);
86 }
87}
88
Chris Lattner9b6d4cb2006-08-23 05:17:46 +000089//===--------------------------------------------------------------------===//
Chris Lattnere5cca062006-10-25 04:29:46 +000090// Statement Parsing Callbacks.
91//===--------------------------------------------------------------------===//
92
93Action::StmtResult
94ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R,
95 StmtTy **Elts, unsigned NumElts) {
Chris Lattner72b7d392006-11-04 06:37:16 +000096 if (NumElts > 1)
Chris Lattnere5cca062006-10-25 04:29:46 +000097 return new CompoundStmt((Stmt**)Elts, NumElts);
98 else if (NumElts == 1)
99 return Elts[0]; // {stmt} -> stmt
100 else
101 return 0; // {} -> ;
102}
103
Chris Lattner6c0ff132006-11-05 00:19:50 +0000104Action::StmtResult
105ASTBuilder::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
106 SourceLocation DotDotDotLoc, ExprTy *RHSVal,
107 SourceLocation ColonLoc, StmtTy *SubStmt) {
108 return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
109}
110
111Action::StmtResult
112ASTBuilder::ParseDefaultStmt(SourceLocation DefaultLoc,
113 SourceLocation ColonLoc, StmtTy *SubStmt) {
114 return new DefaultStmt((Stmt*)SubStmt);
115}
116
117Action::StmtResult
118ASTBuilder::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
119 SourceLocation ColonLoc, StmtTy *SubStmt) {
120 return new LabelStmt(II, (Stmt*)SubStmt);
121}
122
Chris Lattner5f84a062006-10-25 05:55:20 +0000123Action::StmtResult
124ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
125 StmtTy *ThenVal, SourceLocation ElseLoc,
126 StmtTy *ElseVal) {
127 return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
128}
Chris Lattnerf2174b62006-11-04 20:59:27 +0000129Action::StmtResult
130ASTBuilder::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond,
131 StmtTy *Body) {
132 return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
133}
Chris Lattnere5cca062006-10-25 04:29:46 +0000134
135Action::StmtResult
Chris Lattner85ed8732006-11-04 20:40:44 +0000136ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
137 return new WhileStmt((Expr*)Cond, (Stmt*)Body);
138}
139
140Action::StmtResult
141ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
142 SourceLocation WhileLoc, ExprTy *Cond) {
143 return new DoStmt((Stmt*)Body, (Expr*)Cond);
144}
145
Chris Lattner16976d32006-11-05 01:46:01 +0000146Action::StmtResult
147ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
148 StmtTy *First, ExprTy *Second, ExprTy *Third,
149 SourceLocation RParenLoc, StmtTy *Body) {
150 return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
151}
152
153
154Action::StmtResult
155ASTBuilder::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
156 IdentifierInfo *LabelII) {
157 return new GotoStmt(LabelII);
158}
159Action::StmtResult
160ASTBuilder::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
161 ExprTy *DestExp) {
162 return new IndirectGotoStmt((Expr*)DestExp);
163}
164
165Action::StmtResult
166ASTBuilder::ParseContinueStmt(SourceLocation ContinueLoc) {
167 return new ContinueStmt();
168}
169
170Action::StmtResult
171ASTBuilder::ParseBreakStmt(SourceLocation GotoLoc) {
172 return new BreakStmt();
173}
174
175
Chris Lattner85ed8732006-11-04 20:40:44 +0000176Action::StmtResult
Chris Lattnere5cca062006-10-25 04:29:46 +0000177ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc,
178 ExprTy *RetValExp) {
Chris Lattner6d9a6852006-10-25 05:11:20 +0000179 return new ReturnStmt((Expr*)RetValExp);
Chris Lattnere5cca062006-10-25 04:29:46 +0000180}
181
182//===--------------------------------------------------------------------===//
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000183// Expression Parsing Callbacks.
184//===--------------------------------------------------------------------===//
185
Chris Lattnerae319692006-10-25 03:49:28 +0000186Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc,
187 tok::TokenKind Kind) {
188 switch (Kind) {
Chris Lattner879b9ad2006-08-24 04:53:44 +0000189 default:
190 assert(0 && "Unknown simple primary expr!");
191 case tok::identifier: {
192 // Could be enum-constant or decl.
193 //Tok.getIdentifierInfo()
Chris Lattnerf42cce72006-10-25 04:09:21 +0000194 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000195 }
196
197 case tok::char_constant: // constant: character-constant
198 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
199 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
200 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner94b4ce32006-10-06 05:51:35 +0000201 //assert(0 && "FIXME: Unimp so far!");
Chris Lattnerf42cce72006-10-25 04:09:21 +0000202 return new DeclRefExpr(*(Decl*)0);
Chris Lattner879b9ad2006-08-24 04:53:44 +0000203 }
204}
205
Chris Lattnerae319692006-10-25 03:49:28 +0000206Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000207 return new IntegerConstant();
208}
Chris Lattnerae319692006-10-25 03:49:28 +0000209Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000210 return new FloatingConstant();
211}
212
Chris Lattner98286a42006-08-24 05:02:11 +0000213Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L,
214 SourceLocation R,
215 ExprTy *Val) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000216 return Val;
Chris Lattner1b926492006-08-23 06:42:10 +0000217}
218
Chris Lattner697e5d62006-11-09 06:32:27 +0000219
220
221
222/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
223/// not valid.
224static int HexDigitValue(char C) {
225 if (C >= '0' && C <= '9') return C-'0';
226 if (C >= 'a' && C <= 'f') return C-'a'+10;
227 if (C >= 'A' && C <= 'F') return C-'A'+10;
228 return -1;
229}
230
231/// ParseStringExpr - The specified tokens were lexed as pasted string
232/// fragments (e.g. "foo" "bar" L"baz").
233
Chris Lattnerd3e98952006-10-06 05:22:26 +0000234/// ParseStringExpr - This accepts a string after semantic analysis. This string
235/// may be the result of string concatenation ([C99 5.1.1.2, translation phase
236/// #6]), so it may come from multiple tokens.
237///
Chris Lattner697e5d62006-11-09 06:32:27 +0000238Action::ExprResult
239ASTBuilder::ParseStringExpr(const LexerToken *StringToks,
240 unsigned NumStringToks) {
241 assert(NumStringToks && "Must have at least one string!");
Chris Lattnerd3e98952006-10-06 05:22:26 +0000242
Chris Lattner697e5d62006-11-09 06:32:27 +0000243 // Scan all of the string portions, remember the max individual token length,
244 // computing a bound on the concatenated string length, and see whether any
245 // piece is a wide-string. If any of the string portions is a wide-string
246 // literal, the result is a wide-string literal [C99 6.4.5p4].
247 unsigned MaxTokenLength = StringToks[0].getLength();
248 unsigned SizeBound = StringToks[0].getLength()-2; // -2 for "".
249 bool AnyWide = StringToks[0].getKind() == tok::wide_string_literal;
250
251 // The common case is that there is only one string fragment.
252 for (unsigned i = 1; i != NumStringToks; ++i) {
253 // The string could be shorter than this if it needs cleaning, but this is a
254 // reasonable bound, which is all we need.
255 SizeBound += StringToks[i].getLength()-2; // -2 for "".
256
257 // Remember maximum string piece length.
258 if (StringToks[i].getLength() > MaxTokenLength)
259 MaxTokenLength = StringToks[i].getLength();
260
261 // Remember if we see any wide strings.
262 AnyWide |= StringToks[i].getKind() == tok::wide_string_literal;
263 }
264
265
266 // Include space for the null terminator.
267 ++SizeBound;
268
269 // TODO: K&R warning: "traditional C rejects string constant concatenation"
270
271 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
272 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
273 unsigned wchar_tByteWidth = ~0U;
274 if (AnyWide)
275 wchar_tByteWidth =
276 PP.getTargetInfo().getWCharWidth(StringToks[0].getLocation());
277
278 // The output buffer size needs to be large enough to hold wide characters.
279 // This is a worst-case assumption which basically corresponds to L"" "long".
280 if (AnyWide)
281 SizeBound *= wchar_tByteWidth;
282
283 // Create a temporary buffer to hold the result string data.
284 SmallString<512> ResultBuf;
285 ResultBuf.resize(SizeBound);
286
287 // Likewise, but for each string piece.
288 SmallString<512> TokenBuf;
289 TokenBuf.resize(MaxTokenLength);
290
291 // Loop over all the strings, getting their spelling, and expanding them to
292 // wide strings as appropriate.
293 char *ResultPtr = &ResultBuf[0]; // Next byte to fill in.
294
295 for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
296 const char *ThisTokBuf = &TokenBuf[0];
297 // Get the spelling of the token, which eliminates trigraphs, etc. We know
298 // that ThisTokBuf points to a buffer that is big enough for the whole token
299 // and 'spelled' tokens can only shrink.
300 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
301 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
302
303 // TODO: Input character set mapping support.
304
305 // Skip L marker for wide strings.
306 if (ThisTokBuf[0] == 'L') ++ThisTokBuf;
307
308 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
309 ++ThisTokBuf;
310
311 while (ThisTokBuf != ThisTokEnd) {
312 // Is this a span of non-escape characters?
313 if (ThisTokBuf[0] != '\\') {
314 const char *InStart = ThisTokBuf;
315 do {
316 ++ThisTokBuf;
317 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
318
319 // Copy the character span over.
320 unsigned Len = ThisTokBuf-InStart;
321 if (!AnyWide) {
322 memcpy(ResultPtr, InStart, Len);
323 ResultPtr += Len;
324 } else {
325 // Note: our internal rep of wide char tokens is always little-endian.
326 for (; Len; --Len, ++InStart) {
327 *ResultPtr++ = InStart[0];
328 // Add zeros at the end.
329 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
330 *ResultPtr++ = 0;
331 }
332 }
333 continue;
334 }
335
336 // Otherwise, this is an escape character. Skip the '\' char.
337 ++ThisTokBuf;
338
339 // We know that this character can't be off the end of the buffer, because
340 // that would have been \", which would not have been the end of string.
341 unsigned ResultChar = *ThisTokBuf++;
342 switch (ResultChar) {
343 // These map to themselves.
344 case '\\': case '\'': case '"': case '?': break;
345
346 // These have fixed mappings.
347 case 'a':
348 // TODO: K&R: the meaning of '\\a' is different in traditional C
349 ResultChar = 7;
350 break;
351 case 'b':
352 ResultChar = 8;
353 break;
354 case 'e':
355 PP.Diag(StringToks[i], diag::ext_nonstandard_escape, "e");
356 ResultChar = 27;
357 break;
358 case 'f':
359 ResultChar = 12;
360 break;
361 case 'n':
362 ResultChar = 10;
363 break;
364 case 'r':
365 ResultChar = 13;
366 break;
367 case 't':
368 ResultChar = 9;
369 break;
370 case 'v':
371 ResultChar = 11;
372 break;
373
374 //case 'u': case 'U': // FIXME: UCNs.
375 case 'x': // Hex escape.
376 if (ThisTokBuf == ThisTokEnd ||
377 (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) {
378 PP.Diag(StringToks[i], diag::err_hex_escape_no_digits);
379 ResultChar = 0;
380 break;
381 }
382 ++ThisTokBuf; // Consumed one hex digit.
383
384 assert(0 && "hex escape: unimp!");
385 break;
386 case '0': case '1': case '2': case '3':
387 case '4': case '5': case '6': case '7':
388 // Octal escapes.
389 assert(0 && "octal escape: unimp!");
390 break;
391
392 // Otherwise, these are not valid escapes.
393 case '(': case '{': case '[': case '%':
394 // GCC accepts these as extensions. We warn about them as such though.
395 if (!PP.getLangOptions().NoExtensions) {
396 PP.Diag(StringToks[i], diag::ext_nonstandard_escape,
397 std::string()+(char)ResultChar);
398 break;
399 }
400 // FALL THROUGH.
401 default:
402 if (isgraph(ThisTokBuf[0])) {
403 PP.Diag(StringToks[i], diag::ext_unknown_escape,
404 std::string()+(char)ResultChar);
405 } else {
406 PP.Diag(StringToks[i], diag::ext_unknown_escape,
407 "x"+utohexstr(ResultChar));
408 }
409 }
410
411 // Note: our internal rep of wide char tokens is always little-endian.
412 *ResultPtr++ = ResultChar & 0xFF;
413
414 if (AnyWide) {
415 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
416 *ResultPtr++ = ResultChar >> i*8;
417 }
418 }
419 }
420
421 // Add zero terminator.
422 *ResultPtr = 0;
423 if (AnyWide) {
424 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
425 *ResultPtr++ = 0;
426 }
427
428 SmallVector<SourceLocation, 4> StringTokLocs;
429 for (unsigned i = 0; i != NumStringToks; ++i)
430 StringTokLocs.push_back(StringToks[i].getLocation());
431
432 // FIXME: use factory.
433
434 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
435 return new StringExpr(&ResultBuf[0], ResultPtr-&ResultBuf[0], AnyWide);
436}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000437
Chris Lattner1b926492006-08-23 06:42:10 +0000438// Unary Operators. 'Tok' is the token for the operator.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000439Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc,
440 tok::TokenKind Op,
Chris Lattner98286a42006-08-24 05:02:11 +0000441 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000442 UnaryOperator::Opcode Opc;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000443 switch (Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000444 default: assert(0 && "Unknown unary op!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000445 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
446 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
447 case tok::amp: Opc = UnaryOperator::AddrOf; break;
448 case tok::star: Opc = UnaryOperator::Deref; break;
449 case tok::plus: Opc = UnaryOperator::Plus; break;
450 case tok::minus: Opc = UnaryOperator::Minus; break;
451 case tok::tilde: Opc = UnaryOperator::Not; break;
452 case tok::exclaim: Opc = UnaryOperator::LNot; break;
Chris Lattner26115ac2006-08-24 06:10:04 +0000453 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
454 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
Chris Lattnera11999d2006-10-15 22:34:45 +0000455 case tok::kw___real: Opc = UnaryOperator::Real; break;
456 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
457 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
Chris Lattnerc52b1182006-10-25 05:45:55 +0000458 case tok::kw___extension__:
Chris Lattner72b7d392006-11-04 06:37:16 +0000459 return Input;
460 //Opc = UnaryOperator::Extension;
461 //break;
Chris Lattner1b926492006-08-23 06:42:10 +0000462 }
463
Chris Lattner72b7d392006-11-04 06:37:16 +0000464 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000465}
466
Chris Lattner26da7302006-08-24 06:49:19 +0000467Action::ExprResult ASTBuilder::
468ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
469 SourceLocation LParenLoc, TypeTy *Ty,
470 SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000471 return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
Chris Lattner26da7302006-08-24 06:49:19 +0000472}
473
474
Chris Lattnerae319692006-10-25 03:49:28 +0000475Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc,
476 tok::TokenKind Kind,
Chris Lattner98286a42006-08-24 05:02:11 +0000477 ExprTy *Input) {
Chris Lattner1b926492006-08-23 06:42:10 +0000478 UnaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000479 switch (Kind) {
Chris Lattner1b926492006-08-23 06:42:10 +0000480 default: assert(0 && "Unknown unary op!");
481 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
482 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
483 }
484
Chris Lattner72b7d392006-11-04 06:37:16 +0000485 return new UnaryOperator((Expr*)Input, Opc);
Chris Lattner1b926492006-08-23 06:42:10 +0000486}
487
Chris Lattner98286a42006-08-24 05:02:11 +0000488Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000489ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
490 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000491 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx);
Chris Lattnere165d942006-08-24 04:40:38 +0000492}
493
Chris Lattner98286a42006-08-24 05:02:11 +0000494Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000495ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
496 tok::TokenKind OpKind, SourceLocation MemberLoc,
497 IdentifierInfo &Member) {
Chris Lattner6f3a1172006-08-24 05:19:28 +0000498 Decl *MemberDecl = 0;
499 // TODO: Look up MemberDecl.
Chris Lattner72b7d392006-11-04 06:37:16 +0000500 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere165d942006-08-24 04:40:38 +0000501}
502
503/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
504/// This provides the location of the left/right parens and a list of comma
505/// locations.
Chris Lattner98286a42006-08-24 05:02:11 +0000506Action::ExprResult ASTBuilder::
Chris Lattnere165d942006-08-24 04:40:38 +0000507ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
508 ExprTy **Args, unsigned NumArgs,
509 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000510 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
Chris Lattnere165d942006-08-24 04:40:38 +0000511}
512
Chris Lattnere550a4e2006-08-24 06:37:51 +0000513Action::ExprResult ASTBuilder::
514ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
515 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000516 return new CastExpr((Type*)Ty, (Expr*)Op);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000517}
518
519
Chris Lattnere165d942006-08-24 04:40:38 +0000520
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000521// Binary Operators. 'Tok' is the token for the operator.
Chris Lattnerae319692006-10-25 03:49:28 +0000522Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc,
523 tok::TokenKind Kind, ExprTy *LHS,
Chris Lattner98286a42006-08-24 05:02:11 +0000524 ExprTy *RHS) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000525 BinaryOperator::Opcode Opc;
Chris Lattnerae319692006-10-25 03:49:28 +0000526 switch (Kind) {
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000527 default: assert(0 && "Unknown binop!");
528 case tok::star: Opc = BinaryOperator::Mul; break;
529 case tok::slash: Opc = BinaryOperator::Div; break;
530 case tok::percent: Opc = BinaryOperator::Rem; break;
531 case tok::plus: Opc = BinaryOperator::Add; break;
532 case tok::minus: Opc = BinaryOperator::Sub; break;
533 case tok::lessless: Opc = BinaryOperator::Shl; break;
534 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
535 case tok::lessequal: Opc = BinaryOperator::LE; break;
536 case tok::less: Opc = BinaryOperator::LT; break;
537 case tok::greaterequal: Opc = BinaryOperator::GE; break;
538 case tok::greater: Opc = BinaryOperator::GT; break;
539 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
540 case tok::equalequal: Opc = BinaryOperator::EQ; break;
541 case tok::amp: Opc = BinaryOperator::And; break;
542 case tok::caret: Opc = BinaryOperator::Xor; break;
543 case tok::pipe: Opc = BinaryOperator::Or; break;
544 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
545 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
546 case tok::equal: Opc = BinaryOperator::Assign; break;
547 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
548 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
549 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
550 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
551 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
552 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
553 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
554 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
555 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
556 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
557 case tok::comma: Opc = BinaryOperator::Comma; break;
558 }
559
Chris Lattner72b7d392006-11-04 06:37:16 +0000560 return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000561}
562
563/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
564/// in the case of a the GNU conditional expr extension.
Chris Lattner98286a42006-08-24 05:02:11 +0000565Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc,
566 SourceLocation ColonLoc,
567 ExprTy *Cond, ExprTy *LHS,
568 ExprTy *RHS) {
Chris Lattner72b7d392006-11-04 06:37:16 +0000569 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000570}
571