blob: 26a2a11444cb0bb2fec91c8099937dca76fb99f4 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercde626a2006-08-12 08:13:25 +000010// 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
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
25#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +000026#include "clang/Sema/TypoCorrection.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000027#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000028#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000030#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000031using namespace clang;
32
Chris Lattnercde626a2006-08-12 08:13:25 +000033/// getBinOpPrecedence - Return the precedence of the specified binary operator
Chris Lattner8d72f2a2010-07-19 05:07:24 +000034/// token.
Mike Stump11289f42009-09-09 15:08:12 +000035static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000036 bool GreaterThanIsOperator,
37 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000038 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000039 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000040 // C++ [temp.names]p3:
41 // [...] When parsing a template-argument-list, the first
42 // non-nested > is taken as the ending delimiter rather than a
43 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000044 if (GreaterThanIsOperator)
45 return prec::Relational;
46 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregorcbb45d02009-02-25 23:02:36 +000048 case tok::greatergreater:
49 // C++0x [temp.names]p3:
50 //
51 // [...] Similarly, the first non-nested >> is treated as two
52 // consecutive but distinct > tokens, the first of which is
53 // taken as the end of the template-argument-list and completes
54 // the template-id. [...]
55 if (GreaterThanIsOperator || !CPlusPlus0x)
56 return prec::Shift;
57 return prec::Unknown;
58
Chris Lattnercde626a2006-08-12 08:13:25 +000059 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000078 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +000082 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +000083 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +000084 case tok::plus:
85 case tok::minus: return prec::Additive;
86 case tok::percent:
87 case tok::slash:
88 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +000089 case tok::periodstar:
90 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +000091 }
92}
93
94
Chris Lattnerce7e21d2006-08-12 17:22:40 +000095/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000096/// operators.
97///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
Sebastian Redl112a97662009-02-07 00:15:38 +0000107/// pm-expression: [C++ 5.5]
108/// cast-expression
109/// pm-expression '.*' cast-expression
110/// pm-expression '->*' cast-expression
111///
Chris Lattnercde626a2006-08-12 08:13:25 +0000112/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000113/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000114/// cast-expression
115/// multiplicative-expression '*' cast-expression
116/// multiplicative-expression '/' cast-expression
117/// multiplicative-expression '%' cast-expression
118///
119/// additive-expression: [C99 6.5.6]
120/// multiplicative-expression
121/// additive-expression '+' multiplicative-expression
122/// additive-expression '-' multiplicative-expression
123///
124/// shift-expression: [C99 6.5.7]
125/// additive-expression
126/// shift-expression '<<' additive-expression
127/// shift-expression '>>' additive-expression
128///
129/// relational-expression: [C99 6.5.8]
130/// shift-expression
131/// relational-expression '<' shift-expression
132/// relational-expression '>' shift-expression
133/// relational-expression '<=' shift-expression
134/// relational-expression '>=' shift-expression
135///
136/// equality-expression: [C99 6.5.9]
137/// relational-expression
138/// equality-expression '==' relational-expression
139/// equality-expression '!=' relational-expression
140///
141/// AND-expression: [C99 6.5.10]
142/// equality-expression
143/// AND-expression '&' equality-expression
144///
145/// exclusive-OR-expression: [C99 6.5.11]
146/// AND-expression
147/// exclusive-OR-expression '^' AND-expression
148///
149/// inclusive-OR-expression: [C99 6.5.12]
150/// exclusive-OR-expression
151/// inclusive-OR-expression '|' exclusive-OR-expression
152///
153/// logical-AND-expression: [C99 6.5.13]
154/// inclusive-OR-expression
155/// logical-AND-expression '&&' inclusive-OR-expression
156///
157/// logical-OR-expression: [C99 6.5.14]
158/// logical-AND-expression
159/// logical-OR-expression '||' logical-AND-expression
160///
161/// conditional-expression: [C99 6.5.15]
162/// logical-OR-expression
163/// logical-OR-expression '?' expression ':' conditional-expression
164/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000165/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000166///
167/// assignment-expression: [C99 6.5.16]
168/// conditional-expression
169/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000170/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000171///
172/// assignment-operator: one of
173/// = *= /= %= += -= <<= >>= &= ^= |=
174///
175/// expression: [C99 6.5.17]
Douglas Gregor968f23a2011-01-03 19:31:53 +0000176/// assignment-expression ...[opt]
177/// expression ',' assignment-expression ...[opt]
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000178ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
179 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redl90893182008-12-11 22:33:27 +0000180 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000184/// Current token is an Identifier and is not a 'try'. This
Chris Lattner644e1b72007-10-03 22:03:06 +0000185/// routine is necessary to disambiguate @try-statement from,
186/// for example, @encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000187///
John McCalldadc5752010-08-24 06:29:42 +0000188ExprResult
Sebastian Redl90893182008-12-11 22:33:27 +0000189Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000190 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redl90893182008-12-11 22:33:27 +0000191 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000192}
193
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000194/// This routine is called when a leading '__extension__' is seen and
195/// consumed. This is necessary because the token gets consumed in the
196/// process of disambiguating between an expression and a declaration.
John McCalldadc5752010-08-24 06:29:42 +0000197ExprResult
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000198Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCalldadc5752010-08-24 06:29:42 +0000199 ExprResult LHS(true);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000200 {
201 // Silence extension warnings in the sub-expression
202 ExtensionRAIIObject O(Diags);
203
204 LHS = ParseCastExpression(false);
Eli Friedman15af3ee2009-05-16 23:40:44 +0000205 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000206
Douglas Gregor29d907d2010-09-17 22:25:06 +0000207 if (!LHS.isInvalid())
208 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
209 LHS.take());
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000210
Douglas Gregor29d907d2010-09-17 22:25:06 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000212}
213
Chris Lattner0c6c0342006-08-12 18:12:45 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000215ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000216 if (Tok.is(tok::code_completion)) {
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000217 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000218 cutOffParsing();
219 return ExprError();
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000220 }
221
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000222 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000223 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000224
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000225 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
226 /*isAddressOfOperand=*/false,
227 isTypeCast);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000228 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000229}
230
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000231/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
232/// where part of an objc message send has already been parsed. In this case
233/// LBracLoc indicates the location of the '[' of the message send, and either
234/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
235/// message.
236///
237/// Since this handles full assignment-expression's, it handles postfix
238/// expressions and other binary operators for these expressions as well.
John McCalldadc5752010-08-24 06:29:42 +0000239ExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000240Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000241 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +0000242 ParsedType ReceiverType,
John McCallb268a282010-08-23 23:25:46 +0000243 Expr *ReceiverExpr) {
John McCalldadc5752010-08-24 06:29:42 +0000244 ExprResult R
John McCallb268a282010-08-23 23:25:46 +0000245 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
246 ReceiverType, ReceiverExpr);
Douglas Gregoreda7e542010-09-18 01:28:11 +0000247 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor29d907d2010-09-17 22:25:06 +0000248 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000249}
250
251
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000252ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
Richard Smith764d2fe2011-12-20 02:08:33 +0000253 // C++03 [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000254 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000255 // integral constant expression is required (see 5.19) [...].
Richard Smith764d2fe2011-12-20 02:08:33 +0000256 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000257 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smith764d2fe2011-12-20 02:08:33 +0000258 Sema::ConstantEvaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000259
Kaelyn Uhrain01782002012-02-22 01:03:07 +0000260 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Douglas Gregor29d907d2010-09-17 22:25:06 +0000261 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Chris Lattner3b561a32006-08-13 00:12:11 +0000262}
263
Chris Lattnercde626a2006-08-12 08:13:25 +0000264/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
265/// LHS and has a precedence of at least MinPrec.
John McCalldadc5752010-08-24 06:29:42 +0000266ExprResult
267Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor8d4de672010-04-21 22:36:40 +0000268 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
269 GreaterThanIsOperator,
270 getLang().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000271 SourceLocation ColonLoc;
272
Chris Lattnercde626a2006-08-12 08:13:25 +0000273 while (1) {
274 // If this token has a lower precedence than we are allowed to parse (e.g.
275 // because we are called recursively, or because the token is not a binop),
276 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000277 if (NextTokPrec < MinPrec)
Sebastian Redl90893182008-12-11 22:33:27 +0000278 return move(LHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000279
280 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000281 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000282 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000283
Chris Lattner96c3deb2006-08-12 17:13:08 +0000284 // Special case handling for the ternary operator.
John McCalldadc5752010-08-24 06:29:42 +0000285 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000286 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000287 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000288 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
289 ColonProtectionRAIIObject X(*this);
290
Chris Lattner96c3deb2006-08-12 17:13:08 +0000291 // Handle this production specially:
292 // logical-OR-expression '?' expression ':' conditional-expression
293 // In particular, the RHS of the '?' is 'expression', not
294 // 'logical-OR-expression' as we might expect.
295 TernaryMiddle = ParseExpression();
Douglas Gregorec06c122010-09-17 22:41:34 +0000296 if (TernaryMiddle.isInvalid()) {
297 LHS = ExprError();
298 TernaryMiddle = 0;
299 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000300 } else {
301 // Special case handling of "X ? Y : Z" where Y is empty:
302 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000303 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000304 Diag(Tok, diag::ext_gnu_conditional_expr);
305 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000306
Chris Lattner0151b7e2010-04-20 21:33:39 +0000307 if (Tok.is(tok::colon)) {
308 // Eat the colon.
309 ColonLoc = ConsumeToken();
310 } else {
Chandler Carruth0b5cf7c2011-07-26 05:19:46 +0000311 // Otherwise, we're missing a ':'. Assume that this was a typo that
312 // the user forgot. If we're not in a macro expansion, we can suggest
313 // a fixit hint. If there were two spaces before the current token,
Chris Lattnerfb585152010-05-24 22:31:37 +0000314 // suggest inserting the colon in between them, otherwise insert ": ".
315 SourceLocation FILoc = Tok.getLocation();
316 const char *FIText = ": ";
Argyrios Kyrtzidis3ea4adb2011-06-24 17:28:29 +0000317 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis1b07c342012-01-19 15:59:08 +0000318 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
319 assert(FILoc.isFileID());
Chris Lattnerfb585152010-05-24 22:31:37 +0000320 bool IsInvalid = false;
321 const char *SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000322 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000323 if (!IsInvalid && *SourcePtr == ' ') {
324 SourcePtr =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000325 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattnerfb585152010-05-24 22:31:37 +0000326 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000327 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattnerfb585152010-05-24 22:31:37 +0000328 FIText = ":";
329 }
330 }
331 }
332
Ted Kremeneke6013652010-04-12 22:10:35 +0000333 Diag(Tok, diag::err_expected_colon)
Chris Lattnerfb585152010-05-24 22:31:37 +0000334 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner03c40412008-11-23 23:17:07 +0000335 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner0151b7e2010-04-20 21:33:39 +0000336 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000337 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000338 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000339
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000340 // Code completion for the right-hand side of an assignment expression
341 // goes through a special hook that takes the left-hand side into account.
342 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000343 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000344 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000345 return ExprError();
346 }
347
Chris Lattner96c3deb2006-08-12 17:13:08 +0000348 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000349 // ParseCastExpression works here because all RHS expressions in C have it
350 // as a prefix, at least. However, in C++, an assignment-expression could
351 // be a throw-expression, which is not a valid cast-expression.
352 // Therefore we need some special-casing here.
353 // Also note that the third operand of the conditional operator is
Richard Smith9a6403a2012-02-26 23:40:27 +0000354 // an assignment-expression in C++, and in C++11, we can have a
355 // braced-init-list on the RHS of an assignment.
John McCalldadc5752010-08-24 06:29:42 +0000356 ExprResult RHS;
Richard Smith9a6403a2012-02-26 23:40:27 +0000357 if (getLang().CPlusPlus0x && MinPrec == prec::Assignment &&
358 Tok.is(tok::l_brace)) {
359 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
360 RHS = ParseBraceInitializer();
361 if (LHS.isInvalid() || RHS.isInvalid())
362 return ExprError();
363 // A braced-init-list can never be followed by more operators.
364 return Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
365 OpToken.getKind(), LHS.take(), RHS.take());
366 } else if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) {
Sebastian Redl1a99f442009-04-16 17:51:27 +0000367 RHS = ParseAssignmentExpression();
Richard Smith9a6403a2012-02-26 23:40:27 +0000368 } else {
Sebastian Redl1a99f442009-04-16 17:51:27 +0000369 RHS = ParseCastExpression(false);
Richard Smith9a6403a2012-02-26 23:40:27 +0000370 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000371
Douglas Gregor29d907d2010-09-17 22:25:06 +0000372 if (RHS.isInvalid())
373 LHS = ExprError();
374
Chris Lattnercde626a2006-08-12 08:13:25 +0000375 // Remember the precedence of this operator and get the precedence of the
376 // operator immediately to the right of the RHS.
Douglas Gregor8d4de672010-04-21 22:36:40 +0000377 prec::Level ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000378 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
379 getLang().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000380
381 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000382 bool isRightAssoc = ThisPrec == prec::Conditional ||
383 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000384
385 // Get the precedence of the operator to the right of the RHS. If it binds
386 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000387 if (ThisPrec < NextTokPrec ||
388 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000389 // If this is left-associative, only parse things on the RHS that bind
390 // more tightly than the current operator. If it is left-associative, it
391 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
392 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000393 // The function takes ownership of the RHS.
Douglas Gregor29d907d2010-09-17 22:25:06 +0000394 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor8d4de672010-04-21 22:36:40 +0000395 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Douglas Gregor29d907d2010-09-17 22:25:06 +0000396
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000397 if (RHS.isInvalid())
Douglas Gregor29d907d2010-09-17 22:25:06 +0000398 LHS = ExprError();
Chris Lattnercde626a2006-08-12 08:13:25 +0000399
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000400 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
401 getLang().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000402 }
403 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000404
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000405 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000406 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000407 if (TernaryMiddle.isInvalid()) {
408 // If we're using '>>' as an operator within a template
409 // argument list (in C++98), suggest the addition of
410 // parentheses so that the code remains well-formed in C++0x.
411 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
412 SuggestParentheses(OpToken.getLocation(),
413 diag::warn_cxx0x_right_shift_in_template_arg,
414 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
415 Actions.getExprRange(RHS.get()).getEnd()));
416
Douglas Gregor0be31a22010-07-02 17:43:08 +0000417 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCallb268a282010-08-23 23:25:46 +0000418 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor87f95b02009-02-26 21:00:50 +0000419 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000420 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000421 LHS.take(), TernaryMiddle.take(),
422 RHS.take());
Chris Lattner319079c2007-08-31 05:01:50 +0000423 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000424 }
425}
426
Chris Lattnereaf06592006-08-11 02:02:23 +0000427/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000428/// true, parse a unary-expression. isAddressOfOperand exists because an
429/// id-expression that is the operand of address-of gets special treatment
430/// due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000431///
John McCalldadc5752010-08-24 06:29:42 +0000432ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000433 bool isAddressOfOperand,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000434 TypeCastState isTypeCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000435 bool NotCastExpr;
John McCalldadc5752010-08-24 06:29:42 +0000436 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor29d907d2010-09-17 22:25:06 +0000437 isAddressOfOperand,
438 NotCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +0000439 isTypeCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000440 if (NotCastExpr)
441 Diag(Tok, diag::err_expected_expression);
442 return move(Res);
443}
444
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000445namespace {
446class CastExpressionIdValidator : public CorrectionCandidateCallback {
447 public:
448 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
449 : AllowNonTypes(AllowNonTypes) {
450 WantTypeSpecifiers = AllowTypes;
451 }
452
453 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
454 NamedDecl *ND = candidate.getCorrectionDecl();
455 if (!ND)
456 return candidate.isKeyword();
457
458 if (isa<TypeDecl>(ND))
459 return WantTypeSpecifiers;
460 return AllowNonTypes;
461 }
462
463 private:
464 bool AllowNonTypes;
465};
466}
467
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000468/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
469/// true, parse a unary-expression. isAddressOfOperand exists because an
470/// id-expression that is the operand of address-of gets special treatment
471/// due to member pointers. NotCastExpr is set to true if the token is not the
472/// start of a cast-expression, and no diagnostic is emitted in this case.
473///
Chris Lattner4564bc12006-08-10 23:14:52 +0000474/// cast-expression: [C99 6.5.4]
475/// unary-expression
476/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000477///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000478/// unary-expression: [C99 6.5.3]
479/// postfix-expression
480/// '++' unary-expression
481/// '--' unary-expression
482/// unary-operator cast-expression
483/// 'sizeof' unary-expression
484/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +0000485/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000486/// [GNU] '__alignof' unary-expression
487/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000488/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000489/// [GNU] '&&' identifier
Sebastian Redlbd150f42008-11-21 19:14:01 +0000490/// [C++] new-expression
491/// [C++] delete-expression
Sebastian Redl22e3a932010-09-10 20:55:37 +0000492/// [C++0x] 'noexcept' '(' expression ')'
Chris Lattner81b576e2006-08-11 02:13:20 +0000493///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000494/// unary-operator: one of
495/// '&' '*' '+' '-' '~' '!'
496/// [GNU] '__extension__' '__real' '__imag'
497///
Chris Lattner52a99e52006-08-10 20:56:00 +0000498/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000499/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000500/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000501/// constant
502/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000503/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl576fd422009-05-10 18:38:11 +0000504/// [C++0x] 'nullptr' [C++0x 2.14.7]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000505/// '(' expression ')'
Benjamin Kramere56f3932011-12-23 17:00:35 +0000506/// [C11] generic-selection
Chris Lattner52a99e52006-08-10 20:56:00 +0000507/// '__func__' [C99 6.4.2.2]
508/// [GNU] '__FUNCTION__'
509/// [GNU] '__PRETTY_FUNCTION__'
510/// [GNU] '(' compound-statement ')'
511/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
512/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
513/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
514/// assign-expr ')'
515/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000516/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000517/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000518/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump11289f42009-09-09 15:08:12 +0000519/// [OBJC] '@protocol' '(' identifier ')'
520/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000521/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000522/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redl3da34892011-06-05 12:23:16 +0000523/// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3]
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000524/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redl3da34892011-06-05 12:23:16 +0000525/// [C++0x] typename-specifier braced-init-list [C++ 5.2.3]
Bill Wendlinga6930032007-06-29 18:21:34 +0000526/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
527/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
528/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
529/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000530/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
531/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000532/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000533/// [G++] unary-type-trait '(' type-id ')'
534/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley6242b6a2011-04-28 00:16:57 +0000535/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff0ac012832008-08-28 19:20:44 +0000536/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000537///
538/// constant: [C99 6.4.4]
539/// integer-constant
540/// floating-constant
541/// enumeration-constant -> identifier
542/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000543///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000544/// id-expression: [C++ 5.1]
545/// unqualified-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000546/// qualified-id
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000547///
548/// unqualified-id: [C++ 5.1]
549/// identifier
550/// operator-function-id
Douglas Gregor0c78ad92010-04-21 19:57:20 +0000551/// conversion-function-id
552/// '~' class-name
553/// template-id
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000554///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000555/// new-expression: [C++ 5.3.4]
556/// '::'[opt] 'new' new-placement[opt] new-type-id
557/// new-initializer[opt]
558/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
559/// new-initializer[opt]
560///
561/// delete-expression: [C++ 5.3.5]
562/// '::'[opt] 'delete' cast-expression
563/// '::'[opt] 'delete' '[' ']' cast-expression
564///
John Wiegley65497cc2011-04-27 23:09:49 +0000565/// [GNU/Embarcadero] unary-type-trait:
566/// '__is_arithmetic'
567/// '__is_floating_point'
568/// '__is_integral'
569/// '__is_lvalue_expr'
570/// '__is_rvalue_expr'
571/// '__is_complete_type'
572/// '__is_void'
573/// '__is_array'
574/// '__is_function'
575/// '__is_reference'
576/// '__is_lvalue_reference'
577/// '__is_rvalue_reference'
578/// '__is_fundamental'
579/// '__is_object'
580/// '__is_scalar'
581/// '__is_compound'
582/// '__is_pointer'
583/// '__is_member_object_pointer'
584/// '__is_member_function_pointer'
585/// '__is_member_pointer'
586/// '__is_const'
587/// '__is_volatile'
588/// '__is_trivial'
589/// '__is_standard_layout'
590/// '__is_signed'
591/// '__is_unsigned'
592///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000593/// [GNU] unary-type-trait:
Sebastian Redl7dcb1552010-08-31 04:59:00 +0000594/// '__has_nothrow_assign'
595/// '__has_nothrow_copy'
596/// '__has_nothrow_constructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000597/// '__has_trivial_assign' [TODO]
598/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000599/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000600/// '__has_trivial_destructor'
Sebastian Redlb469afb2010-09-02 23:19:42 +0000601/// '__has_virtual_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000602/// '__is_abstract' [TODO]
603/// '__is_class'
604/// '__is_empty' [TODO]
605/// '__is_enum'
Douglas Gregordca70af2011-12-03 18:14:24 +0000606/// '__is_final'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000607/// '__is_pod'
608/// '__is_polymorphic'
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000609/// '__is_trivial'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000610/// '__is_union'
611///
Alexis Huntd9a5cc12011-05-13 00:31:07 +0000612/// [Clang] unary-type-trait:
613/// '__trivially_copyable'
614///
Douglas Gregor8006e762011-01-27 20:28:01 +0000615/// binary-type-trait:
616/// [GNU] '__is_base_of'
617/// [MS] '__is_convertible_to'
John Wiegley65497cc2011-04-27 23:09:49 +0000618/// '__is_convertible'
619/// '__is_same'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000620///
John Wiegley6242b6a2011-04-28 00:16:57 +0000621/// [Embarcadero] array-type-trait:
622/// '__array_rank'
623/// '__array_extent'
624///
John Wiegleyf9f65842011-04-25 06:54:41 +0000625/// [Embarcadero] expression-trait:
626/// '__is_lvalue_expr'
627/// '__is_rvalue_expr'
628///
John McCalldadc5752010-08-24 06:29:42 +0000629ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl22e3a932010-09-10 20:55:37 +0000630 bool isAddressOfOperand,
631 bool &NotCastExpr,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000632 TypeCastState isTypeCast) {
John McCalldadc5752010-08-24 06:29:42 +0000633 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000634 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000635 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner81b576e2006-08-11 02:13:20 +0000637 // This handles all of cast-expression, unary-expression, postfix-expression,
638 // and primary-expression. We handle them together like this for efficiency
639 // and to simplify handling of an expression starting with a '(' token: which
640 // may be one of a parenthesized expression, cast-expression, compound literal
641 // expression, or statement expression.
642 //
643 // If the parsed tokens consist of a primary-expression, the cases below
John McCallb268a282010-08-23 23:25:46 +0000644 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
645 // to handle the postfix expression suffixes. Cases that cannot be followed
646 // by postfix exprs should return without invoking
647 // ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000648 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000649 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000650 // If this expression is limited to being a unary-expression, the parent can
651 // not start a cast expression.
652 ParenParseOption ParenExprType =
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000653 (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallba7bf592010-08-24 05:47:05 +0000654 ParsedType CastTy;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000655 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000656
657 {
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000658 // The inside of the parens don't need to be a colon protected scope, and
659 // isn't immediately a message send.
Chris Lattner3c674cf2009-12-10 02:08:07 +0000660 ColonProtectionRAIIObject X(*this, false);
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000661
Chris Lattner3c674cf2009-12-10 02:08:07 +0000662 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000663 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner3c674cf2009-12-10 02:08:07 +0000664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner81b576e2006-08-11 02:13:20 +0000666 switch (ParenExprType) {
667 case SimpleExpr: break; // Nothing else to do.
668 case CompoundStmt: break; // Nothing else to do.
669 case CompoundLiteral:
670 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
671 // postfix-expression exist, parse them now.
672 break;
673 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000674 // We have parsed the cast-expression and no postfix-expr pieces are
675 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000676 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000677 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000678
John McCallb268a282010-08-23 23:25:46 +0000679 break;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000680 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000681
Chris Lattner52a99e52006-08-10 20:56:00 +0000682 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000683 case tok::numeric_constant:
684 // constant: integer-constant
685 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000686
Steve Naroff83895f72007-09-16 03:34:24 +0000687 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000688 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000689 break;
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000690
Bill Wendling4073ed52007-02-13 01:51:42 +0000691 case tok::kw_true:
692 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000693 return ParseCXXBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000694
Sebastian Redl576fd422009-05-10 18:38:11 +0000695 case tok::kw_nullptr:
Richard Smithb15c11c2011-10-17 23:06:20 +0000696 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl576fd422009-05-10 18:38:11 +0000697 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
698
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000699 case tok::annot_primary_expr:
700 assert(Res.get() == 0 && "Stray primary-expression annotation?");
701 Res = getExprAnnotation(Tok);
702 ConsumeToken();
703 break;
704
David Blaikie15a430a2011-12-04 05:04:18 +0000705 case tok::kw_decltype:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000706 case tok::identifier: { // primary-expression: identifier
707 // unqualified-id: identifier
708 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000709 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000710 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner9a8968b2009-01-04 23:23:14 +0000711 if (getLang().CPlusPlus) {
John McCall64fe2332010-01-07 19:29:58 +0000712 // Avoid the unnecessary parse-time lookup in the common case
713 // where the syntax forbids a type.
714 const Token &Next = NextToken();
715 if (Next.is(tok::coloncolon) ||
716 (!ColonIsSacred && Next.is(tok::colon)) ||
717 Next.is(tok::less) ||
Sebastian Redl867f2282011-12-22 18:58:29 +0000718 Next.is(tok::l_paren) ||
719 Next.is(tok::l_brace)) {
John McCall64fe2332010-01-07 19:29:58 +0000720 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
721 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +0000722 return ExprError();
723 if (!Tok.is(tok::identifier))
John McCall64fe2332010-01-07 19:29:58 +0000724 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
725 }
Chris Lattner9a8968b2009-01-04 23:23:14 +0000726 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000727
Chris Lattner55662902009-10-25 17:04:48 +0000728 // Consume the identifier so that we can see if it is followed by a '(' or
729 // '.'.
730 IdentifierInfo &II = *Tok.getIdentifierInfo();
731 SourceLocation ILoc = ConsumeToken();
732
Chris Lattnera36ec422010-04-11 08:28:14 +0000733 // Support 'Class.property' and 'super.property' notation.
Chris Lattner55662902009-10-25 17:04:48 +0000734 if (getLang().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000735 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattnercd963182010-04-12 06:20:33 +0000736 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000737 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000738 ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000739
Douglas Gregor36107ad2012-02-16 18:19:22 +0000740 // Allow either an identifier or the keyword 'class' (in C++).
741 if (Tok.isNot(tok::identifier) &&
742 !(getLang().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattner55662902009-10-25 17:04:48 +0000743 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000744 return ExprError();
745 }
746 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
747 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000748
749 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
750 ILoc, PropertyLoc);
John McCallb268a282010-08-23 23:25:46 +0000751 break;
Steve Naroff9527bbf2009-03-09 21:12:44 +0000752 }
John McCall8d08b9b2010-08-27 09:08:28 +0000753
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000754 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregored0b69d2010-09-15 16:23:04 +0000755 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000756 // that identifier, this is probably a message send with a missing open
Douglas Gregored0b69d2010-09-15 16:23:04 +0000757 // bracket. Treat it as such.
758 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000759 getCurScope()->isInObjcMethodScope() &&
Douglas Gregored0b69d2010-09-15 16:23:04 +0000760 ((Tok.is(tok::identifier) &&
761 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
762 Tok.is(tok::code_completion))) {
Douglas Gregor7617c7d2010-09-15 15:09:43 +0000763 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
764 0);
765 break;
766 }
767
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000768 // If we have an Objective-C class name followed by an identifier
769 // and either ':' or ']', this is an Objective-C class message
770 // send that's missing the opening '['. Recovery
771 // appropriately. Also take this path if we're performing code
772 // completion after an Objective-C class name.
773 if (getLang().ObjC1 &&
774 ((Tok.is(tok::identifier) && !InMessageExpression) ||
775 Tok.is(tok::code_completion))) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000776 const Token& Next = NextToken();
Douglas Gregord39ae3e2011-02-15 19:17:31 +0000777 if (Tok.is(tok::code_completion) ||
778 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif433c9e12010-09-17 10:21:45 +0000779 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
780 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000781 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000782 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000783 DS.SetRangeStart(ILoc);
784 DS.SetRangeEnd(ILoc);
785 const char *PrevSpec = 0;
786 unsigned DiagID;
Gabor Greif433c9e12010-09-17 10:21:45 +0000787 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000788
789 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
790 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
791 DeclaratorInfo);
792 if (Ty.isInvalid())
793 break;
794
795 Res = ParseObjCMessageExpressionBody(SourceLocation(),
796 SourceLocation(),
797 Ty.get(), 0);
798 break;
799 }
800 }
801
John McCall8d08b9b2010-08-27 09:08:28 +0000802 // Make sure to pass down the right value for isAddressOfOperand.
803 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
804 isAddressOfOperand = false;
Chris Lattner55662902009-10-25 17:04:48 +0000805
Chris Lattnerac18be92006-11-20 06:49:47 +0000806 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
807 // need to know whether or not this identifier is a function designator or
808 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000809 UnqualifiedId Name;
810 CXXScopeSpec ScopeSpec;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000811 SourceLocation TemplateKWLoc;
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +0000812 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
813 isTypeCast != IsTypeCast);
Douglas Gregora121b752009-11-03 16:56:39 +0000814 Name.setIdentifier(&II, ILoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000815 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
816 Name, Tok.is(tok::l_paren),
817 isAddressOfOperand, &Validator);
John McCallb268a282010-08-23 23:25:46 +0000818 break;
Chris Lattnerac18be92006-11-20 06:49:47 +0000819 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000820 case tok::char_constant: // constant: character-constant
Douglas Gregorfb65e592011-07-27 05:40:30 +0000821 case tok::wide_char_constant:
822 case tok::utf16_char_constant:
823 case tok::utf32_char_constant:
Steve Naroff83895f72007-09-16 03:34:24 +0000824 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000825 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000826 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000827 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
828 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
829 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000830 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000831 ConsumeToken();
John McCallb268a282010-08-23 23:25:46 +0000832 break;
Chris Lattner52a99e52006-08-10 20:56:00 +0000833 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000834 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000835 case tok::utf8_string_literal:
836 case tok::utf16_string_literal:
837 case tok::utf32_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000838 Res = ParseStringLiteralExpression();
John McCallb268a282010-08-23 23:25:46 +0000839 break;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000840 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbourne91147592011-04-15 00:35:48 +0000841 Res = ParseGenericSelectionExpression();
842 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000843 case tok::kw___builtin_va_arg:
844 case tok::kw___builtin_offsetof:
845 case tok::kw___builtin_choose_expr:
Tanya Lattner55808c12011-06-04 00:47:47 +0000846 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redl90893182008-12-11 22:33:27 +0000847 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000848 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000849 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth904cb142011-07-08 04:59:44 +0000850
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000851 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
852 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
853 // C++ [expr.unary] has:
854 // unary-expression:
855 // ++ cast-expression
856 // -- cast-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000857 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregora49ccfe2010-08-06 14:50:36 +0000858 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000859 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000860 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000861 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000862 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000863 case tok::amp: { // unary-expression: '&' cast-expression
864 // Special treatment because of member pointers
865 SourceLocation SavedLoc = ConsumeToken();
866 Res = ParseCastExpression(false, true);
867 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000868 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000869 return move(Res);
870 }
871
Chris Lattner81b576e2006-08-11 02:13:20 +0000872 case tok::star: // unary-expression: '*' cast-expression
873 case tok::plus: // unary-expression: '+' cast-expression
874 case tok::minus: // unary-expression: '-' cast-expression
875 case tok::tilde: // unary-expression: '~' cast-expression
876 case tok::exclaim: // unary-expression: '!' cast-expression
877 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000878 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000879 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000880 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000881 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000882 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000883 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000884 }
885
Chris Lattnerc43926f2008-02-02 20:20:10 +0000886 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
887 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000888 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000889 SourceLocation SavedLoc = ConsumeToken();
890 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000891 if (!Res.isInvalid())
John McCallb268a282010-08-23 23:25:46 +0000892 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000893 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000894 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000895 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
896 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000897 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000898 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
899 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000900 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +0000901 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
902 return ParseUnaryExprOrTypeTraitExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000903 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000904 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000905 if (Tok.isNot(tok::identifier))
906 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000907
Chris Lattner9ba479b2011-02-18 21:16:39 +0000908 if (getCurScope()->getFnParent() == 0)
909 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
910
Chris Lattnereefa10e2007-05-28 06:56:27 +0000911 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000912 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
913 Tok.getLocation());
914 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Chris Lattner14a1b642006-10-15 22:33:58 +0000915 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000916 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000917 }
Chris Lattner29375652006-12-04 18:06:35 +0000918 case tok::kw_const_cast:
919 case tok::kw_dynamic_cast:
920 case tok::kw_reinterpret_cast:
921 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000922 Res = ParseCXXCasts();
John McCallb268a282010-08-23 23:25:46 +0000923 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000924 case tok::kw_typeid:
925 Res = ParseCXXTypeid();
John McCallb268a282010-08-23 23:25:46 +0000926 break;
Francois Pichet9f4f2072010-09-08 12:20:18 +0000927 case tok::kw___uuidof:
928 Res = ParseCXXUuidof();
929 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000930 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000931 Res = ParseCXXThis();
John McCallb268a282010-08-23 23:25:46 +0000932 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000933
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000934 case tok::annot_typename:
935 if (isStartOfObjCClassMessageMissingOpenBracket()) {
936 ParsedType Type = getTypeAnnotation(Tok);
937
938 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000939 DeclSpec DS(AttrFactory);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000940 DS.SetRangeStart(Tok.getLocation());
941 DS.SetRangeEnd(Tok.getLastLoc());
942
943 const char *PrevSpec = 0;
944 unsigned DiagID;
Nico Weber77430342010-11-22 10:30:56 +0000945 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
946 PrevSpec, DiagID, Type);
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000947
948 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
949 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
950 if (Ty.isInvalid())
951 break;
952
953 ConsumeToken();
954 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
955 Ty.get(), 0);
956 break;
957 }
958 // Fall through
959
David Blaikie25896afb2012-01-24 05:47:35 +0000960 case tok::annot_decltype:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000961 case tok::kw_char:
962 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000963 case tok::kw_char16_t:
964 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000965 case tok::kw_bool:
966 case tok::kw_short:
967 case tok::kw_int:
968 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000969 case tok::kw___int64:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000970 case tok::kw_signed:
971 case tok::kw_unsigned:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000972 case tok::kw_half:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000973 case tok::kw_float:
974 case tok::kw_double:
975 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000976 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000977 case tok::kw_typeof:
Douglas Gregorabf4a3e2010-09-16 01:51:54 +0000978 case tok::kw___vector: {
Chris Lattner8a38aa82009-01-04 22:28:21 +0000979 if (!getLang().CPlusPlus) {
980 Diag(Tok, diag::err_expected_expression);
981 return ExprError();
982 }
Eli Friedman6d692cc2009-06-11 00:33:41 +0000983
984 if (SavedKind == tok::kw_typename) {
985 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +0000986 // typename-specifier braced-init-list
John McCall1f476a12010-02-26 08:45:28 +0000987 if (TryAnnotateTypeOrScopeToken())
Eli Friedman6d692cc2009-06-11 00:33:41 +0000988 return ExprError();
989 }
990
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000991 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +0000992 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000993 //
John McCall084e83d2011-03-24 11:26:52 +0000994 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000995 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redl3da34892011-06-05 12:23:16 +0000996 if (Tok.isNot(tok::l_paren) &&
997 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl59b5e512008-12-11 21:36:32 +0000998 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
999 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001000
Richard Smith5d164bc2011-10-15 05:09:34 +00001001 if (Tok.is(tok::l_brace))
1002 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1003
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001004 Res = ParseCXXTypeConstructExpression(DS);
John McCallb268a282010-08-23 23:25:46 +00001005 break;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +00001006 }
1007
Douglas Gregor7df89f52010-02-05 19:11:37 +00001008 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001009 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1010 // (We can end up in this situation after tentative parsing.)
1011 if (TryAnnotateTypeOrScopeToken())
1012 return ExprError();
1013 if (!Tok.is(tok::annot_cxxscope))
1014 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001015 NotCastExpr, isTypeCast);
Douglas Gregor2c4a7502010-04-23 02:08:13 +00001016
Douglas Gregor7df89f52010-02-05 19:11:37 +00001017 Token Next = NextToken();
1018 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001019 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001020 if (TemplateId->Kind == TNK_Type_template) {
1021 // We have a qualified template-id that we know refers to a
1022 // type, translate it into a type and continue parsing as a
1023 // cast expression.
1024 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001025 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1026 /*EnteringContext=*/false);
Douglas Gregore7c20652011-03-02 00:47:37 +00001027 AnnotateTemplateIdTokenAsType();
Douglas Gregor7df89f52010-02-05 19:11:37 +00001028 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001029 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001030 }
1031 }
1032
1033 // Parse as an id-expression.
1034 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001035 break;
Douglas Gregor7df89f52010-02-05 19:11:37 +00001036 }
1037
1038 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001039 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001040 if (TemplateId->Kind == TNK_Type_template) {
1041 // We have a template-id that we know refers to a type,
1042 // translate it into a type and continue parsing as a cast
1043 // expression.
1044 AnnotateTemplateIdTokenAsType();
1045 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001046 NotCastExpr, isTypeCast);
Douglas Gregor7df89f52010-02-05 19:11:37 +00001047 }
1048
1049 // Fall through to treat the template-id as an id-expression.
1050 }
1051
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001052 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001053 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCallb268a282010-08-23 23:25:46 +00001054 break;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001055
Chris Lattner122db262009-01-04 22:52:14 +00001056 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001057 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1058 // annotates the token, tail recurse.
1059 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001060 return ExprError();
1061 if (!Tok.is(tok::coloncolon))
Sebastian Redl3d3f75a2009-02-03 20:19:35 +00001062 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1063
Chris Lattner122db262009-01-04 22:52:14 +00001064 // ::new -> [C++] new-expression
1065 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001066 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +00001067 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001068 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +00001069 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001070 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001071
Chris Lattner9a8968b2009-01-04 23:23:14 +00001072 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +00001073 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +00001074 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +00001075 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +00001076
Sebastian Redlbd150f42008-11-21 19:14:01 +00001077 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001078 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001079
1080 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +00001081 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +00001082
Sebastian Redl22e3a932010-09-10 20:55:37 +00001083 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smithb15c11c2011-10-17 23:06:20 +00001084 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001085 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001086 BalancedDelimiterTracker T(*this, tok::l_paren);
1087
1088 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl22e3a932010-09-10 20:55:37 +00001089 return ExprError();
Richard Smith764d2fe2011-12-20 02:08:33 +00001090 // C++11 [expr.unary.noexcept]p1:
Sebastian Redle56be2f72010-09-10 21:57:27 +00001091 // The noexcept operator determines whether the evaluation of its operand,
1092 // which is an unevaluated operand, can throw an exception.
1093 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl22e3a932010-09-10 20:55:37 +00001094 ExprResult Result = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001095
1096 T.consumeClose();
1097
Sebastian Redl22e3a932010-09-10 20:55:37 +00001098 if (!Result.isInvalid())
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001099 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1100 Result.take(), T.getCloseLocation());
Sebastian Redl22e3a932010-09-10 20:55:37 +00001101 return move(Result);
1102 }
1103
Chandler Carruth79803482011-04-23 10:47:20 +00001104 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001105 case tok::kw___is_class:
Eli Friedmanc96d4962009-08-15 21:55:26 +00001106 case tok::kw___is_empty:
Chandler Carruth79803482011-04-23 10:47:20 +00001107 case tok::kw___is_enum:
Sebastian Redl79eba1c2009-12-03 00:13:20 +00001108 case tok::kw___is_literal:
John Wiegley65497cc2011-04-27 23:09:49 +00001109 case tok::kw___is_arithmetic:
1110 case tok::kw___is_integral:
1111 case tok::kw___is_floating_point:
1112 case tok::kw___is_complete_type:
1113 case tok::kw___is_void:
1114 case tok::kw___is_array:
1115 case tok::kw___is_function:
1116 case tok::kw___is_reference:
1117 case tok::kw___is_lvalue_reference:
1118 case tok::kw___is_rvalue_reference:
1119 case tok::kw___is_fundamental:
1120 case tok::kw___is_object:
1121 case tok::kw___is_scalar:
1122 case tok::kw___is_compound:
1123 case tok::kw___is_pointer:
1124 case tok::kw___is_member_object_pointer:
1125 case tok::kw___is_member_function_pointer:
1126 case tok::kw___is_member_pointer:
1127 case tok::kw___is_const:
1128 case tok::kw___is_volatile:
1129 case tok::kw___is_standard_layout:
1130 case tok::kw___is_signed:
1131 case tok::kw___is_unsigned:
Chandler Carruth65fa1fd2011-04-24 02:49:28 +00001132 case tok::kw___is_literal_type:
Chandler Carruth79803482011-04-23 10:47:20 +00001133 case tok::kw___is_pod:
1134 case tok::kw___is_polymorphic:
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +00001135 case tok::kw___is_trivial:
Alexis Huntd9a5cc12011-05-13 00:31:07 +00001136 case tok::kw___is_trivially_copyable:
Chandler Carruth79803482011-04-23 10:47:20 +00001137 case tok::kw___is_union:
Douglas Gregordca70af2011-12-03 18:14:24 +00001138 case tok::kw___is_final:
Anders Carlssonfe63dc52009-04-16 00:08:20 +00001139 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +00001140 case tok::kw___has_trivial_copy:
1141 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +00001142 case tok::kw___has_trivial_destructor:
Sebastian Redl7dcb1552010-08-31 04:59:00 +00001143 case tok::kw___has_nothrow_assign:
1144 case tok::kw___has_nothrow_copy:
1145 case tok::kw___has_nothrow_constructor:
Sebastian Redlb469afb2010-09-02 23:19:42 +00001146 case tok::kw___has_virtual_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +00001147 return ParseUnaryTypeTrait();
1148
Francois Pichet34b21132010-12-08 22:35:30 +00001149 case tok::kw___builtin_types_compatible_p:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001150 case tok::kw___is_base_of:
John Wiegley65497cc2011-04-27 23:09:49 +00001151 case tok::kw___is_same:
1152 case tok::kw___is_convertible:
Douglas Gregor8006e762011-01-27 20:28:01 +00001153 case tok::kw___is_convertible_to:
Douglas Gregor1be329d2012-02-23 07:33:15 +00001154 case tok::kw___is_trivially_assignable:
Francois Pichet9dfa3ce2010-12-07 00:08:36 +00001155 return ParseBinaryTypeTrait();
1156
Douglas Gregor29c42f22012-02-24 07:38:34 +00001157 case tok::kw___is_trivially_constructible:
1158 return ParseTypeTrait();
1159
John Wiegley6242b6a2011-04-28 00:16:57 +00001160 case tok::kw___array_rank:
1161 case tok::kw___array_extent:
1162 return ParseArrayTypeTrait();
1163
John Wiegleyf9f65842011-04-25 06:54:41 +00001164 case tok::kw___is_lvalue_expr:
1165 case tok::kw___is_rvalue_expr:
1166 return ParseExpressionTrait();
1167
Chris Lattner644e1b72007-10-03 22:03:06 +00001168 case tok::at: {
1169 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001170 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +00001171 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001172 case tok::caret:
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001173 Res = ParseBlockLiteralExpression();
1174 break;
1175 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001176 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001177 cutOffParsing();
1178 return ExprError();
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001179 }
Chris Lattner6bf1db12008-12-12 19:20:14 +00001180 case tok::l_square:
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001181 if (getLang().CPlusPlus0x) {
1182 if (getLang().ObjC1) {
Eli Friedmanc7c97142012-01-04 02:40:39 +00001183 // C++11 lambda expressions and Objective-C message sends both start with a
1184 // square bracket. There are three possibilities here:
1185 // we have a valid lambda expression, we have an invalid lambda
1186 // expression, or we have something that doesn't appear to be a lambda.
1187 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001188 Res = TryParseLambdaExpression();
Eli Friedmanc7c97142012-01-04 02:40:39 +00001189 if (!Res.isInvalid() && !Res.get())
Douglas Gregordb0b9f12011-08-04 15:30:47 +00001190 Res = ParseObjCMessageExpression();
1191 break;
1192 }
1193 Res = ParseLambdaExpression();
1194 break;
1195 }
Chandler Carruthc5c3b0a22011-07-08 04:28:55 +00001196 if (getLang().ObjC1) {
1197 Res = ParseObjCMessageExpression();
1198 break;
1199 }
1200 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +00001201 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001202 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001203 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +00001204 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001205
John McCallb268a282010-08-23 23:25:46 +00001206 // These can be followed by postfix-expr pieces.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001207 return ParsePostfixExpressionSuffix(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +00001208}
1209
1210/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1211/// is parsed, this method parses any suffixes that apply.
1212///
1213/// postfix-expression: [C99 6.5.2]
1214/// primary-expression
1215/// postfix-expression '[' expression ']'
Sebastian Redl3da34892011-06-05 12:23:16 +00001216/// postfix-expression '[' braced-init-list ']'
Chris Lattner20c6a452006-08-12 17:40:43 +00001217/// postfix-expression '(' argument-expression-list[opt] ')'
1218/// postfix-expression '.' identifier
1219/// postfix-expression '->' identifier
1220/// postfix-expression '++'
1221/// postfix-expression '--'
1222/// '(' type-name ')' '{' initializer-list '}'
1223/// '(' type-name ')' '{' initializer-list ',' '}'
1224///
1225/// argument-expression-list: [C99 6.5.2]
Douglas Gregor968f23a2011-01-03 19:31:53 +00001226/// argument-expression ...[opt]
1227/// argument-expression-list ',' assignment-expression ...[opt]
Chris Lattner20c6a452006-08-12 17:40:43 +00001228///
John McCalldadc5752010-08-24 06:29:42 +00001229ExprResult
1230Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001231 // Now that the primary-expression piece of the postfix-expression has been
1232 // parsed, see if there are any postfix-expression pieces here.
1233 SourceLocation Loc;
1234 while (1) {
1235 switch (Tok.getKind()) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00001236 case tok::code_completion:
1237 if (InMessageExpression)
1238 return move(LHS);
1239
Douglas Gregoreda7e542010-09-18 01:28:11 +00001240 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001241 cutOffParsing();
1242 return ExprError();
Douglas Gregored0b69d2010-09-15 16:23:04 +00001243
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001244 case tok::identifier:
1245 // If we see identifier: after an expression, and we're not already in a
1246 // message send, then this is probably a message send with a missing
1247 // opening bracket '['.
1248 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregord6d98002010-09-15 14:54:45 +00001249 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001250 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1251 ParsedType(), LHS.get());
1252 break;
1253 }
1254
1255 // Fall through; this isn't a message send.
1256
Chris Lattner20c6a452006-08-12 17:40:43 +00001257 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001258 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001259 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner47054fb2010-05-31 18:18:22 +00001260 // If we have a array postfix expression that starts on a new line and
1261 // Objective-C is enabled, it is highly likely that the user forgot a
1262 // semicolon after the base expression and that the array postfix-expr is
1263 // actually another message send. In this case, do some look-ahead to see
1264 // if the contents of the square brackets are obviously not a valid
1265 // expression and recover by pretending there is no suffix.
1266 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1267 isSimpleObjCMessageExpression())
Douglas Gregor990ccac2010-05-31 14:40:22 +00001268 return move(LHS);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001269
1270 BalancedDelimiterTracker T(*this, tok::l_square);
1271 T.consumeOpen();
1272 Loc = T.getOpenLocation();
Sebastian Redl3da34892011-06-05 12:23:16 +00001273 ExprResult Idx;
Richard Smith5d164bc2011-10-15 05:09:34 +00001274 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1275 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00001276 Idx = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001277 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00001278 Idx = ParseExpression();
Sebastian Redl511ed552008-11-25 22:21:31 +00001279
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001280 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001281
1282 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCallb268a282010-08-23 23:25:46 +00001283 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1284 Idx.take(), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001285 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +00001286 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001287
Chris Lattner89c50c62006-08-11 06:41:18 +00001288 // Match the ']'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001289 T.consumeClose();
Chris Lattner89c50c62006-08-11 06:41:18 +00001290 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001291 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001292
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001293 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1294 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1295 // '(' argument-expression-list[opt] ')'
1296 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001297 InMessageExpressionRAIIObject InMessage(*this, false);
1298
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001299 Expr *ExecConfig = 0;
1300
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001301 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1302 BalancedDelimiterTracker PT(*this, tok::l_paren);
1303
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001304 if (OpKind == tok::lesslessless) {
1305 ExprVector ExecConfigExprs(Actions);
1306 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001307 LLLT.consumeOpen();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001308
1309 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1310 LHS = ExprError();
1311 }
1312
1313 if (LHS.isInvalid()) {
1314 SkipUntil(tok::greatergreatergreater);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001315 } else if (LLLT.consumeClose()) {
1316 // There was an error closing the brackets
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001317 LHS = ExprError();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001318 }
1319
1320 if (!LHS.isInvalid()) {
1321 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1322 LHS = ExprError();
1323 else
1324 Loc = PrevTokLocation;
1325 }
1326
1327 if (!LHS.isInvalid()) {
1328 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001329 LLLT.getOpenLocation(),
1330 move_arg(ExecConfigExprs),
1331 LLLT.getCloseLocation());
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001332 if (ECResult.isInvalid())
1333 LHS = ExprError();
1334 else
1335 ExecConfig = ECResult.get();
1336 }
1337 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001338 PT.consumeOpen();
1339 Loc = PT.getOpenLocation();
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001340 }
1341
Sebastian Redl511ed552008-11-25 22:21:31 +00001342 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001343 CommaLocsTy CommaLocs;
Douglas Gregoreda7e542010-09-18 01:28:11 +00001344
Douglas Gregorcabea402009-09-22 15:41:20 +00001345 if (Tok.is(tok::code_completion)) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00001346 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1347 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001348 cutOffParsing();
1349 return ExprError();
Douglas Gregorcabea402009-09-22 15:41:20 +00001350 }
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001351
1352 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1353 if (Tok.isNot(tok::r_paren)) {
1354 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1355 LHS.get())) {
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001356 LHS = ExprError();
1357 }
Chris Lattner0c6c0342006-08-12 18:12:45 +00001358 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001359 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001360
Chris Lattner89c50c62006-08-11 06:41:18 +00001361 // Match the ')'.
Douglas Gregoreda7e542010-09-18 01:28:11 +00001362 if (LHS.isInvalid()) {
1363 SkipUntil(tok::r_paren);
1364 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001365 PT.consumeClose();
Douglas Gregoreda7e542010-09-18 01:28:11 +00001366 LHS = ExprError();
1367 } else {
1368 assert((ArgExprs.size() == 0 ||
1369 ArgExprs.size()-1 == CommaLocs.size())&&
Chris Lattnere165d942006-08-24 04:40:38 +00001370 "Unexpected number of commas!");
John McCallb268a282010-08-23 23:25:46 +00001371 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbourne5eec5f02011-02-09 21:12:02 +00001372 move_arg(ArgExprs), Tok.getLocation(),
1373 ExecConfig);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001374 PT.consumeClose();
Chris Lattnere165d942006-08-24 04:40:38 +00001375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner89c50c62006-08-11 06:41:18 +00001377 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001378 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001379 case tok::arrow:
1380 case tok::period: {
1381 // postfix-expression: p-e '->' template[opt] id-expression
1382 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001383 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001384 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +00001385
Douglas Gregord8061562009-08-06 03:17:00 +00001386 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001387 ParsedType ObjectType;
Douglas Gregore610ada2010-02-24 18:44:31 +00001388 bool MayBePseudoDestructor = false;
Douglas Gregord8061562009-08-06 03:17:00 +00001389 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCallb268a282010-08-23 23:25:46 +00001390 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregore610ada2010-02-24 18:44:31 +00001391 OpLoc, OpKind, ObjectType,
1392 MayBePseudoDestructor);
Douglas Gregord8061562009-08-06 03:17:00 +00001393 if (LHS.isInvalid())
1394 break;
Douglas Gregore610ada2010-02-24 18:44:31 +00001395
Douglas Gregordf593fb2011-11-07 17:33:42 +00001396 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1397 /*EnteringContext=*/false,
Douglas Gregore610ada2010-02-24 18:44:31 +00001398 &MayBePseudoDestructor);
Douglas Gregor205a3612010-05-27 15:25:59 +00001399 if (SS.isNotEmpty())
John McCallba7bf592010-08-24 05:47:05 +00001400 ObjectType = ParsedType();
Douglas Gregord8061562009-08-06 03:17:00 +00001401 }
1402
Douglas Gregor2436e712009-09-17 21:32:03 +00001403 if (Tok.is(tok::code_completion)) {
1404 // Code completion for a member access expression.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001405 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor2436e712009-09-17 21:32:03 +00001406 OpLoc, OpKind == tok::arrow);
1407
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001408 cutOffParsing();
1409 return ExprError();
Douglas Gregor2436e712009-09-17 21:32:03 +00001410 }
1411
John McCallb268a282010-08-23 23:25:46 +00001412 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1413 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregore610ada2010-02-24 18:44:31 +00001414 ObjectType);
1415 break;
1416 }
1417
1418 // Either the action has told is that this cannot be a
1419 // pseudo-destructor expression (based on the type of base
1420 // expression), or we didn't see a '~' in the right place. We
1421 // can still parse a destructor name here, but in that case it
1422 // names a real destructor.
Francois Pichet64225792011-01-18 05:04:39 +00001423 // Allow explicit constructor calls in Microsoft mode.
1424 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001425 SourceLocation TemplateKWLoc;
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001426 UnqualifiedId Name;
Douglas Gregor36107ad2012-02-16 18:19:22 +00001427 if (getLang().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
1428 // Objective-C++:
1429 // After a '.' in a member access expression, treat the keyword
1430 // 'class' as if it were an identifier.
1431 //
1432 // This hack allows property access to the 'class' method because it is
1433 // such a common method name. For other C++ keywords that are
1434 // Objective-C method names, one must use the message send syntax.
1435 IdentifierInfo *Id = Tok.getIdentifierInfo();
1436 SourceLocation Loc = ConsumeToken();
1437 Name.setIdentifier(Id, Loc);
1438 } else if (ParseUnqualifiedId(SS,
1439 /*EnteringContext=*/false,
1440 /*AllowDestructorName=*/true,
1441 /*AllowConstructorName=*/
1442 getLang().MicrosoftExt,
1443 ObjectType, TemplateKWLoc, Name))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001444 LHS = ExprError();
Douglas Gregor30d60cb2009-11-03 19:44:04 +00001445
1446 if (!LHS.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001447 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001448 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00001449 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1450 Tok.is(tok::l_paren));
Chris Lattner89c50c62006-08-11 06:41:18 +00001451 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001452 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001453 case tok::plusplus: // postfix-expression: postfix-expression '++'
1454 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001455 if (!LHS.isInvalid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001456 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCallb268a282010-08-23 23:25:46 +00001457 Tok.getKind(), LHS.take());
Sebastian Redl511ed552008-11-25 22:21:31 +00001458 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001459 ConsumeToken();
1460 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001461 }
1462 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001463}
1464
Peter Collingbournee190dee2011-03-11 19:24:49 +00001465/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1466/// vec_step and we are at the start of an expression or a parenthesized
1467/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1468/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001469///
1470/// unary-expression: [C99 6.5.3]
1471/// 'sizeof' unary-expression
1472/// 'sizeof' '(' type-name ')'
1473/// [GNU] '__alignof' unary-expression
1474/// [GNU] '__alignof' '(' type-name ')'
1475/// [C++0x] 'alignof' '(' type-id ')'
1476///
1477/// [GNU] typeof-specifier:
1478/// typeof ( expressions )
1479/// typeof ( type-name )
1480/// [GNU/C++] typeof unary-expression
1481///
Peter Collingbournee190dee2011-03-11 19:24:49 +00001482/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1483/// vec_step ( expressions )
1484/// vec_step ( type-name )
1485///
John McCalldadc5752010-08-24 06:29:42 +00001486ExprResult
Peter Collingbournee190dee2011-03-11 19:24:49 +00001487Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1488 bool &isCastExpr,
1489 ParsedType &CastTy,
1490 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001491
1492 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournee190dee2011-03-11 19:24:49 +00001493 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1494 OpTok.is(tok::kw_vec_step)) &&
1495 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001496
John McCalldadc5752010-08-24 06:29:42 +00001497 ExprResult Operand;
Mike Stump11289f42009-09-09 15:08:12 +00001498
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001499 // If the operand doesn't start with an '(', it must be an expression.
1500 if (Tok.isNot(tok::l_paren)) {
1501 isCastExpr = false;
1502 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1503 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1504 return ExprError();
1505 }
Mike Stump11289f42009-09-09 15:08:12 +00001506
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001507 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001508 } else {
1509 // If it starts with a '(', we know that it is either a parenthesized
1510 // type-name, or it is a unary-expression that starts with a compound
1511 // literal, or starts with a primary-expression that is a parenthesized
1512 // expression.
1513 ParenParseOption ExprType = CastExpr;
1514 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001515
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001516 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001517 false, CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001518 CastRange = SourceRange(LParenLoc, RParenLoc);
1519
1520 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1521 // a type.
1522 if (ExprType == CastExpr) {
1523 isCastExpr = true;
1524 return ExprEmpty();
1525 }
1526
Douglas Gregor5dc05532010-07-28 18:22:12 +00001527 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1528 // GNU typeof in C requires the expression to be parenthesized. Not so for
1529 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1530 // the start of a unary-expression, but doesn't include any postfix
1531 // pieces. Parse these now if present.
John McCall3669c802010-08-24 23:41:43 +00001532 if (!Operand.isInvalid())
1533 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor5dc05532010-07-28 18:22:12 +00001534 }
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001535 }
1536
1537 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1538 isCastExpr = false;
1539 return move(Operand);
1540}
1541
Chris Lattner20c6a452006-08-12 17:40:43 +00001542
Peter Collingbournee190dee2011-03-11 19:24:49 +00001543/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Chris Lattner81b576e2006-08-11 02:13:20 +00001544/// unary-expression: [C99 6.5.3]
1545/// 'sizeof' unary-expression
1546/// 'sizeof' '(' type-name ')'
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001547/// [C++0x] 'sizeof' '...' '(' identifier ')'
Chris Lattner81b576e2006-08-11 02:13:20 +00001548/// [GNU] '__alignof' unary-expression
1549/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001550/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournee190dee2011-03-11 19:24:49 +00001551ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001552 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001553 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1554 "Not a sizeof/alignof/vec_step expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001555 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001556 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001557
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001558 // [C++0x] 'sizeof' '...' '(' identifier ')'
1559 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1560 SourceLocation EllipsisLoc = ConsumeToken();
1561 SourceLocation LParenLoc, RParenLoc;
1562 IdentifierInfo *Name = 0;
1563 SourceLocation NameLoc;
1564 if (Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001565 BalancedDelimiterTracker T(*this, tok::l_paren);
1566 T.consumeOpen();
1567 LParenLoc = T.getOpenLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001568 if (Tok.is(tok::identifier)) {
1569 Name = Tok.getIdentifierInfo();
1570 NameLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001571 T.consumeClose();
1572 RParenLoc = T.getCloseLocation();
Douglas Gregor820ba7b2011-01-04 17:33:58 +00001573 if (RParenLoc.isInvalid())
1574 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1575 } else {
1576 Diag(Tok, diag::err_expected_parameter_pack);
1577 SkipUntil(tok::r_paren);
1578 }
1579 } else if (Tok.is(tok::identifier)) {
1580 Name = Tok.getIdentifierInfo();
1581 NameLoc = ConsumeToken();
1582 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1583 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1584 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1585 << Name
1586 << FixItHint::CreateInsertion(LParenLoc, "(")
1587 << FixItHint::CreateInsertion(RParenLoc, ")");
1588 } else {
1589 Diag(Tok, diag::err_sizeof_parameter_pack);
1590 }
1591
1592 if (!Name)
1593 return ExprError();
1594
1595 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1596 OpTok.getLocation(),
1597 *Name, NameLoc,
1598 RParenLoc);
1599 }
Richard Smithb15c11c2011-10-17 23:06:20 +00001600
1601 if (OpTok.is(tok::kw_alignof))
1602 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1603
Eli Friedmane0afc982012-01-21 01:01:51 +00001604 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1605
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001606 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00001607 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001608 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00001609 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1610 isCastExpr,
1611 CastTy,
1612 CastRange);
1613
1614 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1615 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1616 ExprKind = UETT_AlignOf;
1617 else if (OpTok.is(tok::kw_vec_step))
1618 ExprKind = UETT_VecStep;
Sebastian Redl90893182008-12-11 22:33:27 +00001619
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001620 if (isCastExpr)
Peter Collingbournee190dee2011-03-11 19:24:49 +00001621 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1622 ExprKind,
1623 /*isType=*/true,
1624 CastTy.getAsOpaquePtr(),
1625 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001626
Chris Lattner26115ac2006-08-24 06:10:04 +00001627 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001628 if (!Operand.isInvalid())
Peter Collingbournee190dee2011-03-11 19:24:49 +00001629 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1630 ExprKind,
1631 /*isType=*/false,
1632 Operand.release(),
1633 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001634 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001635}
1636
Chris Lattner11124352006-08-12 19:16:08 +00001637/// ParseBuiltinPrimaryExpression
1638///
1639/// primary-expression: [C99 6.5.1]
1640/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1641/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1642/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1643/// assign-expr ')'
1644/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbourne62c21982011-11-05 03:47:48 +00001645/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001646///
Chris Lattner11124352006-08-12 19:16:08 +00001647/// [GNU] offsetof-member-designator:
1648/// [GNU] identifier
1649/// [GNU] offsetof-member-designator '.' identifier
1650/// [GNU] offsetof-member-designator '[' expression ']'
1651///
John McCalldadc5752010-08-24 06:29:42 +00001652ExprResult Parser::ParseBuiltinPrimaryExpression() {
1653 ExprResult Res;
Chris Lattner11124352006-08-12 19:16:08 +00001654 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1655
1656 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001657 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001658
1659 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001660 if (Tok.isNot(tok::l_paren))
1661 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1662 << BuiltinII);
1663
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001664 BalancedDelimiterTracker PT(*this, tok::l_paren);
1665 PT.consumeOpen();
1666
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001667 // TODO: Build AST.
1668
Chris Lattner11124352006-08-12 19:16:08 +00001669 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +00001670 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001671 case tok::kw___builtin_va_arg: {
John McCalldadc5752010-08-24 06:29:42 +00001672 ExprResult Expr(ParseAssignmentExpression());
Chris Lattner0be454e2006-08-12 19:30:51 +00001673
Chris Lattner6d7e6342006-08-15 03:41:14 +00001674 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregoreda7e542010-09-18 01:28:11 +00001675 Expr = ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001676
Douglas Gregor220cac52009-02-18 17:45:20 +00001677 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001678
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001679 if (Tok.isNot(tok::r_paren)) {
1680 Diag(Tok, diag::err_expected_rparen);
Douglas Gregoreda7e542010-09-18 01:28:11 +00001681 Expr = ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001682 }
Douglas Gregoreda7e542010-09-18 01:28:11 +00001683
1684 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor220cac52009-02-18 17:45:20 +00001685 Res = ExprError();
1686 else
John McCallb268a282010-08-23 23:25:46 +00001687 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001688 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001689 }
Chris Lattner687d6092007-08-30 15:51:11 +00001690 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001691 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001692 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001693 if (Ty.isInvalid()) {
1694 SkipUntil(tok::r_paren);
1695 return ExprError();
1696 }
Mike Stump11289f42009-09-09 15:08:12 +00001697
Chris Lattner6d7e6342006-08-15 03:41:14 +00001698 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001699 return ExprError();
1700
Chris Lattner11124352006-08-12 19:16:08 +00001701 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001702 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001703 Diag(Tok, diag::err_expected_ident);
1704 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001705 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001706 }
Sebastian Redl90893182008-12-11 22:33:27 +00001707
Chris Lattner687d6092007-08-30 15:51:11 +00001708 // Keep track of the various subcomponents we see.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001709 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001710
John McCallfaf5fb42010-08-26 23:41:50 +00001711 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001712 Comps.back().isBrackets = false;
1713 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1714 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001715
Sebastian Redl511ed552008-11-25 22:21:31 +00001716 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001717 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001718 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001719 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallfaf5fb42010-08-26 23:41:50 +00001720 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001721 Comps.back().isBrackets = false;
1722 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001723
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001724 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001725 Diag(Tok, diag::err_expected_ident);
1726 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001727 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001728 }
1729 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1730 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001731
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001732 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +00001733 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallfaf5fb42010-08-26 23:41:50 +00001734 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattner687d6092007-08-30 15:51:11 +00001735 Comps.back().isBrackets = true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001736 BalancedDelimiterTracker ST(*this, tok::l_square);
1737 ST.consumeOpen();
1738 Comps.back().LocStart = ST.getOpenLocation();
Chris Lattner11124352006-08-12 19:16:08 +00001739 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001740 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001741 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001742 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001743 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001744 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001745
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001746 ST.consumeClose();
1747 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman5e774b12009-06-27 20:38:33 +00001748 } else {
1749 if (Tok.isNot(tok::r_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001750 PT.consumeClose();
Douglas Gregor220cac52009-02-18 17:45:20 +00001751 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001752 } else if (Ty.isInvalid()) {
1753 Res = ExprError();
1754 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001755 PT.consumeClose();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001756 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001757 Ty.get(), &Comps[0], Comps.size(),
1758 PT.getCloseLocation());
Eli Friedman5e774b12009-06-27 20:38:33 +00001759 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001760 break;
Chris Lattner11124352006-08-12 19:16:08 +00001761 }
1762 }
1763 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001764 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001765 case tok::kw___builtin_choose_expr: {
John McCalldadc5752010-08-24 06:29:42 +00001766 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001767 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001768 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001769 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001770 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001771 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001772 return ExprError();
1773
John McCalldadc5752010-08-24 06:29:42 +00001774 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001775 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001776 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001777 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001778 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001779 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001780 return ExprError();
1781
John McCalldadc5752010-08-24 06:29:42 +00001782 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001783 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001784 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001785 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001786 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001787 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001788 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001789 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001790 }
John McCallb268a282010-08-23 23:25:46 +00001791 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1792 Expr2.take(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001793 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001794 }
Tanya Lattner55808c12011-06-04 00:47:47 +00001795 case tok::kw___builtin_astype: {
1796 // The first argument is an expression to be converted, followed by a comma.
1797 ExprResult Expr(ParseAssignmentExpression());
1798 if (Expr.isInvalid()) {
1799 SkipUntil(tok::r_paren);
1800 return ExprError();
1801 }
1802
1803 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1804 tok::r_paren))
1805 return ExprError();
1806
1807 // Second argument is the type to bitcast to.
1808 TypeResult DestTy = ParseTypeName();
1809 if (DestTy.isInvalid())
1810 return ExprError();
1811
1812 // Attempt to consume the r-paren.
1813 if (Tok.isNot(tok::r_paren)) {
1814 Diag(Tok, diag::err_expected_rparen);
1815 SkipUntil(tok::r_paren);
1816 return ExprError();
1817 }
1818
1819 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1820 ConsumeParen());
1821 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001822 }
Chandler Carruth904cb142011-07-08 04:59:44 +00001823 }
Sebastian Redl59b5e512008-12-11 21:36:32 +00001824
John McCallb268a282010-08-23 23:25:46 +00001825 if (Res.isInvalid())
1826 return ExprError();
1827
Chris Lattner11124352006-08-12 19:16:08 +00001828 // These can be followed by postfix-expr pieces because they are
1829 // primary-expressions.
John McCallb268a282010-08-23 23:25:46 +00001830 return ParsePostfixExpressionSuffix(Res.take());
Chris Lattner11124352006-08-12 19:16:08 +00001831}
1832
Chris Lattner4add4e62006-08-11 01:33:00 +00001833/// ParseParenExpression - This parses the unit that starts with a '(' token,
1834/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001835/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1836/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001837///
1838/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001839/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001840/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1841/// postfix-expression: [C99 6.5.2]
1842/// '(' type-name ')' '{' initializer-list '}'
1843/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001844/// cast-expression: [C99 6.5.4]
1845/// '(' type-name ')' cast-expression
John McCall31168b02011-06-15 23:02:42 +00001846/// [ARC] bridged-cast-expression
1847///
1848/// [ARC] bridged-cast-expression:
1849/// (__bridge type-name) cast-expression
1850/// (__bridge_transfer type-name) cast-expression
1851/// (__bridge_retained type-name) cast-expression
John McCalldadc5752010-08-24 06:29:42 +00001852ExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001853Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001854 bool isTypeCast, ParsedType &CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001855 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001856 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor0db4ccd2009-02-09 21:04:56 +00001857 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001858 BalancedDelimiterTracker T(*this, tok::l_paren);
1859 if (T.consumeOpen())
1860 return ExprError();
1861 SourceLocation OpenLoc = T.getOpenLocation();
1862
John McCalldadc5752010-08-24 06:29:42 +00001863 ExprResult Result(true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001864 bool isAmbiguousTypeId;
John McCallba7bf592010-08-24 05:47:05 +00001865 CastTy = ParsedType();
Sebastian Redl90893182008-12-11 22:33:27 +00001866
Douglas Gregor5e35d592010-09-14 23:59:36 +00001867 if (Tok.is(tok::code_completion)) {
1868 Actions.CodeCompleteOrdinaryName(getCurScope(),
1869 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1870 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001871 cutOffParsing();
Douglas Gregor5e35d592010-09-14 23:59:36 +00001872 return ExprError();
1873 }
John McCallc5e6b972011-04-06 02:35:25 +00001874
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001875 // Diagnose use of bridge casts in non-arc mode.
1876 bool BridgeCast = (getLang().ObjC2 &&
1877 (Tok.is(tok::kw___bridge) ||
1878 Tok.is(tok::kw___bridge_transfer) ||
1879 Tok.is(tok::kw___bridge_retained) ||
1880 Tok.is(tok::kw___bridge_retain)));
1881 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001882 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001883 SourceLocation BridgeKeywordLoc = ConsumeToken();
1884 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenek084e1b42012-02-18 04:42:38 +00001885 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekdd5d0dc2011-12-20 01:03:40 +00001886 << BridgeCastName
1887 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001888 BridgeCast = false;
1889 }
1890
John McCallc5e6b972011-04-06 02:35:25 +00001891 // None of these cases should fall through with an invalid Result
1892 // unless they've already reported an error.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001893 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001894 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall084e83d2011-03-24 11:26:52 +00001895 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001896 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001897 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001898
Chris Lattner366727f2007-07-24 16:58:17 +00001899 // If the substmt parsed correctly, build the AST node.
John McCallc5e6b972011-04-06 02:35:25 +00001900 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001901 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +00001902 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCall0c07bee2011-06-17 21:56:12 +00001903 tok::TokenKind tokenKind = Tok.getKind();
1904 SourceLocation BridgeKeywordLoc = ConsumeToken();
1905
John McCall31168b02011-06-15 23:02:42 +00001906 // Parse an Objective-C ARC ownership cast expression.
1907 ObjCBridgeCastKind Kind;
John McCall0c07bee2011-06-17 21:56:12 +00001908 if (tokenKind == tok::kw___bridge)
John McCall31168b02011-06-15 23:02:42 +00001909 Kind = OBC_Bridge;
John McCall0c07bee2011-06-17 21:56:12 +00001910 else if (tokenKind == tok::kw___bridge_transfer)
John McCall31168b02011-06-15 23:02:42 +00001911 Kind = OBC_BridgeTransfer;
John McCall0c07bee2011-06-17 21:56:12 +00001912 else if (tokenKind == tok::kw___bridge_retained)
John McCall31168b02011-06-15 23:02:42 +00001913 Kind = OBC_BridgeRetained;
John McCall0c07bee2011-06-17 21:56:12 +00001914 else {
1915 // As a hopefully temporary workaround, allow __bridge_retain as
1916 // a synonym for __bridge_retained, but only in system headers.
1917 assert(tokenKind == tok::kw___bridge_retain);
1918 Kind = OBC_BridgeRetained;
1919 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1920 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1921 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1922 "__bridge_retained");
1923 }
John McCall31168b02011-06-15 23:02:42 +00001924
John McCall31168b02011-06-15 23:02:42 +00001925 TypeResult Ty = ParseTypeName();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001926 T.consumeClose();
1927 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001928 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCall31168b02011-06-15 23:02:42 +00001929
1930 if (Ty.isInvalid() || SubExpr.isInvalid())
1931 return ExprError();
1932
1933 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1934 BridgeKeywordLoc, Ty.get(),
1935 RParenLoc, SubExpr.get());
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001936 } else if (ExprType >= CompoundLiteral &&
1937 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001938
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001939 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001940
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001941 // In C++, if the type-id is ambiguous we disambiguate based on context.
1942 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1943 // in which case we should treat it as type-id.
1944 // if stopIfCastExpr is false, we need to determine the context past the
1945 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001946 if (isAmbiguousTypeId && !stopIfCastExpr) {
1947 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1948 RParenLoc = T.getCloseLocation();
1949 return res;
1950 }
Mike Stump11289f42009-09-09 15:08:12 +00001951
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001952 // Parse the type declarator.
1953 DeclSpec DS(AttrFactory);
1954 ParseSpecifierQualifierList(DS);
1955 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1956 ParseDeclarator(DeclaratorInfo);
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001957
Douglas Gregor3e972002010-09-15 23:19:31 +00001958 // If our type is followed by an identifier and either ':' or ']', then
1959 // this is probably an Objective-C message send where the leading '[' is
1960 // missing. Recover as if that were the case.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001961 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1962 !InMessageExpression && getLang().ObjC1 &&
1963 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1964 TypeResult Ty;
1965 {
1966 InMessageExpressionRAIIObject InMessage(*this, false);
1967 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1968 }
Douglas Gregor3e972002010-09-15 23:19:31 +00001969 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1970 SourceLocation(),
1971 Ty.get(), 0);
1972 } else {
1973 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001974 T.consumeClose();
1975 RParenLoc = T.getCloseLocation();
Douglas Gregor3e972002010-09-15 23:19:31 +00001976 if (Tok.is(tok::l_brace)) {
1977 ExprType = CompoundLiteral;
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001978 TypeResult Ty;
1979 {
1980 InMessageExpressionRAIIObject InMessage(*this, false);
1981 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1982 }
Douglas Gregor3e972002010-09-15 23:19:31 +00001983 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnera36ec422010-04-11 08:28:14 +00001984 }
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001985
Douglas Gregor3e972002010-09-15 23:19:31 +00001986 if (ExprType == CastExpr) {
1987 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb5d49352009-01-19 22:31:54 +00001988
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001989 if (DeclaratorInfo.isInvalidType())
Douglas Gregor3e972002010-09-15 23:19:31 +00001990 return ExprError();
1991
Douglas Gregor3e972002010-09-15 23:19:31 +00001992 // Note that this doesn't parse the subsequent cast-expression, it just
1993 // returns the parsed type to the callee.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00001994 if (stopIfCastExpr) {
1995 TypeResult Ty;
1996 {
1997 InMessageExpressionRAIIObject InMessage(*this, false);
1998 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1999 }
2000 CastTy = Ty.get();
Douglas Gregor3e972002010-09-15 23:19:31 +00002001 return ExprResult();
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002002 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002003
2004 // Reject the cast of super idiom in ObjC.
2005 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
2006 Tok.getIdentifierInfo() == Ident_super &&
2007 getCurScope()->isInObjcMethodScope() &&
2008 GetLookAheadToken(1).isNot(tok::period)) {
2009 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2010 << SourceRange(OpenLoc, RParenLoc);
2011 return ExprError();
2012 }
2013
2014 // Parse the cast-expression that follows it next.
2015 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002016 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2017 /*isAddressOfOperand=*/false,
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002018 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002019 if (!Result.isInvalid()) {
2020 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2021 DeclaratorInfo, CastTy,
Douglas Gregor3e972002010-09-15 23:19:31 +00002022 RParenLoc, Result.take());
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002023 }
Douglas Gregor3e972002010-09-15 23:19:31 +00002024 return move(Result);
2025 }
2026
2027 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2028 return ExprError();
2029 }
Argyrios Kyrtzidis7192a3b2011-07-01 22:22:59 +00002030 } else if (isTypeCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00002031 // Parse the expression-list.
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002032 InMessageExpressionRAIIObject InMessage(*this, false);
2033
Nate Begeman5ec4b312009-08-10 23:49:36 +00002034 ExprVector ArgExprs(Actions);
2035 CommaLocsTy CommaLocs;
2036
2037 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2038 ExprType = SimpleExpr;
Sebastian Redla9351792012-02-11 23:51:47 +00002039 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2040 move_arg(ArgExprs));
Nate Begeman5ec4b312009-08-10 23:49:36 +00002041 }
Chris Lattner4add4e62006-08-11 01:33:00 +00002042 } else {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002043 InMessageExpressionRAIIObject InMessage(*this, false);
2044
Kaelyn Uhrain77e21fc2012-01-25 20:49:08 +00002045 Result = ParseExpression(MaybeTypeCast);
Chris Lattner4add4e62006-08-11 01:33:00 +00002046 ExprType = SimpleExpr;
John McCallc5e6b972011-04-06 02:35:25 +00002047
2048 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002049 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCallb268a282010-08-23 23:25:46 +00002050 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00002051 }
Sebastian Redl90893182008-12-11 22:33:27 +00002052
Chris Lattner4564bc12006-08-10 23:14:52 +00002053 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00002054 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00002055 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00002056 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00002057 }
Mike Stump11289f42009-09-09 15:08:12 +00002058
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002059 T.consumeClose();
2060 RParenLoc = T.getCloseLocation();
Sebastian Redl90893182008-12-11 22:33:27 +00002061 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00002062}
Chris Lattnerd3e98952006-10-06 05:22:26 +00002063
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002064/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2065/// and we are at the left brace.
2066///
2067/// postfix-expression: [C99 6.5.2]
2068/// '(' type-name ')' '{' initializer-list '}'
2069/// '(' type-name ')' '{' initializer-list ',' '}'
2070///
John McCalldadc5752010-08-24 06:29:42 +00002071ExprResult
John McCallba7bf592010-08-24 05:47:05 +00002072Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002073 SourceLocation LParenLoc,
2074 SourceLocation RParenLoc) {
2075 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2076 if (!getLang().C99) // Compound literals don't exist in C90.
2077 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCalldadc5752010-08-24 06:29:42 +00002078 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002079 if (!Result.isInvalid() && Ty)
John McCallb268a282010-08-23 23:25:46 +00002080 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00002081 return move(Result);
2082}
2083
Chris Lattnerd3e98952006-10-06 05:22:26 +00002084/// ParseStringLiteralExpression - This handles the various token types that
2085/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2086/// translation phase #6].
2087///
2088/// primary-expression: [C99 6.5.1]
2089/// string-literal
John McCalldadc5752010-08-24 06:29:42 +00002090ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002091 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00002092
Chris Lattnerd3e98952006-10-06 05:22:26 +00002093 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2094 // considered to be strings for concatenation purposes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002095 SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00002096
Chris Lattnerd3e98952006-10-06 05:22:26 +00002097 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00002098 StringToks.push_back(Tok);
2099 ConsumeStringToken();
2100 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00002101
2102 // Pass the set of string tokens, ready for concatenation, to the actions.
Alexis Hunt3b791862010-08-30 17:47:05 +00002103 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00002104}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002105
Benjamin Kramere56f3932011-12-23 17:00:35 +00002106/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2107/// [C11 6.5.1.1].
Peter Collingbourne91147592011-04-15 00:35:48 +00002108///
2109/// generic-selection:
2110/// _Generic ( assignment-expression , generic-assoc-list )
2111/// generic-assoc-list:
2112/// generic-association
2113/// generic-assoc-list , generic-association
2114/// generic-association:
2115/// type-name : assignment-expression
2116/// default : assignment-expression
2117ExprResult Parser::ParseGenericSelectionExpression() {
2118 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2119 SourceLocation KeyLoc = ConsumeToken();
2120
Benjamin Kramere56f3932011-12-23 17:00:35 +00002121 if (!getLang().C11)
2122 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbourne91147592011-04-15 00:35:48 +00002123
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002124 BalancedDelimiterTracker T(*this, tok::l_paren);
2125 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne91147592011-04-15 00:35:48 +00002126 return ExprError();
2127
2128 ExprResult ControllingExpr;
2129 {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002130 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbourne91147592011-04-15 00:35:48 +00002131 // not evaluated."
2132 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2133 ControllingExpr = ParseAssignmentExpression();
2134 if (ControllingExpr.isInvalid()) {
2135 SkipUntil(tok::r_paren);
2136 return ExprError();
2137 }
2138 }
2139
2140 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2141 SkipUntil(tok::r_paren);
2142 return ExprError();
2143 }
2144
2145 SourceLocation DefaultLoc;
2146 TypeVector Types(Actions);
2147 ExprVector Exprs(Actions);
2148 while (1) {
2149 ParsedType Ty;
2150 if (Tok.is(tok::kw_default)) {
Benjamin Kramere56f3932011-12-23 17:00:35 +00002151 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbourne91147592011-04-15 00:35:48 +00002152 // generic association."
2153 if (!DefaultLoc.isInvalid()) {
2154 Diag(Tok, diag::err_duplicate_default_assoc);
2155 Diag(DefaultLoc, diag::note_previous_default_assoc);
2156 SkipUntil(tok::r_paren);
2157 return ExprError();
2158 }
2159 DefaultLoc = ConsumeToken();
2160 Ty = ParsedType();
2161 } else {
2162 ColonProtectionRAIIObject X(*this);
2163 TypeResult TR = ParseTypeName();
2164 if (TR.isInvalid()) {
2165 SkipUntil(tok::r_paren);
2166 return ExprError();
2167 }
2168 Ty = TR.release();
2169 }
2170 Types.push_back(Ty);
2171
2172 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2173 SkipUntil(tok::r_paren);
2174 return ExprError();
2175 }
2176
2177 // FIXME: These expressions should be parsed in a potentially potentially
2178 // evaluated context.
2179 ExprResult ER(ParseAssignmentExpression());
2180 if (ER.isInvalid()) {
2181 SkipUntil(tok::r_paren);
2182 return ExprError();
2183 }
2184 Exprs.push_back(ER.release());
2185
2186 if (Tok.isNot(tok::comma))
2187 break;
2188 ConsumeToken();
2189 }
2190
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002191 T.consumeClose();
2192 if (T.getCloseLocation().isInvalid())
Peter Collingbourne91147592011-04-15 00:35:48 +00002193 return ExprError();
2194
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002195 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2196 T.getCloseLocation(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002197 ControllingExpr.release(),
2198 move_arg(Types), move_arg(Exprs));
2199}
2200
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002201/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2202///
2203/// argument-expression-list:
2204/// assignment-expression
2205/// argument-expression-list , assignment-expression
2206///
2207/// [C++] expression-list:
Sebastian Redl3da34892011-06-05 12:23:16 +00002208/// [C++] assignment-expression
2209/// [C++] expression-list , assignment-expression
2210///
2211/// [C++0x] expression-list:
2212/// [C++0x] initializer-list
2213///
2214/// [C++0x] initializer-list
2215/// [C++0x] initializer-clause ...[opt]
2216/// [C++0x] initializer-list , initializer-clause ...[opt]
2217///
2218/// [C++0x] initializer-clause:
2219/// [C++0x] assignment-expression
2220/// [C++0x] braced-init-list
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002221///
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002222bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2223 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallfaf5fb42010-08-26 23:41:50 +00002224 void (Sema::*Completer)(Scope *S,
John McCall37ad5512010-08-23 06:44:23 +00002225 Expr *Data,
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002226 llvm::ArrayRef<Expr *> Args),
John McCall37ad5512010-08-23 06:44:23 +00002227 Expr *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002228 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00002229 if (Tok.is(tok::code_completion)) {
2230 if (Completer)
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002231 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor9c1f1bf2011-02-17 03:09:23 +00002232 else
2233 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002234 cutOffParsing();
2235 return true;
Douglas Gregorcabea402009-09-22 15:41:20 +00002236 }
Sebastian Redl3da34892011-06-05 12:23:16 +00002237
2238 ExprResult Expr;
Richard Smith5d164bc2011-10-15 05:09:34 +00002239 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2240 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl3da34892011-06-05 12:23:16 +00002241 Expr = ParseBraceInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00002242 } else
Sebastian Redl3da34892011-06-05 12:23:16 +00002243 Expr = ParseAssignmentExpression();
2244
Douglas Gregor968f23a2011-01-03 19:31:53 +00002245 if (Tok.is(tok::ellipsis))
2246 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002247 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002248 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00002249
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002250 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00002251
2252 if (Tok.isNot(tok::comma))
2253 return false;
2254 // Move to the next argument, remember where the comma was.
2255 CommaLocs.push_back(ConsumeToken());
2256 }
2257}
Steve Naroff0ac012832008-08-28 19:20:44 +00002258
Mike Stump82f071f2009-02-04 22:31:32 +00002259/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2260///
2261/// [clang] block-id:
2262/// [clang] specifier-qualifier-list block-declarator
2263///
2264void Parser::ParseBlockId() {
Douglas Gregor643c3302010-10-18 21:34:55 +00002265 if (Tok.is(tok::code_completion)) {
2266 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002267 return cutOffParsing();
Douglas Gregor643c3302010-10-18 21:34:55 +00002268 }
2269
Mike Stump82f071f2009-02-04 22:31:32 +00002270 // Parse the specifier-qualifier-list piece.
John McCall084e83d2011-03-24 11:26:52 +00002271 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002272 ParseSpecifierQualifierList(DS);
2273
2274 // Parse the block-declarator.
2275 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2276 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002277
Mike Stump56ed2ea2009-04-29 21:40:37 +00002278 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall084e83d2011-03-24 11:26:52 +00002279 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump56ed2ea2009-04-29 21:40:37 +00002280
John McCall53fa7142010-12-24 02:08:15 +00002281 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002282
Mike Stump82f071f2009-02-04 22:31:32 +00002283 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002284 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump82f071f2009-02-04 22:31:32 +00002285}
2286
Steve Naroff0ac012832008-08-28 19:20:44 +00002287/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00002288/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00002289///
2290/// block-literal:
2291/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00002292/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00002293/// [clang] block-args:
2294/// [clang] '(' parameter-list ')'
2295///
John McCalldadc5752010-08-24 06:29:42 +00002296ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00002297 assert(Tok.is(tok::caret) && "block literal starts with ^");
2298 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002299
Chris Lattnerf6801202009-03-05 07:32:12 +00002300 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2301 "block literal parsing");
2302
Mike Stump11289f42009-09-09 15:08:12 +00002303 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00002304 // argument decls, decls within the compound expression, etc. This also
2305 // allows determining whether a variable reference inside the block is
2306 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002307 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002308 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00002309
2310 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002311 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump11289f42009-09-09 15:08:12 +00002312
Steve Naroff0ac012832008-08-28 19:20:44 +00002313 // Parse the return type if present.
John McCall084e83d2011-03-24 11:26:52 +00002314 DeclSpec DS(AttrFactory);
Mike Stump82f071f2009-02-04 22:31:32 +00002315 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002316 // FIXME: Since the return type isn't actually parsed, it can't be used to
2317 // fill ParamInfo with an initial valid range, so do it manually.
2318 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002319
Steve Naroff0ac012832008-08-28 19:20:44 +00002320 // If this block has arguments, parse them. There is no ambiguity here with
2321 // the expression case, because the expression case requires a parameter list.
2322 if (Tok.is(tok::l_paren)) {
2323 ParseParenDeclarator(ParamInfo);
2324 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002325 // SetIdentifier sets the source range end, but in this case we're past
2326 // that location.
2327 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00002328 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002329 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00002330 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00002331 // If there was an error parsing the arguments, they may have
2332 // tried to use ^(x+y) which requires an argument list. Just
2333 // skip the whole block literal.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002334 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002335 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00002336 }
Mike Stump88788fe2009-04-29 19:03:13 +00002337
John McCall53fa7142010-12-24 02:08:15 +00002338 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002339
Mike Stump82f071f2009-02-04 22:31:32 +00002340 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002341 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpd73e4412009-04-14 18:24:37 +00002342 } else if (!Tok.is(tok::l_brace)) {
Mike Stump82f071f2009-02-04 22:31:32 +00002343 ParseBlockId();
Steve Naroff0ac012832008-08-28 19:20:44 +00002344 } else {
2345 // Otherwise, pretend we saw (void).
John McCall084e83d2011-03-24 11:26:52 +00002346 ParsedAttributes attrs(AttrFactory);
2347 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002348 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002349 0, 0, 0,
Douglas Gregor54992352011-01-26 03:43:54 +00002350 true, SourceLocation(),
Douglas Gregorad69e652011-07-13 21:47:47 +00002351 SourceLocation(),
Douglas Gregore248eea2011-10-19 06:04:55 +00002352 SourceLocation(),
2353 SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00002354 EST_None,
2355 SourceLocation(),
2356 0, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002357 CaretLoc, CaretLoc,
2358 ParamInfo),
John McCall084e83d2011-03-24 11:26:52 +00002359 attrs, CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00002360
John McCall53fa7142010-12-24 02:08:15 +00002361 MaybeParseGNUAttributes(ParamInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00002362
Mike Stump82f071f2009-02-04 22:31:32 +00002363 // Inform sema that we are starting a block.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002364 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff0ac012832008-08-28 19:20:44 +00002365 }
2366
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002367
John McCalldadc5752010-08-24 06:29:42 +00002368 ExprResult Result(true);
Chris Lattner9eac9312009-03-27 04:18:06 +00002369 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002370 // Saw something like: ^expr
2371 Diag(Tok, diag::err_expected_expression);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002372 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00002373 return ExprError();
2374 }
Mike Stump11289f42009-09-09 15:08:12 +00002375
John McCalldadc5752010-08-24 06:29:42 +00002376 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002377 BlockScope.Exit();
Chris Lattner9eac9312009-03-27 04:18:06 +00002378 if (!Stmt.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00002379 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9eac9312009-03-27 04:18:06 +00002380 else
Douglas Gregor0be31a22010-07-02 17:43:08 +00002381 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002382 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00002383}