blob: 22c5841e45f7f8bc82a12030bdf0c6f66d32d54f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
25#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhraincd78e612012-01-25 20:49:08 +000026#include "clang/Sema/TypoCorrection.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000027#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000028#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/SmallString.h"
31using namespace clang;
32
Reid Spencer5f016e22007-07-11 17:01:13 +000033/// getBinOpPrecedence - Return the precedence of the specified binary operator
Chris Lattnerdfe503e2010-07-19 05:07:24 +000034/// token.
Mike Stump1eb44332009-09-09 15:08:12 +000035static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000036 bool GreaterThanIsOperator,
37 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000039 case tok::greater:
Douglas Gregor3965b7b2009-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 Gregor55f6b142009-02-09 18:46:07 +000044 if (GreaterThanIsOperator)
45 return prec::Relational;
46 return prec::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor3965b7b2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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;
78 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +000082 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +000083 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl22460502009-02-07 00:15:38 +000089 case tok::periodstar:
90 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 }
92}
93
94
95/// ParseExpression - Simple precedence-based parser for binary/ternary
96/// operators.
97///
98/// 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 Redl22460502009-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///
Reid Spencer5f016e22007-07-11 17:01:13 +0000112/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000113/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl3201f6b2009-04-16 17:51:27 +0000165/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000166///
167/// assignment-expression: [C99 6.5.16]
168/// conditional-expression
169/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000170/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000171///
172/// assignment-operator: one of
173/// = *= /= %= += -= <<= >>= &= ^= |=
174///
175/// expression: [C99 6.5.17]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000176/// assignment-expression ...[opt]
177/// expression ',' assignment-expression ...[opt]
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000178ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
179 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000180 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Mike Stump1eb44332009-09-09 15:08:12 +0000183/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000184/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000185/// routine is necessary to disambiguate @try-statement from,
186/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000187///
John McCall60d7b3a2010-08-24 06:29:42 +0000188ExprResult
Sebastian Redld8c4e152008-12-11 22:33:27 +0000189Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000190 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000191 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000192}
193
Eli Friedmanadf077f2009-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 McCall60d7b3a2010-08-24 06:29:42 +0000197ExprResult
Eli Friedmanadf077f2009-01-27 08:43:38 +0000198Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000199 ExprResult LHS(true);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000200 {
201 // Silence extension warnings in the sub-expression
202 ExtensionRAIIObject O(Diags);
203
204 LHS = ParseCastExpression(false);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000205 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000206
Douglas Gregor200b2922010-09-17 22:25:06 +0000207 if (!LHS.isInvalid())
208 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
209 LHS.take());
Eli Friedmanadf077f2009-01-27 08:43:38 +0000210
Douglas Gregor200b2922010-09-17 22:25:06 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmanadf077f2009-01-27 08:43:38 +0000212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000215ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000216 if (Tok.is(tok::code_completion)) {
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000217 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000218 cutOffParsing();
219 return ExprError();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000220 }
221
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000222 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000223 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000224
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000225 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
226 /*isAddressOfOperand=*/false,
227 isTypeCast);
Douglas Gregor200b2922010-09-17 22:25:06 +0000228 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000229}
230
Chris Lattnerb93fb492008-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 McCall60d7b3a2010-08-24 06:29:42 +0000239ExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000240Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000241 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +0000242 ParsedType ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000243 Expr *ReceiverExpr) {
John McCall60d7b3a2010-08-24 06:29:42 +0000244 ExprResult R
John McCall9ae2f072010-08-23 23:25:46 +0000245 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
246 ReceiverType, ReceiverExpr);
Douglas Gregorac5fd842010-09-18 01:28:11 +0000247 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor200b2922010-09-17 22:25:06 +0000248 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000249}
250
251
John McCall60d7b3a2010-08-24 06:29:42 +0000252ExprResult Parser::ParseConstantExpression() {
Richard Smithf6702a32011-12-20 02:08:33 +0000253 // C++03 [basic.def.odr]p2:
Mike Stump1eb44332009-09-09 15:08:12 +0000254 // An expression is potentially evaluated unless it appears where an
Douglas Gregore0762c92009-06-19 23:52:42 +0000255 // integral constant expression is required (see 5.19) [...].
Richard Smithf6702a32011-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 Gregorac7610d2009-06-22 20:57:11 +0000257 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smithf6702a32011-12-20 02:08:33 +0000258 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
John McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult LHS(ParseCastExpression(false));
Douglas Gregor200b2922010-09-17 22:25:06 +0000261 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
265/// LHS and has a precedence of at least MinPrec.
John McCall60d7b3a2010-08-24 06:29:42 +0000266ExprResult
267Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000268 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
269 GreaterThanIsOperator,
270 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 SourceLocation ColonLoc;
272
273 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 Redl0e9eabc2008-12-09 13:15:23 +0000277 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000278 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
280 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000281 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000285 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000287 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor94859892010-09-17 22:41:34 +0000296 if (TernaryMiddle.isInvalid()) {
297 LHS = ExprError();
298 TernaryMiddle = 0;
299 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 } else {
301 // Special case handling of "X ? Y : Z" where Y is empty:
302 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000303 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 Diag(Tok, diag::ext_gnu_conditional_expr);
305 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000306
Chris Lattnere5deae92010-04-20 21:33:39 +0000307 if (Tok.is(tok::colon)) {
308 // Eat the colon.
309 ColonLoc = ConsumeToken();
310 } else {
Chandler Carruthb00d37e2011-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 Lattner24728822010-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 Kyrtzidisb5303aa2011-06-24 17:28:29 +0000317 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000318 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
319 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000320 bool IsInvalid = false;
321 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000322 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000323 if (!IsInvalid && *SourcePtr == ' ') {
324 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000325 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000326 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000327 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000328 FIText = ":";
329 }
330 }
331 }
332
Ted Kremenek987aa872010-04-12 22:10:35 +0000333 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000334 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000335 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000336 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000339
Douglas Gregor5ac3bdb2010-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 Gregor23c94db2010-07-02 17:43:08 +0000343 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000344 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000345 return ExprError();
346 }
347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-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
354 // an assignment-expression in C++.
John McCall60d7b3a2010-08-24 06:29:42 +0000355 ExprResult RHS;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000356 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
357 RHS = ParseAssignmentExpression();
358 else
359 RHS = ParseCastExpression(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360
Douglas Gregor200b2922010-09-17 22:25:06 +0000361 if (RHS.isInvalid())
362 LHS = ExprError();
363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // Remember the precedence of this operator and get the precedence of the
365 // operator immediately to the right of the RHS.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000366 prec::Level ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000367 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
368 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369
370 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000371 bool isRightAssoc = ThisPrec == prec::Conditional ||
372 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
374 // Get the precedence of the operator to the right of the RHS. If it binds
375 // more tightly with RHS than we do, evaluate it completely first.
376 if (ThisPrec < NextTokPrec ||
377 (ThisPrec == NextTokPrec && isRightAssoc)) {
378 // If this is left-associative, only parse things on the RHS that bind
379 // more tightly than the current operator. If it is left-associative, it
380 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
381 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000382 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000383 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000384 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Douglas Gregor200b2922010-09-17 22:25:06 +0000385
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000386 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000387 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000388
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000389 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
390 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 }
392 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000393
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000394 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000395 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000396 if (TernaryMiddle.isInvalid()) {
397 // If we're using '>>' as an operator within a template
398 // argument list (in C++98), suggest the addition of
399 // parentheses so that the code remains well-formed in C++0x.
400 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
401 SuggestParentheses(OpToken.getLocation(),
402 diag::warn_cxx0x_right_shift_in_template_arg,
403 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
404 Actions.getExprRange(RHS.get()).getEnd()));
405
Douglas Gregor23c94db2010-07-02 17:43:08 +0000406 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000407 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000408 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000409 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000410 LHS.take(), TernaryMiddle.take(),
411 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000412 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 }
414}
415
416/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000417/// true, parse a unary-expression. isAddressOfOperand exists because an
418/// id-expression that is the operand of address-of gets special treatment
419/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000420///
John McCall60d7b3a2010-08-24 06:29:42 +0000421ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000422 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000423 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000424 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000425 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000426 isAddressOfOperand,
427 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000428 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000429 if (NotCastExpr)
430 Diag(Tok, diag::err_expected_expression);
431 return move(Res);
432}
433
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000434namespace {
435class CastExpressionIdValidator : public CorrectionCandidateCallback {
436 public:
437 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
438 : AllowNonTypes(AllowNonTypes) {
439 WantTypeSpecifiers = AllowTypes;
440 }
441
442 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
443 NamedDecl *ND = candidate.getCorrectionDecl();
444 if (!ND)
445 return candidate.isKeyword();
446
447 if (isa<TypeDecl>(ND))
448 return WantTypeSpecifiers;
449 return AllowNonTypes;
450 }
451
452 private:
453 bool AllowNonTypes;
454};
455}
456
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000457/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
458/// true, parse a unary-expression. isAddressOfOperand exists because an
459/// id-expression that is the operand of address-of gets special treatment
460/// due to member pointers. NotCastExpr is set to true if the token is not the
461/// start of a cast-expression, and no diagnostic is emitted in this case.
462///
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// cast-expression: [C99 6.5.4]
464/// unary-expression
465/// '(' type-name ')' cast-expression
466///
467/// unary-expression: [C99 6.5.3]
468/// postfix-expression
469/// '++' unary-expression
470/// '--' unary-expression
471/// unary-operator cast-expression
472/// 'sizeof' unary-expression
473/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +0000474/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000475/// [GNU] '__alignof' unary-expression
476/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000477/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000478/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000479/// [C++] new-expression
480/// [C++] delete-expression
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000481/// [C++0x] 'noexcept' '(' expression ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000482///
483/// unary-operator: one of
484/// '&' '*' '+' '-' '~' '!'
485/// [GNU] '__extension__' '__real' '__imag'
486///
487/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000488/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000489/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000490/// constant
491/// string-literal
492/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000493/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000494/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000495/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000496/// '__func__' [C99 6.4.2.2]
497/// [GNU] '__FUNCTION__'
498/// [GNU] '__PRETTY_FUNCTION__'
499/// [GNU] '(' compound-statement ')'
500/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
501/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
502/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
503/// assign-expr ')'
504/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000505/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000506/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000507/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000508/// [OBJC] '@protocol' '(' identifier ')'
509/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000510/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000511/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000512/// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000513/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000514/// [C++0x] typename-specifier braced-init-list [C++ 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
516/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
517/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
518/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000519/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
520/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000521/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000522/// [G++] unary-type-trait '(' type-id ')'
523/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000524/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000525/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000526///
527/// constant: [C99 6.4.4]
528/// integer-constant
529/// floating-constant
530/// enumeration-constant -> identifier
531/// character-constant
532///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000533/// id-expression: [C++ 5.1]
534/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000535/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000536///
537/// unqualified-id: [C++ 5.1]
538/// identifier
539/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000540/// conversion-function-id
541/// '~' class-name
542/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000543///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000544/// new-expression: [C++ 5.3.4]
545/// '::'[opt] 'new' new-placement[opt] new-type-id
546/// new-initializer[opt]
547/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
548/// new-initializer[opt]
549///
550/// delete-expression: [C++ 5.3.5]
551/// '::'[opt] 'delete' cast-expression
552/// '::'[opt] 'delete' '[' ']' cast-expression
553///
John Wiegley20c0da72011-04-27 23:09:49 +0000554/// [GNU/Embarcadero] unary-type-trait:
555/// '__is_arithmetic'
556/// '__is_floating_point'
557/// '__is_integral'
558/// '__is_lvalue_expr'
559/// '__is_rvalue_expr'
560/// '__is_complete_type'
561/// '__is_void'
562/// '__is_array'
563/// '__is_function'
564/// '__is_reference'
565/// '__is_lvalue_reference'
566/// '__is_rvalue_reference'
567/// '__is_fundamental'
568/// '__is_object'
569/// '__is_scalar'
570/// '__is_compound'
571/// '__is_pointer'
572/// '__is_member_object_pointer'
573/// '__is_member_function_pointer'
574/// '__is_member_pointer'
575/// '__is_const'
576/// '__is_volatile'
577/// '__is_trivial'
578/// '__is_standard_layout'
579/// '__is_signed'
580/// '__is_unsigned'
581///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000582/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000583/// '__has_nothrow_assign'
584/// '__has_nothrow_copy'
585/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000586/// '__has_trivial_assign' [TODO]
587/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000588/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000589/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000590/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000591/// '__is_abstract' [TODO]
592/// '__is_class'
593/// '__is_empty' [TODO]
594/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000595/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000596/// '__is_pod'
597/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000598/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000599/// '__is_union'
600///
Sean Huntfeb375d2011-05-13 00:31:07 +0000601/// [Clang] unary-type-trait:
602/// '__trivially_copyable'
603///
Douglas Gregor9f361132011-01-27 20:28:01 +0000604/// binary-type-trait:
605/// [GNU] '__is_base_of'
606/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000607/// '__is_convertible'
608/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000609///
John Wiegley21ff2e52011-04-28 00:16:57 +0000610/// [Embarcadero] array-type-trait:
611/// '__array_rank'
612/// '__array_extent'
613///
John Wiegley55262202011-04-25 06:54:41 +0000614/// [Embarcadero] expression-trait:
615/// '__is_lvalue_expr'
616/// '__is_rvalue_expr'
617///
John McCall60d7b3a2010-08-24 06:29:42 +0000618ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000619 bool isAddressOfOperand,
620 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000621 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000622 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000624 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // This handles all of cast-expression, unary-expression, postfix-expression,
627 // and primary-expression. We handle them together like this for efficiency
628 // and to simplify handling of an expression starting with a '(' token: which
629 // may be one of a parenthesized expression, cast-expression, compound literal
630 // expression, or statement expression.
631 //
632 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000633 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
634 // to handle the postfix expression suffixes. Cases that cannot be followed
635 // by postfix exprs should return without invoking
636 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 switch (SavedKind) {
638 case tok::l_paren: {
639 // If this expression is limited to being a unary-expression, the parent can
640 // not start a cast expression.
641 ParenParseOption ParenExprType =
Douglas Gregord4206632010-08-06 14:50:36 +0000642 (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000643 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000645
646 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000647 // The inside of the parens don't need to be a colon protected scope, and
648 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000649 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000650
Chris Lattner932dff72009-12-10 02:08:07 +0000651 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000652 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 switch (ParenExprType) {
656 case SimpleExpr: break; // Nothing else to do.
657 case CompoundStmt: break; // Nothing else to do.
658 case CompoundLiteral:
659 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
660 // postfix-expression exist, parse them now.
661 break;
662 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000663 // We have parsed the cast-expression and no postfix-expr pieces are
664 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000665 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667
John McCall9ae2f072010-08-23 23:25:46 +0000668 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // primary-expression
672 case tok::numeric_constant:
673 // constant: integer-constant
674 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000675
Steve Narofff69936d2007-09-16 03:34:24 +0000676 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000678 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679
680 case tok::kw_true:
681 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000682 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000683
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000684 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000685 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000686 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
687
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000688 case tok::annot_primary_expr:
689 assert(Res.get() == 0 && "Stray primary-expression annotation?");
690 Res = getExprAnnotation(Tok);
691 ConsumeToken();
692 break;
693
David Blaikie42d6d0c2011-12-04 05:04:18 +0000694 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000695 case tok::identifier: { // primary-expression: identifier
696 // unqualified-id: identifier
697 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000698 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000699 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000700 if (getLang().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000701 // Avoid the unnecessary parse-time lookup in the common case
702 // where the syntax forbids a type.
703 const Token &Next = NextToken();
704 if (Next.is(tok::coloncolon) ||
705 (!ColonIsSacred && Next.is(tok::colon)) ||
706 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000707 Next.is(tok::l_paren) ||
708 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000709 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
710 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000711 return ExprError();
712 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000713 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
714 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000715 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000717 // Consume the identifier so that we can see if it is followed by a '(' or
718 // '.'.
719 IdentifierInfo &II = *Tok.getIdentifierInfo();
720 SourceLocation ILoc = ConsumeToken();
721
Chris Lattnereb483eb2010-04-11 08:28:14 +0000722 // Support 'Class.property' and 'super.property' notation.
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000723 if (getLang().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000724 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000725 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000726 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000727 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000728
Steve Naroff61f72cb2009-03-09 21:12:44 +0000729 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000730 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000731 return ExprError();
732 }
733 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
734 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000735
736 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
737 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000738 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000739 }
John McCall9c72c602010-08-27 09:08:28 +0000740
Douglas Gregorfa885c12010-09-15 15:09:43 +0000741 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000742 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000743 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000744 // bracket. Treat it as such.
745 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000746 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000747 ((Tok.is(tok::identifier) &&
748 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
749 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000750 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
751 0);
752 break;
753 }
754
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000755 // If we have an Objective-C class name followed by an identifier
756 // and either ':' or ']', this is an Objective-C class message
757 // send that's missing the opening '['. Recovery
758 // appropriately. Also take this path if we're performing code
759 // completion after an Objective-C class name.
760 if (getLang().ObjC1 &&
761 ((Tok.is(tok::identifier) && !InMessageExpression) ||
762 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000763 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000764 if (Tok.is(tok::code_completion) ||
765 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000766 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
767 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000768 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000769 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000770 DS.SetRangeStart(ILoc);
771 DS.SetRangeEnd(ILoc);
772 const char *PrevSpec = 0;
773 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000774 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000775
776 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
777 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
778 DeclaratorInfo);
779 if (Ty.isInvalid())
780 break;
781
782 Res = ParseObjCMessageExpressionBody(SourceLocation(),
783 SourceLocation(),
784 Ty.get(), 0);
785 break;
786 }
787 }
788
John McCall9c72c602010-08-27 09:08:28 +0000789 // Make sure to pass down the right value for isAddressOfOperand.
790 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
791 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
794 // need to know whether or not this identifier is a function designator or
795 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000796 UnqualifiedId Name;
797 CXXScopeSpec ScopeSpec;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000798 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
799 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000800 Name.setIdentifier(&II, ILoc);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000801 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, Name,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000802 Tok.is(tok::l_paren), isAddressOfOperand,
803 &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000804 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 }
806 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000807 case tok::wide_char_constant:
808 case tok::utf16_char_constant:
809 case tok::utf32_char_constant:
Steve Narofff69936d2007-09-16 03:34:24 +0000810 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000812 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
814 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
815 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000816 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000818 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 case tok::string_literal: // primary-expression: string-literal
820 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000821 case tok::utf8_string_literal:
822 case tok::utf16_string_literal:
823 case tok::utf32_string_literal:
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 Res = ParseStringLiteralExpression();
John McCall9ae2f072010-08-23 23:25:46 +0000825 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000826 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000827 Res = ParseGenericSelectionExpression();
828 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 case tok::kw___builtin_va_arg:
830 case tok::kw___builtin_offsetof:
831 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000832 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000833 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000834 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000835 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000836
Douglas Gregord4206632010-08-06 14:50:36 +0000837 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
838 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
839 // C++ [expr.unary] has:
840 // unary-expression:
841 // ++ cast-expression
842 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregord4206632010-08-06 14:50:36 +0000844 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000845 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000846 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000847 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000849 case tok::amp: { // unary-expression: '&' cast-expression
850 // Special treatment because of member pointers
851 SourceLocation SavedLoc = ConsumeToken();
852 Res = ParseCastExpression(false, true);
853 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000854 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000855 return move(Res);
856 }
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 case tok::star: // unary-expression: '*' cast-expression
859 case tok::plus: // unary-expression: '+' cast-expression
860 case tok::minus: // unary-expression: '-' cast-expression
861 case tok::tilde: // unary-expression: '~' cast-expression
862 case tok::exclaim: // unary-expression: '!' cast-expression
863 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000864 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SourceLocation SavedLoc = ConsumeToken();
866 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000867 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000868 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000869 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000870 }
871
Chris Lattner35080842008-02-02 20:20:10 +0000872 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
873 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000874 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000875 SourceLocation SavedLoc = ConsumeToken();
876 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000877 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000878 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000879 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 }
881 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
882 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000883 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
885 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000886 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000887 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
888 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 case tok::ampamp: { // unary-expression: '&&' identifier
890 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000891 if (Tok.isNot(tok::identifier))
892 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000893
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000894 if (getCurScope()->getFnParent() == 0)
895 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000898 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
899 Tok.getLocation());
900 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000902 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 }
904 case tok::kw_const_cast:
905 case tok::kw_dynamic_cast:
906 case tok::kw_reinterpret_cast:
907 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000908 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000909 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000910 case tok::kw_typeid:
911 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000912 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000913 case tok::kw___uuidof:
914 Res = ParseCXXUuidof();
915 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000916 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000917 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000918 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000919
Douglas Gregor9497a732010-09-16 01:51:54 +0000920 case tok::annot_typename:
921 if (isStartOfObjCClassMessageMissingOpenBracket()) {
922 ParsedType Type = getTypeAnnotation(Tok);
923
924 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000925 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000926 DS.SetRangeStart(Tok.getLocation());
927 DS.SetRangeEnd(Tok.getLastLoc());
928
929 const char *PrevSpec = 0;
930 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000931 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
932 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000933
934 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
935 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
936 if (Ty.isInvalid())
937 break;
938
939 ConsumeToken();
940 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
941 Ty.get(), 0);
942 break;
943 }
944 // Fall through
945
David Blaikie5e089fe2012-01-24 05:47:35 +0000946 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000947 case tok::kw_char:
948 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000949 case tok::kw_char16_t:
950 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000951 case tok::kw_bool:
952 case tok::kw_short:
953 case tok::kw_int:
954 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000955 case tok::kw___int64:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000956 case tok::kw_signed:
957 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000958 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 case tok::kw_float:
960 case tok::kw_double:
961 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000962 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000963 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +0000964 case tok::kw___vector: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000965 if (!getLang().CPlusPlus) {
966 Diag(Tok, diag::err_expected_expression);
967 return ExprError();
968 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000969
970 if (SavedKind == tok::kw_typename) {
971 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000972 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +0000973 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000974 return ExprError();
975 }
976
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000977 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000978 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000979 //
John McCall0b7e6782011-03-24 11:26:52 +0000980 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000981 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000982 if (Tok.isNot(tok::l_paren) &&
983 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000984 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
985 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000986
Richard Smith7fe62082011-10-15 05:09:34 +0000987 if (Tok.is(tok::l_brace))
988 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
989
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000990 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +0000991 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000992 }
993
Douglas Gregorae4c77d2010-02-05 19:11:37 +0000994 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +0000995 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
996 // (We can end up in this situation after tentative parsing.)
997 if (TryAnnotateTypeOrScopeToken())
998 return ExprError();
999 if (!Tok.is(tok::annot_cxxscope))
1000 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001001 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001002
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001003 Token Next = NextToken();
1004 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001005 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001006 if (TemplateId->Kind == TNK_Type_template) {
1007 // We have a qualified template-id that we know refers to a
1008 // type, translate it into a type and continue parsing as a
1009 // cast expression.
1010 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001011 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1012 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001013 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001014 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001015 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001016 }
1017 }
1018
1019 // Parse as an id-expression.
1020 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001021 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001022 }
1023
1024 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001025 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001026 if (TemplateId->Kind == TNK_Type_template) {
1027 // We have a template-id that we know refers to a type,
1028 // translate it into a type and continue parsing as a cast
1029 // expression.
1030 AnnotateTemplateIdTokenAsType();
1031 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001032 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001033 }
1034
1035 // Fall through to treat the template-id as an id-expression.
1036 }
1037
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001038 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001039 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001040 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001041
Chris Lattner74ba4102009-01-04 22:52:14 +00001042 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001043 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1044 // annotates the token, tail recurse.
1045 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001046 return ExprError();
1047 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001048 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1049
Chris Lattner74ba4102009-01-04 22:52:14 +00001050 // ::new -> [C++] new-expression
1051 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001052 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001053 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001054 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001055 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001056 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001058 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001059 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001060 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001061 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001062
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001063 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001064 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001065
1066 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001067 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001068
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001069 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001070 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001071 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001072 BalancedDelimiterTracker T(*this, tok::l_paren);
1073
1074 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001075 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001076 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001077 // The noexcept operator determines whether the evaluation of its operand,
1078 // which is an unevaluated operand, can throw an exception.
1079 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001080 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001081
1082 T.consumeClose();
1083
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001084 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001085 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1086 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001087 return move(Result);
1088 }
1089
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001090 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001091 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001092 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001093 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001094 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001095 case tok::kw___is_arithmetic:
1096 case tok::kw___is_integral:
1097 case tok::kw___is_floating_point:
1098 case tok::kw___is_complete_type:
1099 case tok::kw___is_void:
1100 case tok::kw___is_array:
1101 case tok::kw___is_function:
1102 case tok::kw___is_reference:
1103 case tok::kw___is_lvalue_reference:
1104 case tok::kw___is_rvalue_reference:
1105 case tok::kw___is_fundamental:
1106 case tok::kw___is_object:
1107 case tok::kw___is_scalar:
1108 case tok::kw___is_compound:
1109 case tok::kw___is_pointer:
1110 case tok::kw___is_member_object_pointer:
1111 case tok::kw___is_member_function_pointer:
1112 case tok::kw___is_member_pointer:
1113 case tok::kw___is_const:
1114 case tok::kw___is_volatile:
1115 case tok::kw___is_standard_layout:
1116 case tok::kw___is_signed:
1117 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001118 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001119 case tok::kw___is_pod:
1120 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001121 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001122 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001123 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001124 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001125 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001126 case tok::kw___has_trivial_copy:
1127 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001128 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001129 case tok::kw___has_nothrow_assign:
1130 case tok::kw___has_nothrow_copy:
1131 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001132 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001133 return ParseUnaryTypeTrait();
1134
Francois Pichetf1872372010-12-08 22:35:30 +00001135 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001136 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001137 case tok::kw___is_same:
1138 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001139 case tok::kw___is_convertible_to:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001140 return ParseBinaryTypeTrait();
1141
John Wiegley21ff2e52011-04-28 00:16:57 +00001142 case tok::kw___array_rank:
1143 case tok::kw___array_extent:
1144 return ParseArrayTypeTrait();
1145
John Wiegley55262202011-04-25 06:54:41 +00001146 case tok::kw___is_lvalue_expr:
1147 case tok::kw___is_rvalue_expr:
1148 return ParseExpressionTrait();
1149
Chris Lattnerc97c2042007-10-03 22:03:06 +00001150 case tok::at: {
1151 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001152 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001153 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001154 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001155 Res = ParseBlockLiteralExpression();
1156 break;
1157 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001158 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001159 cutOffParsing();
1160 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001161 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001162 case tok::l_square:
Douglas Gregorae7902c2011-08-04 15:30:47 +00001163 if (getLang().CPlusPlus0x) {
1164 if (getLang().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001165 // C++11 lambda expressions and Objective-C message sends both start with a
1166 // square bracket. There are three possibilities here:
1167 // we have a valid lambda expression, we have an invalid lambda
1168 // expression, or we have something that doesn't appear to be a lambda.
1169 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001170 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001171 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001172 Res = ParseObjCMessageExpression();
1173 break;
1174 }
1175 Res = ParseLambdaExpression();
1176 break;
1177 }
Chandler Carruthbb399022011-07-08 04:28:55 +00001178 if (getLang().ObjC1) {
1179 Res = ParseObjCMessageExpression();
1180 break;
1181 }
1182 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001184 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001185 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001187
John McCall9ae2f072010-08-23 23:25:46 +00001188 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001189 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190}
1191
1192/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1193/// is parsed, this method parses any suffixes that apply.
1194///
1195/// postfix-expression: [C99 6.5.2]
1196/// primary-expression
1197/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001198/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001199/// postfix-expression '(' argument-expression-list[opt] ')'
1200/// postfix-expression '.' identifier
1201/// postfix-expression '->' identifier
1202/// postfix-expression '++'
1203/// postfix-expression '--'
1204/// '(' type-name ')' '{' initializer-list '}'
1205/// '(' type-name ')' '{' initializer-list ',' '}'
1206///
1207/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001208/// argument-expression ...[opt]
1209/// argument-expression-list ',' assignment-expression ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001210///
John McCall60d7b3a2010-08-24 06:29:42 +00001211ExprResult
1212Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 // Now that the primary-expression piece of the postfix-expression has been
1214 // parsed, see if there are any postfix-expression pieces here.
1215 SourceLocation Loc;
1216 while (1) {
1217 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001218 case tok::code_completion:
1219 if (InMessageExpression)
1220 return move(LHS);
1221
Douglas Gregorac5fd842010-09-18 01:28:11 +00001222 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001223 cutOffParsing();
1224 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001225
Douglas Gregor0fbda682010-09-15 14:51:05 +00001226 case tok::identifier:
1227 // If we see identifier: after an expression, and we're not already in a
1228 // message send, then this is probably a message send with a missing
1229 // opening bracket '['.
1230 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001231 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001232 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1233 ParsedType(), LHS.get());
1234 break;
1235 }
1236
1237 // Fall through; this isn't a message send.
1238
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001240 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001242 // If we have a array postfix expression that starts on a new line and
1243 // Objective-C is enabled, it is highly likely that the user forgot a
1244 // semicolon after the base expression and that the array postfix-expr is
1245 // actually another message send. In this case, do some look-ahead to see
1246 // if the contents of the square brackets are obviously not a valid
1247 // expression and recover by pretending there is no suffix.
1248 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1249 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001250 return move(LHS);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001251
1252 BalancedDelimiterTracker T(*this, tok::l_square);
1253 T.consumeOpen();
1254 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001255 ExprResult Idx;
Richard Smith7fe62082011-10-15 05:09:34 +00001256 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1257 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001258 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001259 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001260 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001261
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001263
1264 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001265 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1266 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001267 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001268 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001269
1270 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001271 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 break;
1273 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001274
Peter Collingbournebf36e252011-02-09 21:12:02 +00001275 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1276 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1277 // '(' argument-expression-list[opt] ')'
1278 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001279 InMessageExpressionRAIIObject InMessage(*this, false);
1280
Peter Collingbournebf36e252011-02-09 21:12:02 +00001281 Expr *ExecConfig = 0;
1282
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001283 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1284 BalancedDelimiterTracker PT(*this, tok::l_paren);
1285
Peter Collingbournebf36e252011-02-09 21:12:02 +00001286 if (OpKind == tok::lesslessless) {
1287 ExprVector ExecConfigExprs(Actions);
1288 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001289 LLLT.consumeOpen();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001290
1291 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1292 LHS = ExprError();
1293 }
1294
1295 if (LHS.isInvalid()) {
1296 SkipUntil(tok::greatergreatergreater);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001297 } else if (LLLT.consumeClose()) {
1298 // There was an error closing the brackets
Peter Collingbournebf36e252011-02-09 21:12:02 +00001299 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001300 }
1301
1302 if (!LHS.isInvalid()) {
1303 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1304 LHS = ExprError();
1305 else
1306 Loc = PrevTokLocation;
1307 }
1308
1309 if (!LHS.isInvalid()) {
1310 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001311 LLLT.getOpenLocation(),
1312 move_arg(ExecConfigExprs),
1313 LLLT.getCloseLocation());
Peter Collingbournebf36e252011-02-09 21:12:02 +00001314 if (ECResult.isInvalid())
1315 LHS = ExprError();
1316 else
1317 ExecConfig = ECResult.get();
1318 }
1319 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001320 PT.consumeOpen();
1321 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001322 }
1323
Sebastian Redla55e52c2008-11-25 22:21:31 +00001324 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001325 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001326
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001327 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 Actions.CodeCompleteCall(getCurScope(), LHS.get(), 0, 0);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001329 cutOffParsing();
1330 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001331 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001332
1333 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1334 if (Tok.isNot(tok::r_paren)) {
1335 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1336 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001337 LHS = ExprError();
1338 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 }
1340 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001341
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001343 if (LHS.isInvalid()) {
1344 SkipUntil(tok::r_paren);
1345 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001346 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001347 LHS = ExprError();
1348 } else {
1349 assert((ArgExprs.size() == 0 ||
1350 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001352 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001353 move_arg(ArgExprs), Tok.getLocation(),
1354 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001355 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 break;
1359 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001360 case tok::arrow:
1361 case tok::period: {
1362 // postfix-expression: p-e '->' template[opt] id-expression
1363 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 tok::TokenKind OpKind = Tok.getKind();
1365 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001366
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001367 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001368 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001369 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001370 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001371 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001372 OpLoc, OpKind, ObjectType,
1373 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001374 if (LHS.isInvalid())
1375 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001376
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001377 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1378 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001379 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001380 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001381 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001382 }
1383
Douglas Gregor81b747b2009-09-17 21:32:03 +00001384 if (Tok.is(tok::code_completion)) {
1385 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001386 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001387 OpLoc, OpKind == tok::arrow);
1388
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001389 cutOffParsing();
1390 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001391 }
1392
John McCall9ae2f072010-08-23 23:25:46 +00001393 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1394 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001395 ObjectType);
1396 break;
1397 }
1398
1399 // Either the action has told is that this cannot be a
1400 // pseudo-destructor expression (based on the type of base
1401 // expression), or we didn't see a '~' in the right place. We
1402 // can still parse a destructor name here, but in that case it
1403 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001404 // Allow explicit constructor calls in Microsoft mode.
1405 // FIXME: Add support for explicit call of template constructor.
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001406 UnqualifiedId Name;
1407 if (ParseUnqualifiedId(SS,
1408 /*EnteringContext=*/false,
1409 /*AllowDestructorName=*/true,
Francois Pichet62ec1f22011-09-17 17:15:52 +00001410 /*AllowConstructorName=*/ getLang().MicrosoftExt,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001411 ObjectType,
1412 Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001413 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001414
1415 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001416 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Douglas Gregord4dca082010-02-24 18:44:31 +00001417 OpKind, SS, Name, ObjCImpDecl,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001418 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 break;
1420 }
1421 case tok::plusplus: // postfix-expression: postfix-expression '++'
1422 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001423 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001424 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001425 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001426 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 ConsumeToken();
1428 break;
1429 }
1430 }
1431}
1432
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001433/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1434/// vec_step and we are at the start of an expression or a parenthesized
1435/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1436/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001437///
1438/// unary-expression: [C99 6.5.3]
1439/// 'sizeof' unary-expression
1440/// 'sizeof' '(' type-name ')'
1441/// [GNU] '__alignof' unary-expression
1442/// [GNU] '__alignof' '(' type-name ')'
1443/// [C++0x] 'alignof' '(' type-id ')'
1444///
1445/// [GNU] typeof-specifier:
1446/// typeof ( expressions )
1447/// typeof ( type-name )
1448/// [GNU/C++] typeof unary-expression
1449///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001450/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1451/// vec_step ( expressions )
1452/// vec_step ( type-name )
1453///
John McCall60d7b3a2010-08-24 06:29:42 +00001454ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001455Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1456 bool &isCastExpr,
1457 ParsedType &CastTy,
1458 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001459
1460 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001461 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1462 OpTok.is(tok::kw_vec_step)) &&
1463 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001464
John McCall60d7b3a2010-08-24 06:29:42 +00001465 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001467 // If the operand doesn't start with an '(', it must be an expression.
1468 if (Tok.isNot(tok::l_paren)) {
1469 isCastExpr = false;
1470 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1471 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1472 return ExprError();
1473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001475 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001476 } else {
1477 // If it starts with a '(', we know that it is either a parenthesized
1478 // type-name, or it is a unary-expression that starts with a compound
1479 // literal, or starts with a primary-expression that is a parenthesized
1480 // expression.
1481 ParenParseOption ExprType = CastExpr;
1482 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001484 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001485 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001486 CastRange = SourceRange(LParenLoc, RParenLoc);
1487
1488 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1489 // a type.
1490 if (ExprType == CastExpr) {
1491 isCastExpr = true;
1492 return ExprEmpty();
1493 }
1494
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001495 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1496 // GNU typeof in C requires the expression to be parenthesized. Not so for
1497 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1498 // the start of a unary-expression, but doesn't include any postfix
1499 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001500 if (!Operand.isInvalid())
1501 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001502 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001503 }
1504
1505 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1506 isCastExpr = false;
1507 return move(Operand);
1508}
1509
Reid Spencer5f016e22007-07-11 17:01:13 +00001510
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001511/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001512/// unary-expression: [C99 6.5.3]
1513/// 'sizeof' unary-expression
1514/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001515/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001516/// [GNU] '__alignof' unary-expression
1517/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001518/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001519ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001520 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001521 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1522 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001523 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregoree8aff02011-01-04 17:33:58 +00001526 // [C++0x] 'sizeof' '...' '(' identifier ')'
1527 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1528 SourceLocation EllipsisLoc = ConsumeToken();
1529 SourceLocation LParenLoc, RParenLoc;
1530 IdentifierInfo *Name = 0;
1531 SourceLocation NameLoc;
1532 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001533 BalancedDelimiterTracker T(*this, tok::l_paren);
1534 T.consumeOpen();
1535 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001536 if (Tok.is(tok::identifier)) {
1537 Name = Tok.getIdentifierInfo();
1538 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001539 T.consumeClose();
1540 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001541 if (RParenLoc.isInvalid())
1542 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1543 } else {
1544 Diag(Tok, diag::err_expected_parameter_pack);
1545 SkipUntil(tok::r_paren);
1546 }
1547 } else if (Tok.is(tok::identifier)) {
1548 Name = Tok.getIdentifierInfo();
1549 NameLoc = ConsumeToken();
1550 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1551 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1552 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1553 << Name
1554 << FixItHint::CreateInsertion(LParenLoc, "(")
1555 << FixItHint::CreateInsertion(RParenLoc, ")");
1556 } else {
1557 Diag(Tok, diag::err_sizeof_parameter_pack);
1558 }
1559
1560 if (!Name)
1561 return ExprError();
1562
1563 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1564 OpTok.getLocation(),
1565 *Name, NameLoc,
1566 RParenLoc);
1567 }
Richard Smith841804b2011-10-17 23:06:20 +00001568
1569 if (OpTok.is(tok::kw_alignof))
1570 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1571
Eli Friedman71b8fb52012-01-21 01:01:51 +00001572 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1573
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001574 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001575 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001576 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001577 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1578 isCastExpr,
1579 CastTy,
1580 CastRange);
1581
1582 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1583 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1584 ExprKind = UETT_AlignOf;
1585 else if (OpTok.is(tok::kw_vec_step))
1586 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001587
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001588 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001589 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1590 ExprKind,
1591 /*isType=*/true,
1592 CastTy.getAsOpaquePtr(),
1593 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001594
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001596 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001597 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1598 ExprKind,
1599 /*isType=*/false,
1600 Operand.release(),
1601 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001602 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001603}
1604
1605/// ParseBuiltinPrimaryExpression
1606///
1607/// primary-expression: [C99 6.5.1]
1608/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1609/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1610/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1611/// assign-expr ')'
1612/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001613/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001614///
Reid Spencer5f016e22007-07-11 17:01:13 +00001615/// [GNU] offsetof-member-designator:
1616/// [GNU] identifier
1617/// [GNU] offsetof-member-designator '.' identifier
1618/// [GNU] offsetof-member-designator '[' expression ']'
1619///
John McCall60d7b3a2010-08-24 06:29:42 +00001620ExprResult Parser::ParseBuiltinPrimaryExpression() {
1621 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1623
1624 tok::TokenKind T = Tok.getKind();
1625 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1626
1627 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001628 if (Tok.isNot(tok::l_paren))
1629 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1630 << BuiltinII);
1631
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001632 BalancedDelimiterTracker PT(*this, tok::l_paren);
1633 PT.consumeOpen();
1634
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 // TODO: Build AST.
1636
1637 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001638 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001639 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001640 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001641
1642 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001643 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001644
Douglas Gregor809070a2009-02-18 17:45:20 +00001645 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001646
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001647 if (Tok.isNot(tok::r_paren)) {
1648 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001649 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001650 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001651
1652 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001653 Res = ExprError();
1654 else
John McCall9ae2f072010-08-23 23:25:46 +00001655 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001657 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001658 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001659 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001660 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001661 if (Ty.isInvalid()) {
1662 SkipUntil(tok::r_paren);
1663 return ExprError();
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001667 return ExprError();
1668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001670 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001671 Diag(Tok, diag::err_expected_ident);
1672 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001673 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001674 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001675
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001676 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001677 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001678
John McCallf312b1e2010-08-26 23:41:50 +00001679 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001680 Comps.back().isBrackets = false;
1681 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1682 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001683
Sebastian Redla55e52c2008-11-25 22:21:31 +00001684 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001686 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001688 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001689 Comps.back().isBrackets = false;
1690 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001691
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001692 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001693 Diag(Tok, diag::err_expected_ident);
1694 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001695 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001696 }
1697 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1698 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001699
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001700 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001702 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001703 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001704 BalancedDelimiterTracker ST(*this, tok::l_square);
1705 ST.consumeOpen();
1706 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001708 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001710 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001712 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001713
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001714 ST.consumeClose();
1715 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001716 } else {
1717 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001718 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001719 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001720 } else if (Ty.isInvalid()) {
1721 Res = ExprError();
1722 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001723 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001724 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001725 Ty.get(), &Comps[0], Comps.size(),
1726 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001727 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001728 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001729 }
1730 }
1731 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001732 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001733 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001734 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001735 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001736 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001737 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001738 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001740 return ExprError();
1741
John McCall60d7b3a2010-08-24 06:29:42 +00001742 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001743 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001744 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001745 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001746 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001748 return ExprError();
1749
John McCall60d7b3a2010-08-24 06:29:42 +00001750 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001751 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001752 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001753 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001754 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001755 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001756 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001757 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001758 }
John McCall9ae2f072010-08-23 23:25:46 +00001759 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1760 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001761 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001762 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001763 case tok::kw___builtin_astype: {
1764 // The first argument is an expression to be converted, followed by a comma.
1765 ExprResult Expr(ParseAssignmentExpression());
1766 if (Expr.isInvalid()) {
1767 SkipUntil(tok::r_paren);
1768 return ExprError();
1769 }
1770
1771 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1772 tok::r_paren))
1773 return ExprError();
1774
1775 // Second argument is the type to bitcast to.
1776 TypeResult DestTy = ParseTypeName();
1777 if (DestTy.isInvalid())
1778 return ExprError();
1779
1780 // Attempt to consume the r-paren.
1781 if (Tok.isNot(tok::r_paren)) {
1782 Diag(Tok, diag::err_expected_rparen);
1783 SkipUntil(tok::r_paren);
1784 return ExprError();
1785 }
1786
1787 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1788 ConsumeParen());
1789 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001790 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001791 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001792
John McCall9ae2f072010-08-23 23:25:46 +00001793 if (Res.isInvalid())
1794 return ExprError();
1795
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 // These can be followed by postfix-expr pieces because they are
1797 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001798 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001799}
1800
1801/// ParseParenExpression - This parses the unit that starts with a '(' token,
1802/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001803/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1804/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001805///
1806/// primary-expression: [C99 6.5.1]
1807/// '(' expression ')'
1808/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1809/// postfix-expression: [C99 6.5.2]
1810/// '(' type-name ')' '{' initializer-list '}'
1811/// '(' type-name ')' '{' initializer-list ',' '}'
1812/// cast-expression: [C99 6.5.4]
1813/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001814/// [ARC] bridged-cast-expression
1815///
1816/// [ARC] bridged-cast-expression:
1817/// (__bridge type-name) cast-expression
1818/// (__bridge_transfer type-name) cast-expression
1819/// (__bridge_retained type-name) cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001820ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001821Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001822 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001823 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001824 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001825 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001826 BalancedDelimiterTracker T(*this, tok::l_paren);
1827 if (T.consumeOpen())
1828 return ExprError();
1829 SourceLocation OpenLoc = T.getOpenLocation();
1830
John McCall60d7b3a2010-08-24 06:29:42 +00001831 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001832 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001833 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001834
Douglas Gregor02688102010-09-14 23:59:36 +00001835 if (Tok.is(tok::code_completion)) {
1836 Actions.CodeCompleteOrdinaryName(getCurScope(),
1837 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1838 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001839 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001840 return ExprError();
1841 }
John McCallb3c49062011-04-06 02:35:25 +00001842
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001843 // Diagnose use of bridge casts in non-arc mode.
1844 bool BridgeCast = (getLang().ObjC2 &&
1845 (Tok.is(tok::kw___bridge) ||
1846 Tok.is(tok::kw___bridge_transfer) ||
1847 Tok.is(tok::kw___bridge_retained) ||
1848 Tok.is(tok::kw___bridge_retain)));
1849 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001850 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001851 SourceLocation BridgeKeywordLoc = ConsumeToken();
1852 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001853 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_nonarc)
1854 << BridgeCastName
1855 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001856 BridgeCast = false;
1857 }
1858
John McCallb3c49062011-04-06 02:35:25 +00001859 // None of these cases should fall through with an invalid Result
1860 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001861 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall0b7e6782011-03-24 11:26:52 +00001863 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001864 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001866
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001867 // If the substmt parsed correctly, build the AST node.
John McCallb3c49062011-04-06 02:35:25 +00001868 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001869 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001870 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001871 tok::TokenKind tokenKind = Tok.getKind();
1872 SourceLocation BridgeKeywordLoc = ConsumeToken();
1873
John McCallf85e1932011-06-15 23:02:42 +00001874 // Parse an Objective-C ARC ownership cast expression.
1875 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001876 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001877 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001878 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001879 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001880 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001881 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001882 else {
1883 // As a hopefully temporary workaround, allow __bridge_retain as
1884 // a synonym for __bridge_retained, but only in system headers.
1885 assert(tokenKind == tok::kw___bridge_retain);
1886 Kind = OBC_BridgeRetained;
1887 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1888 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1889 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1890 "__bridge_retained");
1891 }
John McCallf85e1932011-06-15 23:02:42 +00001892
John McCallf85e1932011-06-15 23:02:42 +00001893 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001894 T.consumeClose();
1895 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001896 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001897
1898 if (Ty.isInvalid() || SubExpr.isInvalid())
1899 return ExprError();
1900
1901 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1902 BridgeKeywordLoc, Ty.get(),
1903 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001904 } else if (ExprType >= CompoundLiteral &&
1905 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001909 // In C++, if the type-id is ambiguous we disambiguate based on context.
1910 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1911 // in which case we should treat it as type-id.
1912 // if stopIfCastExpr is false, we need to determine the context past the
1913 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001914 if (isAmbiguousTypeId && !stopIfCastExpr) {
1915 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1916 RParenLoc = T.getCloseLocation();
1917 return res;
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001920 // Parse the type declarator.
1921 DeclSpec DS(AttrFactory);
1922 ParseSpecifierQualifierList(DS);
1923 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1924 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00001925
Douglas Gregor77328d12010-09-15 23:19:31 +00001926 // If our type is followed by an identifier and either ':' or ']', then
1927 // this is probably an Objective-C message send where the leading '[' is
1928 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001929 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1930 !InMessageExpression && getLang().ObjC1 &&
1931 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1932 TypeResult Ty;
1933 {
1934 InMessageExpressionRAIIObject InMessage(*this, false);
1935 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1936 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001937 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1938 SourceLocation(),
1939 Ty.get(), 0);
1940 } else {
1941 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001942 T.consumeClose();
1943 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00001944 if (Tok.is(tok::l_brace)) {
1945 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001946 TypeResult Ty;
1947 {
1948 InMessageExpressionRAIIObject InMessage(*this, false);
1949 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1950 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001951 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00001952 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001953
Douglas Gregor77328d12010-09-15 23:19:31 +00001954 if (ExprType == CastExpr) {
1955 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001956
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001957 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00001958 return ExprError();
1959
Douglas Gregor77328d12010-09-15 23:19:31 +00001960 // Note that this doesn't parse the subsequent cast-expression, it just
1961 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001962 if (stopIfCastExpr) {
1963 TypeResult Ty;
1964 {
1965 InMessageExpressionRAIIObject InMessage(*this, false);
1966 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1967 }
1968 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00001969 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001970 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001971
1972 // Reject the cast of super idiom in ObjC.
1973 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
1974 Tok.getIdentifierInfo() == Ident_super &&
1975 getCurScope()->isInObjcMethodScope() &&
1976 GetLookAheadToken(1).isNot(tok::period)) {
1977 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
1978 << SourceRange(OpenLoc, RParenLoc);
1979 return ExprError();
1980 }
1981
1982 // Parse the cast-expression that follows it next.
1983 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001984 Result = ParseCastExpression(/*isUnaryExpression=*/false,
1985 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00001986 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001987 if (!Result.isInvalid()) {
1988 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
1989 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00001990 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001991 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001992 return move(Result);
1993 }
1994
1995 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1996 return ExprError();
1997 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001998 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00001999 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002000 InMessageExpressionRAIIObject InMessage(*this, false);
2001
Nate Begeman2ef13e52009-08-10 23:49:36 +00002002 ExprVector ArgExprs(Actions);
2003 CommaLocsTy CommaLocs;
2004
2005 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2006 ExprType = SimpleExpr;
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00002007 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
Argyrios Kyrtzidis707f1012011-07-01 22:22:54 +00002008 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002009 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002011 InMessageExpressionRAIIObject InMessage(*this, false);
2012
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002013 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002014 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002015
2016 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002017 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002018 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002020
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002022 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002024 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 }
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002027 T.consumeClose();
2028 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002029 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002030}
2031
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002032/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2033/// and we are at the left brace.
2034///
2035/// postfix-expression: [C99 6.5.2]
2036/// '(' type-name ')' '{' initializer-list '}'
2037/// '(' type-name ')' '{' initializer-list ',' '}'
2038///
John McCall60d7b3a2010-08-24 06:29:42 +00002039ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002040Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002041 SourceLocation LParenLoc,
2042 SourceLocation RParenLoc) {
2043 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2044 if (!getLang().C99) // Compound literals don't exist in C90.
2045 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002046 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002047 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002048 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002049 return move(Result);
2050}
2051
Reid Spencer5f016e22007-07-11 17:01:13 +00002052/// ParseStringLiteralExpression - This handles the various token types that
2053/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2054/// translation phase #6].
2055///
2056/// primary-expression: [C99 6.5.1]
2057/// string-literal
John McCall60d7b3a2010-08-24 06:29:42 +00002058ExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002060
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2062 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002063 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002064
Reid Spencer5f016e22007-07-11 17:01:13 +00002065 do {
2066 StringToks.push_back(Tok);
2067 ConsumeStringToken();
2068 } while (isTokenStringLiteral());
2069
2070 // Pass the set of string tokens, ready for concatenation, to the actions.
Sean Hunt6cf75022010-08-30 17:47:05 +00002071 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00002072}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002073
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002074/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2075/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002076///
2077/// generic-selection:
2078/// _Generic ( assignment-expression , generic-assoc-list )
2079/// generic-assoc-list:
2080/// generic-association
2081/// generic-assoc-list , generic-association
2082/// generic-association:
2083/// type-name : assignment-expression
2084/// default : assignment-expression
2085ExprResult Parser::ParseGenericSelectionExpression() {
2086 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2087 SourceLocation KeyLoc = ConsumeToken();
2088
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002089 if (!getLang().C11)
2090 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002091
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002092 BalancedDelimiterTracker T(*this, tok::l_paren);
2093 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002094 return ExprError();
2095
2096 ExprResult ControllingExpr;
2097 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002098 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002099 // not evaluated."
2100 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2101 ControllingExpr = ParseAssignmentExpression();
2102 if (ControllingExpr.isInvalid()) {
2103 SkipUntil(tok::r_paren);
2104 return ExprError();
2105 }
2106 }
2107
2108 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2109 SkipUntil(tok::r_paren);
2110 return ExprError();
2111 }
2112
2113 SourceLocation DefaultLoc;
2114 TypeVector Types(Actions);
2115 ExprVector Exprs(Actions);
2116 while (1) {
2117 ParsedType Ty;
2118 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002119 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002120 // generic association."
2121 if (!DefaultLoc.isInvalid()) {
2122 Diag(Tok, diag::err_duplicate_default_assoc);
2123 Diag(DefaultLoc, diag::note_previous_default_assoc);
2124 SkipUntil(tok::r_paren);
2125 return ExprError();
2126 }
2127 DefaultLoc = ConsumeToken();
2128 Ty = ParsedType();
2129 } else {
2130 ColonProtectionRAIIObject X(*this);
2131 TypeResult TR = ParseTypeName();
2132 if (TR.isInvalid()) {
2133 SkipUntil(tok::r_paren);
2134 return ExprError();
2135 }
2136 Ty = TR.release();
2137 }
2138 Types.push_back(Ty);
2139
2140 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2141 SkipUntil(tok::r_paren);
2142 return ExprError();
2143 }
2144
2145 // FIXME: These expressions should be parsed in a potentially potentially
2146 // evaluated context.
2147 ExprResult ER(ParseAssignmentExpression());
2148 if (ER.isInvalid()) {
2149 SkipUntil(tok::r_paren);
2150 return ExprError();
2151 }
2152 Exprs.push_back(ER.release());
2153
2154 if (Tok.isNot(tok::comma))
2155 break;
2156 ConsumeToken();
2157 }
2158
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002159 T.consumeClose();
2160 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002161 return ExprError();
2162
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002163 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2164 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002165 ControllingExpr.release(),
2166 move_arg(Types), move_arg(Exprs));
2167}
2168
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002169/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2170///
2171/// argument-expression-list:
2172/// assignment-expression
2173/// argument-expression-list , assignment-expression
2174///
2175/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002176/// [C++] assignment-expression
2177/// [C++] expression-list , assignment-expression
2178///
2179/// [C++0x] expression-list:
2180/// [C++0x] initializer-list
2181///
2182/// [C++0x] initializer-list
2183/// [C++0x] initializer-clause ...[opt]
2184/// [C++0x] initializer-list , initializer-clause ...[opt]
2185///
2186/// [C++0x] initializer-clause:
2187/// [C++0x] assignment-expression
2188/// [C++0x] braced-init-list
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002189///
Chris Lattner5f9e2722011-07-23 10:55:15 +00002190bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2191 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002192 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002193 Expr *Data,
2194 Expr **Args,
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002195 unsigned NumArgs),
John McCallca0408f2010-08-23 06:44:23 +00002196 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002197 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002198 if (Tok.is(tok::code_completion)) {
2199 if (Completer)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002200 (Actions.*Completer)(getCurScope(), Data, Exprs.data(), Exprs.size());
Douglas Gregor4706e872011-02-17 03:09:23 +00002201 else
2202 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002203 cutOffParsing();
2204 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002205 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002206
2207 ExprResult Expr;
Richard Smith7fe62082011-10-15 05:09:34 +00002208 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2209 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002210 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002211 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002212 Expr = ParseAssignmentExpression();
2213
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002214 if (Tok.is(tok::ellipsis))
2215 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002216 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002217 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002218
Sebastian Redleffa8d12008-12-10 00:02:53 +00002219 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002220
2221 if (Tok.isNot(tok::comma))
2222 return false;
2223 // Move to the next argument, remember where the comma was.
2224 CommaLocs.push_back(ConsumeToken());
2225 }
2226}
Steve Naroff296e8d52008-08-28 19:20:44 +00002227
Mike Stump98eb8a72009-02-04 22:31:32 +00002228/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2229///
2230/// [clang] block-id:
2231/// [clang] specifier-qualifier-list block-declarator
2232///
2233void Parser::ParseBlockId() {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002234 if (Tok.is(tok::code_completion)) {
2235 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002236 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002237 }
2238
Mike Stump98eb8a72009-02-04 22:31:32 +00002239 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002240 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002241 ParseSpecifierQualifierList(DS);
2242
2243 // Parse the block-declarator.
2244 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2245 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002246
Mike Stump6c92fa72009-04-29 21:40:37 +00002247 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002248 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002249
John McCall7f040a92010-12-24 02:08:15 +00002250 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002251
Mike Stump98eb8a72009-02-04 22:31:32 +00002252 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002253 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002254}
2255
Steve Naroff296e8d52008-08-28 19:20:44 +00002256/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002257/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002258///
2259/// block-literal:
2260/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002261/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002262/// [clang] block-args:
2263/// [clang] '(' parameter-list ')'
2264///
John McCall60d7b3a2010-08-24 06:29:42 +00002265ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002266 assert(Tok.is(tok::caret) && "block literal starts with ^");
2267 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002268
Chris Lattner6b91f002009-03-05 07:32:12 +00002269 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2270 "block literal parsing");
2271
Mike Stump1eb44332009-09-09 15:08:12 +00002272 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002273 // argument decls, decls within the compound expression, etc. This also
2274 // allows determining whether a variable reference inside the block is
2275 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002276 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
2277 Scope::BreakScope | Scope::ContinueScope |
2278 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002279
2280 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002281 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Steve Naroff296e8d52008-08-28 19:20:44 +00002283 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002284 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002285 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002286 // FIXME: Since the return type isn't actually parsed, it can't be used to
2287 // fill ParamInfo with an initial valid range, so do it manually.
2288 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002289
Steve Naroff296e8d52008-08-28 19:20:44 +00002290 // If this block has arguments, parse them. There is no ambiguity here with
2291 // the expression case, because the expression case requires a parameter list.
2292 if (Tok.is(tok::l_paren)) {
2293 ParseParenDeclarator(ParamInfo);
2294 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002295 // SetIdentifier sets the source range end, but in this case we're past
2296 // that location.
2297 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002298 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002299 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002300 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002301 // If there was an error parsing the arguments, they may have
2302 // tried to use ^(x+y) which requires an argument list. Just
2303 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002304 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002305 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002306 }
Mike Stump19c30c02009-04-29 19:03:13 +00002307
John McCall7f040a92010-12-24 02:08:15 +00002308 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002309
Mike Stump98eb8a72009-02-04 22:31:32 +00002310 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002311 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002312 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002313 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00002314 } else {
2315 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002316 ParsedAttributes attrs(AttrFactory);
2317 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002318 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002319 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002320 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002321 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002322 SourceLocation(),
2323 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002324 EST_None,
2325 SourceLocation(),
2326 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002327 CaretLoc, CaretLoc,
2328 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002329 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002330
John McCall7f040a92010-12-24 02:08:15 +00002331 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002332
Mike Stump98eb8a72009-02-04 22:31:32 +00002333 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002334 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002335 }
2336
Sebastian Redl1d922962008-12-13 15:32:12 +00002337
John McCall60d7b3a2010-08-24 06:29:42 +00002338 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002339 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002340 // Saw something like: ^expr
2341 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002342 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002343 return ExprError();
2344 }
Mike Stump1eb44332009-09-09 15:08:12 +00002345
John McCall60d7b3a2010-08-24 06:29:42 +00002346 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002347 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002348 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002349 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002350 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002351 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002352 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002353}