blob: bfbac3ac26828ff4c3990835ac5d7159852fe8f3 [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 Lattner6b91f002009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000027#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/SmallString.h"
30using namespace clang;
31
32/// PrecedenceLevels - These are precedences for the binary/ternary operators in
33/// the C99 grammar. These have been named to relate with the C99 grammar
34/// productions. Low precedences numbers bind more weakly than high numbers.
35namespace prec {
36 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000037 Unknown = 0, // Not binary operator.
38 Comma = 1, // ,
39 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
40 Conditional = 3, // ?
41 LogicalOr = 4, // ||
42 LogicalAnd = 5, // &&
43 InclusiveOr = 6, // |
44 ExclusiveOr = 7, // ^
45 And = 8, // &
46 Equality = 9, // ==, !=
47 Relational = 10, // >=, <=, >, <
48 Shift = 11, // <<, >>
49 Additive = 12, // -, +
50 Multiplicative = 13, // *, /, %
51 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000052 };
53}
54
55
56/// getBinOpPrecedence - Return the precedence of the specified binary operator
57/// token. This returns:
58///
Douglas Gregor55f6b142009-02-09 18:46:07 +000059static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000060 bool GreaterThanIsOperator,
61 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000063 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000064 // C++ [temp.names]p3:
65 // [...] When parsing a template-argument-list, the first
66 // non-nested > is taken as the ending delimiter rather than a
67 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000068 if (GreaterThanIsOperator)
69 return prec::Relational;
70 return prec::Unknown;
71
Douglas Gregor3965b7b2009-02-25 23:02:36 +000072 case tok::greatergreater:
73 // C++0x [temp.names]p3:
74 //
75 // [...] Similarly, the first non-nested >> is treated as two
76 // consecutive but distinct > tokens, the first of which is
77 // taken as the end of the template-argument-list and completes
78 // the template-id. [...]
79 if (GreaterThanIsOperator || !CPlusPlus0x)
80 return prec::Shift;
81 return prec::Unknown;
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 default: return prec::Unknown;
84 case tok::comma: return prec::Comma;
85 case tok::equal:
86 case tok::starequal:
87 case tok::slashequal:
88 case tok::percentequal:
89 case tok::plusequal:
90 case tok::minusequal:
91 case tok::lesslessequal:
92 case tok::greatergreaterequal:
93 case tok::ampequal:
94 case tok::caretequal:
95 case tok::pipeequal: return prec::Assignment;
96 case tok::question: return prec::Conditional;
97 case tok::pipepipe: return prec::LogicalOr;
98 case tok::ampamp: return prec::LogicalAnd;
99 case tok::pipe: return prec::InclusiveOr;
100 case tok::caret: return prec::ExclusiveOr;
101 case tok::amp: return prec::And;
102 case tok::exclaimequal:
103 case tok::equalequal: return prec::Equality;
104 case tok::lessequal:
105 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000106 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000107 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 case tok::plus:
109 case tok::minus: return prec::Additive;
110 case tok::percent:
111 case tok::slash:
112 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000113 case tok::periodstar:
114 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116}
117
118
119/// ParseExpression - Simple precedence-based parser for binary/ternary
120/// operators.
121///
122/// Note: we diverge from the C99 grammar when parsing the assignment-expression
123/// production. C99 specifies that the LHS of an assignment operator should be
124/// parsed as a unary-expression, but consistency dictates that it be a
125/// conditional-expession. In practice, the important thing here is that the
126/// LHS of an assignment has to be an l-value, which productions between
127/// unary-expression and conditional-expression don't produce. Because we want
128/// consistency, we parse the LHS as a conditional-expression, then check for
129/// l-value-ness in semantic analysis stages.
130///
Sebastian Redl22460502009-02-07 00:15:38 +0000131/// pm-expression: [C++ 5.5]
132/// cast-expression
133/// pm-expression '.*' cast-expression
134/// pm-expression '->*' cast-expression
135///
Reid Spencer5f016e22007-07-11 17:01:13 +0000136/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000137/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// cast-expression
139/// multiplicative-expression '*' cast-expression
140/// multiplicative-expression '/' cast-expression
141/// multiplicative-expression '%' cast-expression
142///
143/// additive-expression: [C99 6.5.6]
144/// multiplicative-expression
145/// additive-expression '+' multiplicative-expression
146/// additive-expression '-' multiplicative-expression
147///
148/// shift-expression: [C99 6.5.7]
149/// additive-expression
150/// shift-expression '<<' additive-expression
151/// shift-expression '>>' additive-expression
152///
153/// relational-expression: [C99 6.5.8]
154/// shift-expression
155/// relational-expression '<' shift-expression
156/// relational-expression '>' shift-expression
157/// relational-expression '<=' shift-expression
158/// relational-expression '>=' shift-expression
159///
160/// equality-expression: [C99 6.5.9]
161/// relational-expression
162/// equality-expression '==' relational-expression
163/// equality-expression '!=' relational-expression
164///
165/// AND-expression: [C99 6.5.10]
166/// equality-expression
167/// AND-expression '&' equality-expression
168///
169/// exclusive-OR-expression: [C99 6.5.11]
170/// AND-expression
171/// exclusive-OR-expression '^' AND-expression
172///
173/// inclusive-OR-expression: [C99 6.5.12]
174/// exclusive-OR-expression
175/// inclusive-OR-expression '|' exclusive-OR-expression
176///
177/// logical-AND-expression: [C99 6.5.13]
178/// inclusive-OR-expression
179/// logical-AND-expression '&&' inclusive-OR-expression
180///
181/// logical-OR-expression: [C99 6.5.14]
182/// logical-AND-expression
183/// logical-OR-expression '||' logical-AND-expression
184///
185/// conditional-expression: [C99 6.5.15]
186/// logical-OR-expression
187/// logical-OR-expression '?' expression ':' conditional-expression
188/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000189/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000190///
191/// assignment-expression: [C99 6.5.16]
192/// conditional-expression
193/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000194/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000195///
196/// assignment-operator: one of
197/// = *= /= %= += -= <<= >>= &= ^= |=
198///
199/// expression: [C99 6.5.17]
200/// assignment-expression
201/// expression ',' assignment-expression
202///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000203Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000204 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000205 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000206
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000207 OwningExprResult LHS(ParseCastExpression(false));
208 if (LHS.isInvalid()) return move(LHS);
209
Sebastian Redld8c4e152008-12-11 22:33:27 +0000210 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000213/// This routine is called when the '@' is seen and consumed.
214/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000215/// routine is necessary to disambiguate @try-statement from,
216/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000217///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000218Parser::OwningExprResult
219Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000220 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000221 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000222
Sebastian Redld8c4e152008-12-11 22:33:27 +0000223 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000224}
225
Eli Friedmanadf077f2009-01-27 08:43:38 +0000226/// This routine is called when a leading '__extension__' is seen and
227/// consumed. This is necessary because the token gets consumed in the
228/// process of disambiguating between an expression and a declaration.
229Parser::OwningExprResult
230Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
231 // FIXME: The handling for throw is almost certainly wrong.
232 if (Tok.is(tok::kw_throw))
233 return ParseThrowExpression();
234
235 OwningExprResult LHS(ParseCastExpression(false));
236 if (LHS.isInvalid()) return move(LHS);
237
238 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000239 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000240 if (LHS.isInvalid()) return move(LHS);
241
242 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
243}
244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
246///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000247Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000248 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000249 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000250
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000251 OwningExprResult LHS(ParseCastExpression(false));
252 if (LHS.isInvalid()) return move(LHS);
253
Sebastian Redld8c4e152008-12-11 22:33:27 +0000254 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Chris Lattnerb93fb492008-06-02 21:31:07 +0000257/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
258/// where part of an objc message send has already been parsed. In this case
259/// LBracLoc indicates the location of the '[' of the message send, and either
260/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
261/// message.
262///
263/// Since this handles full assignment-expression's, it handles postfix
264/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000265Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000266Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000267 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000268 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000269 ExprArg ReceiverExpr) {
270 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
271 ReceiverName,
272 move(ReceiverExpr)));
273 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000274 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000275 if (R.isInvalid()) return move(R);
276 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000277}
278
279
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000280Parser::OwningExprResult Parser::ParseConstantExpression() {
281 OwningExprResult LHS(ParseCastExpression(false));
282 if (LHS.isInvalid()) return move(LHS);
283
Sebastian Redld8c4e152008-12-11 22:33:27 +0000284 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000289Parser::OwningExprResult
290Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
292 GreaterThanIsOperator,
293 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 SourceLocation ColonLoc;
295
296 while (1) {
297 // If this token has a lower precedence than we are allowed to parse (e.g.
298 // because we are called recursively, or because the token is not a binop),
299 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000301 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302
303 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000304 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000308 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000310 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Handle this production specially:
312 // logical-OR-expression '?' expression ':' conditional-expression
313 // In particular, the RHS of the '?' is 'expression', not
314 // 'logical-OR-expression' as we might expect.
315 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000317 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 } else {
319 // Special case handling of "X ? Y : Z" where Y is empty:
320 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000321 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 Diag(Tok, diag::ext_gnu_conditional_expr);
323 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000324
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000325 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000327 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000328 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // Eat the colon.
332 ColonLoc = ConsumeToken();
333 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000336 // ParseCastExpression works here because all RHS expressions in C have it
337 // as a prefix, at least. However, in C++, an assignment-expression could
338 // be a throw-expression, which is not a valid cast-expression.
339 // Therefore we need some special-casing here.
340 // Also note that the third operand of the conditional operator is
341 // an assignment-expression in C++.
342 OwningExprResult RHS(Actions);
343 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
344 RHS = ParseAssignmentExpression();
345 else
346 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000347 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000348 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349
350 // Remember the precedence of this operator and get the precedence of the
351 // operator immediately to the right of the RHS.
352 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000353 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
354 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000355
356 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000357 bool isRightAssoc = ThisPrec == prec::Conditional ||
358 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
360 // Get the precedence of the operator to the right of the RHS. If it binds
361 // more tightly with RHS than we do, evaluate it completely first.
362 if (ThisPrec < NextTokPrec ||
363 (ThisPrec == NextTokPrec && isRightAssoc)) {
364 // If this is left-associative, only parse things on the RHS that bind
365 // more tightly than the current operator. If it is left-associative, it
366 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
367 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000368 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000369 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000370 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000371 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000372
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000373 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
374 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 }
376 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000377
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000378 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000379 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000380 if (TernaryMiddle.isInvalid()) {
381 // If we're using '>>' as an operator within a template
382 // argument list (in C++98), suggest the addition of
383 // parentheses so that the code remains well-formed in C++0x.
384 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
385 SuggestParentheses(OpToken.getLocation(),
386 diag::warn_cxx0x_right_shift_in_template_arg,
387 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
388 Actions.getExprRange(RHS.get()).getEnd()));
389
Sebastian Redleffa8d12008-12-10 00:02:53 +0000390 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000391 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000392 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000393 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000394 move(LHS), move(TernaryMiddle),
395 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000396 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 }
398}
399
400/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000401/// true, parse a unary-expression. isAddressOfOperand exists because an
402/// id-expression that is the operand of address-of gets special treatment
403/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000404///
405/// cast-expression: [C99 6.5.4]
406/// unary-expression
407/// '(' type-name ')' cast-expression
408///
409/// unary-expression: [C99 6.5.3]
410/// postfix-expression
411/// '++' unary-expression
412/// '--' unary-expression
413/// unary-operator cast-expression
414/// 'sizeof' unary-expression
415/// 'sizeof' '(' type-name ')'
416/// [GNU] '__alignof' unary-expression
417/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000418/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000420/// [C++] new-expression
421/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000422///
423/// unary-operator: one of
424/// '&' '*' '+' '-' '~' '!'
425/// [GNU] '__extension__' '__real' '__imag'
426///
427/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000428/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000429/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000430/// constant
431/// string-literal
432/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000433/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// '(' expression ')'
435/// '__func__' [C99 6.4.2.2]
436/// [GNU] '__FUNCTION__'
437/// [GNU] '__PRETTY_FUNCTION__'
438/// [GNU] '(' compound-statement ')'
439/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
440/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
441/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
442/// assign-expr ')'
443/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000444/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000445/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000446/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000447/// [OBJC] '@protocol' '(' identifier ')'
448/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000449/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000450/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
451/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000452/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
453/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
454/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
455/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000456/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
457/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000458/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000459/// [G++] unary-type-trait '(' type-id ')'
460/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000461/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000462///
463/// constant: [C99 6.4.4]
464/// integer-constant
465/// floating-constant
466/// enumeration-constant -> identifier
467/// character-constant
468///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000469/// id-expression: [C++ 5.1]
470/// unqualified-id
471/// qualified-id [TODO]
472///
473/// unqualified-id: [C++ 5.1]
474/// identifier
475/// operator-function-id
476/// conversion-function-id [TODO]
477/// '~' class-name [TODO]
478/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000479///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000480/// new-expression: [C++ 5.3.4]
481/// '::'[opt] 'new' new-placement[opt] new-type-id
482/// new-initializer[opt]
483/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
484/// new-initializer[opt]
485///
486/// delete-expression: [C++ 5.3.5]
487/// '::'[opt] 'delete' cast-expression
488/// '::'[opt] 'delete' '[' ']' cast-expression
489///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000490/// [GNU] unary-type-trait:
491/// '__has_nothrow_assign' [TODO]
492/// '__has_nothrow_copy' [TODO]
493/// '__has_nothrow_constructor' [TODO]
494/// '__has_trivial_assign' [TODO]
495/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000496/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000497/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000498/// '__has_virtual_destructor' [TODO]
499/// '__is_abstract' [TODO]
500/// '__is_class'
501/// '__is_empty' [TODO]
502/// '__is_enum'
503/// '__is_pod'
504/// '__is_polymorphic'
505/// '__is_union'
506///
507/// [GNU] binary-type-trait:
508/// '__is_base_of' [TODO]
509///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000510Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
511 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000512 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 tok::TokenKind SavedKind = Tok.getKind();
514
515 // This handles all of cast-expression, unary-expression, postfix-expression,
516 // and primary-expression. We handle them together like this for efficiency
517 // and to simplify handling of an expression starting with a '(' token: which
518 // may be one of a parenthesized expression, cast-expression, compound literal
519 // expression, or statement expression.
520 //
521 // If the parsed tokens consist of a primary-expression, the cases below
522 // call ParsePostfixExpressionSuffix to handle the postfix expression
523 // suffixes. Cases that cannot be followed by postfix exprs should
524 // return without invoking ParsePostfixExpressionSuffix.
525 switch (SavedKind) {
526 case tok::l_paren: {
527 // If this expression is limited to being a unary-expression, the parent can
528 // not start a cast expression.
529 ParenParseOption ParenExprType =
530 isUnaryExpression ? CompoundLiteral : CastExpr;
531 TypeTy *CastTy;
532 SourceLocation LParenLoc = Tok.getLocation();
533 SourceLocation RParenLoc;
534 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000535 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000536
537 switch (ParenExprType) {
538 case SimpleExpr: break; // Nothing else to do.
539 case CompoundStmt: break; // Nothing else to do.
540 case CompoundLiteral:
541 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
542 // postfix-expression exist, parse them now.
543 break;
544 case CastExpr:
545 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
546 // the cast-expression that follows it next.
547 // TODO: For cast expression with CastTy.
548 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000549 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000550 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000551 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000555 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000557
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 // primary-expression
559 case tok::numeric_constant:
560 // constant: integer-constant
561 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000562
Steve Narofff69936d2007-09-16 03:34:24 +0000563 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000567 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000568
569 case tok::kw_true:
570 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000571 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000572
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000573 case tok::kw_nullptr:
574 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
575
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000576 case tok::identifier: { // primary-expression: identifier
577 // unqualified-id: identifier
578 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000579 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000580 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000581 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000582 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
583 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000584 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000585 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000586
Steve Naroff61f72cb2009-03-09 21:12:44 +0000587 // Support 'Class.property' notation.
588 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
589 // 'super' (which is inappropriate here).
590 if (getLang().ObjC1 &&
591 Actions.getTypeName(*Tok.getIdentifierInfo(),
592 Tok.getLocation(), CurScope) &&
593 NextToken().is(tok::period)) {
594 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
595 SourceLocation IdentLoc = ConsumeToken();
596 SourceLocation DotLoc = ConsumeToken();
597
598 if (Tok.isNot(tok::identifier)) {
599 Diag(Tok, diag::err_expected_ident);
600 return ExprError();
601 }
602 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
603 SourceLocation PropertyLoc = ConsumeToken();
604
605 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
606 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000607 // These can be followed by postfix-expr pieces.
608 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000609 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // Consume the identifier so that we can see if it is followed by a '('.
611 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
612 // need to know whether or not this identifier is a function designator or
613 // not.
614 IdentifierInfo &II = *Tok.getIdentifierInfo();
615 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000616 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000618 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 }
620 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000621 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 ConsumeToken();
623 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000624 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
626 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
627 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000628 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 ConsumeToken();
630 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000631 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 case tok::string_literal: // primary-expression: string-literal
633 case tok::wide_string_literal:
634 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000635 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000637 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 case tok::kw___builtin_va_arg:
639 case tok::kw___builtin_offsetof:
640 case tok::kw___builtin_choose_expr:
641 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000642 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000643 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000644 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000645 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 case tok::plusplus: // unary-expression: '++' unary-expression
647 case tok::minusminus: { // unary-expression: '--' unary-expression
648 SourceLocation SavedLoc = ConsumeToken();
649 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000650 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000651 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000652 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000654 case tok::amp: { // unary-expression: '&' cast-expression
655 // Special treatment because of member pointers
656 SourceLocation SavedLoc = ConsumeToken();
657 Res = ParseCastExpression(false, true);
658 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000659 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000660 return move(Res);
661 }
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case tok::star: // unary-expression: '*' cast-expression
664 case tok::plus: // unary-expression: '+' cast-expression
665 case tok::minus: // unary-expression: '-' cast-expression
666 case tok::tilde: // unary-expression: '~' cast-expression
667 case tok::exclaim: // unary-expression: '!' cast-expression
668 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000669 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 SourceLocation SavedLoc = ConsumeToken();
671 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000672 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000673 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000674 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000675 }
676
Chris Lattner35080842008-02-02 20:20:10 +0000677 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
678 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000679 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000680 SourceLocation SavedLoc = ConsumeToken();
681 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000682 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000683 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000684 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 }
686 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
687 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000688 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
690 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000691 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000692 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 case tok::ampamp: { // unary-expression: '&&' identifier
694 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000695 if (Tok.isNot(tok::identifier))
696 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000697
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000699 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 Tok.getIdentifierInfo());
701 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000702 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 }
704 case tok::kw_const_cast:
705 case tok::kw_dynamic_cast:
706 case tok::kw_reinterpret_cast:
707 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000708 Res = ParseCXXCasts();
709 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000710 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000711 case tok::kw_typeid:
712 Res = ParseCXXTypeid();
713 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000714 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000715 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000716 Res = ParseCXXThis();
717 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000718 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000719
720 case tok::kw_char:
721 case tok::kw_wchar_t:
722 case tok::kw_bool:
723 case tok::kw_short:
724 case tok::kw_int:
725 case tok::kw_long:
726 case tok::kw_signed:
727 case tok::kw_unsigned:
728 case tok::kw_float:
729 case tok::kw_double:
730 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000731 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000732 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000733 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000734 if (!getLang().CPlusPlus) {
735 Diag(Tok, diag::err_expected_expression);
736 return ExprError();
737 }
738
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000739 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
740 //
741 DeclSpec DS;
742 ParseCXXSimpleTypeSpecifier(DS);
743 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000744 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
745 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000746
747 Res = ParseCXXTypeConstructExpression(DS);
748 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000749 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000750 }
751
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000752 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
753 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
754 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000755 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000756 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000757
Chris Lattner74ba4102009-01-04 22:52:14 +0000758 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000759 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
760 // annotates the token, tail recurse.
761 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000762 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
763
Chris Lattner74ba4102009-01-04 22:52:14 +0000764 // ::new -> [C++] new-expression
765 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000766 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000767 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000768 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000769 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000770 return ParseCXXDeleteExpression(true, CCLoc);
771
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000772 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000773 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000774 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000775 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000776
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000777 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000778 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000779
780 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000781 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000782
Sebastian Redl64b45f72009-01-05 20:52:13 +0000783 case tok::kw___is_pod: // [GNU] unary-type-trait
784 case tok::kw___is_class:
785 case tok::kw___is_enum:
786 case tok::kw___is_union:
787 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000788 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000789 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000790 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000791 return ParseUnaryTypeTrait();
792
Chris Lattnerc97c2042007-10-03 22:03:06 +0000793 case tok::at: {
794 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000795 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000796 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000797 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000798 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000799 case tok::l_square:
800 // These can be followed by postfix-expr pieces.
801 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000802 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000803 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 default:
805 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000806 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 // unreachable.
810 abort();
811}
812
813/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
814/// is parsed, this method parses any suffixes that apply.
815///
816/// postfix-expression: [C99 6.5.2]
817/// primary-expression
818/// postfix-expression '[' expression ']'
819/// postfix-expression '(' argument-expression-list[opt] ')'
820/// postfix-expression '.' identifier
821/// postfix-expression '->' identifier
822/// postfix-expression '++'
823/// postfix-expression '--'
824/// '(' type-name ')' '{' initializer-list '}'
825/// '(' type-name ')' '{' initializer-list ',' '}'
826///
827/// argument-expression-list: [C99 6.5.2]
828/// argument-expression
829/// argument-expression-list ',' assignment-expression
830///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000831Parser::OwningExprResult
832Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 // Now that the primary-expression piece of the postfix-expression has been
834 // parsed, see if there are any postfix-expression pieces here.
835 SourceLocation Loc;
836 while (1) {
837 switch (Tok.getKind()) {
838 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000839 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
841 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000842 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000843
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000845
846 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000847 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
848 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000849 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000850 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000851
852 // Match the ']'.
853 MatchRHSPunctuation(tok::r_square, Loc);
854 break;
855 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000856
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000858 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000859 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000862
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000863 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000864 if (ParseExpressionList(ArgExprs, CommaLocs)) {
865 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000866 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 }
868 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000871 if (Tok.isNot(tok::r_paren)) {
872 MatchRHSPunctuation(tok::r_paren, Loc);
873 return ExprError();
874 }
875
876 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
878 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000879 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000880 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000881 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000883
884 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 break;
886 }
887 case tok::arrow: // postfix-expression: p-e '->' identifier
888 case tok::period: { // postfix-expression: p-e '.' identifier
889 tok::TokenKind OpKind = Tok.getKind();
890 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000891
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000892 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000894 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000896
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000897 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000898 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000899 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000900 *Tok.getIdentifierInfo(),
901 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000902 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 ConsumeToken();
904 break;
905 }
906 case tok::plusplus: // postfix-expression: postfix-expression '++'
907 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000908 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000909 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000910 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000911 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 ConsumeToken();
913 break;
914 }
915 }
916}
917
918
919/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
920/// unary-expression: [C99 6.5.3]
921/// 'sizeof' unary-expression
922/// 'sizeof' '(' type-name ')'
923/// [GNU] '__alignof' unary-expression
924/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000925/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000926Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000927 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
928 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000930 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 ConsumeToken();
932
933 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000934 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000935 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 Operand = ParseCastExpression(true);
937 } else {
938 // If it starts with a '(', we know that it is either a parenthesized
939 // type-name, or it is a unary-expression that starts with a compound
940 // literal, or starts with a primary-expression that is a parenthesized
941 // expression.
942 ParenParseOption ExprType = CastExpr;
943 TypeTy *CastTy;
944 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
945 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000946
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
948 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000949 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000950 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000951 OpTok.is(tok::kw_sizeof),
952 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000953 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000954
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000955 // If this is a parenthesized expression, it is the start of a
956 // unary-expression, but doesn't include any postfix pieces. Parse these
957 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000958 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000960
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000962 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000963 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
964 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000965 /*isType=*/false,
966 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000967 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000968}
969
970/// ParseBuiltinPrimaryExpression
971///
972/// primary-expression: [C99 6.5.1]
973/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
974/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
975/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
976/// assign-expr ')'
977/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
978///
979/// [GNU] offsetof-member-designator:
980/// [GNU] identifier
981/// [GNU] offsetof-member-designator '.' identifier
982/// [GNU] offsetof-member-designator '[' expression ']'
983///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000984Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000985 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
987
988 tok::TokenKind T = Tok.getKind();
989 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
990
991 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000992 if (Tok.isNot(tok::l_paren))
993 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
994 << BuiltinII);
995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 SourceLocation LParenLoc = ConsumeParen();
997 // TODO: Build AST.
998
999 switch (T) {
1000 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001001 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001002 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001003 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001005 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 }
1007
1008 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001009 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001010
Douglas Gregor809070a2009-02-18 17:45:20 +00001011 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001012
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001013 if (Tok.isNot(tok::r_paren)) {
1014 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001015 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001016 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001017 if (Ty.isInvalid())
1018 Res = ExprError();
1019 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001020 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001022 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001023 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001024 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001025 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001026 if (Ty.isInvalid()) {
1027 SkipUntil(tok::r_paren);
1028 return ExprError();
1029 }
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001032 return ExprError();
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001035 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001036 Diag(Tok, diag::err_expected_ident);
1037 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001038 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001039 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001040
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001041 // Keep track of the various subcomponents we see.
1042 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001043
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001044 Comps.push_back(Action::OffsetOfComponent());
1045 Comps.back().isBrackets = false;
1046 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1047 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001048
Sebastian Redla55e52c2008-11-25 22:21:31 +00001049 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001051 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001053 Comps.push_back(Action::OffsetOfComponent());
1054 Comps.back().isBrackets = false;
1055 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001057 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001058 Diag(Tok, diag::err_expected_ident);
1059 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001060 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001061 }
1062 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1063 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001064
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001065 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001067 Comps.push_back(Action::OffsetOfComponent());
1068 Comps.back().isBrackets = true;
1069 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001071 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001073 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001075 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001076
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001077 Comps.back().LocEnd =
1078 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001079 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001080 if (Ty.isInvalid())
1081 Res = ExprError();
1082 else
1083 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1084 Ty.get(), &Comps[0],
1085 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001086 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001088 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001089 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
1091 }
1092 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001093 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001094 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001095 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001096 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001097 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001098 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001099 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001101 return ExprError();
1102
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001103 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001104 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001105 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001106 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001107 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001109 return ExprError();
1110
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001111 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001112 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001113 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001114 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001115 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001116 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001117 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001118 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001119 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001120 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1121 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001122 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001123 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001125 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001128 return ExprError();
1129
Douglas Gregor809070a2009-02-18 17:45:20 +00001130 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001131
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001132 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001133 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001134 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001135 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001136
1137 if (Ty1.isInvalid() || Ty2.isInvalid())
1138 Res = ExprError();
1139 else
1140 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1141 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001142 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001143 }
1144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // These can be followed by postfix-expr pieces because they are
1146 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001147 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001148}
1149
1150/// ParseParenExpression - This parses the unit that starts with a '(' token,
1151/// based on what is allowed by ExprType. The actual thing parsed is returned
1152/// in ExprType.
1153///
1154/// primary-expression: [C99 6.5.1]
1155/// '(' expression ')'
1156/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1157/// postfix-expression: [C99 6.5.2]
1158/// '(' type-name ')' '{' initializer-list '}'
1159/// '(' type-name ')' '{' initializer-list ',' '}'
1160/// cast-expression: [C99 6.5.4]
1161/// '(' type-name ')' cast-expression
1162///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001163Parser::OwningExprResult
1164Parser::ParseParenExpression(ParenParseOption &ExprType,
1165 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001166 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001167 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001169 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001171
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001172 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001174 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001176
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001177 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001178 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001179 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001180
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001181 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001183 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001184
1185 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001186 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 RParenLoc = ConsumeParen();
1188 else
1189 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001190
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001191 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 if (!getLang().C99) // Compound literals don't exist in C90.
1193 Diag(OpenLoc, diag::ext_c99_compound_literal);
1194 Result = ParseInitializer();
1195 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001196 if (!Result.isInvalid() && !Ty.isInvalid())
1197 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001198 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001199 return move(Result);
1200 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001201
Chris Lattner42ece642008-12-12 06:00:12 +00001202 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001203 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 // returns the parsed type to the callee.
1205 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001206
1207 if (Ty.isInvalid())
1208 return ExprError();
1209
1210 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001211 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001213
Chris Lattner42ece642008-12-12 06:00:12 +00001214 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1215 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 } else {
1217 Result = ParseExpression();
1218 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001219 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001220 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001222
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001224 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001226 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 }
Chris Lattner42ece642008-12-12 06:00:12 +00001228
1229 if (Tok.is(tok::r_paren))
1230 RParenLoc = ConsumeParen();
1231 else
1232 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001233
1234 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001235}
1236
1237/// ParseStringLiteralExpression - This handles the various token types that
1238/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1239/// translation phase #6].
1240///
1241/// primary-expression: [C99 6.5.1]
1242/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001243Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001245
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1247 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001248 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001249
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 do {
1251 StringToks.push_back(Tok);
1252 ConsumeStringToken();
1253 } while (isTokenStringLiteral());
1254
1255 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001256 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001257}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001258
1259/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1260///
1261/// argument-expression-list:
1262/// assignment-expression
1263/// argument-expression-list , assignment-expression
1264///
1265/// [C++] expression-list:
1266/// [C++] assignment-expression
1267/// [C++] expression-list , assignment-expression
1268///
1269bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1270 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001271 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001272 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001273 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001274
Sebastian Redleffa8d12008-12-10 00:02:53 +00001275 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001276
1277 if (Tok.isNot(tok::comma))
1278 return false;
1279 // Move to the next argument, remember where the comma was.
1280 CommaLocs.push_back(ConsumeToken());
1281 }
1282}
Steve Naroff296e8d52008-08-28 19:20:44 +00001283
Mike Stump98eb8a72009-02-04 22:31:32 +00001284/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1285///
1286/// [clang] block-id:
1287/// [clang] specifier-qualifier-list block-declarator
1288///
1289void Parser::ParseBlockId() {
1290 // Parse the specifier-qualifier-list piece.
1291 DeclSpec DS;
1292 ParseSpecifierQualifierList(DS);
1293
1294 // Parse the block-declarator.
1295 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1296 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001297
Mike Stump6c92fa72009-04-29 21:40:37 +00001298 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1299 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1300 SourceLocation());
1301
Mike Stump19c30c02009-04-29 19:03:13 +00001302 if (Tok.is(tok::kw___attribute)) {
1303 SourceLocation Loc;
1304 AttributeList *AttrList = ParseAttributes(&Loc);
1305 DeclaratorInfo.AddAttributes(AttrList, Loc);
1306 }
1307
Mike Stump98eb8a72009-02-04 22:31:32 +00001308 // Inform sema that we are starting a block.
1309 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1310}
1311
Steve Naroff296e8d52008-08-28 19:20:44 +00001312/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001313/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001314///
1315/// block-literal:
1316/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001317/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001318/// [clang] block-args:
1319/// [clang] '(' parameter-list ')'
1320///
Sebastian Redl1d922962008-12-13 15:32:12 +00001321Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001322 assert(Tok.is(tok::caret) && "block literal starts with ^");
1323 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001324
Chris Lattner6b91f002009-03-05 07:32:12 +00001325 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1326 "block literal parsing");
1327
Steve Naroff296e8d52008-08-28 19:20:44 +00001328 // Enter a scope to hold everything within the block. This includes the
1329 // argument decls, decls within the compound expression, etc. This also
1330 // allows determining whether a variable reference inside the block is
1331 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001332 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1333 Scope::BreakScope | Scope::ContinueScope |
1334 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001335
1336 // Inform sema that we are starting a block.
1337 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001338
Steve Naroff296e8d52008-08-28 19:20:44 +00001339 // Parse the return type if present.
1340 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001341 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001342 // FIXME: Since the return type isn't actually parsed, it can't be used to
1343 // fill ParamInfo with an initial valid range, so do it manually.
1344 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001345
Steve Naroff296e8d52008-08-28 19:20:44 +00001346 // If this block has arguments, parse them. There is no ambiguity here with
1347 // the expression case, because the expression case requires a parameter list.
1348 if (Tok.is(tok::l_paren)) {
1349 ParseParenDeclarator(ParamInfo);
1350 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001351 // SetIdentifier sets the source range end, but in this case we're past
1352 // that location.
1353 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001354 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001355 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001356 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001357 // If there was an error parsing the arguments, they may have
1358 // tried to use ^(x+y) which requires an argument list. Just
1359 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001360 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001361 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001362 }
Mike Stump19c30c02009-04-29 19:03:13 +00001363
1364 if (Tok.is(tok::kw___attribute)) {
1365 SourceLocation Loc;
1366 AttributeList *AttrList = ParseAttributes(&Loc);
1367 ParamInfo.AddAttributes(AttrList, Loc);
1368 }
1369
Mike Stump98eb8a72009-02-04 22:31:32 +00001370 // Inform sema that we are starting a block.
1371 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001372 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001373 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001374 } else {
1375 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001376 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1377 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001378 0, 0, 0,
1379 false, false, 0, 0,
1380 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001381 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001382
1383 if (Tok.is(tok::kw___attribute)) {
1384 SourceLocation Loc;
1385 AttributeList *AttrList = ParseAttributes(&Loc);
1386 ParamInfo.AddAttributes(AttrList, Loc);
1387 }
1388
Mike Stump98eb8a72009-02-04 22:31:32 +00001389 // Inform sema that we are starting a block.
1390 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001391 }
1392
Sebastian Redl1d922962008-12-13 15:32:12 +00001393
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001394 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001395 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001396 // Saw something like: ^expr
1397 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001398 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001399 return ExprError();
1400 }
Chris Lattner9af55002009-03-27 04:18:06 +00001401
1402 OwningStmtResult Stmt(ParseCompoundStatementBody());
1403 if (!Stmt.isInvalid())
1404 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1405 else
1406 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001407 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001408}