blob: d0808a5f97d5f3ea9416d6f80caf70c69f819882 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000026#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor55f6b142009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000059 bool GreaterThanIsOperator,
60 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000062 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000063 // C++ [temp.names]p3:
64 // [...] When parsing a template-argument-list, the first
65 // non-nested > is taken as the ending delimiter rather than a
66 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000067 if (GreaterThanIsOperator)
68 return prec::Relational;
69 return prec::Unknown;
70
Douglas Gregor3965b7b2009-02-25 23:02:36 +000071 case tok::greatergreater:
72 // C++0x [temp.names]p3:
73 //
74 // [...] Similarly, the first non-nested >> is treated as two
75 // consecutive but distinct > tokens, the first of which is
76 // taken as the end of the template-argument-list and completes
77 // the template-id. [...]
78 if (GreaterThanIsOperator || !CPlusPlus0x)
79 return prec::Shift;
80 return prec::Unknown;
81
Reid Spencer5f016e22007-07-11 17:01:13 +000082 default: return prec::Unknown;
83 case tok::comma: return prec::Comma;
84 case tok::equal:
85 case tok::starequal:
86 case tok::slashequal:
87 case tok::percentequal:
88 case tok::plusequal:
89 case tok::minusequal:
90 case tok::lesslessequal:
91 case tok::greatergreaterequal:
92 case tok::ampequal:
93 case tok::caretequal:
94 case tok::pipeequal: return prec::Assignment;
95 case tok::question: return prec::Conditional;
96 case tok::pipepipe: return prec::LogicalOr;
97 case tok::ampamp: return prec::LogicalAnd;
98 case tok::pipe: return prec::InclusiveOr;
99 case tok::caret: return prec::ExclusiveOr;
100 case tok::amp: return prec::And;
101 case tok::exclaimequal:
102 case tok::equalequal: return prec::Equality;
103 case tok::lessequal:
104 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000105 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000106 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 case tok::plus:
108 case tok::minus: return prec::Additive;
109 case tok::percent:
110 case tok::slash:
111 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000112 case tok::periodstar:
113 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
117
118/// ParseExpression - Simple precedence-based parser for binary/ternary
119/// operators.
120///
121/// Note: we diverge from the C99 grammar when parsing the assignment-expression
122/// production. C99 specifies that the LHS of an assignment operator should be
123/// parsed as a unary-expression, but consistency dictates that it be a
124/// conditional-expession. In practice, the important thing here is that the
125/// LHS of an assignment has to be an l-value, which productions between
126/// unary-expression and conditional-expression don't produce. Because we want
127/// consistency, we parse the LHS as a conditional-expression, then check for
128/// l-value-ness in semantic analysis stages.
129///
Sebastian Redl22460502009-02-07 00:15:38 +0000130/// pm-expression: [C++ 5.5]
131/// cast-expression
132/// pm-expression '.*' cast-expression
133/// pm-expression '->*' cast-expression
134///
Reid Spencer5f016e22007-07-11 17:01:13 +0000135/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000136/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000137/// cast-expression
138/// multiplicative-expression '*' cast-expression
139/// multiplicative-expression '/' cast-expression
140/// multiplicative-expression '%' cast-expression
141///
142/// additive-expression: [C99 6.5.6]
143/// multiplicative-expression
144/// additive-expression '+' multiplicative-expression
145/// additive-expression '-' multiplicative-expression
146///
147/// shift-expression: [C99 6.5.7]
148/// additive-expression
149/// shift-expression '<<' additive-expression
150/// shift-expression '>>' additive-expression
151///
152/// relational-expression: [C99 6.5.8]
153/// shift-expression
154/// relational-expression '<' shift-expression
155/// relational-expression '>' shift-expression
156/// relational-expression '<=' shift-expression
157/// relational-expression '>=' shift-expression
158///
159/// equality-expression: [C99 6.5.9]
160/// relational-expression
161/// equality-expression '==' relational-expression
162/// equality-expression '!=' relational-expression
163///
164/// AND-expression: [C99 6.5.10]
165/// equality-expression
166/// AND-expression '&' equality-expression
167///
168/// exclusive-OR-expression: [C99 6.5.11]
169/// AND-expression
170/// exclusive-OR-expression '^' AND-expression
171///
172/// inclusive-OR-expression: [C99 6.5.12]
173/// exclusive-OR-expression
174/// inclusive-OR-expression '|' exclusive-OR-expression
175///
176/// logical-AND-expression: [C99 6.5.13]
177/// inclusive-OR-expression
178/// logical-AND-expression '&&' inclusive-OR-expression
179///
180/// logical-OR-expression: [C99 6.5.14]
181/// logical-AND-expression
182/// logical-OR-expression '||' logical-AND-expression
183///
184/// conditional-expression: [C99 6.5.15]
185/// logical-OR-expression
186/// logical-OR-expression '?' expression ':' conditional-expression
187/// [GNU] logical-OR-expression '?' ':' conditional-expression
188///
189/// assignment-expression: [C99 6.5.16]
190/// conditional-expression
191/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000192/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000193///
194/// assignment-operator: one of
195/// = *= /= %= += -= <<= >>= &= ^= |=
196///
197/// expression: [C99 6.5.17]
198/// assignment-expression
199/// expression ',' assignment-expression
200///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000201Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000202 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000203 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000204
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000205 OwningExprResult LHS(ParseCastExpression(false));
206 if (LHS.isInvalid()) return move(LHS);
207
Sebastian Redld8c4e152008-12-11 22:33:27 +0000208 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209}
210
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000211/// This routine is called when the '@' is seen and consumed.
212/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000213/// routine is necessary to disambiguate @try-statement from,
214/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000215///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000216Parser::OwningExprResult
217Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000218 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000219 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000220
Sebastian Redld8c4e152008-12-11 22:33:27 +0000221 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000222}
223
Eli Friedmanadf077f2009-01-27 08:43:38 +0000224/// This routine is called when a leading '__extension__' is seen and
225/// consumed. This is necessary because the token gets consumed in the
226/// process of disambiguating between an expression and a declaration.
227Parser::OwningExprResult
228Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
229 // FIXME: The handling for throw is almost certainly wrong.
230 if (Tok.is(tok::kw_throw))
231 return ParseThrowExpression();
232
233 OwningExprResult LHS(ParseCastExpression(false));
234 if (LHS.isInvalid()) return move(LHS);
235
236 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000237 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000238 if (LHS.isInvalid()) return move(LHS);
239
240 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
241}
242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
244///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000245Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000246 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000247 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000248
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000249 OwningExprResult LHS(ParseCastExpression(false));
250 if (LHS.isInvalid()) return move(LHS);
251
Sebastian Redld8c4e152008-12-11 22:33:27 +0000252 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
Chris Lattnerb93fb492008-06-02 21:31:07 +0000255/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
256/// where part of an objc message send has already been parsed. In this case
257/// LBracLoc indicates the location of the '[' of the message send, and either
258/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
259/// message.
260///
261/// Since this handles full assignment-expression's, it handles postfix
262/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000263Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000264Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000265 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000266 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000267 ExprArg ReceiverExpr) {
268 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
269 ReceiverName,
270 move(ReceiverExpr)));
271 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000272 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000273 if (R.isInvalid()) return move(R);
274 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000275}
276
277
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000278Parser::OwningExprResult Parser::ParseConstantExpression() {
279 OwningExprResult LHS(ParseCastExpression(false));
280 if (LHS.isInvalid()) return move(LHS);
281
Sebastian Redld8c4e152008-12-11 22:33:27 +0000282 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
286/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000287Parser::OwningExprResult
288Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000289 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
290 GreaterThanIsOperator,
291 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 SourceLocation ColonLoc;
293
294 while (1) {
295 // If this token has a lower precedence than we are allowed to parse (e.g.
296 // because we are called recursively, or because the token is not a binop),
297 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000298 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000299 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000300
301 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000302 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000306 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000308 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // Handle this production specially:
310 // logical-OR-expression '?' expression ':' conditional-expression
311 // In particular, the RHS of the '?' is 'expression', not
312 // 'logical-OR-expression' as we might expect.
313 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000314 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000315 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 } else {
317 // Special case handling of "X ? Y : Z" where Y is empty:
318 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000319 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 Diag(Tok, diag::ext_gnu_conditional_expr);
321 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000322
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000323 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000325 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000326 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // Eat the colon.
330 ColonLoc = ConsumeToken();
331 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000334 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000335 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000336 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
338 // Remember the precedence of this operator and get the precedence of the
339 // operator immediately to the right of the RHS.
340 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000341 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
342 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343
344 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000345 bool isRightAssoc = ThisPrec == prec::Conditional ||
346 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000347
348 // Get the precedence of the operator to the right of the RHS. If it binds
349 // more tightly with RHS than we do, evaluate it completely first.
350 if (ThisPrec < NextTokPrec ||
351 (ThisPrec == NextTokPrec && isRightAssoc)) {
352 // If this is left-associative, only parse things on the RHS that bind
353 // more tightly than the current operator. If it is left-associative, it
354 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
355 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000356 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000357 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000358 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000359 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000361 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
362 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
364 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000365
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000366 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000367 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000368 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000369 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000370 OpToken.getKind(), move(LHS), move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000371 else
Steve Narofff69936d2007-09-16 03:34:24 +0000372 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000373 move(LHS), move(TernaryMiddle),
374 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000375 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 }
377}
378
379/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000380/// true, parse a unary-expression. isAddressOfOperand exists because an
381/// id-expression that is the operand of address-of gets special treatment
382/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000383///
384/// cast-expression: [C99 6.5.4]
385/// unary-expression
386/// '(' type-name ')' cast-expression
387///
388/// unary-expression: [C99 6.5.3]
389/// postfix-expression
390/// '++' unary-expression
391/// '--' unary-expression
392/// unary-operator cast-expression
393/// 'sizeof' unary-expression
394/// 'sizeof' '(' type-name ')'
395/// [GNU] '__alignof' unary-expression
396/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000397/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000398/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000399/// [C++] new-expression
400/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000401///
402/// unary-operator: one of
403/// '&' '*' '+' '-' '~' '!'
404/// [GNU] '__extension__' '__real' '__imag'
405///
406/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000407/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000408/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// constant
410/// string-literal
411/// [C++] boolean-literal [C++ 2.13.5]
412/// '(' expression ')'
413/// '__func__' [C99 6.4.2.2]
414/// [GNU] '__FUNCTION__'
415/// [GNU] '__PRETTY_FUNCTION__'
416/// [GNU] '(' compound-statement ')'
417/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
418/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
419/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
420/// assign-expr ')'
421/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000422/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000423/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000424/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000425/// [OBJC] '@protocol' '(' identifier ')'
426/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000427/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000428/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
429/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000430/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
431/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
432/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
433/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000434/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
435/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000436/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000437/// [G++] unary-type-trait '(' type-id ')'
438/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000439/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000440///
441/// constant: [C99 6.4.4]
442/// integer-constant
443/// floating-constant
444/// enumeration-constant -> identifier
445/// character-constant
446///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000447/// id-expression: [C++ 5.1]
448/// unqualified-id
449/// qualified-id [TODO]
450///
451/// unqualified-id: [C++ 5.1]
452/// identifier
453/// operator-function-id
454/// conversion-function-id [TODO]
455/// '~' class-name [TODO]
456/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000457///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000458/// new-expression: [C++ 5.3.4]
459/// '::'[opt] 'new' new-placement[opt] new-type-id
460/// new-initializer[opt]
461/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
462/// new-initializer[opt]
463///
464/// delete-expression: [C++ 5.3.5]
465/// '::'[opt] 'delete' cast-expression
466/// '::'[opt] 'delete' '[' ']' cast-expression
467///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000468/// [GNU] unary-type-trait:
469/// '__has_nothrow_assign' [TODO]
470/// '__has_nothrow_copy' [TODO]
471/// '__has_nothrow_constructor' [TODO]
472/// '__has_trivial_assign' [TODO]
473/// '__has_trivial_copy' [TODO]
474/// '__has_trivial_constructor' [TODO]
475/// '__has_trivial_destructor' [TODO]
476/// '__has_virtual_destructor' [TODO]
477/// '__is_abstract' [TODO]
478/// '__is_class'
479/// '__is_empty' [TODO]
480/// '__is_enum'
481/// '__is_pod'
482/// '__is_polymorphic'
483/// '__is_union'
484///
485/// [GNU] binary-type-trait:
486/// '__is_base_of' [TODO]
487///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000488Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
489 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000490 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 tok::TokenKind SavedKind = Tok.getKind();
492
493 // This handles all of cast-expression, unary-expression, postfix-expression,
494 // and primary-expression. We handle them together like this for efficiency
495 // and to simplify handling of an expression starting with a '(' token: which
496 // may be one of a parenthesized expression, cast-expression, compound literal
497 // expression, or statement expression.
498 //
499 // If the parsed tokens consist of a primary-expression, the cases below
500 // call ParsePostfixExpressionSuffix to handle the postfix expression
501 // suffixes. Cases that cannot be followed by postfix exprs should
502 // return without invoking ParsePostfixExpressionSuffix.
503 switch (SavedKind) {
504 case tok::l_paren: {
505 // If this expression is limited to being a unary-expression, the parent can
506 // not start a cast expression.
507 ParenParseOption ParenExprType =
508 isUnaryExpression ? CompoundLiteral : CastExpr;
509 TypeTy *CastTy;
510 SourceLocation LParenLoc = Tok.getLocation();
511 SourceLocation RParenLoc;
512 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000513 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514
515 switch (ParenExprType) {
516 case SimpleExpr: break; // Nothing else to do.
517 case CompoundStmt: break; // Nothing else to do.
518 case CompoundLiteral:
519 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
520 // postfix-expression exist, parse them now.
521 break;
522 case CastExpr:
523 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
524 // the cast-expression that follows it next.
525 // TODO: For cast expression with CastTy.
526 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000527 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000528 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000529 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000533 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 // primary-expression
537 case tok::numeric_constant:
538 // constant: integer-constant
539 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000540
Steve Narofff69936d2007-09-16 03:34:24 +0000541 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000545 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000546
547 case tok::kw_true:
548 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000549 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000551 case tok::identifier: { // primary-expression: identifier
552 // unqualified-id: identifier
553 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000554 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000555 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000556 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000557 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
558 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000559 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000560 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000561
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 // Consume the identifier so that we can see if it is followed by a '('.
563 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
564 // need to know whether or not this identifier is a function designator or
565 // not.
566 IdentifierInfo &II = *Tok.getIdentifierInfo();
567 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000568 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000570 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
572 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000573 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 ConsumeToken();
575 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000576 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
578 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
579 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000580 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 ConsumeToken();
582 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000583 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 case tok::string_literal: // primary-expression: string-literal
585 case tok::wide_string_literal:
586 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000587 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000589 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 case tok::kw___builtin_va_arg:
591 case tok::kw___builtin_offsetof:
592 case tok::kw___builtin_choose_expr:
593 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000594 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000595 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000596 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000597 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 case tok::plusplus: // unary-expression: '++' unary-expression
599 case tok::minusminus: { // unary-expression: '--' unary-expression
600 SourceLocation SavedLoc = ConsumeToken();
601 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000602 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000603 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000604 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000606 case tok::amp: { // unary-expression: '&' cast-expression
607 // Special treatment because of member pointers
608 SourceLocation SavedLoc = ConsumeToken();
609 Res = ParseCastExpression(false, true);
610 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000611 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000612 return move(Res);
613 }
614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 case tok::star: // unary-expression: '*' cast-expression
616 case tok::plus: // unary-expression: '+' cast-expression
617 case tok::minus: // unary-expression: '-' cast-expression
618 case tok::tilde: // unary-expression: '~' cast-expression
619 case tok::exclaim: // unary-expression: '!' cast-expression
620 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000621 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 SourceLocation SavedLoc = ConsumeToken();
623 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000624 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000625 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000626 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000627 }
628
Chris Lattner35080842008-02-02 20:20:10 +0000629 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
630 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000631 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000632 SourceLocation SavedLoc = ConsumeToken();
633 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000634 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000635 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000636 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 }
638 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
639 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000640 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
642 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000643 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000644 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 case tok::ampamp: { // unary-expression: '&&' identifier
646 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000647 if (Tok.isNot(tok::identifier))
648 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000651 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 Tok.getIdentifierInfo());
653 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000654 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 }
656 case tok::kw_const_cast:
657 case tok::kw_dynamic_cast:
658 case tok::kw_reinterpret_cast:
659 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000660 Res = ParseCXXCasts();
661 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000662 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000663 case tok::kw_typeid:
664 Res = ParseCXXTypeid();
665 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000666 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000667 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000668 Res = ParseCXXThis();
669 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000670 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671
672 case tok::kw_char:
673 case tok::kw_wchar_t:
674 case tok::kw_bool:
675 case tok::kw_short:
676 case tok::kw_int:
677 case tok::kw_long:
678 case tok::kw_signed:
679 case tok::kw_unsigned:
680 case tok::kw_float:
681 case tok::kw_double:
682 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000683 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000684 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000685 if (!getLang().CPlusPlus) {
686 Diag(Tok, diag::err_expected_expression);
687 return ExprError();
688 }
689
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000690 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
691 //
692 DeclSpec DS;
693 ParseCXXSimpleTypeSpecifier(DS);
694 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000695 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
696 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000697
698 Res = ParseCXXTypeConstructExpression(DS);
699 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000700 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000701 }
702
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000703 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
704 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
705 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000706 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000707 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000708
Chris Lattner74ba4102009-01-04 22:52:14 +0000709 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000710 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
711 // annotates the token, tail recurse.
712 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000713 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
714
Chris Lattner74ba4102009-01-04 22:52:14 +0000715 // ::new -> [C++] new-expression
716 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000717 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000718 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000719 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000720 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000721 return ParseCXXDeleteExpression(true, CCLoc);
722
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000723 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000724 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000725 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000726 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000727
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000728 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000729 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000730
731 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000732 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000733
Sebastian Redl64b45f72009-01-05 20:52:13 +0000734 case tok::kw___is_pod: // [GNU] unary-type-trait
735 case tok::kw___is_class:
736 case tok::kw___is_enum:
737 case tok::kw___is_union:
738 case tok::kw___is_polymorphic:
739 return ParseUnaryTypeTrait();
740
Chris Lattnerc97c2042007-10-03 22:03:06 +0000741 case tok::at: {
742 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000743 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000744 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000745 case tok::caret:
746 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000747 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000748 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000749 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000750 case tok::l_square:
751 // These can be followed by postfix-expr pieces.
752 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000753 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000754 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 default:
756 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000757 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000759
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 // unreachable.
761 abort();
762}
763
764/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
765/// is parsed, this method parses any suffixes that apply.
766///
767/// postfix-expression: [C99 6.5.2]
768/// primary-expression
769/// postfix-expression '[' expression ']'
770/// postfix-expression '(' argument-expression-list[opt] ')'
771/// postfix-expression '.' identifier
772/// postfix-expression '->' identifier
773/// postfix-expression '++'
774/// postfix-expression '--'
775/// '(' type-name ')' '{' initializer-list '}'
776/// '(' type-name ')' '{' initializer-list ',' '}'
777///
778/// argument-expression-list: [C99 6.5.2]
779/// argument-expression
780/// argument-expression-list ',' assignment-expression
781///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000782Parser::OwningExprResult
783Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // Now that the primary-expression piece of the postfix-expression has been
785 // parsed, see if there are any postfix-expression pieces here.
786 SourceLocation Loc;
787 while (1) {
788 switch (Tok.getKind()) {
789 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000790 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
792 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000793 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000796
797 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000798 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
799 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000800 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000801 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000802
803 // Match the ']'.
804 MatchRHSPunctuation(tok::r_square, Loc);
805 break;
806 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000807
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000809 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000810 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000813
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000814 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000815 if (ParseExpressionList(ArgExprs, CommaLocs)) {
816 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000817 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 }
819 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000820
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000822 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
824 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000825 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000826 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000827 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000829
Chris Lattner2ff54262007-07-21 05:18:12 +0000830 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 break;
832 }
833 case tok::arrow: // postfix-expression: p-e '->' identifier
834 case tok::period: { // postfix-expression: p-e '.' identifier
835 tok::TokenKind OpKind = Tok.getKind();
836 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000837
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000838 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000840 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000842
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000843 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000844 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000845 OpKind, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 *Tok.getIdentifierInfo());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000847 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 ConsumeToken();
849 break;
850 }
851 case tok::plusplus: // postfix-expression: postfix-expression '++'
852 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000853 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000854 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000855 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000856 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 ConsumeToken();
858 break;
859 }
860 }
861}
862
863
864/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
865/// unary-expression: [C99 6.5.3]
866/// 'sizeof' unary-expression
867/// 'sizeof' '(' type-name ')'
868/// [GNU] '__alignof' unary-expression
869/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000870/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000871Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000872 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
873 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000875 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 ConsumeToken();
877
878 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000879 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000880 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 Operand = ParseCastExpression(true);
882 } else {
883 // If it starts with a '(', we know that it is either a parenthesized
884 // type-name, or it is a unary-expression that starts with a compound
885 // literal, or starts with a primary-expression that is a parenthesized
886 // expression.
887 ParenParseOption ExprType = CastExpr;
888 TypeTy *CastTy;
889 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
890 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000891
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
893 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000894 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000895 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000896 OpTok.is(tok::kw_sizeof),
897 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000898 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000899
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000900 // If this is a parenthesized expression, it is the start of a
901 // unary-expression, but doesn't include any postfix pieces. Parse these
902 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000903 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000905
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000907 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000908 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
909 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000910 /*isType=*/false,
911 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000912 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
914
915/// ParseBuiltinPrimaryExpression
916///
917/// primary-expression: [C99 6.5.1]
918/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
919/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
920/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
921/// assign-expr ')'
922/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
923///
924/// [GNU] offsetof-member-designator:
925/// [GNU] identifier
926/// [GNU] offsetof-member-designator '.' identifier
927/// [GNU] offsetof-member-designator '[' expression ']'
928///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000929Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000930 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
932
933 tok::TokenKind T = Tok.getKind();
934 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
935
936 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000937 if (Tok.isNot(tok::l_paren))
938 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
939 << BuiltinII);
940
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 SourceLocation LParenLoc = ConsumeParen();
942 // TODO: Build AST.
943
944 switch (T) {
945 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000946 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000947 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000948 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000950 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 }
952
953 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000954 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000955
Douglas Gregor809070a2009-02-18 17:45:20 +0000956 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000957
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000958 if (Tok.isNot(tok::r_paren)) {
959 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000960 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000961 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000962 if (Ty.isInvalid())
963 Res = ExprError();
964 else
965 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty.get(),
966 ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000968 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000969 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000970 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000971 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000972
973 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000974 return ExprError();
975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000977 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000978 Diag(Tok, diag::err_expected_ident);
979 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000980 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000981 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000982
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000983 // Keep track of the various subcomponents we see.
984 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +0000985
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000986 Comps.push_back(Action::OffsetOfComponent());
987 Comps.back().isBrackets = false;
988 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
989 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000990
Sebastian Redla55e52c2008-11-25 22:21:31 +0000991 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000993 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000995 Comps.push_back(Action::OffsetOfComponent());
996 Comps.back().isBrackets = false;
997 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000998
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000999 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001000 Diag(Tok, diag::err_expected_ident);
1001 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001002 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001003 }
1004 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1005 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001006
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001007 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001009 Comps.push_back(Action::OffsetOfComponent());
1010 Comps.back().isBrackets = true;
1011 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001013 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001015 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001017 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001018
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001019 Comps.back().LocEnd =
1020 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001021 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001022 if (Ty.isInvalid())
1023 Res = ExprError();
1024 else
1025 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1026 Ty.get(), &Comps[0],
1027 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001028 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001030 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001031 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 }
1033 }
1034 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001035 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001036 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001037 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001038 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001039 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001040 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001041 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001043 return ExprError();
1044
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001045 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001046 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001047 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001048 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001049 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001051 return ExprError();
1052
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001053 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001054 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001055 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001057 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001058 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001059 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001060 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001061 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001062 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1063 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001064 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001065 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001067 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001070 return ExprError();
1071
Douglas Gregor809070a2009-02-18 17:45:20 +00001072 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001073
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001074 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001075 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001076 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001077 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001078
1079 if (Ty1.isInvalid() || Ty2.isInvalid())
1080 Res = ExprError();
1081 else
1082 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1083 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001084 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001085 }
1086
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 // These can be followed by postfix-expr pieces because they are
1088 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001089 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001090}
1091
1092/// ParseParenExpression - This parses the unit that starts with a '(' token,
1093/// based on what is allowed by ExprType. The actual thing parsed is returned
1094/// in ExprType.
1095///
1096/// primary-expression: [C99 6.5.1]
1097/// '(' expression ')'
1098/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1099/// postfix-expression: [C99 6.5.2]
1100/// '(' type-name ')' '{' initializer-list '}'
1101/// '(' type-name ')' '{' initializer-list ',' '}'
1102/// cast-expression: [C99 6.5.4]
1103/// '(' type-name ')' cast-expression
1104///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001105Parser::OwningExprResult
1106Parser::ParseParenExpression(ParenParseOption &ExprType,
1107 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001108 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001109 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001111 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001113
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001114 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001116 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001118
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001119 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001120 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1121 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001122 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001123
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001124 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001126 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001127
1128 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001129 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 RParenLoc = ConsumeParen();
1131 else
1132 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001133
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001134 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 if (!getLang().C99) // Compound literals don't exist in C90.
1136 Diag(OpenLoc, diag::ext_c99_compound_literal);
1137 Result = ParseInitializer();
1138 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001139 if (!Result.isInvalid() && !Ty.isInvalid())
1140 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001141 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001142 return move(Result);
1143 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001144
Chris Lattner42ece642008-12-12 06:00:12 +00001145 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001146 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // returns the parsed type to the callee.
1148 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001149
1150 if (Ty.isInvalid())
1151 return ExprError();
1152
1153 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001154 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001156
Chris Lattner42ece642008-12-12 06:00:12 +00001157 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1158 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 } else {
1160 Result = ParseExpression();
1161 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001162 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001163 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001167 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001169 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 }
Chris Lattner42ece642008-12-12 06:00:12 +00001171
1172 if (Tok.is(tok::r_paren))
1173 RParenLoc = ConsumeParen();
1174 else
1175 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001176
1177 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001178}
1179
1180/// ParseStringLiteralExpression - This handles the various token types that
1181/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1182/// translation phase #6].
1183///
1184/// primary-expression: [C99 6.5.1]
1185/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001186Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1190 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001191 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 do {
1194 StringToks.push_back(Tok);
1195 ConsumeStringToken();
1196 } while (isTokenStringLiteral());
1197
1198 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001199 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001200}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001201
1202/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1203///
1204/// argument-expression-list:
1205/// assignment-expression
1206/// argument-expression-list , assignment-expression
1207///
1208/// [C++] expression-list:
1209/// [C++] assignment-expression
1210/// [C++] expression-list , assignment-expression
1211///
1212bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1213 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001214 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001215 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001216 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001217
Sebastian Redleffa8d12008-12-10 00:02:53 +00001218 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001219
1220 if (Tok.isNot(tok::comma))
1221 return false;
1222 // Move to the next argument, remember where the comma was.
1223 CommaLocs.push_back(ConsumeToken());
1224 }
1225}
Steve Naroff296e8d52008-08-28 19:20:44 +00001226
Mike Stump98eb8a72009-02-04 22:31:32 +00001227/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1228///
1229/// [clang] block-id:
1230/// [clang] specifier-qualifier-list block-declarator
1231///
1232void Parser::ParseBlockId() {
1233 // Parse the specifier-qualifier-list piece.
1234 DeclSpec DS;
1235 ParseSpecifierQualifierList(DS);
1236
1237 // Parse the block-declarator.
1238 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1239 ParseDeclarator(DeclaratorInfo);
1240 // Inform sema that we are starting a block.
1241 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1242}
1243
Steve Naroff296e8d52008-08-28 19:20:44 +00001244/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001245/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001246///
1247/// block-literal:
1248/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001249/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001250/// [clang] block-args:
1251/// [clang] '(' parameter-list ')'
1252///
Sebastian Redl1d922962008-12-13 15:32:12 +00001253Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001254 assert(Tok.is(tok::caret) && "block literal starts with ^");
1255 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001256
Steve Naroff296e8d52008-08-28 19:20:44 +00001257 // Enter a scope to hold everything within the block. This includes the
1258 // argument decls, decls within the compound expression, etc. This also
1259 // allows determining whether a variable reference inside the block is
1260 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001261 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1262 Scope::BreakScope | Scope::ContinueScope |
1263 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001264
1265 // Inform sema that we are starting a block.
1266 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001267
Steve Naroff296e8d52008-08-28 19:20:44 +00001268 // Parse the return type if present.
1269 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001270 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001271 // FIXME: Since the return type isn't actually parsed, it can't be used to
1272 // fill ParamInfo with an initial valid range, so do it manually.
1273 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001274
Steve Naroff296e8d52008-08-28 19:20:44 +00001275 // If this block has arguments, parse them. There is no ambiguity here with
1276 // the expression case, because the expression case requires a parameter list.
1277 if (Tok.is(tok::l_paren)) {
1278 ParseParenDeclarator(ParamInfo);
1279 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001280 // SetIdentifier sets the source range end, but in this case we're past
1281 // that location.
1282 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001283 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001284 ParamInfo.SetRangeEnd(Tmp);
Steve Naroff296e8d52008-08-28 19:20:44 +00001285 if (ParamInfo.getInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001286 // If there was an error parsing the arguments, they may have
1287 // tried to use ^(x+y) which requires an argument list. Just
1288 // skip the whole block literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001289 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001290 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001291 // Inform sema that we are starting a block.
1292 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1293 } else if (! Tok.is(tok::l_brace)) {
1294 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001295 } else {
1296 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001297 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1298 SourceLocation(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001299 0, 0, 0, CaretLoc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001300 ParamInfo),
1301 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001302 // Inform sema that we are starting a block.
1303 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001304 }
1305
Sebastian Redl1d922962008-12-13 15:32:12 +00001306
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001307 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001308 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001309 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001310 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001311 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001312 } else {
1313 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001314 }
Mike Stump281481d2009-02-02 23:46:21 +00001315 } else {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001316 // Saw something like: ^expr
1317 Diag(Tok, diag::err_expected_expression);
1318 return ExprError();
1319 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001320 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001321}
1322