blob: 27696c438f032e65a14dd910ceec124bd4d70d86 [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"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Douglas Gregorae4c77d2010-02-05 19:11:37 +000025#include "clang/Parse/Template.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000026#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000027#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/SmallString.h"
30using namespace clang;
31
32/// PrecedenceLevels - These are precedences for the binary/ternary operators in
33/// the C99 grammar. These have been named to relate with the C99 grammar
34/// productions. Low precedences numbers bind more weakly than high numbers.
35namespace prec {
36 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000037 Unknown = 0, // Not binary operator.
38 Comma = 1, // ,
39 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
40 Conditional = 3, // ?
41 LogicalOr = 4, // ||
42 LogicalAnd = 5, // &&
43 InclusiveOr = 6, // |
44 ExclusiveOr = 7, // ^
45 And = 8, // &
46 Equality = 9, // ==, !=
47 Relational = 10, // >=, <=, >, <
48 Shift = 11, // <<, >>
49 Additive = 12, // -, +
50 Multiplicative = 13, // *, /, %
51 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000052 };
53}
54
55
56/// getBinOpPrecedence - Return the precedence of the specified binary operator
57/// token. This returns:
58///
Mike Stump1eb44332009-09-09 15:08:12 +000059static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000060 bool GreaterThanIsOperator,
61 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000063 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000064 // C++ [temp.names]p3:
65 // [...] When parsing a template-argument-list, the first
66 // non-nested > is taken as the ending delimiter rather than a
67 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000068 if (GreaterThanIsOperator)
69 return prec::Relational;
70 return prec::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Douglas Gregor3965b7b2009-02-25 23:02:36 +000072 case tok::greatergreater:
73 // C++0x [temp.names]p3:
74 //
75 // [...] Similarly, the first non-nested >> is treated as two
76 // consecutive but distinct > tokens, the first of which is
77 // taken as the end of the template-argument-list and completes
78 // the template-id. [...]
79 if (GreaterThanIsOperator || !CPlusPlus0x)
80 return prec::Shift;
81 return prec::Unknown;
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 default: return prec::Unknown;
84 case tok::comma: return prec::Comma;
85 case tok::equal:
86 case tok::starequal:
87 case tok::slashequal:
88 case tok::percentequal:
89 case tok::plusequal:
90 case tok::minusequal:
91 case tok::lesslessequal:
92 case tok::greatergreaterequal:
93 case tok::ampequal:
94 case tok::caretequal:
95 case tok::pipeequal: return prec::Assignment;
96 case tok::question: return prec::Conditional;
97 case tok::pipepipe: return prec::LogicalOr;
98 case tok::ampamp: return prec::LogicalAnd;
99 case tok::pipe: return prec::InclusiveOr;
100 case tok::caret: return prec::ExclusiveOr;
101 case tok::amp: return prec::And;
102 case tok::exclaimequal:
103 case tok::equalequal: return prec::Equality;
104 case tok::lessequal:
105 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000106 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000107 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 case tok::plus:
109 case tok::minus: return prec::Additive;
110 case tok::percent:
111 case tok::slash:
112 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000113 case tok::periodstar:
114 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116}
117
118
119/// ParseExpression - Simple precedence-based parser for binary/ternary
120/// operators.
121///
122/// Note: we diverge from the C99 grammar when parsing the assignment-expression
123/// production. C99 specifies that the LHS of an assignment operator should be
124/// parsed as a unary-expression, but consistency dictates that it be a
125/// conditional-expession. In practice, the important thing here is that the
126/// LHS of an assignment has to be an l-value, which productions between
127/// unary-expression and conditional-expression don't produce. Because we want
128/// consistency, we parse the LHS as a conditional-expression, then check for
129/// l-value-ness in semantic analysis stages.
130///
Sebastian Redl22460502009-02-07 00:15:38 +0000131/// pm-expression: [C++ 5.5]
132/// cast-expression
133/// pm-expression '.*' cast-expression
134/// pm-expression '->*' cast-expression
135///
Reid Spencer5f016e22007-07-11 17:01:13 +0000136/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000137/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// cast-expression
139/// multiplicative-expression '*' cast-expression
140/// multiplicative-expression '/' cast-expression
141/// multiplicative-expression '%' cast-expression
142///
143/// additive-expression: [C99 6.5.6]
144/// multiplicative-expression
145/// additive-expression '+' multiplicative-expression
146/// additive-expression '-' multiplicative-expression
147///
148/// shift-expression: [C99 6.5.7]
149/// additive-expression
150/// shift-expression '<<' additive-expression
151/// shift-expression '>>' additive-expression
152///
153/// relational-expression: [C99 6.5.8]
154/// shift-expression
155/// relational-expression '<' shift-expression
156/// relational-expression '>' shift-expression
157/// relational-expression '<=' shift-expression
158/// relational-expression '>=' shift-expression
159///
160/// equality-expression: [C99 6.5.9]
161/// relational-expression
162/// equality-expression '==' relational-expression
163/// equality-expression '!=' relational-expression
164///
165/// AND-expression: [C99 6.5.10]
166/// equality-expression
167/// AND-expression '&' equality-expression
168///
169/// exclusive-OR-expression: [C99 6.5.11]
170/// AND-expression
171/// exclusive-OR-expression '^' AND-expression
172///
173/// inclusive-OR-expression: [C99 6.5.12]
174/// exclusive-OR-expression
175/// inclusive-OR-expression '|' exclusive-OR-expression
176///
177/// logical-AND-expression: [C99 6.5.13]
178/// inclusive-OR-expression
179/// logical-AND-expression '&&' inclusive-OR-expression
180///
181/// logical-OR-expression: [C99 6.5.14]
182/// logical-AND-expression
183/// logical-OR-expression '||' logical-AND-expression
184///
185/// conditional-expression: [C99 6.5.15]
186/// logical-OR-expression
187/// logical-OR-expression '?' expression ':' conditional-expression
188/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000189/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000190///
191/// assignment-expression: [C99 6.5.16]
192/// conditional-expression
193/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000194/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000195///
196/// assignment-operator: one of
197/// = *= /= %= += -= <<= >>= &= ^= |=
198///
199/// expression: [C99 6.5.17]
200/// assignment-expression
201/// expression ',' assignment-expression
202///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000203Parser::OwningExprResult Parser::ParseExpression() {
Mike Stump6ce0c392009-05-15 21:47:08 +0000204 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000205 if (LHS.isInvalid()) return move(LHS);
206
Sebastian Redld8c4e152008-12-11 22:33:27 +0000207 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208}
209
Mike Stump1eb44332009-09-09 15:08:12 +0000210/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000211/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000212/// routine is necessary to disambiguate @try-statement from,
213/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000214///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000215Parser::OwningExprResult
216Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000217 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000218 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000219
Sebastian Redld8c4e152008-12-11 22:33:27 +0000220 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000221}
222
Eli Friedmanadf077f2009-01-27 08:43:38 +0000223/// This routine is called when a leading '__extension__' is seen and
224/// consumed. This is necessary because the token gets consumed in the
225/// process of disambiguating between an expression and a declaration.
226Parser::OwningExprResult
227Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000228 OwningExprResult LHS(Actions, true);
229 {
230 // Silence extension warnings in the sub-expression
231 ExtensionRAIIObject O(Diags);
232
233 LHS = ParseCastExpression(false);
234 if (LHS.isInvalid()) return move(LHS);
235 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000236
237 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000238 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000239 if (LHS.isInvalid()) return move(LHS);
240
241 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
242}
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
245///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000246Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000247 if (Tok.is(tok::code_completion)) {
248 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression);
249 ConsumeToken();
250 }
251
Chris Lattner50dd2892008-02-26 00:51:44 +0000252 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000253 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000254
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000255 OwningExprResult LHS(ParseCastExpression(false));
256 if (LHS.isInvalid()) return move(LHS);
257
Sebastian Redld8c4e152008-12-11 22:33:27 +0000258 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259}
260
Chris Lattnerb93fb492008-06-02 21:31:07 +0000261/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
262/// where part of an objc message send has already been parsed. In this case
263/// LBracLoc indicates the location of the '[' of the message send, and either
264/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
265/// message.
266///
267/// Since this handles full assignment-expression's, it handles postfix
268/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000269Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000270Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000271 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000272 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000273 ExprArg ReceiverExpr) {
274 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
275 ReceiverName,
276 move(ReceiverExpr)));
277 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000278 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000279 if (R.isInvalid()) return move(R);
280 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000281}
282
283
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000284Parser::OwningExprResult Parser::ParseConstantExpression() {
Douglas Gregore0762c92009-06-19 23:52:42 +0000285 // C++ [basic.def.odr]p2:
Mike Stump1eb44332009-09-09 15:08:12 +0000286 // An expression is potentially evaluated unless it appears where an
Douglas Gregore0762c92009-06-19 23:52:42 +0000287 // integral constant expression is required (see 5.19) [...].
Douglas Gregorac7610d2009-06-22 20:57:11 +0000288 EnterExpressionEvaluationContext Unevaluated(Actions,
289 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000291 OwningExprResult LHS(ParseCastExpression(false));
292 if (LHS.isInvalid()) return move(LHS);
293
Sebastian Redld8c4e152008-12-11 22:33:27 +0000294 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000295}
296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
298/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000299Parser::OwningExprResult
300Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Mike Stump1eb44332009-09-09 15:08:12 +0000301 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000302 GreaterThanIsOperator,
303 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 SourceLocation ColonLoc;
305
306 while (1) {
307 // If this token has a lower precedence than we are allowed to parse (e.g.
308 // because we are called recursively, or because the token is not a binop),
309 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000310 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000311 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312
313 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000314 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000318 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000320 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000321 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
322 ColonProtectionRAIIObject X(*this);
323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // Handle this production specially:
325 // logical-OR-expression '?' expression ':' conditional-expression
326 // In particular, the RHS of the '?' is 'expression', not
327 // 'logical-OR-expression' as we might expect.
328 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000329 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000330 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 } else {
332 // Special case handling of "X ? Y : Z" where Y is empty:
333 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000334 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 Diag(Tok, diag::ext_gnu_conditional_expr);
336 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000337
Chris Lattnere5deae92010-04-20 21:33:39 +0000338 if (Tok.is(tok::colon)) {
339 // Eat the colon.
340 ColonLoc = ConsumeToken();
341 } else {
Ted Kremenek987aa872010-04-12 22:10:35 +0000342 Diag(Tok, diag::err_expected_colon)
343 << FixItHint::CreateInsertion(Tok.getLocation(), ": ");
Chris Lattner28eb7e92008-11-23 23:17:07 +0000344 Diag(OpToken, diag::note_matching) << "?";
Chris Lattnere5deae92010-04-20 21:33:39 +0000345 ColonLoc = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000348
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000350 // ParseCastExpression works here because all RHS expressions in C have it
351 // as a prefix, at least. However, in C++, an assignment-expression could
352 // be a throw-expression, which is not a valid cast-expression.
353 // Therefore we need some special-casing here.
354 // Also note that the third operand of the conditional operator is
355 // an assignment-expression in C++.
356 OwningExprResult RHS(Actions);
357 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
358 RHS = ParseAssignmentExpression();
359 else
360 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000361 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000362 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363
364 // Remember the precedence of this operator and get the precedence of the
365 // operator immediately to the right of the RHS.
366 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000367 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
368 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369
370 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000371 bool isRightAssoc = ThisPrec == prec::Conditional ||
372 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
374 // Get the precedence of the operator to the right of the RHS. If it binds
375 // more tightly with RHS than we do, evaluate it completely first.
376 if (ThisPrec < NextTokPrec ||
377 (ThisPrec == NextTokPrec && isRightAssoc)) {
378 // If this is left-associative, only parse things on the RHS that bind
379 // more tightly than the current operator. If it is left-associative, it
380 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
381 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000382 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000383 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000384 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000385 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000386
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000387 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
388 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 }
390 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000391
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000392 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000393 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000394 if (TernaryMiddle.isInvalid()) {
395 // If we're using '>>' as an operator within a template
396 // argument list (in C++98), suggest the addition of
397 // parentheses so that the code remains well-formed in C++0x.
398 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
399 SuggestParentheses(OpToken.getLocation(),
400 diag::warn_cxx0x_right_shift_in_template_arg,
401 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
402 Actions.getExprRange(RHS.get()).getEnd()));
403
Sebastian Redleffa8d12008-12-10 00:02:53 +0000404 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000405 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000406 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000407 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000408 move(LHS), move(TernaryMiddle),
409 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000410 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 }
412}
413
414/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000415/// true, parse a unary-expression. isAddressOfOperand exists because an
416/// id-expression that is the operand of address-of gets special treatment
417/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000418///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000419Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000420 bool isAddressOfOperand,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000421 TypeTy *TypeOfCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000422 bool NotCastExpr;
423 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
424 isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000425 NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000426 TypeOfCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000427 if (NotCastExpr)
428 Diag(Tok, diag::err_expected_expression);
429 return move(Res);
430}
431
432/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
433/// true, parse a unary-expression. isAddressOfOperand exists because an
434/// id-expression that is the operand of address-of gets special treatment
435/// due to member pointers. NotCastExpr is set to true if the token is not the
436/// start of a cast-expression, and no diagnostic is emitted in this case.
437///
Reid Spencer5f016e22007-07-11 17:01:13 +0000438/// cast-expression: [C99 6.5.4]
439/// unary-expression
440/// '(' type-name ')' cast-expression
441///
442/// unary-expression: [C99 6.5.3]
443/// postfix-expression
444/// '++' unary-expression
445/// '--' unary-expression
446/// unary-operator cast-expression
447/// 'sizeof' unary-expression
448/// 'sizeof' '(' type-name ')'
449/// [GNU] '__alignof' unary-expression
450/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000451/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000452/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000453/// [C++] new-expression
454/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000455///
456/// unary-operator: one of
457/// '&' '*' '+' '-' '~' '!'
458/// [GNU] '__extension__' '__real' '__imag'
459///
460/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000461/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000462/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// constant
464/// string-literal
465/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000466/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000467/// '(' expression ')'
468/// '__func__' [C99 6.4.2.2]
469/// [GNU] '__FUNCTION__'
470/// [GNU] '__PRETTY_FUNCTION__'
471/// [GNU] '(' compound-statement ')'
472/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
473/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
474/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
475/// assign-expr ')'
476/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000477/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000478/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000479/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000480/// [OBJC] '@protocol' '(' identifier ')'
481/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000482/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000483/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
484/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000485/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
486/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
487/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
488/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000489/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
490/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000491/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000492/// [G++] unary-type-trait '(' type-id ')'
493/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000494/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000495///
496/// constant: [C99 6.4.4]
497/// integer-constant
498/// floating-constant
499/// enumeration-constant -> identifier
500/// character-constant
501///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000502/// id-expression: [C++ 5.1]
503/// unqualified-id
504/// qualified-id [TODO]
505///
506/// unqualified-id: [C++ 5.1]
507/// identifier
508/// operator-function-id
509/// conversion-function-id [TODO]
510/// '~' class-name [TODO]
511/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000512///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000513/// new-expression: [C++ 5.3.4]
514/// '::'[opt] 'new' new-placement[opt] new-type-id
515/// new-initializer[opt]
516/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
517/// new-initializer[opt]
518///
519/// delete-expression: [C++ 5.3.5]
520/// '::'[opt] 'delete' cast-expression
521/// '::'[opt] 'delete' '[' ']' cast-expression
522///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000523/// [GNU] unary-type-trait:
524/// '__has_nothrow_assign' [TODO]
525/// '__has_nothrow_copy' [TODO]
526/// '__has_nothrow_constructor' [TODO]
527/// '__has_trivial_assign' [TODO]
528/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000529/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000530/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000531/// '__has_virtual_destructor' [TODO]
532/// '__is_abstract' [TODO]
533/// '__is_class'
534/// '__is_empty' [TODO]
535/// '__is_enum'
536/// '__is_pod'
537/// '__is_polymorphic'
538/// '__is_union'
539///
540/// [GNU] binary-type-trait:
541/// '__is_base_of' [TODO]
542///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000543Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000544 bool isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000545 bool &NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000546 TypeTy *TypeOfCast) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000547 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000549 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 // This handles all of cast-expression, unary-expression, postfix-expression,
552 // and primary-expression. We handle them together like this for efficiency
553 // and to simplify handling of an expression starting with a '(' token: which
554 // may be one of a parenthesized expression, cast-expression, compound literal
555 // expression, or statement expression.
556 //
557 // If the parsed tokens consist of a primary-expression, the cases below
558 // call ParsePostfixExpressionSuffix to handle the postfix expression
559 // suffixes. Cases that cannot be followed by postfix exprs should
560 // return without invoking ParsePostfixExpressionSuffix.
561 switch (SavedKind) {
562 case tok::l_paren: {
563 // If this expression is limited to being a unary-expression, the parent can
564 // not start a cast expression.
565 ParenParseOption ParenExprType =
566 isUnaryExpression ? CompoundLiteral : CastExpr;
567 TypeTy *CastTy;
568 SourceLocation LParenLoc = Tok.getLocation();
569 SourceLocation RParenLoc;
Chris Lattner932dff72009-12-10 02:08:07 +0000570
571 {
572 // The inside of the parens don't need to be a colon protected scope.
573 ColonProtectionRAIIObject X(*this, false);
574
575 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
576 TypeOfCast, CastTy, RParenLoc);
577 if (Res.isInvalid()) return move(Res);
578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 switch (ParenExprType) {
581 case SimpleExpr: break; // Nothing else to do.
582 case CompoundStmt: break; // Nothing else to do.
583 case CompoundLiteral:
584 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
585 // postfix-expression exist, parse them now.
586 break;
587 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000588 // We have parsed the cast-expression and no postfix-expr pieces are
589 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000590 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000594 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // primary-expression
598 case tok::numeric_constant:
599 // constant: integer-constant
600 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000601
Steve Narofff69936d2007-09-16 03:34:24 +0000602 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000606 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608 case tok::kw_true:
609 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000610 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000611
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000612 case tok::kw_nullptr:
613 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
614
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000615 case tok::identifier: { // primary-expression: identifier
616 // unqualified-id: identifier
617 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000618 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000619 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000620 if (getLang().CPlusPlus) {
John McCallb6727072010-01-07 19:29:58 +0000621 // Avoid the unnecessary parse-time lookup in the common case
622 // where the syntax forbids a type.
623 const Token &Next = NextToken();
624 if (Next.is(tok::coloncolon) ||
625 (!ColonIsSacred && Next.is(tok::colon)) ||
626 Next.is(tok::less) ||
627 Next.is(tok::l_paren)) {
628 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
629 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000630 return ExprError();
631 if (!Tok.is(tok::identifier))
John McCallb6727072010-01-07 19:29:58 +0000632 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
633 }
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000634 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000635
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000636 // Consume the identifier so that we can see if it is followed by a '(' or
637 // '.'.
638 IdentifierInfo &II = *Tok.getIdentifierInfo();
639 SourceLocation ILoc = ConsumeToken();
640
Chris Lattnereb483eb2010-04-11 08:28:14 +0000641 // Support 'Class.property' and 'super.property' notation.
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000642 if (getLang().ObjC1 && Tok.is(tok::period) &&
Chris Lattner236beab2010-04-12 06:20:33 +0000643 (Actions.getTypeName(II, ILoc, CurScope) ||
644 // Allow the base to be 'super' if in an objc-method.
Chris Lattnerc987a412010-04-12 06:22:50 +0000645 (&II == Ident_super && CurScope->isInObjcMethodScope()))) {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000646 SourceLocation DotLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000647
Steve Naroff61f72cb2009-03-09 21:12:44 +0000648 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000649 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000650 return ExprError();
651 }
652 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
653 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000654
655 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
656 ILoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000657 // These can be followed by postfix-expr pieces.
658 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000659 }
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
662 // need to know whether or not this identifier is a function designator or
663 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000664 UnqualifiedId Name;
665 CXXScopeSpec ScopeSpec;
666 Name.setIdentifier(&II, ILoc);
667 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name,
668 Tok.is(tok::l_paren), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000670 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 }
672 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000673 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 ConsumeToken();
675 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000676 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
678 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
679 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000680 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 ConsumeToken();
682 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000683 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 case tok::string_literal: // primary-expression: string-literal
685 case tok::wide_string_literal:
686 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000687 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000689 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 case tok::kw___builtin_va_arg:
691 case tok::kw___builtin_offsetof:
692 case tok::kw___builtin_choose_expr:
693 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000694 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000695 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000696 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000697 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 case tok::plusplus: // unary-expression: '++' unary-expression
699 case tok::minusminus: { // unary-expression: '--' unary-expression
700 SourceLocation SavedLoc = ConsumeToken();
701 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000703 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000704 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000706 case tok::amp: { // unary-expression: '&' cast-expression
707 // Special treatment because of member pointers
708 SourceLocation SavedLoc = ConsumeToken();
709 Res = ParseCastExpression(false, true);
710 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000711 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000712 return move(Res);
713 }
714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 case tok::star: // unary-expression: '*' cast-expression
716 case tok::plus: // unary-expression: '+' cast-expression
717 case tok::minus: // unary-expression: '-' cast-expression
718 case tok::tilde: // unary-expression: '~' cast-expression
719 case tok::exclaim: // unary-expression: '!' cast-expression
720 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000721 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 SourceLocation SavedLoc = ConsumeToken();
723 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000724 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000725 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000726 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000727 }
728
Chris Lattner35080842008-02-02 20:20:10 +0000729 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
730 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000731 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000732 SourceLocation SavedLoc = ConsumeToken();
733 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000734 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000735 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000736 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
738 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
739 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000740 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
742 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000743 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000744 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 case tok::ampamp: { // unary-expression: '&&' identifier
746 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000747 if (Tok.isNot(tok::identifier))
748 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000751 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 Tok.getIdentifierInfo());
753 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000754 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
756 case tok::kw_const_cast:
757 case tok::kw_dynamic_cast:
758 case tok::kw_reinterpret_cast:
759 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000760 Res = ParseCXXCasts();
761 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000762 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000763 case tok::kw_typeid:
764 Res = ParseCXXTypeid();
765 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000766 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000767 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000768 Res = ParseCXXThis();
769 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000770 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000771
772 case tok::kw_char:
773 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000774 case tok::kw_char16_t:
775 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000776 case tok::kw_bool:
777 case tok::kw_short:
778 case tok::kw_int:
779 case tok::kw_long:
780 case tok::kw_signed:
781 case tok::kw_unsigned:
782 case tok::kw_float:
783 case tok::kw_double:
784 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000785 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000786 case tok::kw_typeof:
John Thompson82287d12010-02-05 00:12:22 +0000787 case tok::kw___vector:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000788 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000789 if (!getLang().CPlusPlus) {
790 Diag(Tok, diag::err_expected_expression);
791 return ExprError();
792 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000793
794 if (SavedKind == tok::kw_typename) {
795 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
John McCall9ba61662010-02-26 08:45:28 +0000796 if (TryAnnotateTypeOrScopeToken())
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000797 return ExprError();
798 }
799
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000800 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
801 //
802 DeclSpec DS;
803 ParseCXXSimpleTypeSpecifier(DS);
804 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000805 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
806 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000807
808 Res = ParseCXXTypeConstructExpression(DS);
809 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000810 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000811 }
812
Douglas Gregorae4c77d2010-02-05 19:11:37 +0000813 case tok::annot_cxxscope: { // [C++] id-expression: qualified-id
814 Token Next = NextToken();
815 if (Next.is(tok::annot_template_id)) {
816 TemplateIdAnnotation *TemplateId
817 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
818 if (TemplateId->Kind == TNK_Type_template) {
819 // We have a qualified template-id that we know refers to a
820 // type, translate it into a type and continue parsing as a
821 // cast expression.
822 CXXScopeSpec SS;
823 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
824 AnnotateTemplateIdTokenAsType(&SS);
825 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
826 NotCastExpr, TypeOfCast);
827 }
828 }
829
830 // Parse as an id-expression.
831 Res = ParseCXXIdExpression(isAddressOfOperand);
832 return ParsePostfixExpressionSuffix(move(Res));
833 }
834
835 case tok::annot_template_id: { // [C++] template-id
836 TemplateIdAnnotation *TemplateId
837 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
838 if (TemplateId->Kind == TNK_Type_template) {
839 // We have a template-id that we know refers to a type,
840 // translate it into a type and continue parsing as a cast
841 // expression.
842 AnnotateTemplateIdTokenAsType();
843 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
844 NotCastExpr, TypeOfCast);
845 }
846
847 // Fall through to treat the template-id as an id-expression.
848 }
849
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000850 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000851 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000852 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000853
Chris Lattner74ba4102009-01-04 22:52:14 +0000854 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000855 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
856 // annotates the token, tail recurse.
857 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +0000858 return ExprError();
859 if (!Tok.is(tok::coloncolon))
Sebastian Redlebc07d52009-02-03 20:19:35 +0000860 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
861
Chris Lattner74ba4102009-01-04 22:52:14 +0000862 // ::new -> [C++] new-expression
863 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000864 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000865 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000866 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000867 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000868 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000870 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000871 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000872 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000873 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000874
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000875 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000876 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000877
878 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000879 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000880
Sebastian Redl64b45f72009-01-05 20:52:13 +0000881 case tok::kw___is_pod: // [GNU] unary-type-trait
882 case tok::kw___is_class:
883 case tok::kw___is_enum:
884 case tok::kw___is_union:
Eli Friedman1d954f62009-08-15 21:55:26 +0000885 case tok::kw___is_empty:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000886 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000887 case tok::kw___is_abstract:
Sebastian Redlccf43502009-12-03 00:13:20 +0000888 case tok::kw___is_literal:
Anders Carlsson347ba892009-04-16 00:08:20 +0000889 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000890 case tok::kw___has_trivial_copy:
891 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +0000892 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000893 return ParseUnaryTypeTrait();
894
Chris Lattnerc97c2042007-10-03 22:03:06 +0000895 case tok::at: {
896 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000897 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000898 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000899 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000900 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Douglas Gregor64538cf2010-04-06 15:09:27 +0000901 case tok::code_completion:
902 Actions.CodeCompleteOrdinaryName(CurScope, Action::CCC_Expression);
903 ConsumeToken();
904 return ParseCastExpression(isUnaryExpression, isAddressOfOperand,
905 NotCastExpr, TypeOfCast);
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000906 case tok::l_square:
907 // These can be followed by postfix-expr pieces.
908 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000909 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Douglas Gregor64538cf2010-04-06 15:09:27 +0000910 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000912 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000913 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000915
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 // unreachable.
917 abort();
918}
919
920/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
921/// is parsed, this method parses any suffixes that apply.
922///
923/// postfix-expression: [C99 6.5.2]
924/// primary-expression
925/// postfix-expression '[' expression ']'
926/// postfix-expression '(' argument-expression-list[opt] ')'
927/// postfix-expression '.' identifier
928/// postfix-expression '->' identifier
929/// postfix-expression '++'
930/// postfix-expression '--'
931/// '(' type-name ')' '{' initializer-list '}'
932/// '(' type-name ')' '{' initializer-list ',' '}'
933///
934/// argument-expression-list: [C99 6.5.2]
935/// argument-expression
936/// argument-expression-list ',' assignment-expression
937///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000938Parser::OwningExprResult
939Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 // Now that the primary-expression piece of the postfix-expression has been
941 // parsed, see if there are any postfix-expression pieces here.
942 SourceLocation Loc;
943 while (1) {
944 switch (Tok.getKind()) {
945 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000946 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
948 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000949 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000950
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000952
953 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000954 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
955 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000956 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000957 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
959 // Match the ']'.
960 MatchRHSPunctuation(tok::r_square, Loc);
961 break;
962 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000963
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000965 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000966 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000967
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000969
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000970 if (Tok.is(tok::code_completion)) {
971 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
972 ConsumeToken();
973 }
974
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000975 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000976 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
977 LHS.get())) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000978 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000979 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 }
981 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000984 if (Tok.isNot(tok::r_paren)) {
985 MatchRHSPunctuation(tok::r_paren, Loc);
986 return ExprError();
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattner1721a2d2009-04-13 00:10:38 +0000989 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
991 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000992 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000993 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000994 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner1721a2d2009-04-13 00:10:38 +0000997 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 break;
999 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001000 case tok::arrow:
1001 case tok::period: {
1002 // postfix-expression: p-e '->' template[opt] id-expression
1003 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 tok::TokenKind OpKind = Tok.getKind();
1005 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001006
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001007 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001008 Action::TypeTy *ObjectType = 0;
Douglas Gregord4dca082010-02-24 18:44:31 +00001009 bool MayBePseudoDestructor = false;
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001010 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001011 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
Douglas Gregord4dca082010-02-24 18:44:31 +00001012 OpLoc, OpKind, ObjectType,
1013 MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001014 if (LHS.isInvalid())
1015 break;
Douglas Gregord4dca082010-02-24 18:44:31 +00001016
Douglas Gregoredc90502010-02-25 04:46:04 +00001017 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false,
Douglas Gregord4dca082010-02-24 18:44:31 +00001018 &MayBePseudoDestructor);
Douglas Gregorfe85ced2009-08-06 03:17:00 +00001019 }
1020
Douglas Gregor81b747b2009-09-17 21:32:03 +00001021 if (Tok.is(tok::code_completion)) {
1022 // Code completion for a member access expression.
1023 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
1024 OpLoc, OpKind == tok::arrow);
1025
1026 ConsumeToken();
1027 }
1028
Douglas Gregord4dca082010-02-24 18:44:31 +00001029 if (MayBePseudoDestructor) {
1030 LHS = ParseCXXPseudoDestructor(move(LHS), OpLoc, OpKind, SS,
1031 ObjectType);
1032 break;
1033 }
1034
1035 // Either the action has told is that this cannot be a
1036 // pseudo-destructor expression (based on the type of base
1037 // expression), or we didn't see a '~' in the right place. We
1038 // can still parse a destructor name here, but in that case it
1039 // names a real destructor.
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001040 UnqualifiedId Name;
1041 if (ParseUnqualifiedId(SS,
1042 /*EnteringContext=*/false,
1043 /*AllowDestructorName=*/true,
1044 /*AllowConstructorName=*/false,
1045 ObjectType,
1046 Name))
1047 return ExprError();
1048
1049 if (!LHS.isInvalid())
Douglas Gregord4dca082010-02-24 18:44:31 +00001050 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc,
1051 OpKind, SS, Name, ObjCImpDecl,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00001052 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 break;
1054 }
1055 case tok::plusplus: // postfix-expression: postfix-expression '++'
1056 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001057 if (!LHS.isInvalid()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001058 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001059 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +00001060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 ConsumeToken();
1062 break;
1063 }
1064 }
1065}
1066
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001067/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
1068/// we are at the start of an expression or a parenthesized type-id.
1069/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
1070/// (isCastExpr == false) or the type (isCastExpr == true).
1071///
1072/// unary-expression: [C99 6.5.3]
1073/// 'sizeof' unary-expression
1074/// 'sizeof' '(' type-name ')'
1075/// [GNU] '__alignof' unary-expression
1076/// [GNU] '__alignof' '(' type-name ')'
1077/// [C++0x] 'alignof' '(' type-id ')'
1078///
1079/// [GNU] typeof-specifier:
1080/// typeof ( expressions )
1081/// typeof ( type-name )
1082/// [GNU/C++] typeof unary-expression
1083///
1084Parser::OwningExprResult
1085Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1086 bool &isCastExpr,
1087 TypeTy *&CastTy,
1088 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001089
1090 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001091 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1092 "Not a typeof/sizeof/alignof expression!");
1093
1094 OwningExprResult Operand(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001096 // If the operand doesn't start with an '(', it must be an expression.
1097 if (Tok.isNot(tok::l_paren)) {
1098 isCastExpr = false;
1099 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1100 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1101 return ExprError();
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Douglas Gregore0762c92009-06-19 23:52:42 +00001104 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001105 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001106 // operand (Clause 5) [...]
1107 //
1108 // The GNU typeof and alignof extensions also behave as unevaluated
1109 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001110 EnterExpressionEvaluationContext Unevaluated(Actions,
1111 Action::Unevaluated);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001112 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001113 } else {
1114 // If it starts with a '(', we know that it is either a parenthesized
1115 // type-name, or it is a unary-expression that starts with a compound
1116 // literal, or starts with a primary-expression that is a parenthesized
1117 // expression.
1118 ParenParseOption ExprType = CastExpr;
1119 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregore0762c92009-06-19 23:52:42 +00001121 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001122 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001123 // operand (Clause 5) [...]
1124 //
1125 // The GNU typeof and alignof extensions also behave as unevaluated
1126 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001127 EnterExpressionEvaluationContext Unevaluated(Actions,
1128 Action::Unevaluated);
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001129 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1130 0/*TypeOfCast*/,
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001131 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001132 CastRange = SourceRange(LParenLoc, RParenLoc);
1133
1134 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1135 // a type.
1136 if (ExprType == CastExpr) {
1137 isCastExpr = true;
1138 return ExprEmpty();
1139 }
1140
Mike Stump1eb44332009-09-09 15:08:12 +00001141 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001142 // unary-expression, but doesn't include any postfix pieces. Parse these
1143 // now if present.
1144 Operand = ParsePostfixExpressionSuffix(move(Operand));
1145 }
1146
1147 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1148 isCastExpr = false;
1149 return move(Operand);
1150}
1151
Reid Spencer5f016e22007-07-11 17:01:13 +00001152
1153/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1154/// unary-expression: [C99 6.5.3]
1155/// 'sizeof' unary-expression
1156/// 'sizeof' '(' type-name ')'
1157/// [GNU] '__alignof' unary-expression
1158/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001159/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +00001160Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001161 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1162 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001164 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001167 bool isCastExpr;
1168 TypeTy *CastTy;
1169 SourceRange CastRange;
1170 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1171 isCastExpr,
1172 CastTy,
1173 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001174
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001175 if (isCastExpr)
1176 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1177 OpTok.is(tok::kw_sizeof),
1178 /*isType=*/true, CastTy,
1179 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001182 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001183 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1184 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001185 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001186 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001187 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188}
1189
1190/// ParseBuiltinPrimaryExpression
1191///
1192/// primary-expression: [C99 6.5.1]
1193/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1194/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1195/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1196/// assign-expr ')'
1197/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001198///
Reid Spencer5f016e22007-07-11 17:01:13 +00001199/// [GNU] offsetof-member-designator:
1200/// [GNU] identifier
1201/// [GNU] offsetof-member-designator '.' identifier
1202/// [GNU] offsetof-member-designator '[' expression ']'
1203///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001204Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001205 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1207
1208 tok::TokenKind T = Tok.getKind();
1209 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1210
1211 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001212 if (Tok.isNot(tok::l_paren))
1213 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1214 << BuiltinII);
1215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 SourceLocation LParenLoc = ConsumeParen();
1217 // TODO: Build AST.
1218
1219 switch (T) {
1220 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001221 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001222 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001223 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001225 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 }
1227
1228 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001229 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001230
Douglas Gregor809070a2009-02-18 17:45:20 +00001231 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001232
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001233 if (Tok.isNot(tok::r_paren)) {
1234 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001235 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001236 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001237 if (Ty.isInvalid())
1238 Res = ExprError();
1239 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001240 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001242 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001243 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001244 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001245 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001246 if (Ty.isInvalid()) {
1247 SkipUntil(tok::r_paren);
1248 return ExprError();
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001252 return ExprError();
1253
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001255 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001256 Diag(Tok, diag::err_expected_ident);
1257 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001258 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001259 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001260
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001261 // Keep track of the various subcomponents we see.
1262 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001263
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001264 Comps.push_back(Action::OffsetOfComponent());
1265 Comps.back().isBrackets = false;
1266 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1267 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001268
Sebastian Redla55e52c2008-11-25 22:21:31 +00001269 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001271 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001273 Comps.push_back(Action::OffsetOfComponent());
1274 Comps.back().isBrackets = false;
1275 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001276
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001277 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001278 Diag(Tok, diag::err_expected_ident);
1279 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001280 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001281 }
1282 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1283 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001284
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001285 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001287 Comps.push_back(Action::OffsetOfComponent());
1288 Comps.back().isBrackets = true;
1289 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001291 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001293 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001295 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001297 Comps.back().LocEnd =
1298 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman309fe0d2009-06-27 20:38:33 +00001299 } else {
1300 if (Tok.isNot(tok::r_paren)) {
1301 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00001302 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001303 } else if (Ty.isInvalid()) {
1304 Res = ExprError();
1305 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001306 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1307 Ty.get(), &Comps[0],
Douglas Gregor809070a2009-02-18 17:45:20 +00001308 Comps.size(), ConsumeParen());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001309 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001310 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 }
1312 }
1313 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001314 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001315 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001316 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001317 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001318 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001319 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001320 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001322 return ExprError();
1323
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001324 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001325 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001326 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001327 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001328 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001330 return ExprError();
1331
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001332 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001333 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001334 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001335 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001336 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001337 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001338 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001339 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001340 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001341 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1342 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001343 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001344 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001346 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001349 return ExprError();
1350
Douglas Gregor809070a2009-02-18 17:45:20 +00001351 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001352
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001353 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001354 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001355 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001356 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001357
1358 if (Ty1.isInvalid() || Ty2.isInvalid())
1359 Res = ExprError();
1360 else
1361 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1362 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001363 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001364 }
1365
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 // These can be followed by postfix-expr pieces because they are
1367 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001368 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001369}
1370
1371/// ParseParenExpression - This parses the unit that starts with a '(' token,
1372/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001373/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1374/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001375///
1376/// primary-expression: [C99 6.5.1]
1377/// '(' expression ')'
1378/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1379/// postfix-expression: [C99 6.5.2]
1380/// '(' type-name ')' '{' initializer-list '}'
1381/// '(' type-name ')' '{' initializer-list ',' '}'
1382/// cast-expression: [C99 6.5.4]
1383/// '(' type-name ')' cast-expression
1384///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001385Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001386Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001387 TypeTy *TypeOfCast, TypeTy *&CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001388 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001389 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001390 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001392 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001393 bool isAmbiguousTypeId;
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001395
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001396 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 Diag(Tok, diag::ext_gnu_statement_expr);
Sean Huntbbd37c62009-11-21 08:43:09 +00001398 OwningStmtResult Stmt(ParseCompoundStatement(0, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001400
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001401 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001402 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001403 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001404
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001405 } else if (ExprType >= CompoundLiteral &&
1406 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001410 // In C++, if the type-id is ambiguous we disambiguate based on context.
1411 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1412 // in which case we should treat it as type-id.
1413 // if stopIfCastExpr is false, we need to determine the context past the
1414 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1415 if (isAmbiguousTypeId && !stopIfCastExpr)
1416 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1417 OpenLoc, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor809070a2009-02-18 17:45:20 +00001419 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001420
1421 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001422 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 RParenLoc = ConsumeParen();
1424 else
1425 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001426
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001427 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001429 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001430 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001431
Chris Lattner42ece642008-12-12 06:00:12 +00001432 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001433 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001434
1435 if (Ty.isInvalid())
1436 return ExprError();
1437
1438 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001439
Chris Lattnera823d6a2010-04-12 06:27:57 +00001440 // Note that this doesn't parse the subsequent cast-expression, it just
1441 // returns the parsed type to the callee.
1442 if (stopIfCastExpr)
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001443 return OwningExprResult(Actions);
Chris Lattnereb483eb2010-04-11 08:28:14 +00001444
Chris Lattnereb483eb2010-04-11 08:28:14 +00001445 // Reject the cast of super idiom in ObjC.
1446 if (Tok.is(tok::identifier) && getLang().ObjC1 &&
Chris Lattnera823d6a2010-04-12 06:27:57 +00001447 Tok.getIdentifierInfo() == Ident_super &&
Chris Lattner8b9f1872010-04-12 17:09:27 +00001448 CurScope->isInObjcMethodScope() &&
1449 GetLookAheadToken(1).isNot(tok::period)) {
Chris Lattnereb483eb2010-04-11 08:28:14 +00001450 Diag(Tok.getLocation(), diag::err_illegal_super_cast)
1451 << SourceRange(OpenLoc, RParenLoc);
1452 return ExprError();
1453 }
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001454
1455 // Parse the cast-expression that follows it next.
1456 // TODO: For cast expression with CastTy.
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001457 Result = ParseCastExpression(false, false, CastTy);
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001458 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001459 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1460 move(Result));
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001461 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001463
Chris Lattner42ece642008-12-12 06:00:12 +00001464 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1465 return ExprError();
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001466 } else if (TypeOfCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00001467 // Parse the expression-list.
1468 ExprVector ArgExprs(Actions);
1469 CommaLocsTy CommaLocs;
1470
1471 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1472 ExprType = SimpleExpr;
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001473 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
1474 move_arg(ArgExprs), TypeOfCast);
Nate Begeman2ef13e52009-08-10 23:49:36 +00001475 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 } else {
1477 Result = ParseExpression();
1478 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001479 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001480 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001482
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001484 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001485 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001486 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Chris Lattner42ece642008-12-12 06:00:12 +00001489 if (Tok.is(tok::r_paren))
1490 RParenLoc = ConsumeParen();
1491 else
1492 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001493
1494 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001495}
1496
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001497/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1498/// and we are at the left brace.
1499///
1500/// postfix-expression: [C99 6.5.2]
1501/// '(' type-name ')' '{' initializer-list '}'
1502/// '(' type-name ')' '{' initializer-list ',' '}'
1503///
1504Parser::OwningExprResult
1505Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1506 SourceLocation LParenLoc,
1507 SourceLocation RParenLoc) {
1508 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1509 if (!getLang().C99) // Compound literals don't exist in C90.
1510 Diag(LParenLoc, diag::ext_c99_compound_literal);
1511 OwningExprResult Result = ParseInitializer();
1512 if (!Result.isInvalid() && Ty)
1513 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1514 return move(Result);
1515}
1516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517/// ParseStringLiteralExpression - This handles the various token types that
1518/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1519/// translation phase #6].
1520///
1521/// primary-expression: [C99 6.5.1]
1522/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001523Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001525
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1527 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001528 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 do {
1531 StringToks.push_back(Tok);
1532 ConsumeStringToken();
1533 } while (isTokenStringLiteral());
1534
1535 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001536 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001537}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001538
1539/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1540///
1541/// argument-expression-list:
1542/// assignment-expression
1543/// argument-expression-list , assignment-expression
1544///
1545/// [C++] expression-list:
1546/// [C++] assignment-expression
1547/// [C++] expression-list , assignment-expression
1548///
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001549bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1550 void (Action::*Completer)(Scope *S,
1551 void *Data,
1552 ExprTy **Args,
1553 unsigned NumArgs),
1554 void *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001555 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001556 if (Tok.is(tok::code_completion)) {
1557 if (Completer)
1558 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1559 ConsumeToken();
1560 }
1561
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001562 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001563 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001564 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001565
Sebastian Redleffa8d12008-12-10 00:02:53 +00001566 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001567
1568 if (Tok.isNot(tok::comma))
1569 return false;
1570 // Move to the next argument, remember where the comma was.
1571 CommaLocs.push_back(ConsumeToken());
1572 }
1573}
Steve Naroff296e8d52008-08-28 19:20:44 +00001574
Mike Stump98eb8a72009-02-04 22:31:32 +00001575/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1576///
1577/// [clang] block-id:
1578/// [clang] specifier-qualifier-list block-declarator
1579///
1580void Parser::ParseBlockId() {
1581 // Parse the specifier-qualifier-list piece.
1582 DeclSpec DS;
1583 ParseSpecifierQualifierList(DS);
1584
1585 // Parse the block-declarator.
1586 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1587 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001588
Mike Stump6c92fa72009-04-29 21:40:37 +00001589 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1590 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1591 SourceLocation());
1592
Mike Stump19c30c02009-04-29 19:03:13 +00001593 if (Tok.is(tok::kw___attribute)) {
1594 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001595 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001596 DeclaratorInfo.AddAttributes(AttrList, Loc);
1597 }
1598
Mike Stump98eb8a72009-02-04 22:31:32 +00001599 // Inform sema that we are starting a block.
1600 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1601}
1602
Steve Naroff296e8d52008-08-28 19:20:44 +00001603/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001604/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001605///
1606/// block-literal:
1607/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001608/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001609/// [clang] block-args:
1610/// [clang] '(' parameter-list ')'
1611///
Sebastian Redl1d922962008-12-13 15:32:12 +00001612Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001613 assert(Tok.is(tok::caret) && "block literal starts with ^");
1614 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001615
Chris Lattner6b91f002009-03-05 07:32:12 +00001616 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1617 "block literal parsing");
1618
Mike Stump1eb44332009-09-09 15:08:12 +00001619 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00001620 // argument decls, decls within the compound expression, etc. This also
1621 // allows determining whether a variable reference inside the block is
1622 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001623 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1624 Scope::BreakScope | Scope::ContinueScope |
1625 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001626
1627 // Inform sema that we are starting a block.
1628 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Steve Naroff296e8d52008-08-28 19:20:44 +00001630 // Parse the return type if present.
1631 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001632 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001633 // FIXME: Since the return type isn't actually parsed, it can't be used to
1634 // fill ParamInfo with an initial valid range, so do it manually.
1635 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001636
Steve Naroff296e8d52008-08-28 19:20:44 +00001637 // If this block has arguments, parse them. There is no ambiguity here with
1638 // the expression case, because the expression case requires a parameter list.
1639 if (Tok.is(tok::l_paren)) {
1640 ParseParenDeclarator(ParamInfo);
1641 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001642 // SetIdentifier sets the source range end, but in this case we're past
1643 // that location.
1644 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001645 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001646 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001647 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001648 // If there was an error parsing the arguments, they may have
1649 // tried to use ^(x+y) which requires an argument list. Just
1650 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001651 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001652 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001653 }
Mike Stump19c30c02009-04-29 19:03:13 +00001654
1655 if (Tok.is(tok::kw___attribute)) {
1656 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001657 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001658 ParamInfo.AddAttributes(AttrList, Loc);
1659 }
1660
Mike Stump98eb8a72009-02-04 22:31:32 +00001661 // Inform sema that we are starting a block.
1662 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001663 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001664 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001665 } else {
1666 // Otherwise, pretend we saw (void).
Mike Stump1eb44332009-09-09 15:08:12 +00001667 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00001668 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001669 0, 0, 0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00001670 false, SourceLocation(),
1671 false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00001672 CaretLoc, CaretLoc,
1673 ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001674 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001675
1676 if (Tok.is(tok::kw___attribute)) {
1677 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001678 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001679 ParamInfo.AddAttributes(AttrList, Loc);
1680 }
1681
Mike Stump98eb8a72009-02-04 22:31:32 +00001682 // Inform sema that we are starting a block.
1683 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001684 }
1685
Sebastian Redl1d922962008-12-13 15:32:12 +00001686
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001687 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001688 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001689 // Saw something like: ^expr
1690 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001691 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001692 return ExprError();
1693 }
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Chris Lattner9af55002009-03-27 04:18:06 +00001695 OwningStmtResult Stmt(ParseCompoundStatementBody());
1696 if (!Stmt.isInvalid())
1697 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1698 else
1699 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001700 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001701}