blob: fada2e319c543f48d29bd56f763ecaad56dc5ace [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
Douglas Gregor8f70bda2012-02-16 18:19:22 +0000729 // Allow either an identifier or the keyword 'class' (in C++).
730 if (Tok.isNot(tok::identifier) &&
731 !(getLang().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000732 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000733 return ExprError();
734 }
735 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
736 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000737
738 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
739 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000740 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000741 }
John McCall9c72c602010-08-27 09:08:28 +0000742
Douglas Gregorfa885c12010-09-15 15:09:43 +0000743 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000744 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000745 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000746 // bracket. Treat it as such.
747 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000748 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000749 ((Tok.is(tok::identifier) &&
750 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
751 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000752 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
753 0);
754 break;
755 }
756
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000757 // If we have an Objective-C class name followed by an identifier
758 // and either ':' or ']', this is an Objective-C class message
759 // send that's missing the opening '['. Recovery
760 // appropriately. Also take this path if we're performing code
761 // completion after an Objective-C class name.
762 if (getLang().ObjC1 &&
763 ((Tok.is(tok::identifier) && !InMessageExpression) ||
764 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000765 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000766 if (Tok.is(tok::code_completion) ||
767 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000768 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
769 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000770 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000771 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000772 DS.SetRangeStart(ILoc);
773 DS.SetRangeEnd(ILoc);
774 const char *PrevSpec = 0;
775 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000776 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000777
778 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
779 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
780 DeclaratorInfo);
781 if (Ty.isInvalid())
782 break;
783
784 Res = ParseObjCMessageExpressionBody(SourceLocation(),
785 SourceLocation(),
786 Ty.get(), 0);
787 break;
788 }
789 }
790
John McCall9c72c602010-08-27 09:08:28 +0000791 // Make sure to pass down the right value for isAddressOfOperand.
792 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
793 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
796 // need to know whether or not this identifier is a function designator or
797 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000798 UnqualifiedId Name;
799 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000800 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000801 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
802 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000803 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000804 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
805 Name, Tok.is(tok::l_paren),
806 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000807 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 }
809 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000810 case tok::wide_char_constant:
811 case tok::utf16_char_constant:
812 case tok::utf32_char_constant:
Steve Narofff69936d2007-09-16 03:34:24 +0000813 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000815 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
817 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
818 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000819 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000821 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 case tok::string_literal: // primary-expression: string-literal
823 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000824 case tok::utf8_string_literal:
825 case tok::utf16_string_literal:
826 case tok::utf32_string_literal:
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 Res = ParseStringLiteralExpression();
John McCall9ae2f072010-08-23 23:25:46 +0000828 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000829 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000830 Res = ParseGenericSelectionExpression();
831 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 case tok::kw___builtin_va_arg:
833 case tok::kw___builtin_offsetof:
834 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000835 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000836 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000837 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000838 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000839
Douglas Gregord4206632010-08-06 14:50:36 +0000840 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
841 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
842 // C++ [expr.unary] has:
843 // unary-expression:
844 // ++ cast-expression
845 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregord4206632010-08-06 14:50:36 +0000847 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000848 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000849 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000850 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000852 case tok::amp: { // unary-expression: '&' cast-expression
853 // Special treatment because of member pointers
854 SourceLocation SavedLoc = ConsumeToken();
855 Res = ParseCastExpression(false, true);
856 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000857 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000858 return move(Res);
859 }
860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 case tok::star: // unary-expression: '*' cast-expression
862 case tok::plus: // unary-expression: '+' cast-expression
863 case tok::minus: // unary-expression: '-' cast-expression
864 case tok::tilde: // unary-expression: '~' cast-expression
865 case tok::exclaim: // unary-expression: '!' cast-expression
866 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000867 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 SourceLocation SavedLoc = ConsumeToken();
869 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000870 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000871 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000872 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000873 }
874
Chris Lattner35080842008-02-02 20:20:10 +0000875 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
876 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000877 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000878 SourceLocation SavedLoc = ConsumeToken();
879 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000880 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000881 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000882 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 }
884 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
885 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000886 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
888 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000889 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000890 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
891 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 case tok::ampamp: { // unary-expression: '&&' identifier
893 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000894 if (Tok.isNot(tok::identifier))
895 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000896
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000897 if (getCurScope()->getFnParent() == 0)
898 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
899
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000901 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
902 Tok.getLocation());
903 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000905 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 }
907 case tok::kw_const_cast:
908 case tok::kw_dynamic_cast:
909 case tok::kw_reinterpret_cast:
910 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000911 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000912 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000913 case tok::kw_typeid:
914 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000915 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000916 case tok::kw___uuidof:
917 Res = ParseCXXUuidof();
918 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000919 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000920 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000921 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000922
Douglas Gregor9497a732010-09-16 01:51:54 +0000923 case tok::annot_typename:
924 if (isStartOfObjCClassMessageMissingOpenBracket()) {
925 ParsedType Type = getTypeAnnotation(Tok);
926
927 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000928 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000929 DS.SetRangeStart(Tok.getLocation());
930 DS.SetRangeEnd(Tok.getLastLoc());
931
932 const char *PrevSpec = 0;
933 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000934 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
935 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000936
937 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
938 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
939 if (Ty.isInvalid())
940 break;
941
942 ConsumeToken();
943 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
944 Ty.get(), 0);
945 break;
946 }
947 // Fall through
948
David Blaikie5e089fe2012-01-24 05:47:35 +0000949 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950 case tok::kw_char:
951 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000952 case tok::kw_char16_t:
953 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000954 case tok::kw_bool:
955 case tok::kw_short:
956 case tok::kw_int:
957 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000958 case tok::kw___int64:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000959 case tok::kw_signed:
960 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000961 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 case tok::kw_float:
963 case tok::kw_double:
964 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000965 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000966 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +0000967 case tok::kw___vector: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000968 if (!getLang().CPlusPlus) {
969 Diag(Tok, diag::err_expected_expression);
970 return ExprError();
971 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000972
973 if (SavedKind == tok::kw_typename) {
974 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000975 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +0000976 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000977 return ExprError();
978 }
979
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000980 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000981 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000982 //
John McCall0b7e6782011-03-24 11:26:52 +0000983 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000984 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000985 if (Tok.isNot(tok::l_paren) &&
986 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000987 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
988 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000989
Richard Smith7fe62082011-10-15 05:09:34 +0000990 if (Tok.is(tok::l_brace))
991 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
992
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000993 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +0000994 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000995 }
996
Douglas Gregorae4c77d2010-02-05 19:11:37 +0000997 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +0000998 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
999 // (We can end up in this situation after tentative parsing.)
1000 if (TryAnnotateTypeOrScopeToken())
1001 return ExprError();
1002 if (!Tok.is(tok::annot_cxxscope))
1003 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001004 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001005
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001006 Token Next = NextToken();
1007 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001008 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001009 if (TemplateId->Kind == TNK_Type_template) {
1010 // We have a qualified template-id that we know refers to a
1011 // type, translate it into a type and continue parsing as a
1012 // cast expression.
1013 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001014 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1015 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001016 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001017 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001018 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001019 }
1020 }
1021
1022 // Parse as an id-expression.
1023 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001024 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001025 }
1026
1027 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001028 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001029 if (TemplateId->Kind == TNK_Type_template) {
1030 // We have a template-id that we know refers to a type,
1031 // translate it into a type and continue parsing as a cast
1032 // expression.
1033 AnnotateTemplateIdTokenAsType();
1034 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001035 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001036 }
1037
1038 // Fall through to treat the template-id as an id-expression.
1039 }
1040
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001041 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001042 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001043 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001044
Chris Lattner74ba4102009-01-04 22:52:14 +00001045 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001046 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1047 // annotates the token, tail recurse.
1048 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001049 return ExprError();
1050 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001051 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1052
Chris Lattner74ba4102009-01-04 22:52:14 +00001053 // ::new -> [C++] new-expression
1054 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001055 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001056 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001057 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001058 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001059 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001061 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001062 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001063 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001064 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001065
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001066 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001067 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001068
1069 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001070 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001071
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001072 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001073 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001074 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001075 BalancedDelimiterTracker T(*this, tok::l_paren);
1076
1077 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001078 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001079 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001080 // The noexcept operator determines whether the evaluation of its operand,
1081 // which is an unevaluated operand, can throw an exception.
1082 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001083 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001084
1085 T.consumeClose();
1086
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001087 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001088 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1089 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001090 return move(Result);
1091 }
1092
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001093 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001094 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001095 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001096 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001097 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001098 case tok::kw___is_arithmetic:
1099 case tok::kw___is_integral:
1100 case tok::kw___is_floating_point:
1101 case tok::kw___is_complete_type:
1102 case tok::kw___is_void:
1103 case tok::kw___is_array:
1104 case tok::kw___is_function:
1105 case tok::kw___is_reference:
1106 case tok::kw___is_lvalue_reference:
1107 case tok::kw___is_rvalue_reference:
1108 case tok::kw___is_fundamental:
1109 case tok::kw___is_object:
1110 case tok::kw___is_scalar:
1111 case tok::kw___is_compound:
1112 case tok::kw___is_pointer:
1113 case tok::kw___is_member_object_pointer:
1114 case tok::kw___is_member_function_pointer:
1115 case tok::kw___is_member_pointer:
1116 case tok::kw___is_const:
1117 case tok::kw___is_volatile:
1118 case tok::kw___is_standard_layout:
1119 case tok::kw___is_signed:
1120 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001121 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001122 case tok::kw___is_pod:
1123 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001124 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001125 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001126 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001127 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001128 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001129 case tok::kw___has_trivial_copy:
1130 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001131 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001132 case tok::kw___has_nothrow_assign:
1133 case tok::kw___has_nothrow_copy:
1134 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001135 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001136 return ParseUnaryTypeTrait();
1137
Francois Pichetf1872372010-12-08 22:35:30 +00001138 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001139 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001140 case tok::kw___is_same:
1141 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001142 case tok::kw___is_convertible_to:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001143 return ParseBinaryTypeTrait();
1144
John Wiegley21ff2e52011-04-28 00:16:57 +00001145 case tok::kw___array_rank:
1146 case tok::kw___array_extent:
1147 return ParseArrayTypeTrait();
1148
John Wiegley55262202011-04-25 06:54:41 +00001149 case tok::kw___is_lvalue_expr:
1150 case tok::kw___is_rvalue_expr:
1151 return ParseExpressionTrait();
1152
Chris Lattnerc97c2042007-10-03 22:03:06 +00001153 case tok::at: {
1154 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001155 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001156 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001157 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001158 Res = ParseBlockLiteralExpression();
1159 break;
1160 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001161 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001162 cutOffParsing();
1163 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001164 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001165 case tok::l_square:
Douglas Gregorae7902c2011-08-04 15:30:47 +00001166 if (getLang().CPlusPlus0x) {
1167 if (getLang().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001168 // C++11 lambda expressions and Objective-C message sends both start with a
1169 // square bracket. There are three possibilities here:
1170 // we have a valid lambda expression, we have an invalid lambda
1171 // expression, or we have something that doesn't appear to be a lambda.
1172 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001173 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001174 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001175 Res = ParseObjCMessageExpression();
1176 break;
1177 }
1178 Res = ParseLambdaExpression();
1179 break;
1180 }
Chandler Carruthbb399022011-07-08 04:28:55 +00001181 if (getLang().ObjC1) {
1182 Res = ParseObjCMessageExpression();
1183 break;
1184 }
1185 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001187 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001188 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001190
John McCall9ae2f072010-08-23 23:25:46 +00001191 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001192 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193}
1194
1195/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1196/// is parsed, this method parses any suffixes that apply.
1197///
1198/// postfix-expression: [C99 6.5.2]
1199/// primary-expression
1200/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001201/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001202/// postfix-expression '(' argument-expression-list[opt] ')'
1203/// postfix-expression '.' identifier
1204/// postfix-expression '->' identifier
1205/// postfix-expression '++'
1206/// postfix-expression '--'
1207/// '(' type-name ')' '{' initializer-list '}'
1208/// '(' type-name ')' '{' initializer-list ',' '}'
1209///
1210/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001211/// argument-expression ...[opt]
1212/// argument-expression-list ',' assignment-expression ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001213///
John McCall60d7b3a2010-08-24 06:29:42 +00001214ExprResult
1215Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // Now that the primary-expression piece of the postfix-expression has been
1217 // parsed, see if there are any postfix-expression pieces here.
1218 SourceLocation Loc;
1219 while (1) {
1220 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001221 case tok::code_completion:
1222 if (InMessageExpression)
1223 return move(LHS);
1224
Douglas Gregorac5fd842010-09-18 01:28:11 +00001225 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001226 cutOffParsing();
1227 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001228
Douglas Gregor0fbda682010-09-15 14:51:05 +00001229 case tok::identifier:
1230 // If we see identifier: after an expression, and we're not already in a
1231 // message send, then this is probably a message send with a missing
1232 // opening bracket '['.
1233 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001234 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001235 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1236 ParsedType(), LHS.get());
1237 break;
1238 }
1239
1240 // Fall through; this isn't a message send.
1241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001243 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001245 // If we have a array postfix expression that starts on a new line and
1246 // Objective-C is enabled, it is highly likely that the user forgot a
1247 // semicolon after the base expression and that the array postfix-expr is
1248 // actually another message send. In this case, do some look-ahead to see
1249 // if the contents of the square brackets are obviously not a valid
1250 // expression and recover by pretending there is no suffix.
1251 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1252 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001253 return move(LHS);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001254
1255 BalancedDelimiterTracker T(*this, tok::l_square);
1256 T.consumeOpen();
1257 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001258 ExprResult Idx;
Richard Smith7fe62082011-10-15 05:09:34 +00001259 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1260 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001261 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001262 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001263 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001264
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001266
1267 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001268 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1269 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001270 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001271 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001272
1273 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001274 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 break;
1276 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001277
Peter Collingbournebf36e252011-02-09 21:12:02 +00001278 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1279 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1280 // '(' argument-expression-list[opt] ')'
1281 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001282 InMessageExpressionRAIIObject InMessage(*this, false);
1283
Peter Collingbournebf36e252011-02-09 21:12:02 +00001284 Expr *ExecConfig = 0;
1285
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001286 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1287 BalancedDelimiterTracker PT(*this, tok::l_paren);
1288
Peter Collingbournebf36e252011-02-09 21:12:02 +00001289 if (OpKind == tok::lesslessless) {
1290 ExprVector ExecConfigExprs(Actions);
1291 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001292 LLLT.consumeOpen();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001293
1294 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1295 LHS = ExprError();
1296 }
1297
1298 if (LHS.isInvalid()) {
1299 SkipUntil(tok::greatergreatergreater);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001300 } else if (LLLT.consumeClose()) {
1301 // There was an error closing the brackets
Peter Collingbournebf36e252011-02-09 21:12:02 +00001302 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001303 }
1304
1305 if (!LHS.isInvalid()) {
1306 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1307 LHS = ExprError();
1308 else
1309 Loc = PrevTokLocation;
1310 }
1311
1312 if (!LHS.isInvalid()) {
1313 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001314 LLLT.getOpenLocation(),
1315 move_arg(ExecConfigExprs),
1316 LLLT.getCloseLocation());
Peter Collingbournebf36e252011-02-09 21:12:02 +00001317 if (ECResult.isInvalid())
1318 LHS = ExprError();
1319 else
1320 ExecConfig = ECResult.get();
1321 }
1322 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001323 PT.consumeOpen();
1324 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001325 }
1326
Sebastian Redla55e52c2008-11-25 22:21:31 +00001327 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001328 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001329
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001330 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 Actions.CodeCompleteCall(getCurScope(), LHS.get(), 0, 0);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001332 cutOffParsing();
1333 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001334 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001335
1336 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1337 if (Tok.isNot(tok::r_paren)) {
1338 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1339 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001340 LHS = ExprError();
1341 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 }
1343 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001344
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001346 if (LHS.isInvalid()) {
1347 SkipUntil(tok::r_paren);
1348 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001349 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001350 LHS = ExprError();
1351 } else {
1352 assert((ArgExprs.size() == 0 ||
1353 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001355 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001356 move_arg(ArgExprs), Tok.getLocation(),
1357 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001358 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 break;
1362 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001363 case tok::arrow:
1364 case tok::period: {
1365 // postfix-expression: p-e '->' template[opt] id-expression
1366 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 tok::TokenKind OpKind = Tok.getKind();
1368 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001369
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001370 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001371 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001372 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001373 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001374 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001375 OpLoc, OpKind, ObjectType,
1376 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001377 if (LHS.isInvalid())
1378 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001379
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001380 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1381 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001382 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001383 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001384 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001385 }
1386
Douglas Gregor81b747b2009-09-17 21:32:03 +00001387 if (Tok.is(tok::code_completion)) {
1388 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001389 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001390 OpLoc, OpKind == tok::arrow);
1391
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001392 cutOffParsing();
1393 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001394 }
1395
John McCall9ae2f072010-08-23 23:25:46 +00001396 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1397 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001398 ObjectType);
1399 break;
1400 }
1401
1402 // Either the action has told is that this cannot be a
1403 // pseudo-destructor expression (based on the type of base
1404 // expression), or we didn't see a '~' in the right place. We
1405 // can still parse a destructor name here, but in that case it
1406 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001407 // Allow explicit constructor calls in Microsoft mode.
1408 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001409 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001410 UnqualifiedId Name;
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001411 if (getLang().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
1412 // Objective-C++:
1413 // After a '.' in a member access expression, treat the keyword
1414 // 'class' as if it were an identifier.
1415 //
1416 // This hack allows property access to the 'class' method because it is
1417 // such a common method name. For other C++ keywords that are
1418 // Objective-C method names, one must use the message send syntax.
1419 IdentifierInfo *Id = Tok.getIdentifierInfo();
1420 SourceLocation Loc = ConsumeToken();
1421 Name.setIdentifier(Id, Loc);
1422 } else if (ParseUnqualifiedId(SS,
1423 /*EnteringContext=*/false,
1424 /*AllowDestructorName=*/true,
1425 /*AllowConstructorName=*/
1426 getLang().MicrosoftExt,
1427 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001428 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001429
1430 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001431 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001432 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001433 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1434 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 break;
1436 }
1437 case tok::plusplus: // postfix-expression: postfix-expression '++'
1438 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001439 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001440 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001441 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001442 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 ConsumeToken();
1444 break;
1445 }
1446 }
1447}
1448
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001449/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1450/// vec_step and we are at the start of an expression or a parenthesized
1451/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1452/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001453///
1454/// unary-expression: [C99 6.5.3]
1455/// 'sizeof' unary-expression
1456/// 'sizeof' '(' type-name ')'
1457/// [GNU] '__alignof' unary-expression
1458/// [GNU] '__alignof' '(' type-name ')'
1459/// [C++0x] 'alignof' '(' type-id ')'
1460///
1461/// [GNU] typeof-specifier:
1462/// typeof ( expressions )
1463/// typeof ( type-name )
1464/// [GNU/C++] typeof unary-expression
1465///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001466/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1467/// vec_step ( expressions )
1468/// vec_step ( type-name )
1469///
John McCall60d7b3a2010-08-24 06:29:42 +00001470ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001471Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1472 bool &isCastExpr,
1473 ParsedType &CastTy,
1474 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001475
1476 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001477 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1478 OpTok.is(tok::kw_vec_step)) &&
1479 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001480
John McCall60d7b3a2010-08-24 06:29:42 +00001481 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001483 // If the operand doesn't start with an '(', it must be an expression.
1484 if (Tok.isNot(tok::l_paren)) {
1485 isCastExpr = false;
1486 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1487 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1488 return ExprError();
1489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001491 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001492 } else {
1493 // If it starts with a '(', we know that it is either a parenthesized
1494 // type-name, or it is a unary-expression that starts with a compound
1495 // literal, or starts with a primary-expression that is a parenthesized
1496 // expression.
1497 ParenParseOption ExprType = CastExpr;
1498 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001500 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001501 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001502 CastRange = SourceRange(LParenLoc, RParenLoc);
1503
1504 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1505 // a type.
1506 if (ExprType == CastExpr) {
1507 isCastExpr = true;
1508 return ExprEmpty();
1509 }
1510
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001511 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1512 // GNU typeof in C requires the expression to be parenthesized. Not so for
1513 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1514 // the start of a unary-expression, but doesn't include any postfix
1515 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001516 if (!Operand.isInvalid())
1517 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001518 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001519 }
1520
1521 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1522 isCastExpr = false;
1523 return move(Operand);
1524}
1525
Reid Spencer5f016e22007-07-11 17:01:13 +00001526
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001527/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001528/// unary-expression: [C99 6.5.3]
1529/// 'sizeof' unary-expression
1530/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001531/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001532/// [GNU] '__alignof' unary-expression
1533/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001534/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001535ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001536 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001537 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1538 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001539 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregoree8aff02011-01-04 17:33:58 +00001542 // [C++0x] 'sizeof' '...' '(' identifier ')'
1543 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1544 SourceLocation EllipsisLoc = ConsumeToken();
1545 SourceLocation LParenLoc, RParenLoc;
1546 IdentifierInfo *Name = 0;
1547 SourceLocation NameLoc;
1548 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001549 BalancedDelimiterTracker T(*this, tok::l_paren);
1550 T.consumeOpen();
1551 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001552 if (Tok.is(tok::identifier)) {
1553 Name = Tok.getIdentifierInfo();
1554 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001555 T.consumeClose();
1556 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001557 if (RParenLoc.isInvalid())
1558 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1559 } else {
1560 Diag(Tok, diag::err_expected_parameter_pack);
1561 SkipUntil(tok::r_paren);
1562 }
1563 } else if (Tok.is(tok::identifier)) {
1564 Name = Tok.getIdentifierInfo();
1565 NameLoc = ConsumeToken();
1566 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1567 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1568 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1569 << Name
1570 << FixItHint::CreateInsertion(LParenLoc, "(")
1571 << FixItHint::CreateInsertion(RParenLoc, ")");
1572 } else {
1573 Diag(Tok, diag::err_sizeof_parameter_pack);
1574 }
1575
1576 if (!Name)
1577 return ExprError();
1578
1579 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1580 OpTok.getLocation(),
1581 *Name, NameLoc,
1582 RParenLoc);
1583 }
Richard Smith841804b2011-10-17 23:06:20 +00001584
1585 if (OpTok.is(tok::kw_alignof))
1586 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1587
Eli Friedman71b8fb52012-01-21 01:01:51 +00001588 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1589
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001590 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001591 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001592 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001593 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1594 isCastExpr,
1595 CastTy,
1596 CastRange);
1597
1598 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1599 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1600 ExprKind = UETT_AlignOf;
1601 else if (OpTok.is(tok::kw_vec_step))
1602 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001603
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001604 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001605 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1606 ExprKind,
1607 /*isType=*/true,
1608 CastTy.getAsOpaquePtr(),
1609 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001610
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001612 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001613 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1614 ExprKind,
1615 /*isType=*/false,
1616 Operand.release(),
1617 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001618 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001619}
1620
1621/// ParseBuiltinPrimaryExpression
1622///
1623/// primary-expression: [C99 6.5.1]
1624/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1625/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1626/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1627/// assign-expr ')'
1628/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001629/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001630///
Reid Spencer5f016e22007-07-11 17:01:13 +00001631/// [GNU] offsetof-member-designator:
1632/// [GNU] identifier
1633/// [GNU] offsetof-member-designator '.' identifier
1634/// [GNU] offsetof-member-designator '[' expression ']'
1635///
John McCall60d7b3a2010-08-24 06:29:42 +00001636ExprResult Parser::ParseBuiltinPrimaryExpression() {
1637 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1639
1640 tok::TokenKind T = Tok.getKind();
1641 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1642
1643 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001644 if (Tok.isNot(tok::l_paren))
1645 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1646 << BuiltinII);
1647
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001648 BalancedDelimiterTracker PT(*this, tok::l_paren);
1649 PT.consumeOpen();
1650
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 // TODO: Build AST.
1652
1653 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001654 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001655 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001656 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001657
1658 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001659 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001660
Douglas Gregor809070a2009-02-18 17:45:20 +00001661 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001662
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001663 if (Tok.isNot(tok::r_paren)) {
1664 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001665 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001666 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001667
1668 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001669 Res = ExprError();
1670 else
John McCall9ae2f072010-08-23 23:25:46 +00001671 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001673 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001674 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001675 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001676 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001677 if (Ty.isInvalid()) {
1678 SkipUntil(tok::r_paren);
1679 return ExprError();
1680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001683 return ExprError();
1684
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001686 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001687 Diag(Tok, diag::err_expected_ident);
1688 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001689 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001690 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001691
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001692 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001693 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001694
John McCallf312b1e2010-08-26 23:41:50 +00001695 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001696 Comps.back().isBrackets = false;
1697 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1698 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001699
Sebastian Redla55e52c2008-11-25 22:21:31 +00001700 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001702 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001703 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001704 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001705 Comps.back().isBrackets = false;
1706 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001707
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001708 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001709 Diag(Tok, diag::err_expected_ident);
1710 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001711 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001712 }
1713 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1714 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001715
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001716 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001718 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001719 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001720 BalancedDelimiterTracker ST(*this, tok::l_square);
1721 ST.consumeOpen();
1722 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001724 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001726 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001727 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001728 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001729
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001730 ST.consumeClose();
1731 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001732 } else {
1733 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001734 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001735 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001736 } else if (Ty.isInvalid()) {
1737 Res = ExprError();
1738 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001739 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001740 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001741 Ty.get(), &Comps[0], Comps.size(),
1742 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001743 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001744 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 }
1746 }
1747 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001748 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001749 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001750 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001751 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001752 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001753 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001754 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001756 return ExprError();
1757
John McCall60d7b3a2010-08-24 06:29:42 +00001758 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001759 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001760 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001761 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001762 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001764 return ExprError();
1765
John McCall60d7b3a2010-08-24 06:29:42 +00001766 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001767 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001768 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001769 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001771 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001772 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001773 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001774 }
John McCall9ae2f072010-08-23 23:25:46 +00001775 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1776 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001777 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001778 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001779 case tok::kw___builtin_astype: {
1780 // The first argument is an expression to be converted, followed by a comma.
1781 ExprResult Expr(ParseAssignmentExpression());
1782 if (Expr.isInvalid()) {
1783 SkipUntil(tok::r_paren);
1784 return ExprError();
1785 }
1786
1787 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1788 tok::r_paren))
1789 return ExprError();
1790
1791 // Second argument is the type to bitcast to.
1792 TypeResult DestTy = ParseTypeName();
1793 if (DestTy.isInvalid())
1794 return ExprError();
1795
1796 // Attempt to consume the r-paren.
1797 if (Tok.isNot(tok::r_paren)) {
1798 Diag(Tok, diag::err_expected_rparen);
1799 SkipUntil(tok::r_paren);
1800 return ExprError();
1801 }
1802
1803 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1804 ConsumeParen());
1805 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001806 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001807 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001808
John McCall9ae2f072010-08-23 23:25:46 +00001809 if (Res.isInvalid())
1810 return ExprError();
1811
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 // These can be followed by postfix-expr pieces because they are
1813 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001814 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001815}
1816
1817/// ParseParenExpression - This parses the unit that starts with a '(' token,
1818/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001819/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1820/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001821///
1822/// primary-expression: [C99 6.5.1]
1823/// '(' expression ')'
1824/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1825/// postfix-expression: [C99 6.5.2]
1826/// '(' type-name ')' '{' initializer-list '}'
1827/// '(' type-name ')' '{' initializer-list ',' '}'
1828/// cast-expression: [C99 6.5.4]
1829/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001830/// [ARC] bridged-cast-expression
1831///
1832/// [ARC] bridged-cast-expression:
1833/// (__bridge type-name) cast-expression
1834/// (__bridge_transfer type-name) cast-expression
1835/// (__bridge_retained type-name) cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001836ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001837Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001838 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001839 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001840 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001841 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001842 BalancedDelimiterTracker T(*this, tok::l_paren);
1843 if (T.consumeOpen())
1844 return ExprError();
1845 SourceLocation OpenLoc = T.getOpenLocation();
1846
John McCall60d7b3a2010-08-24 06:29:42 +00001847 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001848 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001849 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001850
Douglas Gregor02688102010-09-14 23:59:36 +00001851 if (Tok.is(tok::code_completion)) {
1852 Actions.CodeCompleteOrdinaryName(getCurScope(),
1853 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1854 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001855 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001856 return ExprError();
1857 }
John McCallb3c49062011-04-06 02:35:25 +00001858
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001859 // Diagnose use of bridge casts in non-arc mode.
1860 bool BridgeCast = (getLang().ObjC2 &&
1861 (Tok.is(tok::kw___bridge) ||
1862 Tok.is(tok::kw___bridge_transfer) ||
1863 Tok.is(tok::kw___bridge_retained) ||
1864 Tok.is(tok::kw___bridge_retain)));
1865 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001866 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001867 SourceLocation BridgeKeywordLoc = ConsumeToken();
1868 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremeneke698a5c2012-02-18 04:42:38 +00001869 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001870 << BridgeCastName
1871 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001872 BridgeCast = false;
1873 }
1874
John McCallb3c49062011-04-06 02:35:25 +00001875 // None of these cases should fall through with an invalid Result
1876 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001877 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall0b7e6782011-03-24 11:26:52 +00001879 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001880 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001882
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001883 // If the substmt parsed correctly, build the AST node.
John McCallb3c49062011-04-06 02:35:25 +00001884 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001885 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001886 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001887 tok::TokenKind tokenKind = Tok.getKind();
1888 SourceLocation BridgeKeywordLoc = ConsumeToken();
1889
John McCallf85e1932011-06-15 23:02:42 +00001890 // Parse an Objective-C ARC ownership cast expression.
1891 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001892 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001893 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001894 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001895 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001896 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001897 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001898 else {
1899 // As a hopefully temporary workaround, allow __bridge_retain as
1900 // a synonym for __bridge_retained, but only in system headers.
1901 assert(tokenKind == tok::kw___bridge_retain);
1902 Kind = OBC_BridgeRetained;
1903 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1904 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1905 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1906 "__bridge_retained");
1907 }
John McCallf85e1932011-06-15 23:02:42 +00001908
John McCallf85e1932011-06-15 23:02:42 +00001909 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001910 T.consumeClose();
1911 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001912 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001913
1914 if (Ty.isInvalid() || SubExpr.isInvalid())
1915 return ExprError();
1916
1917 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1918 BridgeKeywordLoc, Ty.get(),
1919 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001920 } else if (ExprType >= CompoundLiteral &&
1921 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001925 // In C++, if the type-id is ambiguous we disambiguate based on context.
1926 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1927 // in which case we should treat it as type-id.
1928 // if stopIfCastExpr is false, we need to determine the context past the
1929 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001930 if (isAmbiguousTypeId && !stopIfCastExpr) {
1931 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1932 RParenLoc = T.getCloseLocation();
1933 return res;
1934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001936 // Parse the type declarator.
1937 DeclSpec DS(AttrFactory);
1938 ParseSpecifierQualifierList(DS);
1939 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1940 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00001941
Douglas Gregor77328d12010-09-15 23:19:31 +00001942 // If our type is followed by an identifier and either ':' or ']', then
1943 // this is probably an Objective-C message send where the leading '[' is
1944 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001945 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1946 !InMessageExpression && getLang().ObjC1 &&
1947 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1948 TypeResult Ty;
1949 {
1950 InMessageExpressionRAIIObject InMessage(*this, false);
1951 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1952 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001953 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1954 SourceLocation(),
1955 Ty.get(), 0);
1956 } else {
1957 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001958 T.consumeClose();
1959 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00001960 if (Tok.is(tok::l_brace)) {
1961 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001962 TypeResult Ty;
1963 {
1964 InMessageExpressionRAIIObject InMessage(*this, false);
1965 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1966 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001967 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00001968 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001969
Douglas Gregor77328d12010-09-15 23:19:31 +00001970 if (ExprType == CastExpr) {
1971 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001972
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001973 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00001974 return ExprError();
1975
Douglas Gregor77328d12010-09-15 23:19:31 +00001976 // Note that this doesn't parse the subsequent cast-expression, it just
1977 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001978 if (stopIfCastExpr) {
1979 TypeResult Ty;
1980 {
1981 InMessageExpressionRAIIObject InMessage(*this, false);
1982 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1983 }
1984 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00001985 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001986 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001987
1988 // Reject the cast of super idiom in ObjC.
1989 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
1990 Tok.getIdentifierInfo() == Ident_super &&
1991 getCurScope()->isInObjcMethodScope() &&
1992 GetLookAheadToken(1).isNot(tok::period)) {
1993 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
1994 << SourceRange(OpenLoc, RParenLoc);
1995 return ExprError();
1996 }
1997
1998 // Parse the cast-expression that follows it next.
1999 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002000 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2001 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002002 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002003 if (!Result.isInvalid()) {
2004 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2005 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00002006 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002007 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002008 return move(Result);
2009 }
2010
2011 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2012 return ExprError();
2013 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002014 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002015 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002016 InMessageExpressionRAIIObject InMessage(*this, false);
2017
Nate Begeman2ef13e52009-08-10 23:49:36 +00002018 ExprVector ArgExprs(Actions);
2019 CommaLocsTy CommaLocs;
2020
2021 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2022 ExprType = SimpleExpr;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002023 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2024 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002025 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002027 InMessageExpressionRAIIObject InMessage(*this, false);
2028
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002029 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002030 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002031
2032 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002033 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002034 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002036
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002038 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002040 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 }
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002043 T.consumeClose();
2044 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002045 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002046}
2047
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002048/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2049/// and we are at the left brace.
2050///
2051/// postfix-expression: [C99 6.5.2]
2052/// '(' type-name ')' '{' initializer-list '}'
2053/// '(' type-name ')' '{' initializer-list ',' '}'
2054///
John McCall60d7b3a2010-08-24 06:29:42 +00002055ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002056Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002057 SourceLocation LParenLoc,
2058 SourceLocation RParenLoc) {
2059 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2060 if (!getLang().C99) // Compound literals don't exist in C90.
2061 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002062 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002063 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002064 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002065 return move(Result);
2066}
2067
Reid Spencer5f016e22007-07-11 17:01:13 +00002068/// ParseStringLiteralExpression - This handles the various token types that
2069/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2070/// translation phase #6].
2071///
2072/// primary-expression: [C99 6.5.1]
2073/// string-literal
John McCall60d7b3a2010-08-24 06:29:42 +00002074ExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002076
Reid Spencer5f016e22007-07-11 17:01:13 +00002077 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2078 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002079 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002080
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 do {
2082 StringToks.push_back(Tok);
2083 ConsumeStringToken();
2084 } while (isTokenStringLiteral());
2085
2086 // Pass the set of string tokens, ready for concatenation, to the actions.
Sean Hunt6cf75022010-08-30 17:47:05 +00002087 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00002088}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002089
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002090/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2091/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002092///
2093/// generic-selection:
2094/// _Generic ( assignment-expression , generic-assoc-list )
2095/// generic-assoc-list:
2096/// generic-association
2097/// generic-assoc-list , generic-association
2098/// generic-association:
2099/// type-name : assignment-expression
2100/// default : assignment-expression
2101ExprResult Parser::ParseGenericSelectionExpression() {
2102 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2103 SourceLocation KeyLoc = ConsumeToken();
2104
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002105 if (!getLang().C11)
2106 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002108 BalancedDelimiterTracker T(*this, tok::l_paren);
2109 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002110 return ExprError();
2111
2112 ExprResult ControllingExpr;
2113 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002114 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002115 // not evaluated."
2116 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2117 ControllingExpr = ParseAssignmentExpression();
2118 if (ControllingExpr.isInvalid()) {
2119 SkipUntil(tok::r_paren);
2120 return ExprError();
2121 }
2122 }
2123
2124 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2125 SkipUntil(tok::r_paren);
2126 return ExprError();
2127 }
2128
2129 SourceLocation DefaultLoc;
2130 TypeVector Types(Actions);
2131 ExprVector Exprs(Actions);
2132 while (1) {
2133 ParsedType Ty;
2134 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002135 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002136 // generic association."
2137 if (!DefaultLoc.isInvalid()) {
2138 Diag(Tok, diag::err_duplicate_default_assoc);
2139 Diag(DefaultLoc, diag::note_previous_default_assoc);
2140 SkipUntil(tok::r_paren);
2141 return ExprError();
2142 }
2143 DefaultLoc = ConsumeToken();
2144 Ty = ParsedType();
2145 } else {
2146 ColonProtectionRAIIObject X(*this);
2147 TypeResult TR = ParseTypeName();
2148 if (TR.isInvalid()) {
2149 SkipUntil(tok::r_paren);
2150 return ExprError();
2151 }
2152 Ty = TR.release();
2153 }
2154 Types.push_back(Ty);
2155
2156 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2157 SkipUntil(tok::r_paren);
2158 return ExprError();
2159 }
2160
2161 // FIXME: These expressions should be parsed in a potentially potentially
2162 // evaluated context.
2163 ExprResult ER(ParseAssignmentExpression());
2164 if (ER.isInvalid()) {
2165 SkipUntil(tok::r_paren);
2166 return ExprError();
2167 }
2168 Exprs.push_back(ER.release());
2169
2170 if (Tok.isNot(tok::comma))
2171 break;
2172 ConsumeToken();
2173 }
2174
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002175 T.consumeClose();
2176 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002177 return ExprError();
2178
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002179 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2180 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002181 ControllingExpr.release(),
2182 move_arg(Types), move_arg(Exprs));
2183}
2184
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002185/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2186///
2187/// argument-expression-list:
2188/// assignment-expression
2189/// argument-expression-list , assignment-expression
2190///
2191/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002192/// [C++] assignment-expression
2193/// [C++] expression-list , assignment-expression
2194///
2195/// [C++0x] expression-list:
2196/// [C++0x] initializer-list
2197///
2198/// [C++0x] initializer-list
2199/// [C++0x] initializer-clause ...[opt]
2200/// [C++0x] initializer-list , initializer-clause ...[opt]
2201///
2202/// [C++0x] initializer-clause:
2203/// [C++0x] assignment-expression
2204/// [C++0x] braced-init-list
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002205///
Chris Lattner5f9e2722011-07-23 10:55:15 +00002206bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2207 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002208 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002209 Expr *Data,
2210 Expr **Args,
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002211 unsigned NumArgs),
John McCallca0408f2010-08-23 06:44:23 +00002212 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002213 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002214 if (Tok.is(tok::code_completion)) {
2215 if (Completer)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002216 (Actions.*Completer)(getCurScope(), Data, Exprs.data(), Exprs.size());
Douglas Gregor4706e872011-02-17 03:09:23 +00002217 else
2218 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002219 cutOffParsing();
2220 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002221 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002222
2223 ExprResult Expr;
Richard Smith7fe62082011-10-15 05:09:34 +00002224 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2225 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002226 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002227 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002228 Expr = ParseAssignmentExpression();
2229
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002230 if (Tok.is(tok::ellipsis))
2231 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002232 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002233 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002234
Sebastian Redleffa8d12008-12-10 00:02:53 +00002235 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002236
2237 if (Tok.isNot(tok::comma))
2238 return false;
2239 // Move to the next argument, remember where the comma was.
2240 CommaLocs.push_back(ConsumeToken());
2241 }
2242}
Steve Naroff296e8d52008-08-28 19:20:44 +00002243
Mike Stump98eb8a72009-02-04 22:31:32 +00002244/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2245///
2246/// [clang] block-id:
2247/// [clang] specifier-qualifier-list block-declarator
2248///
2249void Parser::ParseBlockId() {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002250 if (Tok.is(tok::code_completion)) {
2251 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002252 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002253 }
2254
Mike Stump98eb8a72009-02-04 22:31:32 +00002255 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002256 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002257 ParseSpecifierQualifierList(DS);
2258
2259 // Parse the block-declarator.
2260 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2261 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002262
Mike Stump6c92fa72009-04-29 21:40:37 +00002263 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002264 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002265
John McCall7f040a92010-12-24 02:08:15 +00002266 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002267
Mike Stump98eb8a72009-02-04 22:31:32 +00002268 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002269 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002270}
2271
Steve Naroff296e8d52008-08-28 19:20:44 +00002272/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002273/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002274///
2275/// block-literal:
2276/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002277/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002278/// [clang] block-args:
2279/// [clang] '(' parameter-list ')'
2280///
John McCall60d7b3a2010-08-24 06:29:42 +00002281ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002282 assert(Tok.is(tok::caret) && "block literal starts with ^");
2283 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002284
Chris Lattner6b91f002009-03-05 07:32:12 +00002285 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2286 "block literal parsing");
2287
Mike Stump1eb44332009-09-09 15:08:12 +00002288 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002289 // argument decls, decls within the compound expression, etc. This also
2290 // allows determining whether a variable reference inside the block is
2291 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002292 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlab197ba2009-02-09 18:23:29 +00002293 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002294
2295 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002296 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Steve Naroff296e8d52008-08-28 19:20:44 +00002298 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002299 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002300 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002301 // FIXME: Since the return type isn't actually parsed, it can't be used to
2302 // fill ParamInfo with an initial valid range, so do it manually.
2303 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002304
Steve Naroff296e8d52008-08-28 19:20:44 +00002305 // If this block has arguments, parse them. There is no ambiguity here with
2306 // the expression case, because the expression case requires a parameter list.
2307 if (Tok.is(tok::l_paren)) {
2308 ParseParenDeclarator(ParamInfo);
2309 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002310 // SetIdentifier sets the source range end, but in this case we're past
2311 // that location.
2312 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002313 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002314 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002315 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002316 // If there was an error parsing the arguments, they may have
2317 // tried to use ^(x+y) which requires an argument list. Just
2318 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002319 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002320 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002321 }
Mike Stump19c30c02009-04-29 19:03:13 +00002322
John McCall7f040a92010-12-24 02:08:15 +00002323 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002324
Mike Stump98eb8a72009-02-04 22:31:32 +00002325 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002326 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002327 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002328 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00002329 } else {
2330 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002331 ParsedAttributes attrs(AttrFactory);
2332 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002333 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002334 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002335 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002336 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002337 SourceLocation(),
2338 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002339 EST_None,
2340 SourceLocation(),
2341 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002342 CaretLoc, CaretLoc,
2343 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002344 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002345
John McCall7f040a92010-12-24 02:08:15 +00002346 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002347
Mike Stump98eb8a72009-02-04 22:31:32 +00002348 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002349 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002350 }
2351
Sebastian Redl1d922962008-12-13 15:32:12 +00002352
John McCall60d7b3a2010-08-24 06:29:42 +00002353 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002354 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002355 // Saw something like: ^expr
2356 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002357 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002358 return ExprError();
2359 }
Mike Stump1eb44332009-09-09 15:08:12 +00002360
John McCall60d7b3a2010-08-24 06:29:42 +00002361 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002362 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002363 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002364 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002365 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002366 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002367 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002368}