blob: 20c55a1ea38184eb14ec5024a12b9bef30db75c4 [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() {
Mike Stump6ce0c392009-05-15 21:47:08 +0000204 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000205 if (LHS.isInvalid()) return move(LHS);
206
Sebastian Redld8c4e152008-12-11 22:33:27 +0000207 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208}
209
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000210/// This routine is called when the '@' is seen and consumed.
211/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000212/// routine is necessary to disambiguate @try-statement from,
213/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000214///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000215Parser::OwningExprResult
216Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000217 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000218 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000219
Sebastian Redld8c4e152008-12-11 22:33:27 +0000220 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000221}
222
Eli Friedmanadf077f2009-01-27 08:43:38 +0000223/// This routine is called when a leading '__extension__' is seen and
224/// consumed. This is necessary because the token gets consumed in the
225/// process of disambiguating between an expression and a declaration.
226Parser::OwningExprResult
227Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
Mike Stump8d286632009-05-16 04:31:34 +0000228 OwningExprResult LHS(ParseCastExpression(false));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000229 if (LHS.isInvalid()) return move(LHS);
230
231 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000232 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000233 if (LHS.isInvalid()) return move(LHS);
234
235 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
236}
237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
239///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000240Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000241 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000242 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000243
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000244 OwningExprResult LHS(ParseCastExpression(false));
245 if (LHS.isInvalid()) return move(LHS);
246
Sebastian Redld8c4e152008-12-11 22:33:27 +0000247 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249
Chris Lattnerb93fb492008-06-02 21:31:07 +0000250/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
251/// where part of an objc message send has already been parsed. In this case
252/// LBracLoc indicates the location of the '[' of the message send, and either
253/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
254/// message.
255///
256/// Since this handles full assignment-expression's, it handles postfix
257/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000258Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000259Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000260 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000261 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000262 ExprArg ReceiverExpr) {
263 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
264 ReceiverName,
265 move(ReceiverExpr)));
266 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000267 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000268 if (R.isInvalid()) return move(R);
269 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000270}
271
272
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000273Parser::OwningExprResult Parser::ParseConstantExpression() {
274 OwningExprResult LHS(ParseCastExpression(false));
275 if (LHS.isInvalid()) return move(LHS);
276
Sebastian Redld8c4e152008-12-11 22:33:27 +0000277 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000278}
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
281/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000282Parser::OwningExprResult
283Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000284 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
285 GreaterThanIsOperator,
286 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 SourceLocation ColonLoc;
288
289 while (1) {
290 // If this token has a lower precedence than we are allowed to parse (e.g.
291 // because we are called recursively, or because the token is not a binop),
292 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000293 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000294 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000295
296 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000297 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000301 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000303 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Handle this production specially:
305 // logical-OR-expression '?' expression ':' conditional-expression
306 // In particular, the RHS of the '?' is 'expression', not
307 // 'logical-OR-expression' as we might expect.
308 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000309 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000310 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 } else {
312 // Special case handling of "X ? Y : Z" where Y is empty:
313 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000314 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 Diag(Tok, diag::ext_gnu_conditional_expr);
316 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000317
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000318 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000320 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000321 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // Eat the colon.
325 ColonLoc = ConsumeToken();
326 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000329 // ParseCastExpression works here because all RHS expressions in C have it
330 // as a prefix, at least. However, in C++, an assignment-expression could
331 // be a throw-expression, which is not a valid cast-expression.
332 // Therefore we need some special-casing here.
333 // Also note that the third operand of the conditional operator is
334 // an assignment-expression in C++.
335 OwningExprResult RHS(Actions);
336 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
337 RHS = ParseAssignmentExpression();
338 else
339 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000340 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000341 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342
343 // Remember the precedence of this operator and get the precedence of the
344 // operator immediately to the right of the RHS.
345 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000346 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
347 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000348
349 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000350 bool isRightAssoc = ThisPrec == prec::Conditional ||
351 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000352
353 // Get the precedence of the operator to the right of the RHS. If it binds
354 // more tightly with RHS than we do, evaluate it completely first.
355 if (ThisPrec < NextTokPrec ||
356 (ThisPrec == NextTokPrec && isRightAssoc)) {
357 // If this is left-associative, only parse things on the RHS that bind
358 // more tightly than the current operator. If it is left-associative, it
359 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
360 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000361 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000362 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000363 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000364 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000365
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000366 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
367 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 }
369 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000370
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000371 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000372 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000373 if (TernaryMiddle.isInvalid()) {
374 // If we're using '>>' as an operator within a template
375 // argument list (in C++98), suggest the addition of
376 // parentheses so that the code remains well-formed in C++0x.
377 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
378 SuggestParentheses(OpToken.getLocation(),
379 diag::warn_cxx0x_right_shift_in_template_arg,
380 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
381 Actions.getExprRange(RHS.get()).getEnd()));
382
Sebastian Redleffa8d12008-12-10 00:02:53 +0000383 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000384 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000385 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000386 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000387 move(LHS), move(TernaryMiddle),
388 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000389 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 }
391}
392
393/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000394/// true, parse a unary-expression. isAddressOfOperand exists because an
395/// id-expression that is the operand of address-of gets special treatment
396/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000397///
398/// cast-expression: [C99 6.5.4]
399/// unary-expression
400/// '(' type-name ')' cast-expression
401///
402/// unary-expression: [C99 6.5.3]
403/// postfix-expression
404/// '++' unary-expression
405/// '--' unary-expression
406/// unary-operator cast-expression
407/// 'sizeof' unary-expression
408/// 'sizeof' '(' type-name ')'
409/// [GNU] '__alignof' unary-expression
410/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000411/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000412/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000413/// [C++] new-expression
414/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000415///
416/// unary-operator: one of
417/// '&' '*' '+' '-' '~' '!'
418/// [GNU] '__extension__' '__real' '__imag'
419///
420/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000421/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000422/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000423/// constant
424/// string-literal
425/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000426/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000427/// '(' expression ')'
428/// '__func__' [C99 6.4.2.2]
429/// [GNU] '__FUNCTION__'
430/// [GNU] '__PRETTY_FUNCTION__'
431/// [GNU] '(' compound-statement ')'
432/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
433/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
434/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
435/// assign-expr ')'
436/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000437/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000438/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000439/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000440/// [OBJC] '@protocol' '(' identifier ')'
441/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000442/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000443/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
444/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000445/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
446/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
447/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
448/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000449/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
450/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000451/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000452/// [G++] unary-type-trait '(' type-id ')'
453/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000454/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000455///
456/// constant: [C99 6.4.4]
457/// integer-constant
458/// floating-constant
459/// enumeration-constant -> identifier
460/// character-constant
461///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000462/// id-expression: [C++ 5.1]
463/// unqualified-id
464/// qualified-id [TODO]
465///
466/// unqualified-id: [C++ 5.1]
467/// identifier
468/// operator-function-id
469/// conversion-function-id [TODO]
470/// '~' class-name [TODO]
471/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000472///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000473/// new-expression: [C++ 5.3.4]
474/// '::'[opt] 'new' new-placement[opt] new-type-id
475/// new-initializer[opt]
476/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
477/// new-initializer[opt]
478///
479/// delete-expression: [C++ 5.3.5]
480/// '::'[opt] 'delete' cast-expression
481/// '::'[opt] 'delete' '[' ']' cast-expression
482///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000483/// [GNU] unary-type-trait:
484/// '__has_nothrow_assign' [TODO]
485/// '__has_nothrow_copy' [TODO]
486/// '__has_nothrow_constructor' [TODO]
487/// '__has_trivial_assign' [TODO]
488/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000489/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000490/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000491/// '__has_virtual_destructor' [TODO]
492/// '__is_abstract' [TODO]
493/// '__is_class'
494/// '__is_empty' [TODO]
495/// '__is_enum'
496/// '__is_pod'
497/// '__is_polymorphic'
498/// '__is_union'
499///
500/// [GNU] binary-type-trait:
501/// '__is_base_of' [TODO]
502///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000503Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
504 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000505 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 tok::TokenKind SavedKind = Tok.getKind();
507
508 // This handles all of cast-expression, unary-expression, postfix-expression,
509 // and primary-expression. We handle them together like this for efficiency
510 // and to simplify handling of an expression starting with a '(' token: which
511 // may be one of a parenthesized expression, cast-expression, compound literal
512 // expression, or statement expression.
513 //
514 // If the parsed tokens consist of a primary-expression, the cases below
515 // call ParsePostfixExpressionSuffix to handle the postfix expression
516 // suffixes. Cases that cannot be followed by postfix exprs should
517 // return without invoking ParsePostfixExpressionSuffix.
518 switch (SavedKind) {
519 case tok::l_paren: {
520 // If this expression is limited to being a unary-expression, the parent can
521 // not start a cast expression.
522 ParenParseOption ParenExprType =
523 isUnaryExpression ? CompoundLiteral : CastExpr;
524 TypeTy *CastTy;
525 SourceLocation LParenLoc = Tok.getLocation();
526 SourceLocation RParenLoc;
527 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000528 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000529
530 switch (ParenExprType) {
531 case SimpleExpr: break; // Nothing else to do.
532 case CompoundStmt: break; // Nothing else to do.
533 case CompoundLiteral:
534 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
535 // postfix-expression exist, parse them now.
536 break;
537 case CastExpr:
538 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
539 // the cast-expression that follows it next.
540 // TODO: For cast expression with CastTy.
541 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000542 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000543 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000544 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000548 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 // primary-expression
552 case tok::numeric_constant:
553 // constant: integer-constant
554 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000555
Steve Narofff69936d2007-09-16 03:34:24 +0000556 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
562 case tok::kw_true:
563 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000564 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000566 case tok::kw_nullptr:
567 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
568
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000569 case tok::identifier: { // primary-expression: identifier
570 // unqualified-id: identifier
571 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000572 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000573 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000574 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000575 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
576 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000577 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000578 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000579
Steve Naroff61f72cb2009-03-09 21:12:44 +0000580 // Support 'Class.property' notation.
581 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
582 // 'super' (which is inappropriate here).
583 if (getLang().ObjC1 &&
584 Actions.getTypeName(*Tok.getIdentifierInfo(),
585 Tok.getLocation(), CurScope) &&
586 NextToken().is(tok::period)) {
587 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
588 SourceLocation IdentLoc = ConsumeToken();
589 SourceLocation DotLoc = ConsumeToken();
590
591 if (Tok.isNot(tok::identifier)) {
592 Diag(Tok, diag::err_expected_ident);
593 return ExprError();
594 }
595 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
596 SourceLocation PropertyLoc = ConsumeToken();
597
598 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
599 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000600 // These can be followed by postfix-expr pieces.
601 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000602 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Consume the identifier so that we can see if it is followed by a '('.
604 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
605 // need to know whether or not this identifier is a function designator or
606 // not.
607 IdentifierInfo &II = *Tok.getIdentifierInfo();
608 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000609 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000611 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 }
613 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000614 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 ConsumeToken();
616 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000617 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
619 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
620 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000621 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
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::string_literal: // primary-expression: string-literal
626 case tok::wide_string_literal:
627 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000628 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000630 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 case tok::kw___builtin_va_arg:
632 case tok::kw___builtin_offsetof:
633 case tok::kw___builtin_choose_expr:
634 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000635 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000636 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000637 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000638 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 case tok::plusplus: // unary-expression: '++' unary-expression
640 case tok::minusminus: { // unary-expression: '--' unary-expression
641 SourceLocation SavedLoc = ConsumeToken();
642 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000643 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000644 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000645 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000647 case tok::amp: { // unary-expression: '&' cast-expression
648 // Special treatment because of member pointers
649 SourceLocation SavedLoc = ConsumeToken();
650 Res = ParseCastExpression(false, true);
651 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000652 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000653 return move(Res);
654 }
655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 case tok::star: // unary-expression: '*' cast-expression
657 case tok::plus: // unary-expression: '+' cast-expression
658 case tok::minus: // unary-expression: '-' cast-expression
659 case tok::tilde: // unary-expression: '~' cast-expression
660 case tok::exclaim: // unary-expression: '!' cast-expression
661 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000662 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 SourceLocation SavedLoc = ConsumeToken();
664 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000665 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000666 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000667 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000668 }
669
Chris Lattner35080842008-02-02 20:20:10 +0000670 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
671 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000672 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000673 SourceLocation SavedLoc = ConsumeToken();
674 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000675 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000676 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000677 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 }
679 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
680 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000681 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
683 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000684 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000685 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 case tok::ampamp: { // unary-expression: '&&' identifier
687 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000688 if (Tok.isNot(tok::identifier))
689 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000692 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 Tok.getIdentifierInfo());
694 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000695 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 }
697 case tok::kw_const_cast:
698 case tok::kw_dynamic_cast:
699 case tok::kw_reinterpret_cast:
700 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000701 Res = ParseCXXCasts();
702 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000703 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000704 case tok::kw_typeid:
705 Res = ParseCXXTypeid();
706 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000707 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000708 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000709 Res = ParseCXXThis();
710 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000711 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000712
713 case tok::kw_char:
714 case tok::kw_wchar_t:
715 case tok::kw_bool:
716 case tok::kw_short:
717 case tok::kw_int:
718 case tok::kw_long:
719 case tok::kw_signed:
720 case tok::kw_unsigned:
721 case tok::kw_float:
722 case tok::kw_double:
723 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000724 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000725 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000726 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000727 if (!getLang().CPlusPlus) {
728 Diag(Tok, diag::err_expected_expression);
729 return ExprError();
730 }
731
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000732 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
733 //
734 DeclSpec DS;
735 ParseCXXSimpleTypeSpecifier(DS);
736 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000737 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
738 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000739
740 Res = ParseCXXTypeConstructExpression(DS);
741 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000742 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000743 }
744
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000745 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
746 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
747 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000748 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000749 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000750
Chris Lattner74ba4102009-01-04 22:52:14 +0000751 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000752 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
753 // annotates the token, tail recurse.
754 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000755 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
756
Chris Lattner74ba4102009-01-04 22:52:14 +0000757 // ::new -> [C++] new-expression
758 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000759 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000760 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000761 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000762 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000763 return ParseCXXDeleteExpression(true, CCLoc);
764
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000765 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000766 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000767 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000768 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000769
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000770 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000771 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000772
773 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000774 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000775
Sebastian Redl64b45f72009-01-05 20:52:13 +0000776 case tok::kw___is_pod: // [GNU] unary-type-trait
777 case tok::kw___is_class:
778 case tok::kw___is_enum:
779 case tok::kw___is_union:
780 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000781 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000782 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000783 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000784 return ParseUnaryTypeTrait();
785
Chris Lattnerc97c2042007-10-03 22:03:06 +0000786 case tok::at: {
787 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000788 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000789 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000790 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000791 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000792 case tok::l_square:
793 // These can be followed by postfix-expr pieces.
794 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000795 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000796 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 default:
798 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000799 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000801
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 // unreachable.
803 abort();
804}
805
806/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
807/// is parsed, this method parses any suffixes that apply.
808///
809/// postfix-expression: [C99 6.5.2]
810/// primary-expression
811/// postfix-expression '[' expression ']'
812/// postfix-expression '(' argument-expression-list[opt] ')'
813/// postfix-expression '.' identifier
814/// postfix-expression '->' identifier
815/// postfix-expression '++'
816/// postfix-expression '--'
817/// '(' type-name ')' '{' initializer-list '}'
818/// '(' type-name ')' '{' initializer-list ',' '}'
819///
820/// argument-expression-list: [C99 6.5.2]
821/// argument-expression
822/// argument-expression-list ',' assignment-expression
823///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000824Parser::OwningExprResult
825Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 // Now that the primary-expression piece of the postfix-expression has been
827 // parsed, see if there are any postfix-expression pieces here.
828 SourceLocation Loc;
829 while (1) {
830 switch (Tok.getKind()) {
831 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000832 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
834 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000835 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000838
839 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000840 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
841 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000842 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000843 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000844
845 // Match the ']'.
846 MatchRHSPunctuation(tok::r_square, Loc);
847 break;
848 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000851 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000852 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000855
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000856 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000857 if (ParseExpressionList(ArgExprs, CommaLocs)) {
858 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000859 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 }
861 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000864 if (Tok.isNot(tok::r_paren)) {
865 MatchRHSPunctuation(tok::r_paren, Loc);
866 return ExprError();
867 }
868
869 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
871 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000872 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000873 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000874 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000876
877 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 break;
879 }
880 case tok::arrow: // postfix-expression: p-e '->' identifier
881 case tok::period: { // postfix-expression: p-e '.' identifier
882 tok::TokenKind OpKind = Tok.getKind();
883 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000884
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000885 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000887 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000889
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000890 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000891 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000892 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000893 *Tok.getIdentifierInfo(),
894 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000895 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 ConsumeToken();
897 break;
898 }
899 case tok::plusplus: // postfix-expression: postfix-expression '++'
900 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000902 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000903 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000904 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 ConsumeToken();
906 break;
907 }
908 }
909}
910
911
912/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
913/// unary-expression: [C99 6.5.3]
914/// 'sizeof' unary-expression
915/// 'sizeof' '(' type-name ')'
916/// [GNU] '__alignof' unary-expression
917/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000918/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000919Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000920 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
921 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000923 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 ConsumeToken();
925
926 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000927 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000928 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 Operand = ParseCastExpression(true);
930 } else {
931 // If it starts with a '(', we know that it is either a parenthesized
932 // type-name, or it is a unary-expression that starts with a compound
933 // literal, or starts with a primary-expression that is a parenthesized
934 // expression.
935 ParenParseOption ExprType = CastExpr;
936 TypeTy *CastTy;
937 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
938 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
941 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000942 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000943 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000944 OpTok.is(tok::kw_sizeof),
945 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000946 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000947
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000948 // If this is a parenthesized expression, it is the start of a
949 // unary-expression, but doesn't include any postfix pieces. Parse these
950 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000951 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000955 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000956 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
957 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000958 /*isType=*/false,
959 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000960 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000961}
962
963/// ParseBuiltinPrimaryExpression
964///
965/// primary-expression: [C99 6.5.1]
966/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
967/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
968/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
969/// assign-expr ')'
970/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
971///
972/// [GNU] offsetof-member-designator:
973/// [GNU] identifier
974/// [GNU] offsetof-member-designator '.' identifier
975/// [GNU] offsetof-member-designator '[' expression ']'
976///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000977Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000978 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
980
981 tok::TokenKind T = Tok.getKind();
982 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
983
984 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000985 if (Tok.isNot(tok::l_paren))
986 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
987 << BuiltinII);
988
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 SourceLocation LParenLoc = ConsumeParen();
990 // TODO: Build AST.
991
992 switch (T) {
993 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000994 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000995 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000996 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000998 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 }
1000
1001 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001002 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001003
Douglas Gregor809070a2009-02-18 17:45:20 +00001004 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001005
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001006 if (Tok.isNot(tok::r_paren)) {
1007 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001008 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001009 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001010 if (Ty.isInvalid())
1011 Res = ExprError();
1012 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001013 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001015 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001016 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001017 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001018 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001019 if (Ty.isInvalid()) {
1020 SkipUntil(tok::r_paren);
1021 return ExprError();
1022 }
1023
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001025 return ExprError();
1026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001028 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001029 Diag(Tok, diag::err_expected_ident);
1030 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001031 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001032 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001033
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001034 // Keep track of the various subcomponents we see.
1035 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001036
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001037 Comps.push_back(Action::OffsetOfComponent());
1038 Comps.back().isBrackets = false;
1039 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1040 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001041
Sebastian Redla55e52c2008-11-25 22:21:31 +00001042 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001044 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001046 Comps.push_back(Action::OffsetOfComponent());
1047 Comps.back().isBrackets = false;
1048 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001049
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001050 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001051 Diag(Tok, diag::err_expected_ident);
1052 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001053 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001054 }
1055 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1056 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001057
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001058 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001060 Comps.push_back(Action::OffsetOfComponent());
1061 Comps.back().isBrackets = true;
1062 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001064 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001066 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001068 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001069
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001070 Comps.back().LocEnd =
1071 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001072 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001073 if (Ty.isInvalid())
1074 Res = ExprError();
1075 else
1076 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1077 Ty.get(), &Comps[0],
1078 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001079 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001081 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001082 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 }
1084 }
1085 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001086 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001087 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001088 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001089 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001090 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001091 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001092 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001094 return ExprError();
1095
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001096 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001097 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001098 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001099 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001100 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001102 return ExprError();
1103
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001104 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001105 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001106 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001107 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001108 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001109 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001110 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001111 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001112 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001113 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1114 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001115 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001116 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001118 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001119
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001121 return ExprError();
1122
Douglas Gregor809070a2009-02-18 17:45:20 +00001123 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001124
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001125 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001126 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001127 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001128 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001129
1130 if (Ty1.isInvalid() || Ty2.isInvalid())
1131 Res = ExprError();
1132 else
1133 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1134 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001135 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001136 }
1137
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 // These can be followed by postfix-expr pieces because they are
1139 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001140 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001141}
1142
1143/// ParseParenExpression - This parses the unit that starts with a '(' token,
1144/// based on what is allowed by ExprType. The actual thing parsed is returned
1145/// in ExprType.
1146///
1147/// primary-expression: [C99 6.5.1]
1148/// '(' expression ')'
1149/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1150/// postfix-expression: [C99 6.5.2]
1151/// '(' type-name ')' '{' initializer-list '}'
1152/// '(' type-name ')' '{' initializer-list ',' '}'
1153/// cast-expression: [C99 6.5.4]
1154/// '(' type-name ')' cast-expression
1155///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001156Parser::OwningExprResult
1157Parser::ParseParenExpression(ParenParseOption &ExprType,
1158 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001159 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001160 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001162 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001164
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001165 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001167 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001169
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001170 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001171 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001172 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001173
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001174 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001176 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
1178 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001179 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 RParenLoc = ConsumeParen();
1181 else
1182 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001183
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001184 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 if (!getLang().C99) // Compound literals don't exist in C90.
1186 Diag(OpenLoc, diag::ext_c99_compound_literal);
1187 Result = ParseInitializer();
1188 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001189 if (!Result.isInvalid() && !Ty.isInvalid())
1190 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001191 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001192 return move(Result);
1193 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001194
Chris Lattner42ece642008-12-12 06:00:12 +00001195 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001196 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // returns the parsed type to the callee.
1198 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001199
1200 if (Ty.isInvalid())
1201 return ExprError();
1202
1203 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001204 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001206
Chris Lattner42ece642008-12-12 06:00:12 +00001207 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1208 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 } else {
1210 Result = ParseExpression();
1211 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001213 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001217 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001219 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 }
Chris Lattner42ece642008-12-12 06:00:12 +00001221
1222 if (Tok.is(tok::r_paren))
1223 RParenLoc = ConsumeParen();
1224 else
1225 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001226
1227 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228}
1229
1230/// ParseStringLiteralExpression - This handles the various token types that
1231/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1232/// translation phase #6].
1233///
1234/// primary-expression: [C99 6.5.1]
1235/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001236Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001238
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1240 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001241 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001242
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 do {
1244 StringToks.push_back(Tok);
1245 ConsumeStringToken();
1246 } while (isTokenStringLiteral());
1247
1248 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001249 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001250}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001251
1252/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1253///
1254/// argument-expression-list:
1255/// assignment-expression
1256/// argument-expression-list , assignment-expression
1257///
1258/// [C++] expression-list:
1259/// [C++] assignment-expression
1260/// [C++] expression-list , assignment-expression
1261///
1262bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1263 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001264 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001265 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001266 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001267
Sebastian Redleffa8d12008-12-10 00:02:53 +00001268 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001269
1270 if (Tok.isNot(tok::comma))
1271 return false;
1272 // Move to the next argument, remember where the comma was.
1273 CommaLocs.push_back(ConsumeToken());
1274 }
1275}
Steve Naroff296e8d52008-08-28 19:20:44 +00001276
Mike Stump98eb8a72009-02-04 22:31:32 +00001277/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1278///
1279/// [clang] block-id:
1280/// [clang] specifier-qualifier-list block-declarator
1281///
1282void Parser::ParseBlockId() {
1283 // Parse the specifier-qualifier-list piece.
1284 DeclSpec DS;
1285 ParseSpecifierQualifierList(DS);
1286
1287 // Parse the block-declarator.
1288 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1289 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001290
Mike Stump6c92fa72009-04-29 21:40:37 +00001291 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1292 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1293 SourceLocation());
1294
Mike Stump19c30c02009-04-29 19:03:13 +00001295 if (Tok.is(tok::kw___attribute)) {
1296 SourceLocation Loc;
1297 AttributeList *AttrList = ParseAttributes(&Loc);
1298 DeclaratorInfo.AddAttributes(AttrList, Loc);
1299 }
1300
Mike Stump98eb8a72009-02-04 22:31:32 +00001301 // Inform sema that we are starting a block.
1302 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1303}
1304
Steve Naroff296e8d52008-08-28 19:20:44 +00001305/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001306/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001307///
1308/// block-literal:
1309/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001310/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001311/// [clang] block-args:
1312/// [clang] '(' parameter-list ')'
1313///
Sebastian Redl1d922962008-12-13 15:32:12 +00001314Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001315 assert(Tok.is(tok::caret) && "block literal starts with ^");
1316 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001317
Chris Lattner6b91f002009-03-05 07:32:12 +00001318 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1319 "block literal parsing");
1320
Steve Naroff296e8d52008-08-28 19:20:44 +00001321 // Enter a scope to hold everything within the block. This includes the
1322 // argument decls, decls within the compound expression, etc. This also
1323 // allows determining whether a variable reference inside the block is
1324 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001325 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1326 Scope::BreakScope | Scope::ContinueScope |
1327 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001328
1329 // Inform sema that we are starting a block.
1330 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001331
Steve Naroff296e8d52008-08-28 19:20:44 +00001332 // Parse the return type if present.
1333 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001334 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001335 // FIXME: Since the return type isn't actually parsed, it can't be used to
1336 // fill ParamInfo with an initial valid range, so do it manually.
1337 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001338
Steve Naroff296e8d52008-08-28 19:20:44 +00001339 // If this block has arguments, parse them. There is no ambiguity here with
1340 // the expression case, because the expression case requires a parameter list.
1341 if (Tok.is(tok::l_paren)) {
1342 ParseParenDeclarator(ParamInfo);
1343 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001344 // SetIdentifier sets the source range end, but in this case we're past
1345 // that location.
1346 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001347 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001348 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001349 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001350 // If there was an error parsing the arguments, they may have
1351 // tried to use ^(x+y) which requires an argument list. Just
1352 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001353 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001354 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001355 }
Mike Stump19c30c02009-04-29 19:03:13 +00001356
1357 if (Tok.is(tok::kw___attribute)) {
1358 SourceLocation Loc;
1359 AttributeList *AttrList = ParseAttributes(&Loc);
1360 ParamInfo.AddAttributes(AttrList, Loc);
1361 }
1362
Mike Stump98eb8a72009-02-04 22:31:32 +00001363 // Inform sema that we are starting a block.
1364 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001365 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001366 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001367 } else {
1368 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001369 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1370 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001371 0, 0, 0,
1372 false, false, 0, 0,
1373 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001374 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001375
1376 if (Tok.is(tok::kw___attribute)) {
1377 SourceLocation Loc;
1378 AttributeList *AttrList = ParseAttributes(&Loc);
1379 ParamInfo.AddAttributes(AttrList, Loc);
1380 }
1381
Mike Stump98eb8a72009-02-04 22:31:32 +00001382 // Inform sema that we are starting a block.
1383 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001384 }
1385
Sebastian Redl1d922962008-12-13 15:32:12 +00001386
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001387 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001388 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001389 // Saw something like: ^expr
1390 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001391 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001392 return ExprError();
1393 }
Chris Lattner9af55002009-03-27 04:18:06 +00001394
1395 OwningStmtResult Stmt(ParseCompoundStatementBody());
1396 if (!Stmt.isInvalid())
1397 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1398 else
1399 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001400 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001401}