blob: 2f30e6bd2b04bf9351db97e2aa6160cf1d656751 [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
Kaelyn Uhraine43fe992012-02-22 01:03:07 +0000252ExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) {
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
Kaelyn Uhraine43fe992012-02-22 01:03:07 +0000260 ExprResult LHS(ParseCastExpression(false, false, isTypeCast));
Eli Friedmanac626012012-02-29 03:16:56 +0000261 ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
262 return Actions.ActOnConstantExpression(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000263}
264
Reid Spencer5f016e22007-07-11 17:01:13 +0000265/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
266/// LHS and has a precedence of at least MinPrec.
John McCall60d7b3a2010-08-24 06:29:42 +0000267ExprResult
268Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000269 prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(),
270 GreaterThanIsOperator,
271 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 SourceLocation ColonLoc;
273
274 while (1) {
275 // If this token has a lower precedence than we are allowed to parse (e.g.
276 // because we are called recursively, or because the token is not a binop),
277 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000278 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000279 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280
281 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000282 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000286 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000288 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000289 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
290 ColonProtectionRAIIObject X(*this);
291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // Handle this production specially:
293 // logical-OR-expression '?' expression ':' conditional-expression
294 // In particular, the RHS of the '?' is 'expression', not
295 // 'logical-OR-expression' as we might expect.
296 TernaryMiddle = ParseExpression();
Douglas Gregor94859892010-09-17 22:41:34 +0000297 if (TernaryMiddle.isInvalid()) {
298 LHS = ExprError();
299 TernaryMiddle = 0;
300 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 } else {
302 // Special case handling of "X ? Y : Z" where Y is empty:
303 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000304 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 Diag(Tok, diag::ext_gnu_conditional_expr);
306 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000307
Chris Lattnere5deae92010-04-20 21:33:39 +0000308 if (Tok.is(tok::colon)) {
309 // Eat the colon.
310 ColonLoc = ConsumeToken();
311 } else {
Chandler Carruthb00d37e2011-07-26 05:19:46 +0000312 // Otherwise, we're missing a ':'. Assume that this was a typo that
313 // the user forgot. If we're not in a macro expansion, we can suggest
314 // a fixit hint. If there were two spaces before the current token,
Chris Lattner24728822010-05-24 22:31:37 +0000315 // suggest inserting the colon in between them, otherwise insert ": ".
316 SourceLocation FILoc = Tok.getLocation();
317 const char *FIText = ": ";
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +0000318 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000319 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
320 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000321 bool IsInvalid = false;
322 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000323 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000324 if (!IsInvalid && *SourcePtr == ' ') {
325 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000326 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000327 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000328 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000329 FIText = ":";
330 }
331 }
332 }
333
Ted Kremenek987aa872010-04-12 22:10:35 +0000334 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000335 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000336 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000337 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000340
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000341 // Code completion for the right-hand side of an assignment expression
342 // goes through a special hook that takes the left-hand side into account.
343 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000344 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000345 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000346 return ExprError();
347 }
348
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000350 // ParseCastExpression works here because all RHS expressions in C have it
351 // as a prefix, at least. However, in C++, an assignment-expression could
352 // be a throw-expression, which is not a valid cast-expression.
353 // Therefore we need some special-casing here.
354 // Also note that the third operand of the conditional operator is
Richard Smithc56ab432012-02-26 23:40:27 +0000355 // an assignment-expression in C++, and in C++11, we can have a
356 // braced-init-list on the RHS of an assignment.
John McCall60d7b3a2010-08-24 06:29:42 +0000357 ExprResult RHS;
Richard Smithc56ab432012-02-26 23:40:27 +0000358 if (getLang().CPlusPlus0x && MinPrec == prec::Assignment &&
359 Tok.is(tok::l_brace)) {
360 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
361 RHS = ParseBraceInitializer();
362 if (LHS.isInvalid() || RHS.isInvalid())
363 return ExprError();
364 // A braced-init-list can never be followed by more operators.
365 return Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
366 OpToken.getKind(), LHS.take(), RHS.take());
367 } else if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000368 RHS = ParseAssignmentExpression();
Richard Smithc56ab432012-02-26 23:40:27 +0000369 } else {
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000370 RHS = ParseCastExpression(false);
Richard Smithc56ab432012-02-26 23:40:27 +0000371 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000372
Douglas Gregor200b2922010-09-17 22:25:06 +0000373 if (RHS.isInvalid())
374 LHS = ExprError();
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // Remember the precedence of this operator and get the precedence of the
377 // operator immediately to the right of the RHS.
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000378 prec::Level ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000379 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
380 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
382 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000383 bool isRightAssoc = ThisPrec == prec::Conditional ||
384 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000385
386 // Get the precedence of the operator to the right of the RHS. If it binds
387 // more tightly with RHS than we do, evaluate it completely first.
388 if (ThisPrec < NextTokPrec ||
389 (ThisPrec == NextTokPrec && isRightAssoc)) {
390 // If this is left-associative, only parse things on the RHS that bind
391 // more tightly than the current operator. If it is left-associative, it
392 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
393 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000394 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000395 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000396 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Douglas Gregor200b2922010-09-17 22:25:06 +0000397
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000398 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000399 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000401 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
402 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 }
404 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000405
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000406 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000407 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000408 if (TernaryMiddle.isInvalid()) {
409 // If we're using '>>' as an operator within a template
410 // argument list (in C++98), suggest the addition of
411 // parentheses so that the code remains well-formed in C++0x.
412 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
413 SuggestParentheses(OpToken.getLocation(),
414 diag::warn_cxx0x_right_shift_in_template_arg,
415 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
416 Actions.getExprRange(RHS.get()).getEnd()));
417
Douglas Gregor23c94db2010-07-02 17:43:08 +0000418 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000419 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000420 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000421 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000422 LHS.take(), TernaryMiddle.take(),
423 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000424 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 }
426}
427
428/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000429/// true, parse a unary-expression. isAddressOfOperand exists because an
430/// id-expression that is the operand of address-of gets special treatment
431/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000432///
John McCall60d7b3a2010-08-24 06:29:42 +0000433ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000434 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000435 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000436 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000437 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000438 isAddressOfOperand,
439 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000440 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000441 if (NotCastExpr)
442 Diag(Tok, diag::err_expected_expression);
443 return move(Res);
444}
445
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000446namespace {
447class CastExpressionIdValidator : public CorrectionCandidateCallback {
448 public:
449 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
450 : AllowNonTypes(AllowNonTypes) {
451 WantTypeSpecifiers = AllowTypes;
452 }
453
454 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
455 NamedDecl *ND = candidate.getCorrectionDecl();
456 if (!ND)
457 return candidate.isKeyword();
458
459 if (isa<TypeDecl>(ND))
460 return WantTypeSpecifiers;
461 return AllowNonTypes;
462 }
463
464 private:
465 bool AllowNonTypes;
466};
467}
468
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000469/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
470/// true, parse a unary-expression. isAddressOfOperand exists because an
471/// id-expression that is the operand of address-of gets special treatment
472/// due to member pointers. NotCastExpr is set to true if the token is not the
473/// start of a cast-expression, and no diagnostic is emitted in this case.
474///
Reid Spencer5f016e22007-07-11 17:01:13 +0000475/// cast-expression: [C99 6.5.4]
476/// unary-expression
477/// '(' type-name ')' cast-expression
478///
479/// unary-expression: [C99 6.5.3]
480/// postfix-expression
481/// '++' unary-expression
482/// '--' unary-expression
483/// unary-operator cast-expression
484/// 'sizeof' unary-expression
485/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +0000486/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000487/// [GNU] '__alignof' unary-expression
488/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000489/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000490/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000491/// [C++] new-expression
492/// [C++] delete-expression
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000493/// [C++0x] 'noexcept' '(' expression ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000494///
495/// unary-operator: one of
496/// '&' '*' '+' '-' '~' '!'
497/// [GNU] '__extension__' '__real' '__imag'
498///
499/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000500/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000501/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000502/// constant
503/// string-literal
504/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000505/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000506/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000507/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000508/// '__func__' [C99 6.4.2.2]
509/// [GNU] '__FUNCTION__'
510/// [GNU] '__PRETTY_FUNCTION__'
511/// [GNU] '(' compound-statement ')'
512/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
513/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
514/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
515/// assign-expr ')'
516/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000517/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000518/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000519/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000520/// [OBJC] '@protocol' '(' identifier ')'
521/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000522/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000523/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000524/// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000525/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000526/// [C++0x] typename-specifier braced-init-list [C++ 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000527/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
528/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
529/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
530/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000531/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
532/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000533/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000534/// [G++] unary-type-trait '(' type-id ')'
535/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000536/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000537/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000538///
539/// constant: [C99 6.4.4]
540/// integer-constant
541/// floating-constant
542/// enumeration-constant -> identifier
543/// character-constant
544///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000545/// id-expression: [C++ 5.1]
546/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000547/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000548///
549/// unqualified-id: [C++ 5.1]
550/// identifier
551/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000552/// conversion-function-id
553/// '~' class-name
554/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000555///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000556/// new-expression: [C++ 5.3.4]
557/// '::'[opt] 'new' new-placement[opt] new-type-id
558/// new-initializer[opt]
559/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
560/// new-initializer[opt]
561///
562/// delete-expression: [C++ 5.3.5]
563/// '::'[opt] 'delete' cast-expression
564/// '::'[opt] 'delete' '[' ']' cast-expression
565///
John Wiegley20c0da72011-04-27 23:09:49 +0000566/// [GNU/Embarcadero] unary-type-trait:
567/// '__is_arithmetic'
568/// '__is_floating_point'
569/// '__is_integral'
570/// '__is_lvalue_expr'
571/// '__is_rvalue_expr'
572/// '__is_complete_type'
573/// '__is_void'
574/// '__is_array'
575/// '__is_function'
576/// '__is_reference'
577/// '__is_lvalue_reference'
578/// '__is_rvalue_reference'
579/// '__is_fundamental'
580/// '__is_object'
581/// '__is_scalar'
582/// '__is_compound'
583/// '__is_pointer'
584/// '__is_member_object_pointer'
585/// '__is_member_function_pointer'
586/// '__is_member_pointer'
587/// '__is_const'
588/// '__is_volatile'
589/// '__is_trivial'
590/// '__is_standard_layout'
591/// '__is_signed'
592/// '__is_unsigned'
593///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000594/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000595/// '__has_nothrow_assign'
596/// '__has_nothrow_copy'
597/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000598/// '__has_trivial_assign' [TODO]
599/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000600/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000601/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000602/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000603/// '__is_abstract' [TODO]
604/// '__is_class'
605/// '__is_empty' [TODO]
606/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000607/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000608/// '__is_pod'
609/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000610/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000611/// '__is_union'
612///
Sean Huntfeb375d2011-05-13 00:31:07 +0000613/// [Clang] unary-type-trait:
614/// '__trivially_copyable'
615///
Douglas Gregor9f361132011-01-27 20:28:01 +0000616/// binary-type-trait:
617/// [GNU] '__is_base_of'
618/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000619/// '__is_convertible'
620/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000621///
John Wiegley21ff2e52011-04-28 00:16:57 +0000622/// [Embarcadero] array-type-trait:
623/// '__array_rank'
624/// '__array_extent'
625///
John Wiegley55262202011-04-25 06:54:41 +0000626/// [Embarcadero] expression-trait:
627/// '__is_lvalue_expr'
628/// '__is_rvalue_expr'
629///
John McCall60d7b3a2010-08-24 06:29:42 +0000630ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000631 bool isAddressOfOperand,
632 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000633 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000634 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000636 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // This handles all of cast-expression, unary-expression, postfix-expression,
639 // and primary-expression. We handle them together like this for efficiency
640 // and to simplify handling of an expression starting with a '(' token: which
641 // may be one of a parenthesized expression, cast-expression, compound literal
642 // expression, or statement expression.
643 //
644 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000645 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
646 // to handle the postfix expression suffixes. Cases that cannot be followed
647 // by postfix exprs should return without invoking
648 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 switch (SavedKind) {
650 case tok::l_paren: {
651 // If this expression is limited to being a unary-expression, the parent can
652 // not start a cast expression.
653 ParenParseOption ParenExprType =
Douglas Gregord4206632010-08-06 14:50:36 +0000654 (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000655 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000657
658 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000659 // The inside of the parens don't need to be a colon protected scope, and
660 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000661 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000662
Chris Lattner932dff72009-12-10 02:08:07 +0000663 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000664 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000665 }
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 switch (ParenExprType) {
668 case SimpleExpr: break; // Nothing else to do.
669 case CompoundStmt: break; // Nothing else to do.
670 case CompoundLiteral:
671 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
672 // postfix-expression exist, parse them now.
673 break;
674 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000675 // We have parsed the cast-expression and no postfix-expr pieces are
676 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000677 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000679
John McCall9ae2f072010-08-23 23:25:46 +0000680 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 // primary-expression
684 case tok::numeric_constant:
685 // constant: integer-constant
686 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000687
Steve Narofff69936d2007-09-16 03:34:24 +0000688 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000690 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
692 case tok::kw_true:
693 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000694 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000695
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000696 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000697 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000698 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
699
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000700 case tok::annot_primary_expr:
701 assert(Res.get() == 0 && "Stray primary-expression annotation?");
702 Res = getExprAnnotation(Tok);
703 ConsumeToken();
704 break;
705
David Blaikie42d6d0c2011-12-04 05:04:18 +0000706 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000707 case tok::identifier: { // primary-expression: identifier
708 // unqualified-id: identifier
709 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000710 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000711 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000712 if (getLang().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000713 // Avoid the unnecessary parse-time lookup in the common case
714 // where the syntax forbids a type.
715 const Token &Next = NextToken();
716 if (Next.is(tok::coloncolon) ||
717 (!ColonIsSacred && Next.is(tok::colon)) ||
718 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000719 Next.is(tok::l_paren) ||
720 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000721 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
722 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000723 return ExprError();
724 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000725 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
726 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000727 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000728
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000729 // Consume the identifier so that we can see if it is followed by a '(' or
730 // '.'.
731 IdentifierInfo &II = *Tok.getIdentifierInfo();
732 SourceLocation ILoc = ConsumeToken();
733
Chris Lattnereb483eb2010-04-11 08:28:14 +0000734 // Support 'Class.property' and 'super.property' notation.
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000735 if (getLang().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000736 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000737 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000738 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000739 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000740
Douglas Gregor8f70bda2012-02-16 18:19:22 +0000741 // Allow either an identifier or the keyword 'class' (in C++).
742 if (Tok.isNot(tok::identifier) &&
743 !(getLang().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000744 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000745 return ExprError();
746 }
747 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
748 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000749
750 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
751 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000752 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000753 }
John McCall9c72c602010-08-27 09:08:28 +0000754
Douglas Gregorfa885c12010-09-15 15:09:43 +0000755 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000756 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000757 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000758 // bracket. Treat it as such.
759 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000760 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000761 ((Tok.is(tok::identifier) &&
762 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
763 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000764 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
765 0);
766 break;
767 }
768
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000769 // If we have an Objective-C class name followed by an identifier
770 // and either ':' or ']', this is an Objective-C class message
771 // send that's missing the opening '['. Recovery
772 // appropriately. Also take this path if we're performing code
773 // completion after an Objective-C class name.
774 if (getLang().ObjC1 &&
775 ((Tok.is(tok::identifier) && !InMessageExpression) ||
776 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000777 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000778 if (Tok.is(tok::code_completion) ||
779 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000780 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
781 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000782 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000783 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000784 DS.SetRangeStart(ILoc);
785 DS.SetRangeEnd(ILoc);
786 const char *PrevSpec = 0;
787 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000788 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000789
790 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
791 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
792 DeclaratorInfo);
793 if (Ty.isInvalid())
794 break;
795
796 Res = ParseObjCMessageExpressionBody(SourceLocation(),
797 SourceLocation(),
798 Ty.get(), 0);
799 break;
800 }
801 }
802
John McCall9c72c602010-08-27 09:08:28 +0000803 // Make sure to pass down the right value for isAddressOfOperand.
804 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
805 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
808 // need to know whether or not this identifier is a function designator or
809 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000810 UnqualifiedId Name;
811 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000812 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000813 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
814 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000815 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000816 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
817 Name, Tok.is(tok::l_paren),
818 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 }
821 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000822 case tok::wide_char_constant:
823 case tok::utf16_char_constant:
824 case tok::utf32_char_constant:
Steve Narofff69936d2007-09-16 03:34:24 +0000825 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000827 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
829 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
830 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000831 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000833 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 case tok::string_literal: // primary-expression: string-literal
835 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000836 case tok::utf8_string_literal:
837 case tok::utf16_string_literal:
838 case tok::utf32_string_literal:
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 Res = ParseStringLiteralExpression();
John McCall9ae2f072010-08-23 23:25:46 +0000840 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000841 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000842 Res = ParseGenericSelectionExpression();
843 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 case tok::kw___builtin_va_arg:
845 case tok::kw___builtin_offsetof:
846 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000847 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000848 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000849 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000850 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000851
Douglas Gregord4206632010-08-06 14:50:36 +0000852 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
853 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
854 // C++ [expr.unary] has:
855 // unary-expression:
856 // ++ cast-expression
857 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregord4206632010-08-06 14:50:36 +0000859 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000860 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000861 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000862 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000864 case tok::amp: { // unary-expression: '&' cast-expression
865 // Special treatment because of member pointers
866 SourceLocation SavedLoc = ConsumeToken();
867 Res = ParseCastExpression(false, true);
868 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000869 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000870 return move(Res);
871 }
872
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 case tok::star: // unary-expression: '*' cast-expression
874 case tok::plus: // unary-expression: '+' cast-expression
875 case tok::minus: // unary-expression: '-' cast-expression
876 case tok::tilde: // unary-expression: '~' cast-expression
877 case tok::exclaim: // unary-expression: '!' cast-expression
878 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000879 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 SourceLocation SavedLoc = ConsumeToken();
881 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000882 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000883 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000884 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000885 }
886
Chris Lattner35080842008-02-02 20:20:10 +0000887 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
888 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000889 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000890 SourceLocation SavedLoc = ConsumeToken();
891 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000892 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000893 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000894 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 }
896 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
897 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000898 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
900 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000901 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000902 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
903 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 case tok::ampamp: { // unary-expression: '&&' identifier
905 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000906 if (Tok.isNot(tok::identifier))
907 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000908
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000909 if (getCurScope()->getFnParent() == 0)
910 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000913 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
914 Tok.getLocation());
915 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000917 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 }
919 case tok::kw_const_cast:
920 case tok::kw_dynamic_cast:
921 case tok::kw_reinterpret_cast:
922 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000923 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000924 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000925 case tok::kw_typeid:
926 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000927 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000928 case tok::kw___uuidof:
929 Res = ParseCXXUuidof();
930 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000931 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000932 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000933 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000934
Douglas Gregor9497a732010-09-16 01:51:54 +0000935 case tok::annot_typename:
936 if (isStartOfObjCClassMessageMissingOpenBracket()) {
937 ParsedType Type = getTypeAnnotation(Tok);
938
939 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000940 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000941 DS.SetRangeStart(Tok.getLocation());
942 DS.SetRangeEnd(Tok.getLastLoc());
943
944 const char *PrevSpec = 0;
945 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000946 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
947 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000948
949 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
950 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
951 if (Ty.isInvalid())
952 break;
953
954 ConsumeToken();
955 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
956 Ty.get(), 0);
957 break;
958 }
959 // Fall through
960
David Blaikie5e089fe2012-01-24 05:47:35 +0000961 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000962 case tok::kw_char:
963 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000964 case tok::kw_char16_t:
965 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000966 case tok::kw_bool:
967 case tok::kw_short:
968 case tok::kw_int:
969 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000970 case tok::kw___int64:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000971 case tok::kw_signed:
972 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000973 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000974 case tok::kw_float:
975 case tok::kw_double:
976 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000977 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000978 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +0000979 case tok::kw___vector: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000980 if (!getLang().CPlusPlus) {
981 Diag(Tok, diag::err_expected_expression);
982 return ExprError();
983 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000984
985 if (SavedKind == tok::kw_typename) {
986 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000987 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +0000988 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000989 return ExprError();
990 }
991
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000992 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000993 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000994 //
John McCall0b7e6782011-03-24 11:26:52 +0000995 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000996 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000997 if (Tok.isNot(tok::l_paren) &&
998 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000999 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1000 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001001
Richard Smith7fe62082011-10-15 05:09:34 +00001002 if (Tok.is(tok::l_brace))
1003 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1004
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001005 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +00001006 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001007 }
1008
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001009 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +00001010 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1011 // (We can end up in this situation after tentative parsing.)
1012 if (TryAnnotateTypeOrScopeToken())
1013 return ExprError();
1014 if (!Tok.is(tok::annot_cxxscope))
1015 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001016 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001017
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001018 Token Next = NextToken();
1019 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001020 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001021 if (TemplateId->Kind == TNK_Type_template) {
1022 // We have a qualified template-id that we know refers to a
1023 // type, translate it into a type and continue parsing as a
1024 // cast expression.
1025 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001026 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1027 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001028 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001029 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001030 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001031 }
1032 }
1033
1034 // Parse as an id-expression.
1035 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001036 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001037 }
1038
1039 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001040 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001041 if (TemplateId->Kind == TNK_Type_template) {
1042 // We have a template-id that we know refers to a type,
1043 // translate it into a type and continue parsing as a cast
1044 // expression.
1045 AnnotateTemplateIdTokenAsType();
1046 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001047 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001048 }
1049
1050 // Fall through to treat the template-id as an id-expression.
1051 }
1052
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001053 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001054 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001055 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001056
Chris Lattner74ba4102009-01-04 22:52:14 +00001057 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001058 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1059 // annotates the token, tail recurse.
1060 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001061 return ExprError();
1062 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001063 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1064
Chris Lattner74ba4102009-01-04 22:52:14 +00001065 // ::new -> [C++] new-expression
1066 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001067 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001068 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001069 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001070 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001071 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001073 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001074 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001075 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001076 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001077
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001078 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001079 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001080
1081 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001082 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001083
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001084 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001085 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001086 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001087 BalancedDelimiterTracker T(*this, tok::l_paren);
1088
1089 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001090 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001091 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001092 // The noexcept operator determines whether the evaluation of its operand,
1093 // which is an unevaluated operand, can throw an exception.
1094 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001095 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001096
1097 T.consumeClose();
1098
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001099 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001100 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1101 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001102 return move(Result);
1103 }
1104
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001105 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001106 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001107 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001108 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001109 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001110 case tok::kw___is_arithmetic:
1111 case tok::kw___is_integral:
1112 case tok::kw___is_floating_point:
1113 case tok::kw___is_complete_type:
1114 case tok::kw___is_void:
1115 case tok::kw___is_array:
1116 case tok::kw___is_function:
1117 case tok::kw___is_reference:
1118 case tok::kw___is_lvalue_reference:
1119 case tok::kw___is_rvalue_reference:
1120 case tok::kw___is_fundamental:
1121 case tok::kw___is_object:
1122 case tok::kw___is_scalar:
1123 case tok::kw___is_compound:
1124 case tok::kw___is_pointer:
1125 case tok::kw___is_member_object_pointer:
1126 case tok::kw___is_member_function_pointer:
1127 case tok::kw___is_member_pointer:
1128 case tok::kw___is_const:
1129 case tok::kw___is_volatile:
1130 case tok::kw___is_standard_layout:
1131 case tok::kw___is_signed:
1132 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001133 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001134 case tok::kw___is_pod:
1135 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001136 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001137 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001138 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001139 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001140 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001141 case tok::kw___has_trivial_copy:
1142 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001143 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001144 case tok::kw___has_nothrow_assign:
1145 case tok::kw___has_nothrow_copy:
1146 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001147 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001148 return ParseUnaryTypeTrait();
1149
Francois Pichetf1872372010-12-08 22:35:30 +00001150 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001151 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001152 case tok::kw___is_same:
1153 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001154 case tok::kw___is_convertible_to:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00001155 case tok::kw___is_trivially_assignable:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001156 return ParseBinaryTypeTrait();
1157
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001158 case tok::kw___is_trivially_constructible:
1159 return ParseTypeTrait();
1160
John Wiegley21ff2e52011-04-28 00:16:57 +00001161 case tok::kw___array_rank:
1162 case tok::kw___array_extent:
1163 return ParseArrayTypeTrait();
1164
John Wiegley55262202011-04-25 06:54:41 +00001165 case tok::kw___is_lvalue_expr:
1166 case tok::kw___is_rvalue_expr:
1167 return ParseExpressionTrait();
1168
Chris Lattnerc97c2042007-10-03 22:03:06 +00001169 case tok::at: {
1170 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001171 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001172 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001173 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001174 Res = ParseBlockLiteralExpression();
1175 break;
1176 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001177 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001178 cutOffParsing();
1179 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001180 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001181 case tok::l_square:
Douglas Gregorae7902c2011-08-04 15:30:47 +00001182 if (getLang().CPlusPlus0x) {
1183 if (getLang().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001184 // C++11 lambda expressions and Objective-C message sends both start with a
1185 // square bracket. There are three possibilities here:
1186 // we have a valid lambda expression, we have an invalid lambda
1187 // expression, or we have something that doesn't appear to be a lambda.
1188 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001189 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001190 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001191 Res = ParseObjCMessageExpression();
1192 break;
1193 }
1194 Res = ParseLambdaExpression();
1195 break;
1196 }
Chandler Carruthbb399022011-07-08 04:28:55 +00001197 if (getLang().ObjC1) {
1198 Res = ParseObjCMessageExpression();
1199 break;
1200 }
1201 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001203 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001204 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001206
John McCall9ae2f072010-08-23 23:25:46 +00001207 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001208 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209}
1210
1211/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1212/// is parsed, this method parses any suffixes that apply.
1213///
1214/// postfix-expression: [C99 6.5.2]
1215/// primary-expression
1216/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001217/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001218/// postfix-expression '(' argument-expression-list[opt] ')'
1219/// postfix-expression '.' identifier
1220/// postfix-expression '->' identifier
1221/// postfix-expression '++'
1222/// postfix-expression '--'
1223/// '(' type-name ')' '{' initializer-list '}'
1224/// '(' type-name ')' '{' initializer-list ',' '}'
1225///
1226/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001227/// argument-expression ...[opt]
1228/// argument-expression-list ',' assignment-expression ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001229///
John McCall60d7b3a2010-08-24 06:29:42 +00001230ExprResult
1231Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Now that the primary-expression piece of the postfix-expression has been
1233 // parsed, see if there are any postfix-expression pieces here.
1234 SourceLocation Loc;
1235 while (1) {
1236 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001237 case tok::code_completion:
1238 if (InMessageExpression)
1239 return move(LHS);
1240
Douglas Gregorac5fd842010-09-18 01:28:11 +00001241 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001242 cutOffParsing();
1243 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001244
Douglas Gregor0fbda682010-09-15 14:51:05 +00001245 case tok::identifier:
1246 // If we see identifier: after an expression, and we're not already in a
1247 // message send, then this is probably a message send with a missing
1248 // opening bracket '['.
1249 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001250 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001251 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1252 ParsedType(), LHS.get());
1253 break;
1254 }
1255
1256 // Fall through; this isn't a message send.
1257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001259 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001261 // If we have a array postfix expression that starts on a new line and
1262 // Objective-C is enabled, it is highly likely that the user forgot a
1263 // semicolon after the base expression and that the array postfix-expr is
1264 // actually another message send. In this case, do some look-ahead to see
1265 // if the contents of the square brackets are obviously not a valid
1266 // expression and recover by pretending there is no suffix.
1267 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1268 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001269 return move(LHS);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001270
1271 BalancedDelimiterTracker T(*this, tok::l_square);
1272 T.consumeOpen();
1273 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001274 ExprResult Idx;
Richard Smith7fe62082011-10-15 05:09:34 +00001275 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1276 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001277 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001278 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001279 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001280
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001282
1283 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001284 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1285 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001286 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001287 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001288
1289 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001290 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 break;
1292 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001293
Peter Collingbournebf36e252011-02-09 21:12:02 +00001294 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1295 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1296 // '(' argument-expression-list[opt] ')'
1297 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001298 InMessageExpressionRAIIObject InMessage(*this, false);
1299
Peter Collingbournebf36e252011-02-09 21:12:02 +00001300 Expr *ExecConfig = 0;
1301
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001302 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1303 BalancedDelimiterTracker PT(*this, tok::l_paren);
1304
Peter Collingbournebf36e252011-02-09 21:12:02 +00001305 if (OpKind == tok::lesslessless) {
1306 ExprVector ExecConfigExprs(Actions);
1307 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001308 LLLT.consumeOpen();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001309
1310 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1311 LHS = ExprError();
1312 }
1313
1314 if (LHS.isInvalid()) {
1315 SkipUntil(tok::greatergreatergreater);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001316 } else if (LLLT.consumeClose()) {
1317 // There was an error closing the brackets
Peter Collingbournebf36e252011-02-09 21:12:02 +00001318 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001319 }
1320
1321 if (!LHS.isInvalid()) {
1322 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1323 LHS = ExprError();
1324 else
1325 Loc = PrevTokLocation;
1326 }
1327
1328 if (!LHS.isInvalid()) {
1329 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001330 LLLT.getOpenLocation(),
1331 move_arg(ExecConfigExprs),
1332 LLLT.getCloseLocation());
Peter Collingbournebf36e252011-02-09 21:12:02 +00001333 if (ECResult.isInvalid())
1334 LHS = ExprError();
1335 else
1336 ExecConfig = ECResult.get();
1337 }
1338 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001339 PT.consumeOpen();
1340 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001341 }
1342
Sebastian Redla55e52c2008-11-25 22:21:31 +00001343 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001344 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001345
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001346 if (Tok.is(tok::code_completion)) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00001347 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1348 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001349 cutOffParsing();
1350 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001351 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001352
1353 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1354 if (Tok.isNot(tok::r_paren)) {
1355 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1356 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001357 LHS = ExprError();
1358 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 }
1360 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001361
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001363 if (LHS.isInvalid()) {
1364 SkipUntil(tok::r_paren);
1365 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001366 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001367 LHS = ExprError();
1368 } else {
1369 assert((ArgExprs.size() == 0 ||
1370 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001372 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001373 move_arg(ArgExprs), Tok.getLocation(),
1374 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001375 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 break;
1379 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001380 case tok::arrow:
1381 case tok::period: {
1382 // postfix-expression: p-e '->' template[opt] id-expression
1383 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 tok::TokenKind OpKind = Tok.getKind();
1385 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001386
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001387 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001388 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001389 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001390 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001391 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001392 OpLoc, OpKind, ObjectType,
1393 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001394 if (LHS.isInvalid())
1395 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001396
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001397 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1398 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001399 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001400 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001401 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001402 }
1403
Douglas Gregor81b747b2009-09-17 21:32:03 +00001404 if (Tok.is(tok::code_completion)) {
1405 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001406 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001407 OpLoc, OpKind == tok::arrow);
1408
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001409 cutOffParsing();
1410 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001411 }
1412
John McCall9ae2f072010-08-23 23:25:46 +00001413 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1414 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001415 ObjectType);
1416 break;
1417 }
1418
1419 // Either the action has told is that this cannot be a
1420 // pseudo-destructor expression (based on the type of base
1421 // expression), or we didn't see a '~' in the right place. We
1422 // can still parse a destructor name here, but in that case it
1423 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001424 // Allow explicit constructor calls in Microsoft mode.
1425 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001426 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001427 UnqualifiedId Name;
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001428 if (getLang().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
1429 // Objective-C++:
1430 // After a '.' in a member access expression, treat the keyword
1431 // 'class' as if it were an identifier.
1432 //
1433 // This hack allows property access to the 'class' method because it is
1434 // such a common method name. For other C++ keywords that are
1435 // Objective-C method names, one must use the message send syntax.
1436 IdentifierInfo *Id = Tok.getIdentifierInfo();
1437 SourceLocation Loc = ConsumeToken();
1438 Name.setIdentifier(Id, Loc);
1439 } else if (ParseUnqualifiedId(SS,
1440 /*EnteringContext=*/false,
1441 /*AllowDestructorName=*/true,
1442 /*AllowConstructorName=*/
1443 getLang().MicrosoftExt,
1444 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001445 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001446
1447 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001448 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001449 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001450 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1451 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 break;
1453 }
1454 case tok::plusplus: // postfix-expression: postfix-expression '++'
1455 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001456 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001457 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001458 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001459 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 ConsumeToken();
1461 break;
1462 }
1463 }
1464}
1465
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001466/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1467/// vec_step and we are at the start of an expression or a parenthesized
1468/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1469/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001470///
1471/// unary-expression: [C99 6.5.3]
1472/// 'sizeof' unary-expression
1473/// 'sizeof' '(' type-name ')'
1474/// [GNU] '__alignof' unary-expression
1475/// [GNU] '__alignof' '(' type-name ')'
1476/// [C++0x] 'alignof' '(' type-id ')'
1477///
1478/// [GNU] typeof-specifier:
1479/// typeof ( expressions )
1480/// typeof ( type-name )
1481/// [GNU/C++] typeof unary-expression
1482///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001483/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1484/// vec_step ( expressions )
1485/// vec_step ( type-name )
1486///
John McCall60d7b3a2010-08-24 06:29:42 +00001487ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001488Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1489 bool &isCastExpr,
1490 ParsedType &CastTy,
1491 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001492
1493 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001494 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1495 OpTok.is(tok::kw_vec_step)) &&
1496 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001497
John McCall60d7b3a2010-08-24 06:29:42 +00001498 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001500 // If the operand doesn't start with an '(', it must be an expression.
1501 if (Tok.isNot(tok::l_paren)) {
1502 isCastExpr = false;
1503 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1504 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1505 return ExprError();
1506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001508 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001509 } else {
1510 // If it starts with a '(', we know that it is either a parenthesized
1511 // type-name, or it is a unary-expression that starts with a compound
1512 // literal, or starts with a primary-expression that is a parenthesized
1513 // expression.
1514 ParenParseOption ExprType = CastExpr;
1515 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001517 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001518 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001519 CastRange = SourceRange(LParenLoc, RParenLoc);
1520
1521 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1522 // a type.
1523 if (ExprType == CastExpr) {
1524 isCastExpr = true;
1525 return ExprEmpty();
1526 }
1527
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001528 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1529 // GNU typeof in C requires the expression to be parenthesized. Not so for
1530 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1531 // the start of a unary-expression, but doesn't include any postfix
1532 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001533 if (!Operand.isInvalid())
1534 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001535 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001536 }
1537
1538 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1539 isCastExpr = false;
1540 return move(Operand);
1541}
1542
Reid Spencer5f016e22007-07-11 17:01:13 +00001543
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001544/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001545/// unary-expression: [C99 6.5.3]
1546/// 'sizeof' unary-expression
1547/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001548/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001549/// [GNU] '__alignof' unary-expression
1550/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001551/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001552ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001553 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001554 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1555 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001556 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001557 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Douglas Gregoree8aff02011-01-04 17:33:58 +00001559 // [C++0x] 'sizeof' '...' '(' identifier ')'
1560 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1561 SourceLocation EllipsisLoc = ConsumeToken();
1562 SourceLocation LParenLoc, RParenLoc;
1563 IdentifierInfo *Name = 0;
1564 SourceLocation NameLoc;
1565 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001566 BalancedDelimiterTracker T(*this, tok::l_paren);
1567 T.consumeOpen();
1568 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001569 if (Tok.is(tok::identifier)) {
1570 Name = Tok.getIdentifierInfo();
1571 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001572 T.consumeClose();
1573 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001574 if (RParenLoc.isInvalid())
1575 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1576 } else {
1577 Diag(Tok, diag::err_expected_parameter_pack);
1578 SkipUntil(tok::r_paren);
1579 }
1580 } else if (Tok.is(tok::identifier)) {
1581 Name = Tok.getIdentifierInfo();
1582 NameLoc = ConsumeToken();
1583 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1584 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1585 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1586 << Name
1587 << FixItHint::CreateInsertion(LParenLoc, "(")
1588 << FixItHint::CreateInsertion(RParenLoc, ")");
1589 } else {
1590 Diag(Tok, diag::err_sizeof_parameter_pack);
1591 }
1592
1593 if (!Name)
1594 return ExprError();
1595
1596 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1597 OpTok.getLocation(),
1598 *Name, NameLoc,
1599 RParenLoc);
1600 }
Richard Smith841804b2011-10-17 23:06:20 +00001601
1602 if (OpTok.is(tok::kw_alignof))
1603 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1604
Eli Friedman71b8fb52012-01-21 01:01:51 +00001605 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1606
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001607 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001608 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001609 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001610 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1611 isCastExpr,
1612 CastTy,
1613 CastRange);
1614
1615 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1616 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1617 ExprKind = UETT_AlignOf;
1618 else if (OpTok.is(tok::kw_vec_step))
1619 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001620
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001621 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001622 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1623 ExprKind,
1624 /*isType=*/true,
1625 CastTy.getAsOpaquePtr(),
1626 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001627
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001629 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001630 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1631 ExprKind,
1632 /*isType=*/false,
1633 Operand.release(),
1634 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001635 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001636}
1637
1638/// ParseBuiltinPrimaryExpression
1639///
1640/// primary-expression: [C99 6.5.1]
1641/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1642/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1643/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1644/// assign-expr ')'
1645/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001646/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001647///
Reid Spencer5f016e22007-07-11 17:01:13 +00001648/// [GNU] offsetof-member-designator:
1649/// [GNU] identifier
1650/// [GNU] offsetof-member-designator '.' identifier
1651/// [GNU] offsetof-member-designator '[' expression ']'
1652///
John McCall60d7b3a2010-08-24 06:29:42 +00001653ExprResult Parser::ParseBuiltinPrimaryExpression() {
1654 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1656
1657 tok::TokenKind T = Tok.getKind();
1658 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1659
1660 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001661 if (Tok.isNot(tok::l_paren))
1662 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1663 << BuiltinII);
1664
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001665 BalancedDelimiterTracker PT(*this, tok::l_paren);
1666 PT.consumeOpen();
1667
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 // TODO: Build AST.
1669
1670 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001671 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001672 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001673 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001674
1675 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001676 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001677
Douglas Gregor809070a2009-02-18 17:45:20 +00001678 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001679
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001680 if (Tok.isNot(tok::r_paren)) {
1681 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001682 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001683 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001684
1685 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001686 Res = ExprError();
1687 else
John McCall9ae2f072010-08-23 23:25:46 +00001688 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001690 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001691 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001692 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001693 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001694 if (Ty.isInvalid()) {
1695 SkipUntil(tok::r_paren);
1696 return ExprError();
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001700 return ExprError();
1701
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001703 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001704 Diag(Tok, diag::err_expected_ident);
1705 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001706 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001707 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001708
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001709 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001710 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001711
John McCallf312b1e2010-08-26 23:41:50 +00001712 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001713 Comps.back().isBrackets = false;
1714 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1715 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001716
Sebastian Redla55e52c2008-11-25 22:21:31 +00001717 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001719 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001721 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001722 Comps.back().isBrackets = false;
1723 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001724
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001725 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001726 Diag(Tok, diag::err_expected_ident);
1727 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001728 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001729 }
1730 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1731 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001732
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001733 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001735 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001736 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001737 BalancedDelimiterTracker ST(*this, tok::l_square);
1738 ST.consumeOpen();
1739 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001741 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001743 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001745 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001746
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001747 ST.consumeClose();
1748 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001749 } else {
1750 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001751 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001752 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001753 } else if (Ty.isInvalid()) {
1754 Res = ExprError();
1755 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001756 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001757 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001758 Ty.get(), &Comps[0], Comps.size(),
1759 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001760 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001761 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 }
1763 }
1764 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001765 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001766 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001767 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001768 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001769 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001770 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001771 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001773 return ExprError();
1774
John McCall60d7b3a2010-08-24 06:29:42 +00001775 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001776 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001777 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001778 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001779 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001781 return ExprError();
1782
John McCall60d7b3a2010-08-24 06:29:42 +00001783 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001784 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001785 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001786 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001787 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001788 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001789 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001790 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001791 }
John McCall9ae2f072010-08-23 23:25:46 +00001792 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1793 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001794 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001795 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001796 case tok::kw___builtin_astype: {
1797 // The first argument is an expression to be converted, followed by a comma.
1798 ExprResult Expr(ParseAssignmentExpression());
1799 if (Expr.isInvalid()) {
1800 SkipUntil(tok::r_paren);
1801 return ExprError();
1802 }
1803
1804 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1805 tok::r_paren))
1806 return ExprError();
1807
1808 // Second argument is the type to bitcast to.
1809 TypeResult DestTy = ParseTypeName();
1810 if (DestTy.isInvalid())
1811 return ExprError();
1812
1813 // Attempt to consume the r-paren.
1814 if (Tok.isNot(tok::r_paren)) {
1815 Diag(Tok, diag::err_expected_rparen);
1816 SkipUntil(tok::r_paren);
1817 return ExprError();
1818 }
1819
1820 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1821 ConsumeParen());
1822 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001823 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001824 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001825
John McCall9ae2f072010-08-23 23:25:46 +00001826 if (Res.isInvalid())
1827 return ExprError();
1828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // These can be followed by postfix-expr pieces because they are
1830 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001831 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001832}
1833
1834/// ParseParenExpression - This parses the unit that starts with a '(' token,
1835/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001836/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1837/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001838///
1839/// primary-expression: [C99 6.5.1]
1840/// '(' expression ')'
1841/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1842/// postfix-expression: [C99 6.5.2]
1843/// '(' type-name ')' '{' initializer-list '}'
1844/// '(' type-name ')' '{' initializer-list ',' '}'
1845/// cast-expression: [C99 6.5.4]
1846/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001847/// [ARC] bridged-cast-expression
1848///
1849/// [ARC] bridged-cast-expression:
1850/// (__bridge type-name) cast-expression
1851/// (__bridge_transfer type-name) cast-expression
1852/// (__bridge_retained type-name) cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001853ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001854Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001855 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001856 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001857 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001858 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001859 BalancedDelimiterTracker T(*this, tok::l_paren);
1860 if (T.consumeOpen())
1861 return ExprError();
1862 SourceLocation OpenLoc = T.getOpenLocation();
1863
John McCall60d7b3a2010-08-24 06:29:42 +00001864 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001865 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001866 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001867
Douglas Gregor02688102010-09-14 23:59:36 +00001868 if (Tok.is(tok::code_completion)) {
1869 Actions.CodeCompleteOrdinaryName(getCurScope(),
1870 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1871 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001872 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001873 return ExprError();
1874 }
John McCallb3c49062011-04-06 02:35:25 +00001875
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001876 // Diagnose use of bridge casts in non-arc mode.
1877 bool BridgeCast = (getLang().ObjC2 &&
1878 (Tok.is(tok::kw___bridge) ||
1879 Tok.is(tok::kw___bridge_transfer) ||
1880 Tok.is(tok::kw___bridge_retained) ||
1881 Tok.is(tok::kw___bridge_retain)));
1882 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001883 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001884 SourceLocation BridgeKeywordLoc = ConsumeToken();
1885 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremeneke698a5c2012-02-18 04:42:38 +00001886 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001887 << BridgeCastName
1888 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001889 BridgeCast = false;
1890 }
1891
John McCallb3c49062011-04-06 02:35:25 +00001892 // None of these cases should fall through with an invalid Result
1893 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001894 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall0b7e6782011-03-24 11:26:52 +00001896 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001897 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001899
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001900 // If the substmt parsed correctly, build the AST node.
John McCallb3c49062011-04-06 02:35:25 +00001901 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001902 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001903 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001904 tok::TokenKind tokenKind = Tok.getKind();
1905 SourceLocation BridgeKeywordLoc = ConsumeToken();
1906
John McCallf85e1932011-06-15 23:02:42 +00001907 // Parse an Objective-C ARC ownership cast expression.
1908 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001909 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001910 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001911 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001912 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001913 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001914 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001915 else {
1916 // As a hopefully temporary workaround, allow __bridge_retain as
1917 // a synonym for __bridge_retained, but only in system headers.
1918 assert(tokenKind == tok::kw___bridge_retain);
1919 Kind = OBC_BridgeRetained;
1920 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1921 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1922 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1923 "__bridge_retained");
1924 }
John McCallf85e1932011-06-15 23:02:42 +00001925
John McCallf85e1932011-06-15 23:02:42 +00001926 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001927 T.consumeClose();
1928 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001929 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001930
1931 if (Ty.isInvalid() || SubExpr.isInvalid())
1932 return ExprError();
1933
1934 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1935 BridgeKeywordLoc, Ty.get(),
1936 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001937 } else if (ExprType >= CompoundLiteral &&
1938 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Reid Spencer5f016e22007-07-11 17:01:13 +00001940 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001942 // In C++, if the type-id is ambiguous we disambiguate based on context.
1943 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1944 // in which case we should treat it as type-id.
1945 // if stopIfCastExpr is false, we need to determine the context past the
1946 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001947 if (isAmbiguousTypeId && !stopIfCastExpr) {
1948 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1949 RParenLoc = T.getCloseLocation();
1950 return res;
1951 }
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001953 // Parse the type declarator.
1954 DeclSpec DS(AttrFactory);
1955 ParseSpecifierQualifierList(DS);
1956 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1957 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00001958
Douglas Gregor77328d12010-09-15 23:19:31 +00001959 // If our type is followed by an identifier and either ':' or ']', then
1960 // this is probably an Objective-C message send where the leading '[' is
1961 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001962 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1963 !InMessageExpression && getLang().ObjC1 &&
1964 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1965 TypeResult Ty;
1966 {
1967 InMessageExpressionRAIIObject InMessage(*this, false);
1968 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1969 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001970 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1971 SourceLocation(),
1972 Ty.get(), 0);
1973 } else {
1974 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001975 T.consumeClose();
1976 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00001977 if (Tok.is(tok::l_brace)) {
1978 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001979 TypeResult Ty;
1980 {
1981 InMessageExpressionRAIIObject InMessage(*this, false);
1982 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1983 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001984 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00001985 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001986
Douglas Gregor77328d12010-09-15 23:19:31 +00001987 if (ExprType == CastExpr) {
1988 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001989
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001990 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00001991 return ExprError();
1992
Douglas Gregor77328d12010-09-15 23:19:31 +00001993 // Note that this doesn't parse the subsequent cast-expression, it just
1994 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001995 if (stopIfCastExpr) {
1996 TypeResult Ty;
1997 {
1998 InMessageExpressionRAIIObject InMessage(*this, false);
1999 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2000 }
2001 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00002002 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002003 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002004
2005 // Reject the cast of super idiom in ObjC.
2006 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
2007 Tok.getIdentifierInfo() == Ident_super &&
2008 getCurScope()->isInObjcMethodScope() &&
2009 GetLookAheadToken(1).isNot(tok::period)) {
2010 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2011 << SourceRange(OpenLoc, RParenLoc);
2012 return ExprError();
2013 }
2014
2015 // Parse the cast-expression that follows it next.
2016 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002017 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2018 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002019 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002020 if (!Result.isInvalid()) {
2021 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2022 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00002023 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002024 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002025 return move(Result);
2026 }
2027
2028 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2029 return ExprError();
2030 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002031 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002032 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002033 InMessageExpressionRAIIObject InMessage(*this, false);
2034
Nate Begeman2ef13e52009-08-10 23:49:36 +00002035 ExprVector ArgExprs(Actions);
2036 CommaLocsTy CommaLocs;
2037
2038 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2039 ExprType = SimpleExpr;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002040 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2041 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002042 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002044 InMessageExpressionRAIIObject InMessage(*this, false);
2045
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002046 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002048
2049 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002050 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002051 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002053
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002055 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002057 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002058 }
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002060 T.consumeClose();
2061 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002062 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002063}
2064
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002065/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2066/// and we are at the left brace.
2067///
2068/// postfix-expression: [C99 6.5.2]
2069/// '(' type-name ')' '{' initializer-list '}'
2070/// '(' type-name ')' '{' initializer-list ',' '}'
2071///
John McCall60d7b3a2010-08-24 06:29:42 +00002072ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002073Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002074 SourceLocation LParenLoc,
2075 SourceLocation RParenLoc) {
2076 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2077 if (!getLang().C99) // Compound literals don't exist in C90.
2078 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002079 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002080 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002081 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002082 return move(Result);
2083}
2084
Reid Spencer5f016e22007-07-11 17:01:13 +00002085/// ParseStringLiteralExpression - This handles the various token types that
2086/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2087/// translation phase #6].
2088///
2089/// primary-expression: [C99 6.5.1]
2090/// string-literal
John McCall60d7b3a2010-08-24 06:29:42 +00002091ExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002092 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002093
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2095 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002096 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002097
Reid Spencer5f016e22007-07-11 17:01:13 +00002098 do {
2099 StringToks.push_back(Tok);
2100 ConsumeStringToken();
2101 } while (isTokenStringLiteral());
2102
2103 // Pass the set of string tokens, ready for concatenation, to the actions.
Sean Hunt6cf75022010-08-30 17:47:05 +00002104 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00002105}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002106
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002107/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2108/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002109///
2110/// generic-selection:
2111/// _Generic ( assignment-expression , generic-assoc-list )
2112/// generic-assoc-list:
2113/// generic-association
2114/// generic-assoc-list , generic-association
2115/// generic-association:
2116/// type-name : assignment-expression
2117/// default : assignment-expression
2118ExprResult Parser::ParseGenericSelectionExpression() {
2119 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2120 SourceLocation KeyLoc = ConsumeToken();
2121
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002122 if (!getLang().C11)
2123 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002124
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002125 BalancedDelimiterTracker T(*this, tok::l_paren);
2126 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002127 return ExprError();
2128
2129 ExprResult ControllingExpr;
2130 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002131 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002132 // not evaluated."
2133 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2134 ControllingExpr = ParseAssignmentExpression();
2135 if (ControllingExpr.isInvalid()) {
2136 SkipUntil(tok::r_paren);
2137 return ExprError();
2138 }
2139 }
2140
2141 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2142 SkipUntil(tok::r_paren);
2143 return ExprError();
2144 }
2145
2146 SourceLocation DefaultLoc;
2147 TypeVector Types(Actions);
2148 ExprVector Exprs(Actions);
2149 while (1) {
2150 ParsedType Ty;
2151 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002152 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002153 // generic association."
2154 if (!DefaultLoc.isInvalid()) {
2155 Diag(Tok, diag::err_duplicate_default_assoc);
2156 Diag(DefaultLoc, diag::note_previous_default_assoc);
2157 SkipUntil(tok::r_paren);
2158 return ExprError();
2159 }
2160 DefaultLoc = ConsumeToken();
2161 Ty = ParsedType();
2162 } else {
2163 ColonProtectionRAIIObject X(*this);
2164 TypeResult TR = ParseTypeName();
2165 if (TR.isInvalid()) {
2166 SkipUntil(tok::r_paren);
2167 return ExprError();
2168 }
2169 Ty = TR.release();
2170 }
2171 Types.push_back(Ty);
2172
2173 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2174 SkipUntil(tok::r_paren);
2175 return ExprError();
2176 }
2177
2178 // FIXME: These expressions should be parsed in a potentially potentially
2179 // evaluated context.
2180 ExprResult ER(ParseAssignmentExpression());
2181 if (ER.isInvalid()) {
2182 SkipUntil(tok::r_paren);
2183 return ExprError();
2184 }
2185 Exprs.push_back(ER.release());
2186
2187 if (Tok.isNot(tok::comma))
2188 break;
2189 ConsumeToken();
2190 }
2191
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002192 T.consumeClose();
2193 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002194 return ExprError();
2195
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002196 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2197 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002198 ControllingExpr.release(),
2199 move_arg(Types), move_arg(Exprs));
2200}
2201
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002202/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2203///
2204/// argument-expression-list:
2205/// assignment-expression
2206/// argument-expression-list , assignment-expression
2207///
2208/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002209/// [C++] assignment-expression
2210/// [C++] expression-list , assignment-expression
2211///
2212/// [C++0x] expression-list:
2213/// [C++0x] initializer-list
2214///
2215/// [C++0x] initializer-list
2216/// [C++0x] initializer-clause ...[opt]
2217/// [C++0x] initializer-list , initializer-clause ...[opt]
2218///
2219/// [C++0x] initializer-clause:
2220/// [C++0x] assignment-expression
2221/// [C++0x] braced-init-list
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002222///
Chris Lattner5f9e2722011-07-23 10:55:15 +00002223bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2224 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002225 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002226 Expr *Data,
Ahmed Charles13a140c2012-02-25 11:00:22 +00002227 llvm::ArrayRef<Expr *> Args),
John McCallca0408f2010-08-23 06:44:23 +00002228 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002229 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002230 if (Tok.is(tok::code_completion)) {
2231 if (Completer)
Ahmed Charles13a140c2012-02-25 11:00:22 +00002232 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor4706e872011-02-17 03:09:23 +00002233 else
2234 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002235 cutOffParsing();
2236 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002237 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002238
2239 ExprResult Expr;
Richard Smith7fe62082011-10-15 05:09:34 +00002240 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2241 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002242 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002243 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002244 Expr = ParseAssignmentExpression();
2245
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002246 if (Tok.is(tok::ellipsis))
2247 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002248 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002249 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002250
Sebastian Redleffa8d12008-12-10 00:02:53 +00002251 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002252
2253 if (Tok.isNot(tok::comma))
2254 return false;
2255 // Move to the next argument, remember where the comma was.
2256 CommaLocs.push_back(ConsumeToken());
2257 }
2258}
Steve Naroff296e8d52008-08-28 19:20:44 +00002259
Mike Stump98eb8a72009-02-04 22:31:32 +00002260/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2261///
2262/// [clang] block-id:
2263/// [clang] specifier-qualifier-list block-declarator
2264///
2265void Parser::ParseBlockId() {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002266 if (Tok.is(tok::code_completion)) {
2267 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002268 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002269 }
2270
Mike Stump98eb8a72009-02-04 22:31:32 +00002271 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002272 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002273 ParseSpecifierQualifierList(DS);
2274
2275 // Parse the block-declarator.
2276 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2277 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002278
Mike Stump6c92fa72009-04-29 21:40:37 +00002279 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002280 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002281
John McCall7f040a92010-12-24 02:08:15 +00002282 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002283
Mike Stump98eb8a72009-02-04 22:31:32 +00002284 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002285 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002286}
2287
Steve Naroff296e8d52008-08-28 19:20:44 +00002288/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002289/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002290///
2291/// block-literal:
2292/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002293/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002294/// [clang] block-args:
2295/// [clang] '(' parameter-list ')'
2296///
John McCall60d7b3a2010-08-24 06:29:42 +00002297ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002298 assert(Tok.is(tok::caret) && "block literal starts with ^");
2299 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002300
Chris Lattner6b91f002009-03-05 07:32:12 +00002301 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2302 "block literal parsing");
2303
Mike Stump1eb44332009-09-09 15:08:12 +00002304 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002305 // argument decls, decls within the compound expression, etc. This also
2306 // allows determining whether a variable reference inside the block is
2307 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002308 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlab197ba2009-02-09 18:23:29 +00002309 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002310
2311 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002312 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Steve Naroff296e8d52008-08-28 19:20:44 +00002314 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002315 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002316 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002317 // FIXME: Since the return type isn't actually parsed, it can't be used to
2318 // fill ParamInfo with an initial valid range, so do it manually.
2319 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002320
Steve Naroff296e8d52008-08-28 19:20:44 +00002321 // If this block has arguments, parse them. There is no ambiguity here with
2322 // the expression case, because the expression case requires a parameter list.
2323 if (Tok.is(tok::l_paren)) {
2324 ParseParenDeclarator(ParamInfo);
2325 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002326 // SetIdentifier sets the source range end, but in this case we're past
2327 // that location.
2328 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002329 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002330 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002331 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002332 // If there was an error parsing the arguments, they may have
2333 // tried to use ^(x+y) which requires an argument list. Just
2334 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002335 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002336 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002337 }
Mike Stump19c30c02009-04-29 19:03:13 +00002338
John McCall7f040a92010-12-24 02:08:15 +00002339 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002340
Mike Stump98eb8a72009-02-04 22:31:32 +00002341 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002342 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002343 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002344 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00002345 } else {
2346 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002347 ParsedAttributes attrs(AttrFactory);
2348 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002349 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002350 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002351 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002352 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002353 SourceLocation(),
2354 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002355 EST_None,
2356 SourceLocation(),
2357 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002358 CaretLoc, CaretLoc,
2359 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002360 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002361
John McCall7f040a92010-12-24 02:08:15 +00002362 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002363
Mike Stump98eb8a72009-02-04 22:31:32 +00002364 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002365 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002366 }
2367
Sebastian Redl1d922962008-12-13 15:32:12 +00002368
John McCall60d7b3a2010-08-24 06:29:42 +00002369 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002370 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002371 // Saw something like: ^expr
2372 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002373 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002374 return ExprError();
2375 }
Mike Stump1eb44332009-09-09 15:08:12 +00002376
John McCall60d7b3a2010-08-24 06:29:42 +00002377 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002378 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002379 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002380 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002381 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002382 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002383 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002384}