blob: 3f80309b3aa4a39562f01c604acb6fe9016b5f99 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
25#include "clang/Sema/ParsedTemplate.h"
Kaelyn Uhraincd78e612012-01-25 20:49:08 +000026#include "clang/Sema/TypoCorrection.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000027#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000028#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/SmallString.h"
31using namespace clang;
32
Reid Spencer5f016e22007-07-11 17:01:13 +000033/// getBinOpPrecedence - Return the precedence of the specified binary operator
Chris Lattnerdfe503e2010-07-19 05:07:24 +000034/// token.
Mike Stump1eb44332009-09-09 15:08:12 +000035static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000036 bool GreaterThanIsOperator,
37 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000039 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000040 // C++ [temp.names]p3:
41 // [...] When parsing a template-argument-list, the first
42 // non-nested > is taken as the ending delimiter rather than a
43 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000044 if (GreaterThanIsOperator)
45 return prec::Relational;
46 return prec::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor3965b7b2009-02-25 23:02:36 +000048 case tok::greatergreater:
49 // C++0x [temp.names]p3:
50 //
51 // [...] Similarly, the first non-nested >> is treated as two
52 // consecutive but distinct > tokens, the first of which is
53 // taken as the end of the template-argument-list and completes
54 // the template-id. [...]
55 if (GreaterThanIsOperator || !CPlusPlus0x)
56 return prec::Shift;
57 return prec::Unknown;
58
Reid Spencer5f016e22007-07-11 17:01:13 +000059 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
78 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +000082 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +000083 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +000084 case tok::plus:
85 case tok::minus: return prec::Additive;
86 case tok::percent:
87 case tok::slash:
88 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +000089 case tok::periodstar:
90 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 }
92}
93
94
95/// ParseExpression - Simple precedence-based parser for binary/ternary
96/// operators.
97///
98/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
Sebastian Redl22460502009-02-07 00:15:38 +0000107/// pm-expression: [C++ 5.5]
108/// cast-expression
109/// pm-expression '.*' cast-expression
110/// pm-expression '->*' cast-expression
111///
Reid Spencer5f016e22007-07-11 17:01:13 +0000112/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000113/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000114/// cast-expression
115/// multiplicative-expression '*' cast-expression
116/// multiplicative-expression '/' cast-expression
117/// multiplicative-expression '%' cast-expression
118///
119/// additive-expression: [C99 6.5.6]
120/// multiplicative-expression
121/// additive-expression '+' multiplicative-expression
122/// additive-expression '-' multiplicative-expression
123///
124/// shift-expression: [C99 6.5.7]
125/// additive-expression
126/// shift-expression '<<' additive-expression
127/// shift-expression '>>' additive-expression
128///
129/// relational-expression: [C99 6.5.8]
130/// shift-expression
131/// relational-expression '<' shift-expression
132/// relational-expression '>' shift-expression
133/// relational-expression '<=' shift-expression
134/// relational-expression '>=' shift-expression
135///
136/// equality-expression: [C99 6.5.9]
137/// relational-expression
138/// equality-expression '==' relational-expression
139/// equality-expression '!=' relational-expression
140///
141/// AND-expression: [C99 6.5.10]
142/// equality-expression
143/// AND-expression '&' equality-expression
144///
145/// exclusive-OR-expression: [C99 6.5.11]
146/// AND-expression
147/// exclusive-OR-expression '^' AND-expression
148///
149/// inclusive-OR-expression: [C99 6.5.12]
150/// exclusive-OR-expression
151/// inclusive-OR-expression '|' exclusive-OR-expression
152///
153/// logical-AND-expression: [C99 6.5.13]
154/// inclusive-OR-expression
155/// logical-AND-expression '&&' inclusive-OR-expression
156///
157/// logical-OR-expression: [C99 6.5.14]
158/// logical-AND-expression
159/// logical-OR-expression '||' logical-AND-expression
160///
161/// conditional-expression: [C99 6.5.15]
162/// logical-OR-expression
163/// logical-OR-expression '?' expression ':' conditional-expression
164/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000165/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000166///
167/// assignment-expression: [C99 6.5.16]
168/// conditional-expression
169/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000170/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000171///
172/// assignment-operator: one of
173/// = *= /= %= += -= <<= >>= &= ^= |=
174///
175/// expression: [C99 6.5.17]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +0000176/// assignment-expression ...[opt]
177/// expression ',' assignment-expression ...[opt]
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000178ExprResult Parser::ParseExpression(TypeCastState isTypeCast) {
179 ExprResult LHS(ParseAssignmentExpression(isTypeCast));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000180 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Mike Stump1eb44332009-09-09 15:08:12 +0000183/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000184/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000185/// routine is necessary to disambiguate @try-statement from,
186/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000187///
John McCall60d7b3a2010-08-24 06:29:42 +0000188ExprResult
Sebastian Redld8c4e152008-12-11 22:33:27 +0000189Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000190 ExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000191 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000192}
193
Eli Friedmanadf077f2009-01-27 08:43:38 +0000194/// This routine is called when a leading '__extension__' is seen and
195/// consumed. This is necessary because the token gets consumed in the
196/// process of disambiguating between an expression and a declaration.
John McCall60d7b3a2010-08-24 06:29:42 +0000197ExprResult
Eli Friedmanadf077f2009-01-27 08:43:38 +0000198Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
John McCall60d7b3a2010-08-24 06:29:42 +0000199 ExprResult LHS(true);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000200 {
201 // Silence extension warnings in the sub-expression
202 ExtensionRAIIObject O(Diags);
203
204 LHS = ParseCastExpression(false);
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000205 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000206
Douglas Gregor200b2922010-09-17 22:25:06 +0000207 if (!LHS.isInvalid())
208 LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__,
209 LHS.take());
Eli Friedmanadf077f2009-01-27 08:43:38 +0000210
Douglas Gregor200b2922010-09-17 22:25:06 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Eli Friedmanadf077f2009-01-27 08:43:38 +0000212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000215ExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000216 if (Tok.is(tok::code_completion)) {
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000217 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000218 cutOffParsing();
219 return ExprError();
Douglas Gregor01dfea02010-01-10 23:08:15 +0000220 }
221
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000222 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000223 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000224
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000225 ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false,
226 /*isAddressOfOperand=*/false,
227 isTypeCast);
Douglas Gregor200b2922010-09-17 22:25:06 +0000228 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000229}
230
Chris Lattnerb93fb492008-06-02 21:31:07 +0000231/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
232/// where part of an objc message send has already been parsed. In this case
233/// LBracLoc indicates the location of the '[' of the message send, and either
234/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
235/// message.
236///
237/// Since this handles full assignment-expression's, it handles postfix
238/// expressions and other binary operators for these expressions as well.
John McCall60d7b3a2010-08-24 06:29:42 +0000239ExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000240Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Douglas Gregor2725ca82010-04-21 19:57:20 +0000241 SourceLocation SuperLoc,
John McCallb3d87482010-08-24 05:47:05 +0000242 ParsedType ReceiverType,
John McCall9ae2f072010-08-23 23:25:46 +0000243 Expr *ReceiverExpr) {
John McCall60d7b3a2010-08-24 06:29:42 +0000244 ExprResult R
John McCall9ae2f072010-08-23 23:25:46 +0000245 = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc,
246 ReceiverType, ReceiverExpr);
Douglas Gregorac5fd842010-09-18 01:28:11 +0000247 R = ParsePostfixExpressionSuffix(R);
Douglas Gregor200b2922010-09-17 22:25:06 +0000248 return ParseRHSOfBinaryExpression(R, prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000249}
250
251
John McCall60d7b3a2010-08-24 06:29:42 +0000252ExprResult Parser::ParseConstantExpression() {
Richard Smithf6702a32011-12-20 02:08:33 +0000253 // C++03 [basic.def.odr]p2:
Mike Stump1eb44332009-09-09 15:08:12 +0000254 // An expression is potentially evaluated unless it appears where an
Douglas Gregore0762c92009-06-19 23:52:42 +0000255 // integral constant expression is required (see 5.19) [...].
Richard Smithf6702a32011-12-20 02:08:33 +0000256 // C++98 and C++11 have no such rule, but this is only a defect in C++98.
Douglas Gregorac7610d2009-06-22 20:57:11 +0000257 EnterExpressionEvaluationContext Unevaluated(Actions,
Richard Smithf6702a32011-12-20 02:08:33 +0000258 Sema::ConstantEvaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000259
John McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult LHS(ParseCastExpression(false));
Douglas Gregor200b2922010-09-17 22:25:06 +0000261 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
265/// LHS and has a precedence of at least MinPrec.
John McCall60d7b3a2010-08-24 06:29:42 +0000266ExprResult
267Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000268 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
269 GreaterThanIsOperator,
270 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 SourceLocation ColonLoc;
272
273 while (1) {
274 // If this token has a lower precedence than we are allowed to parse (e.g.
275 // because we are called recursively, or because the token is not a binop),
276 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000277 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000278 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
280 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000281 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000285 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000287 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000288 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
289 ColonProtectionRAIIObject X(*this);
290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Handle this production specially:
292 // logical-OR-expression '?' expression ':' conditional-expression
293 // In particular, the RHS of the '?' is 'expression', not
294 // 'logical-OR-expression' as we might expect.
295 TernaryMiddle = ParseExpression();
Douglas Gregor94859892010-09-17 22:41:34 +0000296 if (TernaryMiddle.isInvalid()) {
297 LHS = ExprError();
298 TernaryMiddle = 0;
299 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 } else {
301 // Special case handling of "X ? Y : Z" where Y is empty:
302 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000303 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 Diag(Tok, diag::ext_gnu_conditional_expr);
305 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000306
Chris Lattnere5deae92010-04-20 21:33:39 +0000307 if (Tok.is(tok::colon)) {
308 // Eat the colon.
309 ColonLoc = ConsumeToken();
310 } else {
Chandler Carruthb00d37e2011-07-26 05:19:46 +0000311 // Otherwise, we're missing a ':'. Assume that this was a typo that
312 // the user forgot. If we're not in a macro expansion, we can suggest
313 // a fixit hint. If there were two spaces before the current token,
Chris Lattner24728822010-05-24 22:31:37 +0000314 // suggest inserting the colon in between them, otherwise insert ": ".
315 SourceLocation FILoc = Tok.getLocation();
316 const char *FIText = ": ";
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +0000317 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000318 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
319 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000320 bool IsInvalid = false;
321 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000322 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000323 if (!IsInvalid && *SourcePtr == ' ') {
324 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000325 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000326 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000327 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000328 FIText = ":";
329 }
330 }
331 }
332
Ted Kremenek987aa872010-04-12 22:10:35 +0000333 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000334 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000335 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000336 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000339
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000340 // Code completion for the right-hand side of an assignment expression
341 // goes through a special hook that takes the left-hand side into account.
342 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000343 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000344 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000345 return ExprError();
346 }
347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000349 // ParseCastExpression works here because all RHS expressions in C have it
350 // as a prefix, at least. However, in C++, an assignment-expression could
351 // be a throw-expression, which is not a valid cast-expression.
352 // Therefore we need some special-casing here.
353 // Also note that the third operand of the conditional operator is
354 // an assignment-expression in C++.
John McCall60d7b3a2010-08-24 06:29:42 +0000355 ExprResult RHS;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000356 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
357 RHS = ParseAssignmentExpression();
358 else
359 RHS = ParseCastExpression(false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360
Douglas Gregor200b2922010-09-17 22:25:06 +0000361 if (RHS.isInvalid())
362 LHS = ExprError();
363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // Remember the precedence of this operator and get the precedence of the
365 // operator immediately to the right of the RHS.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000366 prec::Level ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000367 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
368 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369
370 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000371 bool isRightAssoc = ThisPrec == prec::Conditional ||
372 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
374 // Get the precedence of the operator to the right of the RHS. If it binds
375 // more tightly with RHS than we do, evaluate it completely first.
376 if (ThisPrec < NextTokPrec ||
377 (ThisPrec == NextTokPrec && isRightAssoc)) {
378 // If this is left-associative, only parse things on the RHS that bind
379 // more tightly than the current operator. If it is left-associative, it
380 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
381 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000382 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000383 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000384 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Douglas Gregor200b2922010-09-17 22:25:06 +0000385
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000386 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000387 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000388
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000389 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
390 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 }
392 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000393
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000394 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000395 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000396 if (TernaryMiddle.isInvalid()) {
397 // If we're using '>>' as an operator within a template
398 // argument list (in C++98), suggest the addition of
399 // parentheses so that the code remains well-formed in C++0x.
400 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
401 SuggestParentheses(OpToken.getLocation(),
402 diag::warn_cxx0x_right_shift_in_template_arg,
403 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
404 Actions.getExprRange(RHS.get()).getEnd()));
405
Douglas Gregor23c94db2010-07-02 17:43:08 +0000406 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000407 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000408 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000409 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000410 LHS.take(), TernaryMiddle.take(),
411 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000412 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 }
414}
415
416/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000417/// true, parse a unary-expression. isAddressOfOperand exists because an
418/// id-expression that is the operand of address-of gets special treatment
419/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000420///
John McCall60d7b3a2010-08-24 06:29:42 +0000421ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000422 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000423 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000424 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000425 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000426 isAddressOfOperand,
427 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000428 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000429 if (NotCastExpr)
430 Diag(Tok, diag::err_expected_expression);
431 return move(Res);
432}
433
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000434namespace {
435class CastExpressionIdValidator : public CorrectionCandidateCallback {
436 public:
437 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
438 : AllowNonTypes(AllowNonTypes) {
439 WantTypeSpecifiers = AllowTypes;
440 }
441
442 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
443 NamedDecl *ND = candidate.getCorrectionDecl();
444 if (!ND)
445 return candidate.isKeyword();
446
447 if (isa<TypeDecl>(ND))
448 return WantTypeSpecifiers;
449 return AllowNonTypes;
450 }
451
452 private:
453 bool AllowNonTypes;
454};
455}
456
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000457/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
458/// true, parse a unary-expression. isAddressOfOperand exists because an
459/// id-expression that is the operand of address-of gets special treatment
460/// due to member pointers. NotCastExpr is set to true if the token is not the
461/// start of a cast-expression, and no diagnostic is emitted in this case.
462///
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// cast-expression: [C99 6.5.4]
464/// unary-expression
465/// '(' type-name ')' cast-expression
466///
467/// unary-expression: [C99 6.5.3]
468/// postfix-expression
469/// '++' unary-expression
470/// '--' unary-expression
471/// unary-operator cast-expression
472/// 'sizeof' unary-expression
473/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +0000474/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000475/// [GNU] '__alignof' unary-expression
476/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000477/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000478/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000479/// [C++] new-expression
480/// [C++] delete-expression
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000481/// [C++0x] 'noexcept' '(' expression ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000482///
483/// unary-operator: one of
484/// '&' '*' '+' '-' '~' '!'
485/// [GNU] '__extension__' '__real' '__imag'
486///
487/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000488/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000489/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000490/// constant
491/// string-literal
492/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000493/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000494/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000495/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000496/// '__func__' [C99 6.4.2.2]
497/// [GNU] '__FUNCTION__'
498/// [GNU] '__PRETTY_FUNCTION__'
499/// [GNU] '(' compound-statement ')'
500/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
501/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
502/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
503/// assign-expr ')'
504/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000505/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000506/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000507/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000508/// [OBJC] '@protocol' '(' identifier ')'
509/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000510/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000511/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000512/// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000513/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000514/// [C++0x] typename-specifier braced-init-list [C++ 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
516/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
517/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
518/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000519/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
520/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000521/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000522/// [G++] unary-type-trait '(' type-id ')'
523/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000524/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000525/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000526///
527/// constant: [C99 6.4.4]
528/// integer-constant
529/// floating-constant
530/// enumeration-constant -> identifier
531/// character-constant
532///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000533/// id-expression: [C++ 5.1]
534/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000535/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000536///
537/// unqualified-id: [C++ 5.1]
538/// identifier
539/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000540/// conversion-function-id
541/// '~' class-name
542/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000543///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000544/// new-expression: [C++ 5.3.4]
545/// '::'[opt] 'new' new-placement[opt] new-type-id
546/// new-initializer[opt]
547/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
548/// new-initializer[opt]
549///
550/// delete-expression: [C++ 5.3.5]
551/// '::'[opt] 'delete' cast-expression
552/// '::'[opt] 'delete' '[' ']' cast-expression
553///
John Wiegley20c0da72011-04-27 23:09:49 +0000554/// [GNU/Embarcadero] unary-type-trait:
555/// '__is_arithmetic'
556/// '__is_floating_point'
557/// '__is_integral'
558/// '__is_lvalue_expr'
559/// '__is_rvalue_expr'
560/// '__is_complete_type'
561/// '__is_void'
562/// '__is_array'
563/// '__is_function'
564/// '__is_reference'
565/// '__is_lvalue_reference'
566/// '__is_rvalue_reference'
567/// '__is_fundamental'
568/// '__is_object'
569/// '__is_scalar'
570/// '__is_compound'
571/// '__is_pointer'
572/// '__is_member_object_pointer'
573/// '__is_member_function_pointer'
574/// '__is_member_pointer'
575/// '__is_const'
576/// '__is_volatile'
577/// '__is_trivial'
578/// '__is_standard_layout'
579/// '__is_signed'
580/// '__is_unsigned'
581///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000582/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000583/// '__has_nothrow_assign'
584/// '__has_nothrow_copy'
585/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000586/// '__has_trivial_assign' [TODO]
587/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000588/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000589/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000590/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000591/// '__is_abstract' [TODO]
592/// '__is_class'
593/// '__is_empty' [TODO]
594/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000595/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000596/// '__is_pod'
597/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000598/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000599/// '__is_union'
600///
Sean Huntfeb375d2011-05-13 00:31:07 +0000601/// [Clang] unary-type-trait:
602/// '__trivially_copyable'
603///
Douglas Gregor9f361132011-01-27 20:28:01 +0000604/// binary-type-trait:
605/// [GNU] '__is_base_of'
606/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000607/// '__is_convertible'
608/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000609///
John Wiegley21ff2e52011-04-28 00:16:57 +0000610/// [Embarcadero] array-type-trait:
611/// '__array_rank'
612/// '__array_extent'
613///
John Wiegley55262202011-04-25 06:54:41 +0000614/// [Embarcadero] expression-trait:
615/// '__is_lvalue_expr'
616/// '__is_rvalue_expr'
617///
John McCall60d7b3a2010-08-24 06:29:42 +0000618ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000619 bool isAddressOfOperand,
620 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000621 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000622 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000624 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // This handles all of cast-expression, unary-expression, postfix-expression,
627 // and primary-expression. We handle them together like this for efficiency
628 // and to simplify handling of an expression starting with a '(' token: which
629 // may be one of a parenthesized expression, cast-expression, compound literal
630 // expression, or statement expression.
631 //
632 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000633 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
634 // to handle the postfix expression suffixes. Cases that cannot be followed
635 // by postfix exprs should return without invoking
636 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 switch (SavedKind) {
638 case tok::l_paren: {
639 // If this expression is limited to being a unary-expression, the parent can
640 // not start a cast expression.
641 ParenParseOption ParenExprType =
Douglas Gregord4206632010-08-06 14:50:36 +0000642 (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000643 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000645
646 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000647 // The inside of the parens don't need to be a colon protected scope, and
648 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000649 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000650
Chris Lattner932dff72009-12-10 02:08:07 +0000651 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000652 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 switch (ParenExprType) {
656 case SimpleExpr: break; // Nothing else to do.
657 case CompoundStmt: break; // Nothing else to do.
658 case CompoundLiteral:
659 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
660 // postfix-expression exist, parse them now.
661 break;
662 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000663 // We have parsed the cast-expression and no postfix-expr pieces are
664 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000665 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667
John McCall9ae2f072010-08-23 23:25:46 +0000668 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // primary-expression
672 case tok::numeric_constant:
673 // constant: integer-constant
674 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000675
Steve Narofff69936d2007-09-16 03:34:24 +0000676 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000678 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679
680 case tok::kw_true:
681 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000682 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000683
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000684 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000685 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000686 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
687
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000688 case tok::annot_primary_expr:
689 assert(Res.get() == 0 && "Stray primary-expression annotation?");
690 Res = getExprAnnotation(Tok);
691 ConsumeToken();
692 break;
693
David Blaikie42d6d0c2011-12-04 05:04:18 +0000694 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000695 case tok::identifier: { // primary-expression: identifier
696 // unqualified-id: identifier
697 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000698 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000699 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000700 if (getLang().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000701 // Avoid the unnecessary parse-time lookup in the common case
702 // where the syntax forbids a type.
703 const Token &Next = NextToken();
704 if (Next.is(tok::coloncolon) ||
705 (!ColonIsSacred && Next.is(tok::colon)) ||
706 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000707 Next.is(tok::l_paren) ||
708 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000709 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
710 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000711 return ExprError();
712 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000713 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
714 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000715 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000716
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000717 // Consume the identifier so that we can see if it is followed by a '(' or
718 // '.'.
719 IdentifierInfo &II = *Tok.getIdentifierInfo();
720 SourceLocation ILoc = ConsumeToken();
721
Chris Lattnereb483eb2010-04-11 08:28:14 +0000722 // Support 'Class.property' and 'super.property' notation.
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000723 if (getLang().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000724 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000725 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000726 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000727 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000728
Steve Naroff61f72cb2009-03-09 21:12:44 +0000729 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000730 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000731 return ExprError();
732 }
733 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
734 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000735
736 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
737 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000738 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000739 }
John McCall9c72c602010-08-27 09:08:28 +0000740
Douglas Gregorfa885c12010-09-15 15:09:43 +0000741 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000742 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000743 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000744 // bracket. Treat it as such.
745 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000746 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000747 ((Tok.is(tok::identifier) &&
748 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
749 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000750 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
751 0);
752 break;
753 }
754
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000755 // If we have an Objective-C class name followed by an identifier
756 // and either ':' or ']', this is an Objective-C class message
757 // send that's missing the opening '['. Recovery
758 // appropriately. Also take this path if we're performing code
759 // completion after an Objective-C class name.
760 if (getLang().ObjC1 &&
761 ((Tok.is(tok::identifier) && !InMessageExpression) ||
762 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000763 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000764 if (Tok.is(tok::code_completion) ||
765 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000766 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
767 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000768 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000769 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000770 DS.SetRangeStart(ILoc);
771 DS.SetRangeEnd(ILoc);
772 const char *PrevSpec = 0;
773 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000774 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000775
776 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
777 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
778 DeclaratorInfo);
779 if (Ty.isInvalid())
780 break;
781
782 Res = ParseObjCMessageExpressionBody(SourceLocation(),
783 SourceLocation(),
784 Ty.get(), 0);
785 break;
786 }
787 }
788
John McCall9c72c602010-08-27 09:08:28 +0000789 // Make sure to pass down the right value for isAddressOfOperand.
790 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
791 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
794 // need to know whether or not this identifier is a function designator or
795 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000796 UnqualifiedId Name;
797 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000798 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000799 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
800 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000801 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000802 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
803 Name, Tok.is(tok::l_paren),
804 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000805 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 }
807 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000808 case tok::wide_char_constant:
809 case tok::utf16_char_constant:
810 case tok::utf32_char_constant:
Steve Narofff69936d2007-09-16 03:34:24 +0000811 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000813 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
815 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
816 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000817 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case tok::string_literal: // primary-expression: string-literal
821 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000822 case tok::utf8_string_literal:
823 case tok::utf16_string_literal:
824 case tok::utf32_string_literal:
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 Res = ParseStringLiteralExpression();
John McCall9ae2f072010-08-23 23:25:46 +0000826 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000827 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000828 Res = ParseGenericSelectionExpression();
829 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 case tok::kw___builtin_va_arg:
831 case tok::kw___builtin_offsetof:
832 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000833 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000834 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000835 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000836 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000837
Douglas Gregord4206632010-08-06 14:50:36 +0000838 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
839 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
840 // C++ [expr.unary] has:
841 // unary-expression:
842 // ++ cast-expression
843 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregord4206632010-08-06 14:50:36 +0000845 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000846 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000847 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000848 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000850 case tok::amp: { // unary-expression: '&' cast-expression
851 // Special treatment because of member pointers
852 SourceLocation SavedLoc = ConsumeToken();
853 Res = ParseCastExpression(false, true);
854 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000855 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000856 return move(Res);
857 }
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 case tok::star: // unary-expression: '*' cast-expression
860 case tok::plus: // unary-expression: '+' cast-expression
861 case tok::minus: // unary-expression: '-' cast-expression
862 case tok::tilde: // unary-expression: '~' cast-expression
863 case tok::exclaim: // unary-expression: '!' cast-expression
864 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000865 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 SourceLocation SavedLoc = ConsumeToken();
867 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000868 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000869 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000870 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000871 }
872
Chris Lattner35080842008-02-02 20:20:10 +0000873 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
874 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000875 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000876 SourceLocation SavedLoc = ConsumeToken();
877 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000878 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000879 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000880 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 }
882 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
883 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000884 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
886 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000887 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000888 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
889 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 case tok::ampamp: { // unary-expression: '&&' identifier
891 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000892 if (Tok.isNot(tok::identifier))
893 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000894
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000895 if (getCurScope()->getFnParent() == 0)
896 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000899 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
900 Tok.getLocation());
901 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000903 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 }
905 case tok::kw_const_cast:
906 case tok::kw_dynamic_cast:
907 case tok::kw_reinterpret_cast:
908 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000909 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000910 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000911 case tok::kw_typeid:
912 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000913 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000914 case tok::kw___uuidof:
915 Res = ParseCXXUuidof();
916 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000917 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000918 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000919 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000920
Douglas Gregor9497a732010-09-16 01:51:54 +0000921 case tok::annot_typename:
922 if (isStartOfObjCClassMessageMissingOpenBracket()) {
923 ParsedType Type = getTypeAnnotation(Tok);
924
925 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000926 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000927 DS.SetRangeStart(Tok.getLocation());
928 DS.SetRangeEnd(Tok.getLastLoc());
929
930 const char *PrevSpec = 0;
931 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000932 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
933 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000934
935 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
936 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
937 if (Ty.isInvalid())
938 break;
939
940 ConsumeToken();
941 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
942 Ty.get(), 0);
943 break;
944 }
945 // Fall through
946
David Blaikie5e089fe2012-01-24 05:47:35 +0000947 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000948 case tok::kw_char:
949 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000950 case tok::kw_char16_t:
951 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000952 case tok::kw_bool:
953 case tok::kw_short:
954 case tok::kw_int:
955 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000956 case tok::kw___int64:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000957 case tok::kw_signed:
958 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000959 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000960 case tok::kw_float:
961 case tok::kw_double:
962 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000963 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000964 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +0000965 case tok::kw___vector: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000966 if (!getLang().CPlusPlus) {
967 Diag(Tok, diag::err_expected_expression);
968 return ExprError();
969 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000970
971 if (SavedKind == tok::kw_typename) {
972 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000973 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +0000974 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000975 return ExprError();
976 }
977
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000978 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000979 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000980 //
John McCall0b7e6782011-03-24 11:26:52 +0000981 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000982 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000983 if (Tok.isNot(tok::l_paren) &&
984 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000985 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
986 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000987
Richard Smith7fe62082011-10-15 05:09:34 +0000988 if (Tok.is(tok::l_brace))
989 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
990
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000991 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +0000992 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000993 }
994
Douglas Gregorae4c77d2010-02-05 19:11:37 +0000995 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +0000996 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
997 // (We can end up in this situation after tentative parsing.)
998 if (TryAnnotateTypeOrScopeToken())
999 return ExprError();
1000 if (!Tok.is(tok::annot_cxxscope))
1001 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001002 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001003
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001004 Token Next = NextToken();
1005 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001006 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001007 if (TemplateId->Kind == TNK_Type_template) {
1008 // We have a qualified template-id that we know refers to a
1009 // type, translate it into a type and continue parsing as a
1010 // cast expression.
1011 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001012 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1013 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001014 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001015 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001016 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001017 }
1018 }
1019
1020 // Parse as an id-expression.
1021 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001022 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001023 }
1024
1025 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001026 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001027 if (TemplateId->Kind == TNK_Type_template) {
1028 // We have a template-id that we know refers to a type,
1029 // translate it into a type and continue parsing as a cast
1030 // expression.
1031 AnnotateTemplateIdTokenAsType();
1032 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001033 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001034 }
1035
1036 // Fall through to treat the template-id as an id-expression.
1037 }
1038
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001039 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001040 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001041 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001042
Chris Lattner74ba4102009-01-04 22:52:14 +00001043 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001044 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1045 // annotates the token, tail recurse.
1046 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001047 return ExprError();
1048 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001049 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1050
Chris Lattner74ba4102009-01-04 22:52:14 +00001051 // ::new -> [C++] new-expression
1052 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001053 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001054 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001055 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001056 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001057 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001059 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001060 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001061 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001062 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001063
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001064 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001065 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001066
1067 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001068 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001069
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001070 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001071 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001072 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001073 BalancedDelimiterTracker T(*this, tok::l_paren);
1074
1075 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001076 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001077 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001078 // The noexcept operator determines whether the evaluation of its operand,
1079 // which is an unevaluated operand, can throw an exception.
1080 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001081 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001082
1083 T.consumeClose();
1084
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001085 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001086 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1087 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001088 return move(Result);
1089 }
1090
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001091 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001092 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001093 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001094 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001095 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001096 case tok::kw___is_arithmetic:
1097 case tok::kw___is_integral:
1098 case tok::kw___is_floating_point:
1099 case tok::kw___is_complete_type:
1100 case tok::kw___is_void:
1101 case tok::kw___is_array:
1102 case tok::kw___is_function:
1103 case tok::kw___is_reference:
1104 case tok::kw___is_lvalue_reference:
1105 case tok::kw___is_rvalue_reference:
1106 case tok::kw___is_fundamental:
1107 case tok::kw___is_object:
1108 case tok::kw___is_scalar:
1109 case tok::kw___is_compound:
1110 case tok::kw___is_pointer:
1111 case tok::kw___is_member_object_pointer:
1112 case tok::kw___is_member_function_pointer:
1113 case tok::kw___is_member_pointer:
1114 case tok::kw___is_const:
1115 case tok::kw___is_volatile:
1116 case tok::kw___is_standard_layout:
1117 case tok::kw___is_signed:
1118 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001119 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001120 case tok::kw___is_pod:
1121 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001122 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001123 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001124 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001125 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001126 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001127 case tok::kw___has_trivial_copy:
1128 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001129 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001130 case tok::kw___has_nothrow_assign:
1131 case tok::kw___has_nothrow_copy:
1132 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001133 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001134 return ParseUnaryTypeTrait();
1135
Francois Pichetf1872372010-12-08 22:35:30 +00001136 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001137 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001138 case tok::kw___is_same:
1139 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001140 case tok::kw___is_convertible_to:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001141 return ParseBinaryTypeTrait();
1142
John Wiegley21ff2e52011-04-28 00:16:57 +00001143 case tok::kw___array_rank:
1144 case tok::kw___array_extent:
1145 return ParseArrayTypeTrait();
1146
John Wiegley55262202011-04-25 06:54:41 +00001147 case tok::kw___is_lvalue_expr:
1148 case tok::kw___is_rvalue_expr:
1149 return ParseExpressionTrait();
1150
Chris Lattnerc97c2042007-10-03 22:03:06 +00001151 case tok::at: {
1152 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001153 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001154 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001155 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001156 Res = ParseBlockLiteralExpression();
1157 break;
1158 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001159 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001160 cutOffParsing();
1161 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001162 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001163 case tok::l_square:
Douglas Gregorae7902c2011-08-04 15:30:47 +00001164 if (getLang().CPlusPlus0x) {
1165 if (getLang().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001166 // C++11 lambda expressions and Objective-C message sends both start with a
1167 // square bracket. There are three possibilities here:
1168 // we have a valid lambda expression, we have an invalid lambda
1169 // expression, or we have something that doesn't appear to be a lambda.
1170 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001171 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001172 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001173 Res = ParseObjCMessageExpression();
1174 break;
1175 }
1176 Res = ParseLambdaExpression();
1177 break;
1178 }
Chandler Carruthbb399022011-07-08 04:28:55 +00001179 if (getLang().ObjC1) {
1180 Res = ParseObjCMessageExpression();
1181 break;
1182 }
1183 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001185 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001186 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001188
John McCall9ae2f072010-08-23 23:25:46 +00001189 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001190 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191}
1192
1193/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1194/// is parsed, this method parses any suffixes that apply.
1195///
1196/// postfix-expression: [C99 6.5.2]
1197/// primary-expression
1198/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001199/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001200/// postfix-expression '(' argument-expression-list[opt] ')'
1201/// postfix-expression '.' identifier
1202/// postfix-expression '->' identifier
1203/// postfix-expression '++'
1204/// postfix-expression '--'
1205/// '(' type-name ')' '{' initializer-list '}'
1206/// '(' type-name ')' '{' initializer-list ',' '}'
1207///
1208/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001209/// argument-expression ...[opt]
1210/// argument-expression-list ',' assignment-expression ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001211///
John McCall60d7b3a2010-08-24 06:29:42 +00001212ExprResult
1213Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 // Now that the primary-expression piece of the postfix-expression has been
1215 // parsed, see if there are any postfix-expression pieces here.
1216 SourceLocation Loc;
1217 while (1) {
1218 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001219 case tok::code_completion:
1220 if (InMessageExpression)
1221 return move(LHS);
1222
Douglas Gregorac5fd842010-09-18 01:28:11 +00001223 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001224 cutOffParsing();
1225 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001226
Douglas Gregor0fbda682010-09-15 14:51:05 +00001227 case tok::identifier:
1228 // If we see identifier: after an expression, and we're not already in a
1229 // message send, then this is probably a message send with a missing
1230 // opening bracket '['.
1231 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001232 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001233 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1234 ParsedType(), LHS.get());
1235 break;
1236 }
1237
1238 // Fall through; this isn't a message send.
1239
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001241 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001243 // If we have a array postfix expression that starts on a new line and
1244 // Objective-C is enabled, it is highly likely that the user forgot a
1245 // semicolon after the base expression and that the array postfix-expr is
1246 // actually another message send. In this case, do some look-ahead to see
1247 // if the contents of the square brackets are obviously not a valid
1248 // expression and recover by pretending there is no suffix.
1249 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1250 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001251 return move(LHS);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001252
1253 BalancedDelimiterTracker T(*this, tok::l_square);
1254 T.consumeOpen();
1255 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001256 ExprResult Idx;
Richard Smith7fe62082011-10-15 05:09:34 +00001257 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1258 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001259 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001260 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001261 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001264
1265 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001266 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1267 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001268 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001269 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
1271 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001272 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 break;
1274 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001275
Peter Collingbournebf36e252011-02-09 21:12:02 +00001276 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1277 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1278 // '(' argument-expression-list[opt] ')'
1279 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001280 InMessageExpressionRAIIObject InMessage(*this, false);
1281
Peter Collingbournebf36e252011-02-09 21:12:02 +00001282 Expr *ExecConfig = 0;
1283
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001284 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1285 BalancedDelimiterTracker PT(*this, tok::l_paren);
1286
Peter Collingbournebf36e252011-02-09 21:12:02 +00001287 if (OpKind == tok::lesslessless) {
1288 ExprVector ExecConfigExprs(Actions);
1289 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001290 LLLT.consumeOpen();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001291
1292 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1293 LHS = ExprError();
1294 }
1295
1296 if (LHS.isInvalid()) {
1297 SkipUntil(tok::greatergreatergreater);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001298 } else if (LLLT.consumeClose()) {
1299 // There was an error closing the brackets
Peter Collingbournebf36e252011-02-09 21:12:02 +00001300 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001301 }
1302
1303 if (!LHS.isInvalid()) {
1304 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1305 LHS = ExprError();
1306 else
1307 Loc = PrevTokLocation;
1308 }
1309
1310 if (!LHS.isInvalid()) {
1311 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001312 LLLT.getOpenLocation(),
1313 move_arg(ExecConfigExprs),
1314 LLLT.getCloseLocation());
Peter Collingbournebf36e252011-02-09 21:12:02 +00001315 if (ECResult.isInvalid())
1316 LHS = ExprError();
1317 else
1318 ExecConfig = ECResult.get();
1319 }
1320 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001321 PT.consumeOpen();
1322 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001323 }
1324
Sebastian Redla55e52c2008-11-25 22:21:31 +00001325 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001326 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001327
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001328 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 Actions.CodeCompleteCall(getCurScope(), LHS.get(), 0, 0);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001330 cutOffParsing();
1331 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001332 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001333
1334 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1335 if (Tok.isNot(tok::r_paren)) {
1336 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1337 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001338 LHS = ExprError();
1339 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 }
1341 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001344 if (LHS.isInvalid()) {
1345 SkipUntil(tok::r_paren);
1346 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001347 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001348 LHS = ExprError();
1349 } else {
1350 assert((ArgExprs.size() == 0 ||
1351 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001352 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001353 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001354 move_arg(ArgExprs), Tok.getLocation(),
1355 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001356 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 }
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 break;
1360 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001361 case tok::arrow:
1362 case tok::period: {
1363 // postfix-expression: p-e '->' template[opt] id-expression
1364 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 tok::TokenKind OpKind = Tok.getKind();
1366 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001367
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001368 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001369 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001370 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001371 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001372 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001373 OpLoc, OpKind, ObjectType,
1374 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001375 if (LHS.isInvalid())
1376 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001377
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001378 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1379 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001380 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001381 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001382 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001383 }
1384
Douglas Gregor81b747b2009-09-17 21:32:03 +00001385 if (Tok.is(tok::code_completion)) {
1386 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001387 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001388 OpLoc, OpKind == tok::arrow);
1389
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001390 cutOffParsing();
1391 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001392 }
1393
John McCall9ae2f072010-08-23 23:25:46 +00001394 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1395 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001396 ObjectType);
1397 break;
1398 }
1399
1400 // Either the action has told is that this cannot be a
1401 // pseudo-destructor expression (based on the type of base
1402 // expression), or we didn't see a '~' in the right place. We
1403 // can still parse a destructor name here, but in that case it
1404 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001405 // Allow explicit constructor calls in Microsoft mode.
1406 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001407 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001408 UnqualifiedId Name;
1409 if (ParseUnqualifiedId(SS,
1410 /*EnteringContext=*/false,
1411 /*AllowDestructorName=*/true,
Francois Pichet62ec1f22011-09-17 17:15:52 +00001412 /*AllowConstructorName=*/ getLang().MicrosoftExt,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001413 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001414 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001415
1416 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001417 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001418 OpKind, SS, TemplateKWLoc, Name,
1419 ObjCImpDecl, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 break;
1421 }
1422 case tok::plusplus: // postfix-expression: postfix-expression '++'
1423 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001424 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001425 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001426 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001427 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 ConsumeToken();
1429 break;
1430 }
1431 }
1432}
1433
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001434/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1435/// vec_step and we are at the start of an expression or a parenthesized
1436/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1437/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001438///
1439/// unary-expression: [C99 6.5.3]
1440/// 'sizeof' unary-expression
1441/// 'sizeof' '(' type-name ')'
1442/// [GNU] '__alignof' unary-expression
1443/// [GNU] '__alignof' '(' type-name ')'
1444/// [C++0x] 'alignof' '(' type-id ')'
1445///
1446/// [GNU] typeof-specifier:
1447/// typeof ( expressions )
1448/// typeof ( type-name )
1449/// [GNU/C++] typeof unary-expression
1450///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001451/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1452/// vec_step ( expressions )
1453/// vec_step ( type-name )
1454///
John McCall60d7b3a2010-08-24 06:29:42 +00001455ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001456Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1457 bool &isCastExpr,
1458 ParsedType &CastTy,
1459 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001460
1461 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001462 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1463 OpTok.is(tok::kw_vec_step)) &&
1464 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001465
John McCall60d7b3a2010-08-24 06:29:42 +00001466 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001468 // If the operand doesn't start with an '(', it must be an expression.
1469 if (Tok.isNot(tok::l_paren)) {
1470 isCastExpr = false;
1471 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1472 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1473 return ExprError();
1474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001476 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001477 } else {
1478 // If it starts with a '(', we know that it is either a parenthesized
1479 // type-name, or it is a unary-expression that starts with a compound
1480 // literal, or starts with a primary-expression that is a parenthesized
1481 // expression.
1482 ParenParseOption ExprType = CastExpr;
1483 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001485 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001486 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001487 CastRange = SourceRange(LParenLoc, RParenLoc);
1488
1489 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1490 // a type.
1491 if (ExprType == CastExpr) {
1492 isCastExpr = true;
1493 return ExprEmpty();
1494 }
1495
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001496 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1497 // GNU typeof in C requires the expression to be parenthesized. Not so for
1498 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1499 // the start of a unary-expression, but doesn't include any postfix
1500 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001501 if (!Operand.isInvalid())
1502 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001503 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001504 }
1505
1506 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1507 isCastExpr = false;
1508 return move(Operand);
1509}
1510
Reid Spencer5f016e22007-07-11 17:01:13 +00001511
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001512/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001513/// unary-expression: [C99 6.5.3]
1514/// 'sizeof' unary-expression
1515/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001516/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001517/// [GNU] '__alignof' unary-expression
1518/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001519/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001520ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001521 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001522 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1523 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001524 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Douglas Gregoree8aff02011-01-04 17:33:58 +00001527 // [C++0x] 'sizeof' '...' '(' identifier ')'
1528 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1529 SourceLocation EllipsisLoc = ConsumeToken();
1530 SourceLocation LParenLoc, RParenLoc;
1531 IdentifierInfo *Name = 0;
1532 SourceLocation NameLoc;
1533 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001534 BalancedDelimiterTracker T(*this, tok::l_paren);
1535 T.consumeOpen();
1536 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001537 if (Tok.is(tok::identifier)) {
1538 Name = Tok.getIdentifierInfo();
1539 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001540 T.consumeClose();
1541 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001542 if (RParenLoc.isInvalid())
1543 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1544 } else {
1545 Diag(Tok, diag::err_expected_parameter_pack);
1546 SkipUntil(tok::r_paren);
1547 }
1548 } else if (Tok.is(tok::identifier)) {
1549 Name = Tok.getIdentifierInfo();
1550 NameLoc = ConsumeToken();
1551 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1552 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1553 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1554 << Name
1555 << FixItHint::CreateInsertion(LParenLoc, "(")
1556 << FixItHint::CreateInsertion(RParenLoc, ")");
1557 } else {
1558 Diag(Tok, diag::err_sizeof_parameter_pack);
1559 }
1560
1561 if (!Name)
1562 return ExprError();
1563
1564 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1565 OpTok.getLocation(),
1566 *Name, NameLoc,
1567 RParenLoc);
1568 }
Richard Smith841804b2011-10-17 23:06:20 +00001569
1570 if (OpTok.is(tok::kw_alignof))
1571 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1572
Eli Friedman71b8fb52012-01-21 01:01:51 +00001573 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1574
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001575 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001576 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001577 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001578 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1579 isCastExpr,
1580 CastTy,
1581 CastRange);
1582
1583 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1584 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1585 ExprKind = UETT_AlignOf;
1586 else if (OpTok.is(tok::kw_vec_step))
1587 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001588
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001589 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001590 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1591 ExprKind,
1592 /*isType=*/true,
1593 CastTy.getAsOpaquePtr(),
1594 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001595
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001597 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001598 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1599 ExprKind,
1600 /*isType=*/false,
1601 Operand.release(),
1602 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001603 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001604}
1605
1606/// ParseBuiltinPrimaryExpression
1607///
1608/// primary-expression: [C99 6.5.1]
1609/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1610/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1611/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1612/// assign-expr ')'
1613/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001614/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001615///
Reid Spencer5f016e22007-07-11 17:01:13 +00001616/// [GNU] offsetof-member-designator:
1617/// [GNU] identifier
1618/// [GNU] offsetof-member-designator '.' identifier
1619/// [GNU] offsetof-member-designator '[' expression ']'
1620///
John McCall60d7b3a2010-08-24 06:29:42 +00001621ExprResult Parser::ParseBuiltinPrimaryExpression() {
1622 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1624
1625 tok::TokenKind T = Tok.getKind();
1626 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1627
1628 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001629 if (Tok.isNot(tok::l_paren))
1630 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1631 << BuiltinII);
1632
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001633 BalancedDelimiterTracker PT(*this, tok::l_paren);
1634 PT.consumeOpen();
1635
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 // TODO: Build AST.
1637
1638 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001639 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001640 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001641 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001642
1643 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001644 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001645
Douglas Gregor809070a2009-02-18 17:45:20 +00001646 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001647
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001648 if (Tok.isNot(tok::r_paren)) {
1649 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001650 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001651 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001652
1653 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001654 Res = ExprError();
1655 else
John McCall9ae2f072010-08-23 23:25:46 +00001656 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001658 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001659 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001660 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001661 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001662 if (Ty.isInvalid()) {
1663 SkipUntil(tok::r_paren);
1664 return ExprError();
1665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001668 return ExprError();
1669
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001671 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001672 Diag(Tok, diag::err_expected_ident);
1673 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001674 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001675 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001676
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001677 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001678 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001679
John McCallf312b1e2010-08-26 23:41:50 +00001680 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001681 Comps.back().isBrackets = false;
1682 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1683 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001684
Sebastian Redla55e52c2008-11-25 22:21:31 +00001685 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001687 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001689 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001690 Comps.back().isBrackets = false;
1691 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001692
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001693 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001694 Diag(Tok, diag::err_expected_ident);
1695 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001696 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001697 }
1698 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1699 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001700
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001701 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001703 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001704 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001705 BalancedDelimiterTracker ST(*this, tok::l_square);
1706 ST.consumeOpen();
1707 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001709 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001711 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001713 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001714
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001715 ST.consumeClose();
1716 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001717 } else {
1718 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001719 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001720 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001721 } else if (Ty.isInvalid()) {
1722 Res = ExprError();
1723 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001724 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001725 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001726 Ty.get(), &Comps[0], Comps.size(),
1727 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001728 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001729 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 }
1731 }
1732 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001733 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001734 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001735 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001736 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001737 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001738 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001739 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001741 return ExprError();
1742
John McCall60d7b3a2010-08-24 06:29:42 +00001743 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001744 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001745 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001746 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001747 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001749 return ExprError();
1750
John McCall60d7b3a2010-08-24 06:29:42 +00001751 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001752 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001753 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001754 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001755 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001756 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001757 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001758 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001759 }
John McCall9ae2f072010-08-23 23:25:46 +00001760 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1761 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001762 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001763 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001764 case tok::kw___builtin_astype: {
1765 // The first argument is an expression to be converted, followed by a comma.
1766 ExprResult Expr(ParseAssignmentExpression());
1767 if (Expr.isInvalid()) {
1768 SkipUntil(tok::r_paren);
1769 return ExprError();
1770 }
1771
1772 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1773 tok::r_paren))
1774 return ExprError();
1775
1776 // Second argument is the type to bitcast to.
1777 TypeResult DestTy = ParseTypeName();
1778 if (DestTy.isInvalid())
1779 return ExprError();
1780
1781 // Attempt to consume the r-paren.
1782 if (Tok.isNot(tok::r_paren)) {
1783 Diag(Tok, diag::err_expected_rparen);
1784 SkipUntil(tok::r_paren);
1785 return ExprError();
1786 }
1787
1788 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1789 ConsumeParen());
1790 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001791 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001792 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001793
John McCall9ae2f072010-08-23 23:25:46 +00001794 if (Res.isInvalid())
1795 return ExprError();
1796
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // These can be followed by postfix-expr pieces because they are
1798 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001799 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001800}
1801
1802/// ParseParenExpression - This parses the unit that starts with a '(' token,
1803/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001804/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1805/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001806///
1807/// primary-expression: [C99 6.5.1]
1808/// '(' expression ')'
1809/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1810/// postfix-expression: [C99 6.5.2]
1811/// '(' type-name ')' '{' initializer-list '}'
1812/// '(' type-name ')' '{' initializer-list ',' '}'
1813/// cast-expression: [C99 6.5.4]
1814/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001815/// [ARC] bridged-cast-expression
1816///
1817/// [ARC] bridged-cast-expression:
1818/// (__bridge type-name) cast-expression
1819/// (__bridge_transfer type-name) cast-expression
1820/// (__bridge_retained type-name) cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001821ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001822Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001823 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001824 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001825 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001826 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001827 BalancedDelimiterTracker T(*this, tok::l_paren);
1828 if (T.consumeOpen())
1829 return ExprError();
1830 SourceLocation OpenLoc = T.getOpenLocation();
1831
John McCall60d7b3a2010-08-24 06:29:42 +00001832 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001833 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001834 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001835
Douglas Gregor02688102010-09-14 23:59:36 +00001836 if (Tok.is(tok::code_completion)) {
1837 Actions.CodeCompleteOrdinaryName(getCurScope(),
1838 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1839 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001840 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001841 return ExprError();
1842 }
John McCallb3c49062011-04-06 02:35:25 +00001843
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001844 // Diagnose use of bridge casts in non-arc mode.
1845 bool BridgeCast = (getLang().ObjC2 &&
1846 (Tok.is(tok::kw___bridge) ||
1847 Tok.is(tok::kw___bridge_transfer) ||
1848 Tok.is(tok::kw___bridge_retained) ||
1849 Tok.is(tok::kw___bridge_retain)));
1850 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001851 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001852 SourceLocation BridgeKeywordLoc = ConsumeToken();
1853 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001854 Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_nonarc)
1855 << BridgeCastName
1856 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001857 BridgeCast = false;
1858 }
1859
John McCallb3c49062011-04-06 02:35:25 +00001860 // None of these cases should fall through with an invalid Result
1861 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001862 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall0b7e6782011-03-24 11:26:52 +00001864 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001865 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001867
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001868 // If the substmt parsed correctly, build the AST node.
John McCallb3c49062011-04-06 02:35:25 +00001869 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001870 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001871 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001872 tok::TokenKind tokenKind = Tok.getKind();
1873 SourceLocation BridgeKeywordLoc = ConsumeToken();
1874
John McCallf85e1932011-06-15 23:02:42 +00001875 // Parse an Objective-C ARC ownership cast expression.
1876 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001877 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001878 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001879 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001880 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001881 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001882 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001883 else {
1884 // As a hopefully temporary workaround, allow __bridge_retain as
1885 // a synonym for __bridge_retained, but only in system headers.
1886 assert(tokenKind == tok::kw___bridge_retain);
1887 Kind = OBC_BridgeRetained;
1888 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1889 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1890 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1891 "__bridge_retained");
1892 }
John McCallf85e1932011-06-15 23:02:42 +00001893
John McCallf85e1932011-06-15 23:02:42 +00001894 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001895 T.consumeClose();
1896 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001897 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001898
1899 if (Ty.isInvalid() || SubExpr.isInvalid())
1900 return ExprError();
1901
1902 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1903 BridgeKeywordLoc, Ty.get(),
1904 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001905 } else if (ExprType >= CompoundLiteral &&
1906 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001910 // In C++, if the type-id is ambiguous we disambiguate based on context.
1911 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1912 // in which case we should treat it as type-id.
1913 // if stopIfCastExpr is false, we need to determine the context past the
1914 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001915 if (isAmbiguousTypeId && !stopIfCastExpr) {
1916 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1917 RParenLoc = T.getCloseLocation();
1918 return res;
1919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001921 // Parse the type declarator.
1922 DeclSpec DS(AttrFactory);
1923 ParseSpecifierQualifierList(DS);
1924 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1925 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00001926
Douglas Gregor77328d12010-09-15 23:19:31 +00001927 // If our type is followed by an identifier and either ':' or ']', then
1928 // this is probably an Objective-C message send where the leading '[' is
1929 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001930 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1931 !InMessageExpression && getLang().ObjC1 &&
1932 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1933 TypeResult Ty;
1934 {
1935 InMessageExpressionRAIIObject InMessage(*this, false);
1936 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1937 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001938 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1939 SourceLocation(),
1940 Ty.get(), 0);
1941 } else {
1942 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001943 T.consumeClose();
1944 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00001945 if (Tok.is(tok::l_brace)) {
1946 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001947 TypeResult Ty;
1948 {
1949 InMessageExpressionRAIIObject InMessage(*this, false);
1950 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1951 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001952 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00001953 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001954
Douglas Gregor77328d12010-09-15 23:19:31 +00001955 if (ExprType == CastExpr) {
1956 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001957
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001958 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00001959 return ExprError();
1960
Douglas Gregor77328d12010-09-15 23:19:31 +00001961 // Note that this doesn't parse the subsequent cast-expression, it just
1962 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001963 if (stopIfCastExpr) {
1964 TypeResult Ty;
1965 {
1966 InMessageExpressionRAIIObject InMessage(*this, false);
1967 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1968 }
1969 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00001970 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001971 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001972
1973 // Reject the cast of super idiom in ObjC.
1974 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
1975 Tok.getIdentifierInfo() == Ident_super &&
1976 getCurScope()->isInObjcMethodScope() &&
1977 GetLookAheadToken(1).isNot(tok::period)) {
1978 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
1979 << SourceRange(OpenLoc, RParenLoc);
1980 return ExprError();
1981 }
1982
1983 // Parse the cast-expression that follows it next.
1984 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001985 Result = ParseCastExpression(/*isUnaryExpression=*/false,
1986 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00001987 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001988 if (!Result.isInvalid()) {
1989 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
1990 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00001991 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001992 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001993 return move(Result);
1994 }
1995
1996 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1997 return ExprError();
1998 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001999 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002000 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002001 InMessageExpressionRAIIObject InMessage(*this, false);
2002
Nate Begeman2ef13e52009-08-10 23:49:36 +00002003 ExprVector ArgExprs(Actions);
2004 CommaLocsTy CommaLocs;
2005
2006 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2007 ExprType = SimpleExpr;
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00002008 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
Argyrios Kyrtzidis707f1012011-07-01 22:22:54 +00002009 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002010 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002012 InMessageExpressionRAIIObject InMessage(*this, false);
2013
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002014 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002016
2017 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002018 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002019 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002020 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002021
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002023 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002025 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 }
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002028 T.consumeClose();
2029 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002030 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002031}
2032
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002033/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2034/// and we are at the left brace.
2035///
2036/// postfix-expression: [C99 6.5.2]
2037/// '(' type-name ')' '{' initializer-list '}'
2038/// '(' type-name ')' '{' initializer-list ',' '}'
2039///
John McCall60d7b3a2010-08-24 06:29:42 +00002040ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002041Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002042 SourceLocation LParenLoc,
2043 SourceLocation RParenLoc) {
2044 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2045 if (!getLang().C99) // Compound literals don't exist in C90.
2046 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002047 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002048 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002049 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002050 return move(Result);
2051}
2052
Reid Spencer5f016e22007-07-11 17:01:13 +00002053/// ParseStringLiteralExpression - This handles the various token types that
2054/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2055/// translation phase #6].
2056///
2057/// primary-expression: [C99 6.5.1]
2058/// string-literal
John McCall60d7b3a2010-08-24 06:29:42 +00002059ExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002061
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2063 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002064 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 do {
2067 StringToks.push_back(Tok);
2068 ConsumeStringToken();
2069 } while (isTokenStringLiteral());
2070
2071 // Pass the set of string tokens, ready for concatenation, to the actions.
Sean Hunt6cf75022010-08-30 17:47:05 +00002072 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00002073}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002074
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002075/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2076/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002077///
2078/// generic-selection:
2079/// _Generic ( assignment-expression , generic-assoc-list )
2080/// generic-assoc-list:
2081/// generic-association
2082/// generic-assoc-list , generic-association
2083/// generic-association:
2084/// type-name : assignment-expression
2085/// default : assignment-expression
2086ExprResult Parser::ParseGenericSelectionExpression() {
2087 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2088 SourceLocation KeyLoc = ConsumeToken();
2089
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002090 if (!getLang().C11)
2091 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002092
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002093 BalancedDelimiterTracker T(*this, tok::l_paren);
2094 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002095 return ExprError();
2096
2097 ExprResult ControllingExpr;
2098 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002099 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002100 // not evaluated."
2101 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2102 ControllingExpr = ParseAssignmentExpression();
2103 if (ControllingExpr.isInvalid()) {
2104 SkipUntil(tok::r_paren);
2105 return ExprError();
2106 }
2107 }
2108
2109 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2110 SkipUntil(tok::r_paren);
2111 return ExprError();
2112 }
2113
2114 SourceLocation DefaultLoc;
2115 TypeVector Types(Actions);
2116 ExprVector Exprs(Actions);
2117 while (1) {
2118 ParsedType Ty;
2119 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002120 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002121 // generic association."
2122 if (!DefaultLoc.isInvalid()) {
2123 Diag(Tok, diag::err_duplicate_default_assoc);
2124 Diag(DefaultLoc, diag::note_previous_default_assoc);
2125 SkipUntil(tok::r_paren);
2126 return ExprError();
2127 }
2128 DefaultLoc = ConsumeToken();
2129 Ty = ParsedType();
2130 } else {
2131 ColonProtectionRAIIObject X(*this);
2132 TypeResult TR = ParseTypeName();
2133 if (TR.isInvalid()) {
2134 SkipUntil(tok::r_paren);
2135 return ExprError();
2136 }
2137 Ty = TR.release();
2138 }
2139 Types.push_back(Ty);
2140
2141 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2142 SkipUntil(tok::r_paren);
2143 return ExprError();
2144 }
2145
2146 // FIXME: These expressions should be parsed in a potentially potentially
2147 // evaluated context.
2148 ExprResult ER(ParseAssignmentExpression());
2149 if (ER.isInvalid()) {
2150 SkipUntil(tok::r_paren);
2151 return ExprError();
2152 }
2153 Exprs.push_back(ER.release());
2154
2155 if (Tok.isNot(tok::comma))
2156 break;
2157 ConsumeToken();
2158 }
2159
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002160 T.consumeClose();
2161 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002162 return ExprError();
2163
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002164 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2165 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002166 ControllingExpr.release(),
2167 move_arg(Types), move_arg(Exprs));
2168}
2169
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002170/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2171///
2172/// argument-expression-list:
2173/// assignment-expression
2174/// argument-expression-list , assignment-expression
2175///
2176/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002177/// [C++] assignment-expression
2178/// [C++] expression-list , assignment-expression
2179///
2180/// [C++0x] expression-list:
2181/// [C++0x] initializer-list
2182///
2183/// [C++0x] initializer-list
2184/// [C++0x] initializer-clause ...[opt]
2185/// [C++0x] initializer-list , initializer-clause ...[opt]
2186///
2187/// [C++0x] initializer-clause:
2188/// [C++0x] assignment-expression
2189/// [C++0x] braced-init-list
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002190///
Chris Lattner5f9e2722011-07-23 10:55:15 +00002191bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2192 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002193 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002194 Expr *Data,
2195 Expr **Args,
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002196 unsigned NumArgs),
John McCallca0408f2010-08-23 06:44:23 +00002197 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002198 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002199 if (Tok.is(tok::code_completion)) {
2200 if (Completer)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002201 (Actions.*Completer)(getCurScope(), Data, Exprs.data(), Exprs.size());
Douglas Gregor4706e872011-02-17 03:09:23 +00002202 else
2203 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002204 cutOffParsing();
2205 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002206 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002207
2208 ExprResult Expr;
Richard Smith7fe62082011-10-15 05:09:34 +00002209 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2210 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002211 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002212 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002213 Expr = ParseAssignmentExpression();
2214
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002215 if (Tok.is(tok::ellipsis))
2216 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002217 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002218 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002219
Sebastian Redleffa8d12008-12-10 00:02:53 +00002220 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002221
2222 if (Tok.isNot(tok::comma))
2223 return false;
2224 // Move to the next argument, remember where the comma was.
2225 CommaLocs.push_back(ConsumeToken());
2226 }
2227}
Steve Naroff296e8d52008-08-28 19:20:44 +00002228
Mike Stump98eb8a72009-02-04 22:31:32 +00002229/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2230///
2231/// [clang] block-id:
2232/// [clang] specifier-qualifier-list block-declarator
2233///
2234void Parser::ParseBlockId() {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002235 if (Tok.is(tok::code_completion)) {
2236 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002237 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002238 }
2239
Mike Stump98eb8a72009-02-04 22:31:32 +00002240 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002241 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002242 ParseSpecifierQualifierList(DS);
2243
2244 // Parse the block-declarator.
2245 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2246 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002247
Mike Stump6c92fa72009-04-29 21:40:37 +00002248 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002249 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002250
John McCall7f040a92010-12-24 02:08:15 +00002251 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002252
Mike Stump98eb8a72009-02-04 22:31:32 +00002253 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002254 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002255}
2256
Steve Naroff296e8d52008-08-28 19:20:44 +00002257/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002258/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002259///
2260/// block-literal:
2261/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002262/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002263/// [clang] block-args:
2264/// [clang] '(' parameter-list ')'
2265///
John McCall60d7b3a2010-08-24 06:29:42 +00002266ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002267 assert(Tok.is(tok::caret) && "block literal starts with ^");
2268 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002269
Chris Lattner6b91f002009-03-05 07:32:12 +00002270 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2271 "block literal parsing");
2272
Mike Stump1eb44332009-09-09 15:08:12 +00002273 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002274 // argument decls, decls within the compound expression, etc. This also
2275 // allows determining whether a variable reference inside the block is
2276 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002277 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
2278 Scope::BreakScope | Scope::ContinueScope |
2279 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002280
2281 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002282 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Steve Naroff296e8d52008-08-28 19:20:44 +00002284 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002285 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002286 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002287 // FIXME: Since the return type isn't actually parsed, it can't be used to
2288 // fill ParamInfo with an initial valid range, so do it manually.
2289 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002290
Steve Naroff296e8d52008-08-28 19:20:44 +00002291 // If this block has arguments, parse them. There is no ambiguity here with
2292 // the expression case, because the expression case requires a parameter list.
2293 if (Tok.is(tok::l_paren)) {
2294 ParseParenDeclarator(ParamInfo);
2295 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002296 // SetIdentifier sets the source range end, but in this case we're past
2297 // that location.
2298 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002299 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002300 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002301 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002302 // If there was an error parsing the arguments, they may have
2303 // tried to use ^(x+y) which requires an argument list. Just
2304 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002305 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002306 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002307 }
Mike Stump19c30c02009-04-29 19:03:13 +00002308
John McCall7f040a92010-12-24 02:08:15 +00002309 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002310
Mike Stump98eb8a72009-02-04 22:31:32 +00002311 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002312 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002313 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002314 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00002315 } else {
2316 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002317 ParsedAttributes attrs(AttrFactory);
2318 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002319 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002320 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002321 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002322 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002323 SourceLocation(),
2324 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002325 EST_None,
2326 SourceLocation(),
2327 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002328 CaretLoc, CaretLoc,
2329 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002330 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002331
John McCall7f040a92010-12-24 02:08:15 +00002332 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002333
Mike Stump98eb8a72009-02-04 22:31:32 +00002334 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002335 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002336 }
2337
Sebastian Redl1d922962008-12-13 15:32:12 +00002338
John McCall60d7b3a2010-08-24 06:29:42 +00002339 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002340 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002341 // Saw something like: ^expr
2342 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002343 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002344 return ExprError();
2345 }
Mike Stump1eb44332009-09-09 15:08:12 +00002346
John McCall60d7b3a2010-08-24 06:29:42 +00002347 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002348 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002349 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002350 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002351 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002352 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002353 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002354}