blob: 65949edf41385930b3744f9ffa43bc23996b65a1 [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
Richard Smith5e4e58b2012-03-01 02:59:17 +0000285 if (!LHS.isInvalid() && isa<InitListExpr>(LHS.get())) {
286 Diag(OpToken, diag::err_init_list_bin_op)
287 << /*LHS*/0 << PP.getSpelling(OpToken) << LHS.get()->getSourceRange();
288 LHS = ExprError();
289 }
290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Special case handling for the ternary operator.
John McCall60d7b3a2010-08-24 06:29:42 +0000292 ExprResult TernaryMiddle(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000294 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000295 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
296 ColonProtectionRAIIObject X(*this);
297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // Handle this production specially:
299 // logical-OR-expression '?' expression ':' conditional-expression
300 // In particular, the RHS of the '?' is 'expression', not
301 // 'logical-OR-expression' as we might expect.
302 TernaryMiddle = ParseExpression();
Douglas Gregor94859892010-09-17 22:41:34 +0000303 if (TernaryMiddle.isInvalid()) {
304 LHS = ExprError();
305 TernaryMiddle = 0;
306 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 } else {
308 // Special case handling of "X ? Y : Z" where Y is empty:
309 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000310 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 Diag(Tok, diag::ext_gnu_conditional_expr);
312 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000313
Chris Lattnere5deae92010-04-20 21:33:39 +0000314 if (Tok.is(tok::colon)) {
315 // Eat the colon.
316 ColonLoc = ConsumeToken();
317 } else {
Chandler Carruthb00d37e2011-07-26 05:19:46 +0000318 // Otherwise, we're missing a ':'. Assume that this was a typo that
319 // the user forgot. If we're not in a macro expansion, we can suggest
320 // a fixit hint. If there were two spaces before the current token,
Chris Lattner24728822010-05-24 22:31:37 +0000321 // suggest inserting the colon in between them, otherwise insert ": ".
322 SourceLocation FILoc = Tok.getLocation();
323 const char *FIText = ": ";
Argyrios Kyrtzidisb5303aa2011-06-24 17:28:29 +0000324 const SourceManager &SM = PP.getSourceManager();
Argyrios Kyrtzidis69bda4c2012-01-19 15:59:08 +0000325 if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) {
326 assert(FILoc.isFileID());
Chris Lattner24728822010-05-24 22:31:37 +0000327 bool IsInvalid = false;
328 const char *SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000329 SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000330 if (!IsInvalid && *SourcePtr == ' ') {
331 SourcePtr =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000332 SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid);
Chris Lattner24728822010-05-24 22:31:37 +0000333 if (!IsInvalid && *SourcePtr == ' ') {
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000334 FILoc = FILoc.getLocWithOffset(-1);
Chris Lattner24728822010-05-24 22:31:37 +0000335 FIText = ":";
336 }
337 }
338 }
339
Ted Kremenek987aa872010-04-12 22:10:35 +0000340 Diag(Tok, diag::err_expected_colon)
Chris Lattner24728822010-05-24 22:31:37 +0000341 << FixItHint::CreateInsertion(FILoc, FIText);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000342 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000343 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000346
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000347 // Code completion for the right-hand side of an assignment expression
348 // goes through a special hook that takes the left-hand side into account.
349 if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000350 Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000351 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000352 return ExprError();
353 }
354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000356 // ParseCastExpression works here because all RHS expressions in C have it
357 // as a prefix, at least. However, in C++, an assignment-expression could
358 // be a throw-expression, which is not a valid cast-expression.
359 // Therefore we need some special-casing here.
360 // Also note that the third operand of the conditional operator is
Richard Smithc56ab432012-02-26 23:40:27 +0000361 // an assignment-expression in C++, and in C++11, we can have a
Richard Smith5e4e58b2012-03-01 02:59:17 +0000362 // braced-init-list on the RHS of an assignment. For better diagnostics,
363 // parse as if we were allowed braced-init-lists everywhere, and check that
364 // they only appear on the RHS of assignments later.
John McCall60d7b3a2010-08-24 06:29:42 +0000365 ExprResult RHS;
Richard Smith5e4e58b2012-03-01 02:59:17 +0000366 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace))
Richard Smithc56ab432012-02-26 23:40:27 +0000367 RHS = ParseBraceInitializer();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000368 else if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000369 RHS = ParseAssignmentExpression();
Richard Smith5e4e58b2012-03-01 02:59:17 +0000370 else
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000371 RHS = ParseCastExpression(false);
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)) {
Richard Smith5e4e58b2012-03-01 02:59:17 +0000390 if (!LHS.isInvalid() && isa<InitListExpr>(LHS.get())) {
391 Diag(OpToken, diag::err_init_list_bin_op)
392 << /*LHS*/0 << PP.getSpelling(OpToken) << LHS.get()->getSourceRange();
393 LHS = ExprError();
394 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 // If this is left-associative, only parse things on the RHS that bind
396 // more tightly than the current operator. If it is left-associative, it
397 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
398 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000399 // The function takes ownership of the RHS.
Douglas Gregor200b2922010-09-17 22:25:06 +0000400 RHS = ParseRHSOfBinaryExpression(RHS,
Douglas Gregor6aa14d82010-04-21 22:36:40 +0000401 static_cast<prec::Level>(ThisPrec + !isRightAssoc));
Douglas Gregor200b2922010-09-17 22:25:06 +0000402
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000403 if (RHS.isInvalid())
Douglas Gregor200b2922010-09-17 22:25:06 +0000404 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000405
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000406 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
407 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
409 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000410
Richard Smith5e4e58b2012-03-01 02:59:17 +0000411 if (!RHS.isInvalid() && isa<InitListExpr>(RHS.get())) {
412 if (ThisPrec == prec::Assignment) {
413 Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
414 << RHS.get()->getSourceRange();
415 } else {
416 Diag(OpToken, diag::err_init_list_bin_op)
417 << /*RHS*/1 << PP.getSpelling(OpToken) << RHS.get()->getSourceRange();
418 LHS = ExprError();
419 }
420 }
421
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000422 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000423 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000424 if (TernaryMiddle.isInvalid()) {
425 // If we're using '>>' as an operator within a template
426 // argument list (in C++98), suggest the addition of
427 // parentheses so that the code remains well-formed in C++0x.
428 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
429 SuggestParentheses(OpToken.getLocation(),
430 diag::warn_cxx0x_right_shift_in_template_arg,
431 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
432 Actions.getExprRange(RHS.get()).getEnd()));
433
Douglas Gregor23c94db2010-07-02 17:43:08 +0000434 LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000435 OpToken.getKind(), LHS.take(), RHS.take());
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000436 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000437 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000438 LHS.take(), TernaryMiddle.take(),
439 RHS.take());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000440 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 }
442}
443
444/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000445/// true, parse a unary-expression. isAddressOfOperand exists because an
446/// id-expression that is the operand of address-of gets special treatment
447/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000448///
John McCall60d7b3a2010-08-24 06:29:42 +0000449ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000450 bool isAddressOfOperand,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000451 TypeCastState isTypeCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000452 bool NotCastExpr;
John McCall60d7b3a2010-08-24 06:29:42 +0000453 ExprResult Res = ParseCastExpression(isUnaryExpression,
Douglas Gregor200b2922010-09-17 22:25:06 +0000454 isAddressOfOperand,
455 NotCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +0000456 isTypeCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000457 if (NotCastExpr)
458 Diag(Tok, diag::err_expected_expression);
459 return move(Res);
460}
461
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000462namespace {
463class CastExpressionIdValidator : public CorrectionCandidateCallback {
464 public:
465 CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes)
466 : AllowNonTypes(AllowNonTypes) {
467 WantTypeSpecifiers = AllowTypes;
468 }
469
470 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
471 NamedDecl *ND = candidate.getCorrectionDecl();
472 if (!ND)
473 return candidate.isKeyword();
474
475 if (isa<TypeDecl>(ND))
476 return WantTypeSpecifiers;
477 return AllowNonTypes;
478 }
479
480 private:
481 bool AllowNonTypes;
482};
483}
484
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000485/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
486/// true, parse a unary-expression. isAddressOfOperand exists because an
487/// id-expression that is the operand of address-of gets special treatment
488/// due to member pointers. NotCastExpr is set to true if the token is not the
489/// start of a cast-expression, and no diagnostic is emitted in this case.
490///
Reid Spencer5f016e22007-07-11 17:01:13 +0000491/// cast-expression: [C99 6.5.4]
492/// unary-expression
493/// '(' type-name ')' cast-expression
494///
495/// unary-expression: [C99 6.5.3]
496/// postfix-expression
497/// '++' unary-expression
498/// '--' unary-expression
499/// unary-operator cast-expression
500/// 'sizeof' unary-expression
501/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +0000502/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000503/// [GNU] '__alignof' unary-expression
504/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000505/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000506/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000507/// [C++] new-expression
508/// [C++] delete-expression
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000509/// [C++0x] 'noexcept' '(' expression ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000510///
511/// unary-operator: one of
512/// '&' '*' '+' '-' '~' '!'
513/// [GNU] '__extension__' '__real' '__imag'
514///
515/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000516/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000517/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000518/// constant
519/// string-literal
520/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000521/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000522/// '(' expression ')'
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000523/// [C11] generic-selection
Reid Spencer5f016e22007-07-11 17:01:13 +0000524/// '__func__' [C99 6.4.2.2]
525/// [GNU] '__FUNCTION__'
526/// [GNU] '__PRETTY_FUNCTION__'
527/// [GNU] '(' compound-statement ')'
528/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
529/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
530/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
531/// assign-expr ')'
532/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000533/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000534/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000535/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000536/// [OBJC] '@protocol' '(' identifier ')'
537/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000538/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000539/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000540/// [C++0x] simple-type-specifier braced-init-list [C++ 5.2.3]
Douglas Gregor2725ca82010-04-21 19:57:20 +0000541/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000542/// [C++0x] typename-specifier braced-init-list [C++ 5.2.3]
Reid Spencer5f016e22007-07-11 17:01:13 +0000543/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
544/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
545/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
546/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000547/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
548/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000549/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000550/// [G++] unary-type-trait '(' type-id ')'
551/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
John Wiegley21ff2e52011-04-28 00:16:57 +0000552/// [EMBT] array-type-trait '(' type-id ',' integer ')'
Steve Naroff296e8d52008-08-28 19:20:44 +0000553/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000554///
555/// constant: [C99 6.4.4]
556/// integer-constant
557/// floating-constant
558/// enumeration-constant -> identifier
559/// character-constant
560///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000561/// id-expression: [C++ 5.1]
562/// unqualified-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000563/// qualified-id
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000564///
565/// unqualified-id: [C++ 5.1]
566/// identifier
567/// operator-function-id
Douglas Gregor2725ca82010-04-21 19:57:20 +0000568/// conversion-function-id
569/// '~' class-name
570/// template-id
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000571///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000572/// new-expression: [C++ 5.3.4]
573/// '::'[opt] 'new' new-placement[opt] new-type-id
574/// new-initializer[opt]
575/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
576/// new-initializer[opt]
577///
578/// delete-expression: [C++ 5.3.5]
579/// '::'[opt] 'delete' cast-expression
580/// '::'[opt] 'delete' '[' ']' cast-expression
581///
John Wiegley20c0da72011-04-27 23:09:49 +0000582/// [GNU/Embarcadero] unary-type-trait:
583/// '__is_arithmetic'
584/// '__is_floating_point'
585/// '__is_integral'
586/// '__is_lvalue_expr'
587/// '__is_rvalue_expr'
588/// '__is_complete_type'
589/// '__is_void'
590/// '__is_array'
591/// '__is_function'
592/// '__is_reference'
593/// '__is_lvalue_reference'
594/// '__is_rvalue_reference'
595/// '__is_fundamental'
596/// '__is_object'
597/// '__is_scalar'
598/// '__is_compound'
599/// '__is_pointer'
600/// '__is_member_object_pointer'
601/// '__is_member_function_pointer'
602/// '__is_member_pointer'
603/// '__is_const'
604/// '__is_volatile'
605/// '__is_trivial'
606/// '__is_standard_layout'
607/// '__is_signed'
608/// '__is_unsigned'
609///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000610/// [GNU] unary-type-trait:
Sebastian Redlc238f092010-08-31 04:59:00 +0000611/// '__has_nothrow_assign'
612/// '__has_nothrow_copy'
613/// '__has_nothrow_constructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000614/// '__has_trivial_assign' [TODO]
615/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000616/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000617/// '__has_trivial_destructor'
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000618/// '__has_virtual_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000619/// '__is_abstract' [TODO]
620/// '__is_class'
621/// '__is_empty' [TODO]
622/// '__is_enum'
Douglas Gregor5e9392b2011-12-03 18:14:24 +0000623/// '__is_final'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000624/// '__is_pod'
625/// '__is_polymorphic'
Chandler Carruthb7e95892011-04-23 10:47:28 +0000626/// '__is_trivial'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000627/// '__is_union'
628///
Sean Huntfeb375d2011-05-13 00:31:07 +0000629/// [Clang] unary-type-trait:
630/// '__trivially_copyable'
631///
Douglas Gregor9f361132011-01-27 20:28:01 +0000632/// binary-type-trait:
633/// [GNU] '__is_base_of'
634/// [MS] '__is_convertible_to'
John Wiegley20c0da72011-04-27 23:09:49 +0000635/// '__is_convertible'
636/// '__is_same'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000637///
John Wiegley21ff2e52011-04-28 00:16:57 +0000638/// [Embarcadero] array-type-trait:
639/// '__array_rank'
640/// '__array_extent'
641///
John Wiegley55262202011-04-25 06:54:41 +0000642/// [Embarcadero] expression-trait:
643/// '__is_lvalue_expr'
644/// '__is_rvalue_expr'
645///
John McCall60d7b3a2010-08-24 06:29:42 +0000646ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Sebastian Redl02bc21a2010-09-10 20:55:37 +0000647 bool isAddressOfOperand,
648 bool &NotCastExpr,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000649 TypeCastState isTypeCast) {
John McCall60d7b3a2010-08-24 06:29:42 +0000650 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000652 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 // This handles all of cast-expression, unary-expression, postfix-expression,
655 // and primary-expression. We handle them together like this for efficiency
656 // and to simplify handling of an expression starting with a '(' token: which
657 // may be one of a parenthesized expression, cast-expression, compound literal
658 // expression, or statement expression.
659 //
660 // If the parsed tokens consist of a primary-expression, the cases below
John McCall9ae2f072010-08-23 23:25:46 +0000661 // break out of the switch; at the end we call ParsePostfixExpressionSuffix
662 // to handle the postfix expression suffixes. Cases that cannot be followed
663 // by postfix exprs should return without invoking
664 // ParsePostfixExpressionSuffix.
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 switch (SavedKind) {
666 case tok::l_paren: {
667 // If this expression is limited to being a unary-expression, the parent can
668 // not start a cast expression.
669 ParenParseOption ParenExprType =
Douglas Gregord4206632010-08-06 14:50:36 +0000670 (isUnaryExpression && !getLang().CPlusPlus)? CompoundLiteral : CastExpr;
John McCallb3d87482010-08-24 05:47:05 +0000671 ParsedType CastTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000673
674 {
Douglas Gregor0fbda682010-09-15 14:51:05 +0000675 // The inside of the parens don't need to be a colon protected scope, and
676 // isn't immediately a message send.
Chris Lattner932dff72009-12-10 02:08:07 +0000677 ColonProtectionRAIIObject X(*this, false);
Douglas Gregor0fbda682010-09-15 14:51:05 +0000678
Chris Lattner932dff72009-12-10 02:08:07 +0000679 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000680 isTypeCast == IsTypeCast, CastTy, RParenLoc);
Chris Lattner932dff72009-12-10 02:08:07 +0000681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 switch (ParenExprType) {
684 case SimpleExpr: break; // Nothing else to do.
685 case CompoundStmt: break; // Nothing else to do.
686 case CompoundLiteral:
687 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
688 // postfix-expression exist, parse them now.
689 break;
690 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000691 // We have parsed the cast-expression and no postfix-expr pieces are
692 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000693 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000695
John McCall9ae2f072010-08-23 23:25:46 +0000696 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 // primary-expression
700 case tok::numeric_constant:
701 // constant: integer-constant
702 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000703
Steve Narofff69936d2007-09-16 03:34:24 +0000704 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000706 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000707
708 case tok::kw_true:
709 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000710 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000711
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000712 case tok::kw_nullptr:
Richard Smith841804b2011-10-17 23:06:20 +0000713 Diag(Tok, diag::warn_cxx98_compat_nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000714 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
715
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000716 case tok::annot_primary_expr:
717 assert(Res.get() == 0 && "Stray primary-expression annotation?");
718 Res = getExprAnnotation(Tok);
719 ConsumeToken();
720 break;
721
David Blaikie42d6d0c2011-12-04 05:04:18 +0000722 case tok::kw_decltype:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000723 case tok::identifier: { // primary-expression: identifier
724 // unqualified-id: identifier
725 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000726 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000727 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000728 if (getLang().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000729 // Avoid the unnecessary parse-time lookup in the common case
730 // where the syntax forbids a type.
731 const Token &Next = NextToken();
732 if (Next.is(tok::coloncolon) ||
733 (!ColonIsSacred && Next.is(tok::colon)) ||
734 Next.is(tok::less) ||
Sebastian Redl62f13c92011-12-22 18:58:29 +0000735 Next.is(tok::l_paren) ||
736 Next.is(tok::l_brace)) {
John McCallb6727072010-01-07 19:29:58 +0000737 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
738 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000739 return ExprError();
740 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000741 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
742 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000743 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000744
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000745 // Consume the identifier so that we can see if it is followed by a '(' or
746 // '.'.
747 IdentifierInfo &II = *Tok.getIdentifierInfo();
748 SourceLocation ILoc = ConsumeToken();
749
Chris Lattnereb483eb2010-04-11 08:28:14 +0000750 // Support 'Class.property' and 'super.property' notation.
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000751 if (getLang().ObjC1 && Tok.is(tok::period) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000752 (Actions.getTypeName(II, ILoc, getCurScope()) ||
Chris Lattner236beab2010-04-12 06:20:33 +0000753 // Allow the base to be 'super' if in an objc-method.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000754 (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) {
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000755 ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000756
Douglas Gregor8f70bda2012-02-16 18:19:22 +0000757 // Allow either an identifier or the keyword 'class' (in C++).
758 if (Tok.isNot(tok::identifier) &&
759 !(getLang().CPlusPlus && Tok.is(tok::kw_class))) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000760 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000761 return ExprError();
762 }
763 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
764 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000765
766 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
767 ILoc, PropertyLoc);
John McCall9ae2f072010-08-23 23:25:46 +0000768 break;
Steve Naroff61f72cb2009-03-09 21:12:44 +0000769 }
John McCall9c72c602010-08-27 09:08:28 +0000770
Douglas Gregorfa885c12010-09-15 15:09:43 +0000771 // In an Objective-C method, if we have "super" followed by an identifier,
Douglas Gregor78edf512010-09-15 16:23:04 +0000772 // the token sequence is ill-formed. However, if there's a ':' or ']' after
Douglas Gregorfa885c12010-09-15 15:09:43 +0000773 // that identifier, this is probably a message send with a missing open
Douglas Gregor78edf512010-09-15 16:23:04 +0000774 // bracket. Treat it as such.
775 if (getLang().ObjC1 && &II == Ident_super && !InMessageExpression &&
Douglas Gregorfa885c12010-09-15 15:09:43 +0000776 getCurScope()->isInObjcMethodScope() &&
Douglas Gregor78edf512010-09-15 16:23:04 +0000777 ((Tok.is(tok::identifier) &&
778 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) ||
779 Tok.is(tok::code_completion))) {
Douglas Gregorfa885c12010-09-15 15:09:43 +0000780 Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(),
781 0);
782 break;
783 }
784
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000785 // If we have an Objective-C class name followed by an identifier
786 // and either ':' or ']', this is an Objective-C class message
787 // send that's missing the opening '['. Recovery
788 // appropriately. Also take this path if we're performing code
789 // completion after an Objective-C class name.
790 if (getLang().ObjC1 &&
791 ((Tok.is(tok::identifier) && !InMessageExpression) ||
792 Tok.is(tok::code_completion))) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000793 const Token& Next = NextToken();
Douglas Gregor97d7ff02011-02-15 19:17:31 +0000794 if (Tok.is(tok::code_completion) ||
795 Next.is(tok::colon) || Next.is(tok::r_square))
Gabor Greif9fe871a2010-09-17 10:21:45 +0000796 if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope()))
797 if (Typ.get()->isObjCObjectOrInterfaceType()) {
Douglas Gregor9497a732010-09-16 01:51:54 +0000798 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000799 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000800 DS.SetRangeStart(ILoc);
801 DS.SetRangeEnd(ILoc);
802 const char *PrevSpec = 0;
803 unsigned DiagID;
Gabor Greif9fe871a2010-09-17 10:21:45 +0000804 DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ);
Douglas Gregor9497a732010-09-16 01:51:54 +0000805
806 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
807 TypeResult Ty = Actions.ActOnTypeName(getCurScope(),
808 DeclaratorInfo);
809 if (Ty.isInvalid())
810 break;
811
812 Res = ParseObjCMessageExpressionBody(SourceLocation(),
813 SourceLocation(),
814 Ty.get(), 0);
815 break;
816 }
817 }
818
John McCall9c72c602010-08-27 09:08:28 +0000819 // Make sure to pass down the right value for isAddressOfOperand.
820 if (isAddressOfOperand && isPostfixExpressionSuffixStart())
821 isAddressOfOperand = false;
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000822
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
824 // need to know whether or not this identifier is a function designator or
825 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000826 UnqualifiedId Name;
827 CXXScopeSpec ScopeSpec;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000828 SourceLocation TemplateKWLoc;
Kaelyn Uhraincd78e612012-01-25 20:49:08 +0000829 CastExpressionIdValidator Validator(isTypeCast != NotTypeCast,
830 isTypeCast != IsTypeCast);
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000831 Name.setIdentifier(&II, ILoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000832 Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc,
833 Name, Tok.is(tok::l_paren),
834 isAddressOfOperand, &Validator);
John McCall9ae2f072010-08-23 23:25:46 +0000835 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 }
837 case tok::char_constant: // constant: character-constant
Douglas Gregor5cee1192011-07-27 05:40:30 +0000838 case tok::wide_char_constant:
839 case tok::utf16_char_constant:
840 case tok::utf32_char_constant:
Steve Narofff69936d2007-09-16 03:34:24 +0000841 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000843 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
845 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
846 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000847 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 ConsumeToken();
John McCall9ae2f072010-08-23 23:25:46 +0000849 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 case tok::string_literal: // primary-expression: string-literal
851 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000852 case tok::utf8_string_literal:
853 case tok::utf16_string_literal:
854 case tok::utf32_string_literal:
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 Res = ParseStringLiteralExpression();
John McCall9ae2f072010-08-23 23:25:46 +0000856 break;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000857 case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1]
Peter Collingbournef111d932011-04-15 00:35:48 +0000858 Res = ParseGenericSelectionExpression();
859 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 case tok::kw___builtin_va_arg:
861 case tok::kw___builtin_offsetof:
862 case tok::kw___builtin_choose_expr:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000863 case tok::kw___builtin_astype: // primary-expression: [OCL] as_type()
Sebastian Redld8c4e152008-12-11 22:33:27 +0000864 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000865 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000866 return Actions.ActOnGNUNullExpr(ConsumeToken());
Chandler Carruth3c7fddd2011-07-08 04:59:44 +0000867
Douglas Gregord4206632010-08-06 14:50:36 +0000868 case tok::plusplus: // unary-expression: '++' unary-expression [C99]
869 case tok::minusminus: { // unary-expression: '--' unary-expression [C99]
870 // C++ [expr.unary] has:
871 // unary-expression:
872 // ++ cast-expression
873 // -- cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 SourceLocation SavedLoc = ConsumeToken();
Douglas Gregord4206632010-08-06 14:50:36 +0000875 Res = ParseCastExpression(!getLang().CPlusPlus);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000876 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000877 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000878 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000880 case tok::amp: { // unary-expression: '&' cast-expression
881 // Special treatment because of member pointers
882 SourceLocation SavedLoc = ConsumeToken();
883 Res = ParseCastExpression(false, true);
884 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000885 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redlebc07d52009-02-03 20:19:35 +0000886 return move(Res);
887 }
888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 case tok::star: // unary-expression: '*' cast-expression
890 case tok::plus: // unary-expression: '+' cast-expression
891 case tok::minus: // unary-expression: '-' cast-expression
892 case tok::tilde: // unary-expression: '~' cast-expression
893 case tok::exclaim: // unary-expression: '!' cast-expression
894 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000895 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 SourceLocation SavedLoc = ConsumeToken();
897 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000899 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000900 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000901 }
902
Chris Lattner35080842008-02-02 20:20:10 +0000903 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
904 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000905 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000906 SourceLocation SavedLoc = ConsumeToken();
907 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000908 if (!Res.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +0000909 Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000910 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 }
912 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
913 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000914 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
916 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000917 // unary-expression: 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000918 case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression
919 return ParseUnaryExprOrTypeTraitExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 case tok::ampamp: { // unary-expression: '&&' identifier
921 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000922 if (Tok.isNot(tok::identifier))
923 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000924
Chris Lattnerfebb5b82011-02-18 21:16:39 +0000925 if (getCurScope()->getFnParent() == 0)
926 return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn));
927
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Chris Lattner337e5502011-02-18 01:27:55 +0000929 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
930 Tok.getLocation());
931 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000933 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 }
935 case tok::kw_const_cast:
936 case tok::kw_dynamic_cast:
937 case tok::kw_reinterpret_cast:
938 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000939 Res = ParseCXXCasts();
John McCall9ae2f072010-08-23 23:25:46 +0000940 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000941 case tok::kw_typeid:
942 Res = ParseCXXTypeid();
John McCall9ae2f072010-08-23 23:25:46 +0000943 break;
Francois Pichet01b7c302010-09-08 12:20:18 +0000944 case tok::kw___uuidof:
945 Res = ParseCXXUuidof();
946 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000947 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000948 Res = ParseCXXThis();
John McCall9ae2f072010-08-23 23:25:46 +0000949 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000950
Douglas Gregor9497a732010-09-16 01:51:54 +0000951 case tok::annot_typename:
952 if (isStartOfObjCClassMessageMissingOpenBracket()) {
953 ParsedType Type = getTypeAnnotation(Tok);
954
955 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000956 DeclSpec DS(AttrFactory);
Douglas Gregor9497a732010-09-16 01:51:54 +0000957 DS.SetRangeStart(Tok.getLocation());
958 DS.SetRangeEnd(Tok.getLastLoc());
959
960 const char *PrevSpec = 0;
961 unsigned DiagID;
Nico Weber253e80b2010-11-22 10:30:56 +0000962 DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(),
963 PrevSpec, DiagID, Type);
Douglas Gregor9497a732010-09-16 01:51:54 +0000964
965 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
966 TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
967 if (Ty.isInvalid())
968 break;
969
970 ConsumeToken();
971 Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
972 Ty.get(), 0);
973 break;
974 }
975 // Fall through
976
David Blaikie5e089fe2012-01-24 05:47:35 +0000977 case tok::annot_decltype:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000978 case tok::kw_char:
979 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000980 case tok::kw_char16_t:
981 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000982 case tok::kw_bool:
983 case tok::kw_short:
984 case tok::kw_int:
985 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000986 case tok::kw___int64:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000987 case tok::kw_signed:
988 case tok::kw_unsigned:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000989 case tok::kw_half:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000990 case tok::kw_float:
991 case tok::kw_double:
992 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000993 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000994 case tok::kw_typeof:
Douglas Gregor9497a732010-09-16 01:51:54 +0000995 case tok::kw___vector: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000996 if (!getLang().CPlusPlus) {
997 Diag(Tok, diag::err_expected_expression);
998 return ExprError();
999 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001000
1001 if (SavedKind == tok::kw_typename) {
1002 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001003 // typename-specifier braced-init-list
John McCall9ba61662010-02-26 08:45:28 +00001004 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +00001005 return ExprError();
1006 }
1007
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001008 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001009 // simple-type-specifier braced-init-list
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001010 //
John McCall0b7e6782011-03-24 11:26:52 +00001011 DeclSpec DS(AttrFactory);
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001012 ParseCXXSimpleTypeSpecifier(DS);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001013 if (Tok.isNot(tok::l_paren) &&
1014 (!getLang().CPlusPlus0x || Tok.isNot(tok::l_brace)))
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001015 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
1016 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001017
Richard Smith7fe62082011-10-15 05:09:34 +00001018 if (Tok.is(tok::l_brace))
1019 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1020
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001021 Res = ParseCXXTypeConstructExpression(DS);
John McCall9ae2f072010-08-23 23:25:46 +00001022 break;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001023 }
1024
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001025 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
Douglas Gregor4074eef2010-04-23 02:08:13 +00001026 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
1027 // (We can end up in this situation after tentative parsing.)
1028 if (TryAnnotateTypeOrScopeToken())
1029 return ExprError();
1030 if (!Tok.is(tok::annot_cxxscope))
1031 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001032 NotCastExpr, isTypeCast);
Douglas Gregor4074eef2010-04-23 02:08:13 +00001033
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001034 Token Next = NextToken();
1035 if (Next.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001036 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001037 if (TemplateId->Kind == TNK_Type_template) {
1038 // We have a qualified template-id that we know refers to a
1039 // type, translate it into a type and continue parsing as a
1040 // cast expression.
1041 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001042 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1043 /*EnteringContext=*/false);
Douglas Gregor059101f2011-03-02 00:47:37 +00001044 AnnotateTemplateIdTokenAsType();
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001045 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001046 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001047 }
1048 }
1049
1050 // Parse as an id-expression.
1051 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001052 break;
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001053 }
1054
1055 case tok::annot_template_id: { // [C++] template-id
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001056 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001057 if (TemplateId->Kind == TNK_Type_template) {
1058 // We have a template-id that we know refers to a type,
1059 // translate it into a type and continue parsing as a cast
1060 // expression.
1061 AnnotateTemplateIdTokenAsType();
1062 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001063 NotCastExpr, isTypeCast);
Douglas Gregorae4c77d2010-02-05 19:11:37 +00001064 }
1065
1066 // Fall through to treat the template-id as an id-expression.
1067 }
1068
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001069 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +00001070 Res = ParseCXXIdExpression(isAddressOfOperand);
John McCall9ae2f072010-08-23 23:25:46 +00001071 break;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001072
Chris Lattner74ba4102009-01-04 22:52:14 +00001073 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +00001074 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
1075 // annotates the token, tail recurse.
1076 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001077 return ExprError();
1078 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +00001079 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
1080
Chris Lattner74ba4102009-01-04 22:52:14 +00001081 // ::new -> [C++] new-expression
1082 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +00001083 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +00001084 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +00001085 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +00001086 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +00001087 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001089 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +00001090 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001091 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +00001092 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +00001093
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001094 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001095 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001096
1097 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +00001098 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001099
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001100 case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')'
Richard Smith841804b2011-10-17 23:06:20 +00001101 Diag(Tok, diag::warn_cxx98_compat_noexcept_expr);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001102 SourceLocation KeyLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001103 BalancedDelimiterTracker T(*this, tok::l_paren);
1104
1105 if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept"))
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001106 return ExprError();
Richard Smithf6702a32011-12-20 02:08:33 +00001107 // C++11 [expr.unary.noexcept]p1:
Sebastian Redlbd7c8492010-09-10 21:57:27 +00001108 // The noexcept operator determines whether the evaluation of its operand,
1109 // which is an unevaluated operand, can throw an exception.
1110 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001111 ExprResult Result = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001112
1113 T.consumeClose();
1114
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001115 if (!Result.isInvalid())
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001116 Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(),
1117 Result.take(), T.getCloseLocation());
Sebastian Redl02bc21a2010-09-10 20:55:37 +00001118 return move(Result);
1119 }
1120
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001121 case tok::kw___is_abstract: // [GNU] unary-type-trait
Sebastian Redl64b45f72009-01-05 20:52:13 +00001122 case tok::kw___is_class:
Eli Friedman1d954f62009-08-15 21:55:26 +00001123 case tok::kw___is_empty:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001124 case tok::kw___is_enum:
Sebastian Redlccf43502009-12-03 00:13:20 +00001125 case tok::kw___is_literal:
John Wiegley20c0da72011-04-27 23:09:49 +00001126 case tok::kw___is_arithmetic:
1127 case tok::kw___is_integral:
1128 case tok::kw___is_floating_point:
1129 case tok::kw___is_complete_type:
1130 case tok::kw___is_void:
1131 case tok::kw___is_array:
1132 case tok::kw___is_function:
1133 case tok::kw___is_reference:
1134 case tok::kw___is_lvalue_reference:
1135 case tok::kw___is_rvalue_reference:
1136 case tok::kw___is_fundamental:
1137 case tok::kw___is_object:
1138 case tok::kw___is_scalar:
1139 case tok::kw___is_compound:
1140 case tok::kw___is_pointer:
1141 case tok::kw___is_member_object_pointer:
1142 case tok::kw___is_member_function_pointer:
1143 case tok::kw___is_member_pointer:
1144 case tok::kw___is_const:
1145 case tok::kw___is_volatile:
1146 case tok::kw___is_standard_layout:
1147 case tok::kw___is_signed:
1148 case tok::kw___is_unsigned:
Chandler Carruth38402812011-04-24 02:49:28 +00001149 case tok::kw___is_literal_type:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001150 case tok::kw___is_pod:
1151 case tok::kw___is_polymorphic:
Chandler Carruthb7e95892011-04-23 10:47:28 +00001152 case tok::kw___is_trivial:
Sean Huntfeb375d2011-05-13 00:31:07 +00001153 case tok::kw___is_trivially_copyable:
Chandler Carruth4e61ddd2011-04-23 10:47:20 +00001154 case tok::kw___is_union:
Douglas Gregor5e9392b2011-12-03 18:14:24 +00001155 case tok::kw___is_final:
Anders Carlsson347ba892009-04-16 00:08:20 +00001156 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00001157 case tok::kw___has_trivial_copy:
1158 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +00001159 case tok::kw___has_trivial_destructor:
Sebastian Redlc238f092010-08-31 04:59:00 +00001160 case tok::kw___has_nothrow_assign:
1161 case tok::kw___has_nothrow_copy:
1162 case tok::kw___has_nothrow_constructor:
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001163 case tok::kw___has_virtual_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +00001164 return ParseUnaryTypeTrait();
1165
Francois Pichetf1872372010-12-08 22:35:30 +00001166 case tok::kw___builtin_types_compatible_p:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001167 case tok::kw___is_base_of:
John Wiegley20c0da72011-04-27 23:09:49 +00001168 case tok::kw___is_same:
1169 case tok::kw___is_convertible:
Douglas Gregor9f361132011-01-27 20:28:01 +00001170 case tok::kw___is_convertible_to:
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00001171 case tok::kw___is_trivially_assignable:
Francois Pichet6ad6f282010-12-07 00:08:36 +00001172 return ParseBinaryTypeTrait();
1173
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001174 case tok::kw___is_trivially_constructible:
1175 return ParseTypeTrait();
1176
John Wiegley21ff2e52011-04-28 00:16:57 +00001177 case tok::kw___array_rank:
1178 case tok::kw___array_extent:
1179 return ParseArrayTypeTrait();
1180
John Wiegley55262202011-04-25 06:54:41 +00001181 case tok::kw___is_lvalue_expr:
1182 case tok::kw___is_rvalue_expr:
1183 return ParseExpressionTrait();
1184
Chris Lattnerc97c2042007-10-03 22:03:06 +00001185 case tok::at: {
1186 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001187 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +00001188 }
Steve Naroff296e8d52008-08-28 19:20:44 +00001189 case tok::caret:
Chandler Carruthbb399022011-07-08 04:28:55 +00001190 Res = ParseBlockLiteralExpression();
1191 break;
1192 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001193 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001194 cutOffParsing();
1195 return ExprError();
Chandler Carruthbb399022011-07-08 04:28:55 +00001196 }
Chris Lattnerfdb548e2008-12-12 19:20:14 +00001197 case tok::l_square:
Douglas Gregorae7902c2011-08-04 15:30:47 +00001198 if (getLang().CPlusPlus0x) {
1199 if (getLang().ObjC1) {
Eli Friedmandc3b7232012-01-04 02:40:39 +00001200 // C++11 lambda expressions and Objective-C message sends both start with a
1201 // square bracket. There are three possibilities here:
1202 // we have a valid lambda expression, we have an invalid lambda
1203 // expression, or we have something that doesn't appear to be a lambda.
1204 // If we're in the last case, we fall back to ParseObjCMessageExpression.
Douglas Gregorae7902c2011-08-04 15:30:47 +00001205 Res = TryParseLambdaExpression();
Eli Friedmandc3b7232012-01-04 02:40:39 +00001206 if (!Res.isInvalid() && !Res.get())
Douglas Gregorae7902c2011-08-04 15:30:47 +00001207 Res = ParseObjCMessageExpression();
1208 break;
1209 }
1210 Res = ParseLambdaExpression();
1211 break;
1212 }
Chandler Carruthbb399022011-07-08 04:28:55 +00001213 if (getLang().ObjC1) {
1214 Res = ParseObjCMessageExpression();
1215 break;
1216 }
1217 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001219 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001220 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001222
John McCall9ae2f072010-08-23 23:25:46 +00001223 // These can be followed by postfix-expr pieces.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001224 return ParsePostfixExpressionSuffix(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225}
1226
1227/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
1228/// is parsed, this method parses any suffixes that apply.
1229///
1230/// postfix-expression: [C99 6.5.2]
1231/// primary-expression
1232/// postfix-expression '[' expression ']'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001233/// postfix-expression '[' braced-init-list ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001234/// postfix-expression '(' argument-expression-list[opt] ')'
1235/// postfix-expression '.' identifier
1236/// postfix-expression '->' identifier
1237/// postfix-expression '++'
1238/// postfix-expression '--'
1239/// '(' type-name ')' '{' initializer-list '}'
1240/// '(' type-name ')' '{' initializer-list ',' '}'
1241///
1242/// argument-expression-list: [C99 6.5.2]
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00001243/// argument-expression ...[opt]
1244/// argument-expression-list ',' assignment-expression ...[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001245///
John McCall60d7b3a2010-08-24 06:29:42 +00001246ExprResult
1247Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Now that the primary-expression piece of the postfix-expression has been
1249 // parsed, see if there are any postfix-expression pieces here.
1250 SourceLocation Loc;
1251 while (1) {
1252 switch (Tok.getKind()) {
Douglas Gregor78edf512010-09-15 16:23:04 +00001253 case tok::code_completion:
1254 if (InMessageExpression)
1255 return move(LHS);
1256
Douglas Gregorac5fd842010-09-18 01:28:11 +00001257 Actions.CodeCompletePostfixExpression(getCurScope(), LHS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001258 cutOffParsing();
1259 return ExprError();
Douglas Gregor78edf512010-09-15 16:23:04 +00001260
Douglas Gregor0fbda682010-09-15 14:51:05 +00001261 case tok::identifier:
1262 // If we see identifier: after an expression, and we're not already in a
1263 // message send, then this is probably a message send with a missing
1264 // opening bracket '['.
1265 if (getLang().ObjC1 && !InMessageExpression &&
Douglas Gregorb65042d2010-09-15 14:54:45 +00001266 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
Douglas Gregor0fbda682010-09-15 14:51:05 +00001267 LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(),
1268 ParsedType(), LHS.get());
1269 break;
1270 }
1271
1272 // Fall through; this isn't a message send.
1273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001275 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattnerc59cb382010-05-31 18:18:22 +00001277 // If we have a array postfix expression that starts on a new line and
1278 // Objective-C is enabled, it is highly likely that the user forgot a
1279 // semicolon after the base expression and that the array postfix-expr is
1280 // actually another message send. In this case, do some look-ahead to see
1281 // if the contents of the square brackets are obviously not a valid
1282 // expression and recover by pretending there is no suffix.
1283 if (getLang().ObjC1 && Tok.isAtStartOfLine() &&
1284 isSimpleObjCMessageExpression())
Douglas Gregor1b730e82010-05-31 14:40:22 +00001285 return move(LHS);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001286
1287 BalancedDelimiterTracker T(*this, tok::l_square);
1288 T.consumeOpen();
1289 Loc = T.getOpenLocation();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001290 ExprResult Idx;
Richard Smith7fe62082011-10-15 05:09:34 +00001291 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1292 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001293 Idx = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001294 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001295 Idx = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001296
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001298
1299 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
John McCall9ae2f072010-08-23 23:25:46 +00001300 LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc,
1301 Idx.take(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001302 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001303 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001304
1305 // Match the ']'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001306 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 break;
1308 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001309
Peter Collingbournebf36e252011-02-09 21:12:02 +00001310 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
1311 case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>'
1312 // '(' argument-expression-list[opt] ')'
1313 tok::TokenKind OpKind = Tok.getKind();
Douglas Gregor0fbda682010-09-15 14:51:05 +00001314 InMessageExpressionRAIIObject InMessage(*this, false);
1315
Peter Collingbournebf36e252011-02-09 21:12:02 +00001316 Expr *ExecConfig = 0;
1317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001318 BalancedDelimiterTracker LLLT(*this, tok::lesslessless);
1319 BalancedDelimiterTracker PT(*this, tok::l_paren);
1320
Peter Collingbournebf36e252011-02-09 21:12:02 +00001321 if (OpKind == tok::lesslessless) {
1322 ExprVector ExecConfigExprs(Actions);
1323 CommaLocsTy ExecConfigCommaLocs;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001324 LLLT.consumeOpen();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001325
1326 if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) {
1327 LHS = ExprError();
1328 }
1329
1330 if (LHS.isInvalid()) {
1331 SkipUntil(tok::greatergreatergreater);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001332 } else if (LLLT.consumeClose()) {
1333 // There was an error closing the brackets
Peter Collingbournebf36e252011-02-09 21:12:02 +00001334 LHS = ExprError();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001335 }
1336
1337 if (!LHS.isInvalid()) {
1338 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, ""))
1339 LHS = ExprError();
1340 else
1341 Loc = PrevTokLocation;
1342 }
1343
1344 if (!LHS.isInvalid()) {
1345 ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001346 LLLT.getOpenLocation(),
1347 move_arg(ExecConfigExprs),
1348 LLLT.getCloseLocation());
Peter Collingbournebf36e252011-02-09 21:12:02 +00001349 if (ECResult.isInvalid())
1350 LHS = ExprError();
1351 else
1352 ExecConfig = ECResult.get();
1353 }
1354 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001355 PT.consumeOpen();
1356 Loc = PT.getOpenLocation();
Peter Collingbournebf36e252011-02-09 21:12:02 +00001357 }
1358
Sebastian Redla55e52c2008-11-25 22:21:31 +00001359 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001360 CommaLocsTy CommaLocs;
Douglas Gregorac5fd842010-09-18 01:28:11 +00001361
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001362 if (Tok.is(tok::code_completion)) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00001363 Actions.CodeCompleteCall(getCurScope(), LHS.get(),
1364 llvm::ArrayRef<Expr *>());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001365 cutOffParsing();
1366 return ExprError();
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001367 }
Peter Collingbournebf36e252011-02-09 21:12:02 +00001368
1369 if (OpKind == tok::l_paren || !LHS.isInvalid()) {
1370 if (Tok.isNot(tok::r_paren)) {
1371 if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall,
1372 LHS.get())) {
Peter Collingbournebf36e252011-02-09 21:12:02 +00001373 LHS = ExprError();
1374 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 }
1376 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001377
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Match the ')'.
Douglas Gregorac5fd842010-09-18 01:28:11 +00001379 if (LHS.isInvalid()) {
1380 SkipUntil(tok::r_paren);
1381 } else if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001382 PT.consumeClose();
Douglas Gregorac5fd842010-09-18 01:28:11 +00001383 LHS = ExprError();
1384 } else {
1385 assert((ArgExprs.size() == 0 ||
1386 ArgExprs.size()-1 == CommaLocs.size())&&
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 "Unexpected number of commas!");
John McCall9ae2f072010-08-23 23:25:46 +00001388 LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc,
Peter Collingbournebf36e252011-02-09 21:12:02 +00001389 move_arg(ArgExprs), Tok.getLocation(),
1390 ExecConfig);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001391 PT.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 break;
1395 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001396 case tok::arrow:
1397 case tok::period: {
1398 // postfix-expression: p-e '->' template[opt] id-expression
1399 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 tok::TokenKind OpKind = Tok.getKind();
1401 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001402
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001403 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001404 ParsedType ObjectType;
Douglas Gregord4dca082010-02-24 18:44:31 +00001405 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001406 if (getLang().CPlusPlus && !LHS.isInvalid()) {
John McCall9ae2f072010-08-23 23:25:46 +00001407 LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(),
Douglas Gregord4dca082010-02-24 18:44:31 +00001408 OpLoc, OpKind, ObjectType,
1409 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001410 if (LHS.isInvalid())
1411 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001412
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001413 ParseOptionalCXXScopeSpecifier(SS, ObjectType,
1414 /*EnteringContext=*/false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001415 &MayBePseudoDestructor);
Douglas Gregor9f716e42010-05-27 15:25:59 +00001416 if (SS.isNotEmpty())
John McCallb3d87482010-08-24 05:47:05 +00001417 ObjectType = ParsedType();
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001418 }
1419
Douglas Gregor81b747b2009-09-17 21:32:03 +00001420 if (Tok.is(tok::code_completion)) {
1421 // Code completion for a member access expression.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001422 Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(),
Douglas Gregor81b747b2009-09-17 21:32:03 +00001423 OpLoc, OpKind == tok::arrow);
1424
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001425 cutOffParsing();
1426 return ExprError();
Douglas Gregor81b747b2009-09-17 21:32:03 +00001427 }
1428
John McCall9ae2f072010-08-23 23:25:46 +00001429 if (MayBePseudoDestructor && !LHS.isInvalid()) {
1430 LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS,
Douglas Gregord4dca082010-02-24 18:44:31 +00001431 ObjectType);
1432 break;
1433 }
1434
1435 // Either the action has told is that this cannot be a
1436 // pseudo-destructor expression (based on the type of base
1437 // expression), or we didn't see a '~' in the right place. We
1438 // can still parse a destructor name here, but in that case it
1439 // names a real destructor.
Francois Pichetdbee3412011-01-18 05:04:39 +00001440 // Allow explicit constructor calls in Microsoft mode.
1441 // FIXME: Add support for explicit call of template constructor.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001442 SourceLocation TemplateKWLoc;
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001443 UnqualifiedId Name;
Douglas Gregor8f70bda2012-02-16 18:19:22 +00001444 if (getLang().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) {
1445 // Objective-C++:
1446 // After a '.' in a member access expression, treat the keyword
1447 // 'class' as if it were an identifier.
1448 //
1449 // This hack allows property access to the 'class' method because it is
1450 // such a common method name. For other C++ keywords that are
1451 // Objective-C method names, one must use the message send syntax.
1452 IdentifierInfo *Id = Tok.getIdentifierInfo();
1453 SourceLocation Loc = ConsumeToken();
1454 Name.setIdentifier(Id, Loc);
1455 } else if (ParseUnqualifiedId(SS,
1456 /*EnteringContext=*/false,
1457 /*AllowDestructorName=*/true,
1458 /*AllowConstructorName=*/
1459 getLang().MicrosoftExt,
1460 ObjectType, TemplateKWLoc, Name))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001461 LHS = ExprError();
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001462
1463 if (!LHS.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001464 LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001465 OpKind, SS, TemplateKWLoc, Name,
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001466 CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0,
1467 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 break;
1469 }
1470 case tok::plusplus: // postfix-expression: postfix-expression '++'
1471 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001472 if (!LHS.isInvalid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +00001474 Tok.getKind(), LHS.take());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001475 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 ConsumeToken();
1477 break;
1478 }
1479 }
1480}
1481
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001482/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/
1483/// vec_step and we are at the start of an expression or a parenthesized
1484/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the
1485/// expression (isCastExpr == false) or the type (isCastExpr == true).
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001486///
1487/// unary-expression: [C99 6.5.3]
1488/// 'sizeof' unary-expression
1489/// 'sizeof' '(' type-name ')'
1490/// [GNU] '__alignof' unary-expression
1491/// [GNU] '__alignof' '(' type-name ')'
1492/// [C++0x] 'alignof' '(' type-id ')'
1493///
1494/// [GNU] typeof-specifier:
1495/// typeof ( expressions )
1496/// typeof ( type-name )
1497/// [GNU/C++] typeof unary-expression
1498///
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001499/// [OpenCL 1.1 6.11.12] vec_step built-in function:
1500/// vec_step ( expressions )
1501/// vec_step ( type-name )
1502///
John McCall60d7b3a2010-08-24 06:29:42 +00001503ExprResult
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001504Parser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok,
1505 bool &isCastExpr,
1506 ParsedType &CastTy,
1507 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001508
1509 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001510 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) ||
1511 OpTok.is(tok::kw_vec_step)) &&
1512 "Not a typeof/sizeof/alignof/vec_step expression!");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001513
John McCall60d7b3a2010-08-24 06:29:42 +00001514 ExprResult Operand;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001516 // If the operand doesn't start with an '(', it must be an expression.
1517 if (Tok.isNot(tok::l_paren)) {
1518 isCastExpr = false;
1519 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1520 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1521 return ExprError();
1522 }
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001524 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001525 } else {
1526 // If it starts with a '(', we know that it is either a parenthesized
1527 // type-name, or it is a unary-expression that starts with a compound
1528 // literal, or starts with a primary-expression that is a parenthesized
1529 // expression.
1530 ParenParseOption ExprType = CastExpr;
1531 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001533 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001534 false, CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001535 CastRange = SourceRange(LParenLoc, RParenLoc);
1536
1537 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1538 // a type.
1539 if (ExprType == CastExpr) {
1540 isCastExpr = true;
1541 return ExprEmpty();
1542 }
1543
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001544 if (getLang().CPlusPlus || OpTok.isNot(tok::kw_typeof)) {
1545 // GNU typeof in C requires the expression to be parenthesized. Not so for
1546 // sizeof/alignof or in C++. Therefore, the parenthesized expression is
1547 // the start of a unary-expression, but doesn't include any postfix
1548 // pieces. Parse these now if present.
John McCall124300e2010-08-24 23:41:43 +00001549 if (!Operand.isInvalid())
1550 Operand = ParsePostfixExpressionSuffix(Operand.get());
Douglas Gregor2a3a1bd2010-07-28 18:22:12 +00001551 }
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001552 }
1553
1554 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1555 isCastExpr = false;
1556 return move(Operand);
1557}
1558
Reid Spencer5f016e22007-07-11 17:01:13 +00001559
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001560/// ParseUnaryExprOrTypeTraitExpression - Parse a sizeof or alignof expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001561/// unary-expression: [C99 6.5.3]
1562/// 'sizeof' unary-expression
1563/// 'sizeof' '(' type-name ')'
Douglas Gregoree8aff02011-01-04 17:33:58 +00001564/// [C++0x] 'sizeof' '...' '(' identifier ')'
Reid Spencer5f016e22007-07-11 17:01:13 +00001565/// [GNU] '__alignof' unary-expression
1566/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001567/// [C++0x] 'alignof' '(' type-id ')'
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001568ExprResult Parser::ParseUnaryExprOrTypeTraitExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001569 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001570 || Tok.is(tok::kw_alignof) || Tok.is(tok::kw_vec_step)) &&
1571 "Not a sizeof/alignof/vec_step expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001572 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Douglas Gregoree8aff02011-01-04 17:33:58 +00001575 // [C++0x] 'sizeof' '...' '(' identifier ')'
1576 if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) {
1577 SourceLocation EllipsisLoc = ConsumeToken();
1578 SourceLocation LParenLoc, RParenLoc;
1579 IdentifierInfo *Name = 0;
1580 SourceLocation NameLoc;
1581 if (Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001582 BalancedDelimiterTracker T(*this, tok::l_paren);
1583 T.consumeOpen();
1584 LParenLoc = T.getOpenLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001585 if (Tok.is(tok::identifier)) {
1586 Name = Tok.getIdentifierInfo();
1587 NameLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001588 T.consumeClose();
1589 RParenLoc = T.getCloseLocation();
Douglas Gregoree8aff02011-01-04 17:33:58 +00001590 if (RParenLoc.isInvalid())
1591 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1592 } else {
1593 Diag(Tok, diag::err_expected_parameter_pack);
1594 SkipUntil(tok::r_paren);
1595 }
1596 } else if (Tok.is(tok::identifier)) {
1597 Name = Tok.getIdentifierInfo();
1598 NameLoc = ConsumeToken();
1599 LParenLoc = PP.getLocForEndOfToken(EllipsisLoc);
1600 RParenLoc = PP.getLocForEndOfToken(NameLoc);
1601 Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack)
1602 << Name
1603 << FixItHint::CreateInsertion(LParenLoc, "(")
1604 << FixItHint::CreateInsertion(RParenLoc, ")");
1605 } else {
1606 Diag(Tok, diag::err_sizeof_parameter_pack);
1607 }
1608
1609 if (!Name)
1610 return ExprError();
1611
1612 return Actions.ActOnSizeofParameterPackExpr(getCurScope(),
1613 OpTok.getLocation(),
1614 *Name, NameLoc,
1615 RParenLoc);
1616 }
Richard Smith841804b2011-10-17 23:06:20 +00001617
1618 if (OpTok.is(tok::kw_alignof))
1619 Diag(OpTok, diag::warn_cxx98_compat_alignof);
1620
Eli Friedman71b8fb52012-01-21 01:01:51 +00001621 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1622
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001623 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00001624 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001625 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001626 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok,
1627 isCastExpr,
1628 CastTy,
1629 CastRange);
1630
1631 UnaryExprOrTypeTrait ExprKind = UETT_SizeOf;
1632 if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof))
1633 ExprKind = UETT_AlignOf;
1634 else if (OpTok.is(tok::kw_vec_step))
1635 ExprKind = UETT_VecStep;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001636
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001637 if (isCastExpr)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001638 return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1639 ExprKind,
1640 /*isType=*/true,
1641 CastTy.getAsOpaquePtr(),
1642 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001645 if (!Operand.isInvalid())
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001646 Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(),
1647 ExprKind,
1648 /*isType=*/false,
1649 Operand.release(),
1650 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001651 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001652}
1653
1654/// ParseBuiltinPrimaryExpression
1655///
1656/// primary-expression: [C99 6.5.1]
1657/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1658/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1659/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1660/// assign-expr ')'
1661/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Peter Collingbournea1606712011-11-05 03:47:48 +00001662/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001663///
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// [GNU] offsetof-member-designator:
1665/// [GNU] identifier
1666/// [GNU] offsetof-member-designator '.' identifier
1667/// [GNU] offsetof-member-designator '[' expression ']'
1668///
John McCall60d7b3a2010-08-24 06:29:42 +00001669ExprResult Parser::ParseBuiltinPrimaryExpression() {
1670 ExprResult Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1672
1673 tok::TokenKind T = Tok.getKind();
1674 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1675
1676 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001677 if (Tok.isNot(tok::l_paren))
1678 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1679 << BuiltinII);
1680
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001681 BalancedDelimiterTracker PT(*this, tok::l_paren);
1682 PT.consumeOpen();
1683
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 // TODO: Build AST.
1685
1686 switch (T) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001687 default: llvm_unreachable("Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001688 case tok::kw___builtin_va_arg: {
John McCall60d7b3a2010-08-24 06:29:42 +00001689 ExprResult Expr(ParseAssignmentExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001690
1691 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Douglas Gregorac5fd842010-09-18 01:28:11 +00001692 Expr = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001693
Douglas Gregor809070a2009-02-18 17:45:20 +00001694 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001695
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001696 if (Tok.isNot(tok::r_paren)) {
1697 Diag(Tok, diag::err_expected_rparen);
Douglas Gregorac5fd842010-09-18 01:28:11 +00001698 Expr = ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001699 }
Douglas Gregorac5fd842010-09-18 01:28:11 +00001700
1701 if (Expr.isInvalid() || Ty.isInvalid())
Douglas Gregor809070a2009-02-18 17:45:20 +00001702 Res = ExprError();
1703 else
John McCall9ae2f072010-08-23 23:25:46 +00001704 Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001706 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001707 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001708 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001709 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001710 if (Ty.isInvalid()) {
1711 SkipUntil(tok::r_paren);
1712 return ExprError();
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001716 return ExprError();
1717
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001719 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001720 Diag(Tok, diag::err_expected_ident);
1721 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001722 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001723 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001724
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001725 // Keep track of the various subcomponents we see.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001726 SmallVector<Sema::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001727
John McCallf312b1e2010-08-26 23:41:50 +00001728 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001729 Comps.back().isBrackets = false;
1730 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1731 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001732
Sebastian Redla55e52c2008-11-25 22:21:31 +00001733 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001735 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 // offsetof-member-designator: offsetof-member-designator '.' identifier
John McCallf312b1e2010-08-26 23:41:50 +00001737 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001738 Comps.back().isBrackets = false;
1739 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001740
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001741 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001742 Diag(Tok, diag::err_expected_ident);
1743 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001744 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001745 }
1746 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1747 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001748
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001749 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 // offsetof-member-designator: offsetof-member-design '[' expression ']'
John McCallf312b1e2010-08-26 23:41:50 +00001751 Comps.push_back(Sema::OffsetOfComponent());
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001752 Comps.back().isBrackets = true;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001753 BalancedDelimiterTracker ST(*this, tok::l_square);
1754 ST.consumeOpen();
1755 Comps.back().LocStart = ST.getOpenLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001757 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001759 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001761 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001762
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001763 ST.consumeClose();
1764 Comps.back().LocEnd = ST.getCloseLocation();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001765 } else {
1766 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001767 PT.consumeClose();
Douglas Gregor809070a2009-02-18 17:45:20 +00001768 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001769 } else if (Ty.isInvalid()) {
1770 Res = ExprError();
1771 } else {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001772 PT.consumeClose();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001773 Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001774 Ty.get(), &Comps[0], Comps.size(),
1775 PT.getCloseLocation());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001776 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001777 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 }
1779 }
1780 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001781 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001782 case tok::kw___builtin_choose_expr: {
John McCall60d7b3a2010-08-24 06:29:42 +00001783 ExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001784 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001785 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001786 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001787 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001789 return ExprError();
1790
John McCall60d7b3a2010-08-24 06:29:42 +00001791 ExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001792 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001793 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001794 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001795 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001797 return ExprError();
1798
John McCall60d7b3a2010-08-24 06:29:42 +00001799 ExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001800 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001801 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001802 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001803 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001804 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001805 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001806 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001807 }
John McCall9ae2f072010-08-23 23:25:46 +00001808 Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(),
1809 Expr2.take(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001810 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001811 }
Tanya Lattner61eee0c2011-06-04 00:47:47 +00001812 case tok::kw___builtin_astype: {
1813 // The first argument is an expression to be converted, followed by a comma.
1814 ExprResult Expr(ParseAssignmentExpression());
1815 if (Expr.isInvalid()) {
1816 SkipUntil(tok::r_paren);
1817 return ExprError();
1818 }
1819
1820 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",
1821 tok::r_paren))
1822 return ExprError();
1823
1824 // Second argument is the type to bitcast to.
1825 TypeResult DestTy = ParseTypeName();
1826 if (DestTy.isInvalid())
1827 return ExprError();
1828
1829 // Attempt to consume the r-paren.
1830 if (Tok.isNot(tok::r_paren)) {
1831 Diag(Tok, diag::err_expected_rparen);
1832 SkipUntil(tok::r_paren);
1833 return ExprError();
1834 }
1835
1836 Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc,
1837 ConsumeParen());
1838 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001839 }
Chandler Carruth3c7fddd2011-07-08 04:59:44 +00001840 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001841
John McCall9ae2f072010-08-23 23:25:46 +00001842 if (Res.isInvalid())
1843 return ExprError();
1844
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 // These can be followed by postfix-expr pieces because they are
1846 // primary-expressions.
John McCall9ae2f072010-08-23 23:25:46 +00001847 return ParsePostfixExpressionSuffix(Res.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001848}
1849
1850/// ParseParenExpression - This parses the unit that starts with a '(' token,
1851/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001852/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1853/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001854///
1855/// primary-expression: [C99 6.5.1]
1856/// '(' expression ')'
1857/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1858/// postfix-expression: [C99 6.5.2]
1859/// '(' type-name ')' '{' initializer-list '}'
1860/// '(' type-name ')' '{' initializer-list ',' '}'
1861/// cast-expression: [C99 6.5.4]
1862/// '(' type-name ')' cast-expression
John McCallf85e1932011-06-15 23:02:42 +00001863/// [ARC] bridged-cast-expression
1864///
1865/// [ARC] bridged-cast-expression:
1866/// (__bridge type-name) cast-expression
1867/// (__bridge_transfer type-name) cast-expression
1868/// (__bridge_retained type-name) cast-expression
John McCall60d7b3a2010-08-24 06:29:42 +00001869ExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001870Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001871 bool isTypeCast, ParsedType &CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001872 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001873 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001874 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001875 BalancedDelimiterTracker T(*this, tok::l_paren);
1876 if (T.consumeOpen())
1877 return ExprError();
1878 SourceLocation OpenLoc = T.getOpenLocation();
1879
John McCall60d7b3a2010-08-24 06:29:42 +00001880 ExprResult Result(true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001881 bool isAmbiguousTypeId;
John McCallb3d87482010-08-24 05:47:05 +00001882 CastTy = ParsedType();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001883
Douglas Gregor02688102010-09-14 23:59:36 +00001884 if (Tok.is(tok::code_completion)) {
1885 Actions.CodeCompleteOrdinaryName(getCurScope(),
1886 ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression
1887 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001888 cutOffParsing();
Douglas Gregor02688102010-09-14 23:59:36 +00001889 return ExprError();
1890 }
John McCallb3c49062011-04-06 02:35:25 +00001891
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001892 // Diagnose use of bridge casts in non-arc mode.
1893 bool BridgeCast = (getLang().ObjC2 &&
1894 (Tok.is(tok::kw___bridge) ||
1895 Tok.is(tok::kw___bridge_transfer) ||
1896 Tok.is(tok::kw___bridge_retained) ||
1897 Tok.is(tok::kw___bridge_retain)));
1898 if (BridgeCast && !getLang().ObjCAutoRefCount) {
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001899 StringRef BridgeCastName = Tok.getName();
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001900 SourceLocation BridgeKeywordLoc = ConsumeToken();
1901 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
Ted Kremeneke698a5c2012-02-18 04:42:38 +00001902 Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc)
Ted Kremenekd9d12e02011-12-20 01:03:40 +00001903 << BridgeCastName
1904 << FixItHint::CreateReplacement(BridgeKeywordLoc, "");
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001905 BridgeCast = false;
1906 }
1907
John McCallb3c49062011-04-06 02:35:25 +00001908 // None of these cases should fall through with an invalid Result
1909 // unless they've already reported an error.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001910 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 Diag(Tok, diag::ext_gnu_statement_expr);
John McCall0b7e6782011-03-24 11:26:52 +00001912 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001913 StmtResult Stmt(ParseCompoundStatement(attrs, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001915
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001916 // If the substmt parsed correctly, build the AST node.
John McCallb3c49062011-04-06 02:35:25 +00001917 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00001918 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation());
Fariborz Jahanian00852e42011-12-19 21:06:15 +00001919 } else if (ExprType >= CompoundLiteral && BridgeCast) {
John McCallb64915a2011-06-17 21:56:12 +00001920 tok::TokenKind tokenKind = Tok.getKind();
1921 SourceLocation BridgeKeywordLoc = ConsumeToken();
1922
John McCallf85e1932011-06-15 23:02:42 +00001923 // Parse an Objective-C ARC ownership cast expression.
1924 ObjCBridgeCastKind Kind;
John McCallb64915a2011-06-17 21:56:12 +00001925 if (tokenKind == tok::kw___bridge)
John McCallf85e1932011-06-15 23:02:42 +00001926 Kind = OBC_Bridge;
John McCallb64915a2011-06-17 21:56:12 +00001927 else if (tokenKind == tok::kw___bridge_transfer)
John McCallf85e1932011-06-15 23:02:42 +00001928 Kind = OBC_BridgeTransfer;
John McCallb64915a2011-06-17 21:56:12 +00001929 else if (tokenKind == tok::kw___bridge_retained)
John McCallf85e1932011-06-15 23:02:42 +00001930 Kind = OBC_BridgeRetained;
John McCallb64915a2011-06-17 21:56:12 +00001931 else {
1932 // As a hopefully temporary workaround, allow __bridge_retain as
1933 // a synonym for __bridge_retained, but only in system headers.
1934 assert(tokenKind == tok::kw___bridge_retain);
1935 Kind = OBC_BridgeRetained;
1936 if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc))
1937 Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain)
1938 << FixItHint::CreateReplacement(BridgeKeywordLoc,
1939 "__bridge_retained");
1940 }
John McCallf85e1932011-06-15 23:02:42 +00001941
John McCallf85e1932011-06-15 23:02:42 +00001942 TypeResult Ty = ParseTypeName();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001943 T.consumeClose();
1944 RParenLoc = T.getCloseLocation();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001945 ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false);
John McCallf85e1932011-06-15 23:02:42 +00001946
1947 if (Ty.isInvalid() || SubExpr.isInvalid())
1948 return ExprError();
1949
1950 return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind,
1951 BridgeKeywordLoc, Ty.get(),
1952 RParenLoc, SubExpr.get());
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001953 } else if (ExprType >= CompoundLiteral &&
1954 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001958 // In C++, if the type-id is ambiguous we disambiguate based on context.
1959 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1960 // in which case we should treat it as type-id.
1961 // if stopIfCastExpr is false, we need to determine the context past the
1962 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001963 if (isAmbiguousTypeId && !stopIfCastExpr) {
1964 ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T);
1965 RParenLoc = T.getCloseLocation();
1966 return res;
1967 }
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001969 // Parse the type declarator.
1970 DeclSpec DS(AttrFactory);
1971 ParseSpecifierQualifierList(DS);
1972 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1973 ParseDeclarator(DeclaratorInfo);
Douglas Gregor0fbda682010-09-15 14:51:05 +00001974
Douglas Gregor77328d12010-09-15 23:19:31 +00001975 // If our type is followed by an identifier and either ':' or ']', then
1976 // this is probably an Objective-C message send where the leading '[' is
1977 // missing. Recover as if that were the case.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001978 if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) &&
1979 !InMessageExpression && getLang().ObjC1 &&
1980 (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) {
1981 TypeResult Ty;
1982 {
1983 InMessageExpressionRAIIObject InMessage(*this, false);
1984 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1985 }
Douglas Gregor77328d12010-09-15 23:19:31 +00001986 Result = ParseObjCMessageExpressionBody(SourceLocation(),
1987 SourceLocation(),
1988 Ty.get(), 0);
1989 } else {
1990 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001991 T.consumeClose();
1992 RParenLoc = T.getCloseLocation();
Douglas Gregor77328d12010-09-15 23:19:31 +00001993 if (Tok.is(tok::l_brace)) {
1994 ExprType = CompoundLiteral;
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00001995 TypeResult Ty;
1996 {
1997 InMessageExpressionRAIIObject InMessage(*this, false);
1998 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1999 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002000 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnereb483eb2010-04-11 08:28:14 +00002001 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00002002
Douglas Gregor77328d12010-09-15 23:19:31 +00002003 if (ExprType == CastExpr) {
2004 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00002005
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002006 if (DeclaratorInfo.isInvalidType())
Douglas Gregor77328d12010-09-15 23:19:31 +00002007 return ExprError();
2008
Douglas Gregor77328d12010-09-15 23:19:31 +00002009 // Note that this doesn't parse the subsequent cast-expression, it just
2010 // returns the parsed type to the callee.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002011 if (stopIfCastExpr) {
2012 TypeResult Ty;
2013 {
2014 InMessageExpressionRAIIObject InMessage(*this, false);
2015 Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2016 }
2017 CastTy = Ty.get();
Douglas Gregor77328d12010-09-15 23:19:31 +00002018 return ExprResult();
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002019 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002020
2021 // Reject the cast of super idiom in ObjC.
2022 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
2023 Tok.getIdentifierInfo() == Ident_super &&
2024 getCurScope()->isInObjcMethodScope() &&
2025 GetLookAheadToken(1).isNot(tok::period)) {
2026 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
2027 << SourceRange(OpenLoc, RParenLoc);
2028 return ExprError();
2029 }
2030
2031 // Parse the cast-expression that follows it next.
2032 // TODO: For cast expression with CastTy.
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002033 Result = ParseCastExpression(/*isUnaryExpression=*/false,
2034 /*isAddressOfOperand=*/false,
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002035 /*isTypeCast=*/IsTypeCast);
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002036 if (!Result.isInvalid()) {
2037 Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc,
2038 DeclaratorInfo, CastTy,
Douglas Gregor77328d12010-09-15 23:19:31 +00002039 RParenLoc, Result.take());
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002040 }
Douglas Gregor77328d12010-09-15 23:19:31 +00002041 return move(Result);
2042 }
2043
2044 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
2045 return ExprError();
2046 }
Argyrios Kyrtzidis0a851832011-07-01 22:22:59 +00002047 } else if (isTypeCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00002048 // Parse the expression-list.
Douglas Gregor0fbda682010-09-15 14:51:05 +00002049 InMessageExpressionRAIIObject InMessage(*this, false);
2050
Nate Begeman2ef13e52009-08-10 23:49:36 +00002051 ExprVector ArgExprs(Actions);
2052 CommaLocsTy CommaLocs;
2053
2054 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
2055 ExprType = SimpleExpr;
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00002056 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
2057 move_arg(ArgExprs));
Nate Begeman2ef13e52009-08-10 23:49:36 +00002058 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 } else {
Douglas Gregor0fbda682010-09-15 14:51:05 +00002060 InMessageExpressionRAIIObject InMessage(*this, false);
2061
Kaelyn Uhraincd78e612012-01-25 20:49:08 +00002062 Result = ParseExpression(MaybeTypeCast);
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 ExprType = SimpleExpr;
John McCallb3c49062011-04-06 02:35:25 +00002064
2065 // Don't build a paren expression unless we actually match a ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002066 if (!Result.isInvalid() && Tok.is(tok::r_paren))
John McCall9ae2f072010-08-23 23:25:46 +00002067 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00002069
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00002071 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002072 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00002073 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 }
Mike Stump1eb44332009-09-09 15:08:12 +00002075
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002076 T.consumeClose();
2077 RParenLoc = T.getCloseLocation();
Sebastian Redld8c4e152008-12-11 22:33:27 +00002078 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002079}
2080
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002081/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
2082/// and we are at the left brace.
2083///
2084/// postfix-expression: [C99 6.5.2]
2085/// '(' type-name ')' '{' initializer-list '}'
2086/// '(' type-name ')' '{' initializer-list ',' '}'
2087///
John McCall60d7b3a2010-08-24 06:29:42 +00002088ExprResult
John McCallb3d87482010-08-24 05:47:05 +00002089Parser::ParseCompoundLiteralExpression(ParsedType Ty,
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002090 SourceLocation LParenLoc,
2091 SourceLocation RParenLoc) {
2092 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
2093 if (!getLang().C99) // Compound literals don't exist in C90.
2094 Diag(LParenLoc, diag::ext_c99_compound_literal);
John McCall60d7b3a2010-08-24 06:29:42 +00002095 ExprResult Result = ParseInitializer();
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002096 if (!Result.isInvalid() && Ty)
John McCall9ae2f072010-08-23 23:25:46 +00002097 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take());
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00002098 return move(Result);
2099}
2100
Reid Spencer5f016e22007-07-11 17:01:13 +00002101/// ParseStringLiteralExpression - This handles the various token types that
2102/// form string literals, and also handles string concatenation [C99 5.1.1.2,
2103/// translation phase #6].
2104///
2105/// primary-expression: [C99 6.5.1]
2106/// string-literal
John McCall60d7b3a2010-08-24 06:29:42 +00002107ExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00002109
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
2111 // considered to be strings for concatenation purposes.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002112 SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00002113
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 do {
2115 StringToks.push_back(Tok);
2116 ConsumeStringToken();
2117 } while (isTokenStringLiteral());
2118
2119 // Pass the set of string tokens, ready for concatenation, to the actions.
Sean Hunt6cf75022010-08-30 17:47:05 +00002120 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00002121}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002122
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002123/// ParseGenericSelectionExpression - Parse a C11 generic-selection
2124/// [C11 6.5.1.1].
Peter Collingbournef111d932011-04-15 00:35:48 +00002125///
2126/// generic-selection:
2127/// _Generic ( assignment-expression , generic-assoc-list )
2128/// generic-assoc-list:
2129/// generic-association
2130/// generic-assoc-list , generic-association
2131/// generic-association:
2132/// type-name : assignment-expression
2133/// default : assignment-expression
2134ExprResult Parser::ParseGenericSelectionExpression() {
2135 assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected");
2136 SourceLocation KeyLoc = ConsumeToken();
2137
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002138 if (!getLang().C11)
2139 Diag(KeyLoc, diag::ext_c11_generic_selection);
Peter Collingbournef111d932011-04-15 00:35:48 +00002140
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002141 BalancedDelimiterTracker T(*this, tok::l_paren);
2142 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbournef111d932011-04-15 00:35:48 +00002143 return ExprError();
2144
2145 ExprResult ControllingExpr;
2146 {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002147 // C11 6.5.1.1p3 "The controlling expression of a generic selection is
Peter Collingbournef111d932011-04-15 00:35:48 +00002148 // not evaluated."
2149 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
2150 ControllingExpr = ParseAssignmentExpression();
2151 if (ControllingExpr.isInvalid()) {
2152 SkipUntil(tok::r_paren);
2153 return ExprError();
2154 }
2155 }
2156
2157 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) {
2158 SkipUntil(tok::r_paren);
2159 return ExprError();
2160 }
2161
2162 SourceLocation DefaultLoc;
2163 TypeVector Types(Actions);
2164 ExprVector Exprs(Actions);
2165 while (1) {
2166 ParsedType Ty;
2167 if (Tok.is(tok::kw_default)) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002168 // C11 6.5.1.1p2 "A generic selection shall have no more than one default
Peter Collingbournef111d932011-04-15 00:35:48 +00002169 // generic association."
2170 if (!DefaultLoc.isInvalid()) {
2171 Diag(Tok, diag::err_duplicate_default_assoc);
2172 Diag(DefaultLoc, diag::note_previous_default_assoc);
2173 SkipUntil(tok::r_paren);
2174 return ExprError();
2175 }
2176 DefaultLoc = ConsumeToken();
2177 Ty = ParsedType();
2178 } else {
2179 ColonProtectionRAIIObject X(*this);
2180 TypeResult TR = ParseTypeName();
2181 if (TR.isInvalid()) {
2182 SkipUntil(tok::r_paren);
2183 return ExprError();
2184 }
2185 Ty = TR.release();
2186 }
2187 Types.push_back(Ty);
2188
2189 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) {
2190 SkipUntil(tok::r_paren);
2191 return ExprError();
2192 }
2193
2194 // FIXME: These expressions should be parsed in a potentially potentially
2195 // evaluated context.
2196 ExprResult ER(ParseAssignmentExpression());
2197 if (ER.isInvalid()) {
2198 SkipUntil(tok::r_paren);
2199 return ExprError();
2200 }
2201 Exprs.push_back(ER.release());
2202
2203 if (Tok.isNot(tok::comma))
2204 break;
2205 ConsumeToken();
2206 }
2207
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002208 T.consumeClose();
2209 if (T.getCloseLocation().isInvalid())
Peter Collingbournef111d932011-04-15 00:35:48 +00002210 return ExprError();
2211
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002212 return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc,
2213 T.getCloseLocation(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002214 ControllingExpr.release(),
2215 move_arg(Types), move_arg(Exprs));
2216}
2217
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002218/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
2219///
2220/// argument-expression-list:
2221/// assignment-expression
2222/// argument-expression-list , assignment-expression
2223///
2224/// [C++] expression-list:
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002225/// [C++] assignment-expression
2226/// [C++] expression-list , assignment-expression
2227///
2228/// [C++0x] expression-list:
2229/// [C++0x] initializer-list
2230///
2231/// [C++0x] initializer-list
2232/// [C++0x] initializer-clause ...[opt]
2233/// [C++0x] initializer-list , initializer-clause ...[opt]
2234///
2235/// [C++0x] initializer-clause:
2236/// [C++0x] assignment-expression
2237/// [C++0x] braced-init-list
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002238///
Chris Lattner5f9e2722011-07-23 10:55:15 +00002239bool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs,
2240 SmallVectorImpl<SourceLocation> &CommaLocs,
John McCallf312b1e2010-08-26 23:41:50 +00002241 void (Sema::*Completer)(Scope *S,
John McCallca0408f2010-08-23 06:44:23 +00002242 Expr *Data,
Ahmed Charles13a140c2012-02-25 11:00:22 +00002243 llvm::ArrayRef<Expr *> Args),
John McCallca0408f2010-08-23 06:44:23 +00002244 Expr *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002245 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002246 if (Tok.is(tok::code_completion)) {
2247 if (Completer)
Ahmed Charles13a140c2012-02-25 11:00:22 +00002248 (Actions.*Completer)(getCurScope(), Data, Exprs);
Douglas Gregor4706e872011-02-17 03:09:23 +00002249 else
2250 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002251 cutOffParsing();
2252 return true;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00002253 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002254
2255 ExprResult Expr;
Richard Smith7fe62082011-10-15 05:09:34 +00002256 if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
2257 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002258 Expr = ParseBraceInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00002259 } else
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002260 Expr = ParseAssignmentExpression();
2261
Douglas Gregordcaa1ca2011-01-03 19:31:53 +00002262 if (Tok.is(tok::ellipsis))
2263 Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002264 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002265 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00002266
Sebastian Redleffa8d12008-12-10 00:02:53 +00002267 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00002268
2269 if (Tok.isNot(tok::comma))
2270 return false;
2271 // Move to the next argument, remember where the comma was.
2272 CommaLocs.push_back(ConsumeToken());
2273 }
2274}
Steve Naroff296e8d52008-08-28 19:20:44 +00002275
Mike Stump98eb8a72009-02-04 22:31:32 +00002276/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
2277///
2278/// [clang] block-id:
2279/// [clang] specifier-qualifier-list block-declarator
2280///
2281void Parser::ParseBlockId() {
Douglas Gregor75ab4142010-10-18 21:34:55 +00002282 if (Tok.is(tok::code_completion)) {
2283 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002284 return cutOffParsing();
Douglas Gregor75ab4142010-10-18 21:34:55 +00002285 }
2286
Mike Stump98eb8a72009-02-04 22:31:32 +00002287 // Parse the specifier-qualifier-list piece.
John McCall0b7e6782011-03-24 11:26:52 +00002288 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002289 ParseSpecifierQualifierList(DS);
2290
2291 // Parse the block-declarator.
2292 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
2293 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002294
Mike Stump6c92fa72009-04-29 21:40:37 +00002295 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002296 DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation());
Mike Stump6c92fa72009-04-29 21:40:37 +00002297
John McCall7f040a92010-12-24 02:08:15 +00002298 MaybeParseGNUAttributes(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002299
Mike Stump98eb8a72009-02-04 22:31:32 +00002300 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002301 Actions.ActOnBlockArguments(DeclaratorInfo, getCurScope());
Mike Stump98eb8a72009-02-04 22:31:32 +00002302}
2303
Steve Naroff296e8d52008-08-28 19:20:44 +00002304/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00002305/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00002306///
2307/// block-literal:
2308/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00002309/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00002310/// [clang] block-args:
2311/// [clang] '(' parameter-list ')'
2312///
John McCall60d7b3a2010-08-24 06:29:42 +00002313ExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00002314 assert(Tok.is(tok::caret) && "block literal starts with ^");
2315 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00002316
Chris Lattner6b91f002009-03-05 07:32:12 +00002317 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
2318 "block literal parsing");
2319
Mike Stump1eb44332009-09-09 15:08:12 +00002320 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00002321 // argument decls, decls within the compound expression, etc. This also
2322 // allows determining whether a variable reference inside the block is
2323 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002324 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
Sebastian Redlab197ba2009-02-09 18:23:29 +00002325 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00002326
2327 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002328 Actions.ActOnBlockStart(CaretLoc, getCurScope());
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Steve Naroff296e8d52008-08-28 19:20:44 +00002330 // Parse the return type if present.
John McCall0b7e6782011-03-24 11:26:52 +00002331 DeclSpec DS(AttrFactory);
Mike Stump98eb8a72009-02-04 22:31:32 +00002332 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002333 // FIXME: Since the return type isn't actually parsed, it can't be used to
2334 // fill ParamInfo with an initial valid range, so do it manually.
2335 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00002336
Steve Naroff296e8d52008-08-28 19:20:44 +00002337 // If this block has arguments, parse them. There is no ambiguity here with
2338 // the expression case, because the expression case requires a parameter list.
2339 if (Tok.is(tok::l_paren)) {
2340 ParseParenDeclarator(ParamInfo);
2341 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00002342 // SetIdentifier sets the source range end, but in this case we're past
2343 // that location.
2344 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00002345 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002346 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00002347 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002348 // If there was an error parsing the arguments, they may have
2349 // tried to use ^(x+y) which requires an argument list. Just
2350 // skip the whole block literal.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002351 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002352 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00002353 }
Mike Stump19c30c02009-04-29 19:03:13 +00002354
John McCall7f040a92010-12-24 02:08:15 +00002355 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002356
Mike Stump98eb8a72009-02-04 22:31:32 +00002357 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002358 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Mike Stumpaa771a82009-04-14 18:24:37 +00002359 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00002360 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00002361 } else {
2362 // Otherwise, pretend we saw (void).
John McCall0b7e6782011-03-24 11:26:52 +00002363 ParsedAttributes attrs(AttrFactory);
2364 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002365 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002366 0, 0, 0,
Douglas Gregor83f51722011-01-26 03:43:54 +00002367 true, SourceLocation(),
Douglas Gregor90ebed02011-07-13 21:47:47 +00002368 SourceLocation(),
Douglas Gregor43f51032011-10-19 06:04:55 +00002369 SourceLocation(),
2370 SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00002371 EST_None,
2372 SourceLocation(),
2373 0, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002374 CaretLoc, CaretLoc,
2375 ParamInfo),
John McCall0b7e6782011-03-24 11:26:52 +00002376 attrs, CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00002377
John McCall7f040a92010-12-24 02:08:15 +00002378 MaybeParseGNUAttributes(ParamInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00002379
Mike Stump98eb8a72009-02-04 22:31:32 +00002380 // Inform sema that we are starting a block.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002381 Actions.ActOnBlockArguments(ParamInfo, getCurScope());
Steve Naroff296e8d52008-08-28 19:20:44 +00002382 }
2383
Sebastian Redl1d922962008-12-13 15:32:12 +00002384
John McCall60d7b3a2010-08-24 06:29:42 +00002385 ExprResult Result(true);
Chris Lattner9af55002009-03-27 04:18:06 +00002386 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002387 // Saw something like: ^expr
2388 Diag(Tok, diag::err_expected_expression);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002389 Actions.ActOnBlockError(CaretLoc, getCurScope());
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00002390 return ExprError();
2391 }
Mike Stump1eb44332009-09-09 15:08:12 +00002392
John McCall60d7b3a2010-08-24 06:29:42 +00002393 StmtResult Stmt(ParseCompoundStatementBody());
Douglas Gregorc9977d02011-03-16 17:05:57 +00002394 BlockScope.Exit();
Chris Lattner9af55002009-03-27 04:18:06 +00002395 if (!Stmt.isInvalid())
John McCall9ae2f072010-08-23 23:25:46 +00002396 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope());
Chris Lattner9af55002009-03-27 04:18:06 +00002397 else
Douglas Gregor23c94db2010-07-02 17:43:08 +00002398 Actions.ActOnBlockError(CaretLoc, getCurScope());
Sebastian Redl1d922962008-12-13 15:32:12 +00002399 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00002400}