blob: bdbc67f782dcfd09c99984c1f037e3d4c028f6fc [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercde626a2006-08-12 08:13:25 +000010// 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
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff0ac012832008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerf6801202009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000026#include "RAIIObjectsForParser.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000029using namespace clang;
30
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000031/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000032/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl112a97662009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Chris Lattnercde626a2006-08-12 08:13:25 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Mike Stump11289f42009-09-09 15:08:12 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorcbb45d02009-02-25 23:02:36 +000059 bool GreaterThanIsOperator,
60 bool CPlusPlus0x) {
Chris Lattnercde626a2006-08-12 08:13:25 +000061 switch (Kind) {
Douglas Gregor8bf42052009-02-09 18:46:07 +000062 case tok::greater:
Douglas Gregorcbb45d02009-02-25 23:02:36 +000063 // C++ [temp.names]p3:
64 // [...] When parsing a template-argument-list, the first
65 // non-nested > is taken as the ending delimiter rather than a
66 // greater-than operator. [...]
Douglas Gregor8bf42052009-02-09 18:46:07 +000067 if (GreaterThanIsOperator)
68 return prec::Relational;
69 return prec::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +000070
Douglas Gregorcbb45d02009-02-25 23:02:36 +000071 case tok::greatergreater:
72 // C++0x [temp.names]p3:
73 //
74 // [...] Similarly, the first non-nested >> is treated as two
75 // consecutive but distinct > tokens, the first of which is
76 // taken as the end of the template-argument-list and completes
77 // the template-id. [...]
78 if (GreaterThanIsOperator || !CPlusPlus0x)
79 return prec::Shift;
80 return prec::Unknown;
81
Chris Lattnercde626a2006-08-12 08:13:25 +000082 default: return prec::Unknown;
83 case tok::comma: return prec::Comma;
84 case tok::equal:
85 case tok::starequal:
86 case tok::slashequal:
87 case tok::percentequal:
88 case tok::plusequal:
89 case tok::minusequal:
90 case tok::lesslessequal:
91 case tok::greatergreaterequal:
92 case tok::ampequal:
93 case tok::caretequal:
94 case tok::pipeequal: return prec::Assignment;
95 case tok::question: return prec::Conditional;
96 case tok::pipepipe: return prec::LogicalOr;
97 case tok::ampamp: return prec::LogicalAnd;
98 case tok::pipe: return prec::InclusiveOr;
99 case tok::caret: return prec::ExclusiveOr;
100 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +0000101 case tok::exclaimequal:
102 case tok::equalequal: return prec::Equality;
103 case tok::lessequal:
104 case tok::less:
Douglas Gregor8bf42052009-02-09 18:46:07 +0000105 case tok::greaterequal: return prec::Relational;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000106 case tok::lessless: return prec::Shift;
Chris Lattnercde626a2006-08-12 08:13:25 +0000107 case tok::plus:
108 case tok::minus: return prec::Additive;
109 case tok::percent:
110 case tok::slash:
111 case tok::star: return prec::Multiplicative;
Sebastian Redl112a97662009-02-07 00:15:38 +0000112 case tok::periodstar:
113 case tok::arrowstar: return prec::PointerToMember;
Chris Lattnercde626a2006-08-12 08:13:25 +0000114 }
115}
116
117
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000118/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +0000119/// operators.
120///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000121/// Note: we diverge from the C99 grammar when parsing the assignment-expression
122/// production. C99 specifies that the LHS of an assignment operator should be
123/// parsed as a unary-expression, but consistency dictates that it be a
124/// conditional-expession. In practice, the important thing here is that the
125/// LHS of an assignment has to be an l-value, which productions between
126/// unary-expression and conditional-expression don't produce. Because we want
127/// consistency, we parse the LHS as a conditional-expression, then check for
128/// l-value-ness in semantic analysis stages.
129///
Sebastian Redl112a97662009-02-07 00:15:38 +0000130/// pm-expression: [C++ 5.5]
131/// cast-expression
132/// pm-expression '.*' cast-expression
133/// pm-expression '->*' cast-expression
134///
Chris Lattnercde626a2006-08-12 08:13:25 +0000135/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl112a97662009-02-07 00:15:38 +0000136/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000137/// cast-expression
138/// multiplicative-expression '*' cast-expression
139/// multiplicative-expression '/' cast-expression
140/// multiplicative-expression '%' cast-expression
141///
142/// additive-expression: [C99 6.5.6]
143/// multiplicative-expression
144/// additive-expression '+' multiplicative-expression
145/// additive-expression '-' multiplicative-expression
146///
147/// shift-expression: [C99 6.5.7]
148/// additive-expression
149/// shift-expression '<<' additive-expression
150/// shift-expression '>>' additive-expression
151///
152/// relational-expression: [C99 6.5.8]
153/// shift-expression
154/// relational-expression '<' shift-expression
155/// relational-expression '>' shift-expression
156/// relational-expression '<=' shift-expression
157/// relational-expression '>=' shift-expression
158///
159/// equality-expression: [C99 6.5.9]
160/// relational-expression
161/// equality-expression '==' relational-expression
162/// equality-expression '!=' relational-expression
163///
164/// AND-expression: [C99 6.5.10]
165/// equality-expression
166/// AND-expression '&' equality-expression
167///
168/// exclusive-OR-expression: [C99 6.5.11]
169/// AND-expression
170/// exclusive-OR-expression '^' AND-expression
171///
172/// inclusive-OR-expression: [C99 6.5.12]
173/// exclusive-OR-expression
174/// inclusive-OR-expression '|' exclusive-OR-expression
175///
176/// logical-AND-expression: [C99 6.5.13]
177/// inclusive-OR-expression
178/// logical-AND-expression '&&' inclusive-OR-expression
179///
180/// logical-OR-expression: [C99 6.5.14]
181/// logical-AND-expression
182/// logical-OR-expression '||' logical-AND-expression
183///
184/// conditional-expression: [C99 6.5.15]
185/// logical-OR-expression
186/// logical-OR-expression '?' expression ':' conditional-expression
187/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl1a99f442009-04-16 17:51:27 +0000188/// [C++] the third operand is an assignment-expression
Chris Lattnercde626a2006-08-12 08:13:25 +0000189///
190/// assignment-expression: [C99 6.5.16]
191/// conditional-expression
192/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000193/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000194///
195/// assignment-operator: one of
196/// = *= /= %= += -= <<= >>= &= ^= |=
197///
198/// expression: [C99 6.5.17]
199/// assignment-expression
200/// expression ',' assignment-expression
201///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000202Parser::OwningExprResult Parser::ParseExpression() {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000203 if (Tok.is(tok::code_completion)) {
204 Actions.CodeCompleteOrdinaryName(CurScope);
205 ConsumeToken();
206 }
207
Mike Stump76b824c2009-05-15 21:47:08 +0000208 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl59b5e512008-12-11 21:36:32 +0000209 if (LHS.isInvalid()) return move(LHS);
210
Sebastian Redl90893182008-12-11 22:33:27 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000212}
213
Mike Stump11289f42009-09-09 15:08:12 +0000214/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000215/// Current token is an Identifier and is not a 'try'. This
Chris Lattner644e1b72007-10-03 22:03:06 +0000216/// routine is necessary to disambiguate @try-statement from,
217/// for example, @encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000218///
Sebastian Redl90893182008-12-11 22:33:27 +0000219Parser::OwningExprResult
220Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000221 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redl90893182008-12-11 22:33:27 +0000222 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000223
Sebastian Redl90893182008-12-11 22:33:27 +0000224 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000225}
226
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000227/// This routine is called when a leading '__extension__' is seen and
228/// consumed. This is necessary because the token gets consumed in the
229/// process of disambiguating between an expression and a declaration.
230Parser::OwningExprResult
231Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000232 OwningExprResult LHS(Actions, true);
233 {
234 // Silence extension warnings in the sub-expression
235 ExtensionRAIIObject O(Diags);
236
237 LHS = ParseCastExpression(false);
238 if (LHS.isInvalid()) return move(LHS);
239 }
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000240
241 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000242 move(LHS));
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000243 if (LHS.isInvalid()) return move(LHS);
244
245 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
246}
247
Chris Lattner0c6c0342006-08-12 18:12:45 +0000248/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
249///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000250Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000251 if (Tok.is(tok::kw_throw))
Sebastian Redld65cea82008-12-11 22:51:44 +0000252 return ParseThrowExpression();
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000253
Sebastian Redl59b5e512008-12-11 21:36:32 +0000254 OwningExprResult LHS(ParseCastExpression(false));
255 if (LHS.isInvalid()) return move(LHS);
256
Sebastian Redl90893182008-12-11 22:33:27 +0000257 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000258}
259
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000260/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
261/// where part of an objc message send has already been parsed. In this case
262/// LBracLoc indicates the location of the '[' of the message send, and either
263/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
264/// message.
265///
266/// Since this handles full assignment-expression's, it handles postfix
267/// expressions and other binary operators for these expressions as well.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000268Parser::OwningExprResult
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000269Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +0000270 SourceLocation NameLoc,
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000271 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000272 ExprArg ReceiverExpr) {
273 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
274 ReceiverName,
275 move(ReceiverExpr)));
276 if (R.isInvalid()) return move(R);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000277 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000278 if (R.isInvalid()) return move(R);
279 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000280}
281
282
Sebastian Redl59b5e512008-12-11 21:36:32 +0000283Parser::OwningExprResult Parser::ParseConstantExpression() {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000284 // C++ [basic.def.odr]p2:
Mike Stump11289f42009-09-09 15:08:12 +0000285 // An expression is potentially evaluated unless it appears where an
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000286 // integral constant expression is required (see 5.19) [...].
Douglas Gregor0b6a6242009-06-22 20:57:11 +0000287 EnterExpressionEvaluationContext Unevaluated(Actions,
288 Action::Unevaluated);
Mike Stump11289f42009-09-09 15:08:12 +0000289
Sebastian Redl59b5e512008-12-11 21:36:32 +0000290 OwningExprResult LHS(ParseCastExpression(false));
291 if (LHS.isInvalid()) return move(LHS);
292
Sebastian Redl90893182008-12-11 22:33:27 +0000293 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner3b561a32006-08-13 00:12:11 +0000294}
295
Chris Lattnercde626a2006-08-12 08:13:25 +0000296/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
297/// LHS and has a precedence of at least MinPrec.
Sebastian Redl90893182008-12-11 22:33:27 +0000298Parser::OwningExprResult
299Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Mike Stump11289f42009-09-09 15:08:12 +0000300 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000301 GreaterThanIsOperator,
302 getLang().CPlusPlus0x);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000303 SourceLocation ColonLoc;
304
Chris Lattnercde626a2006-08-12 08:13:25 +0000305 while (1) {
306 // If this token has a lower precedence than we are allowed to parse (e.g.
307 // because we are called recursively, or because the token is not a binop),
308 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000309 if (NextTokPrec < MinPrec)
Sebastian Redl90893182008-12-11 22:33:27 +0000310 return move(LHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000311
312 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000313 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000314 ConsumeToken();
Sebastian Redl112a97662009-02-07 00:15:38 +0000315
Chris Lattner96c3deb2006-08-12 17:13:08 +0000316 // Special case handling for the ternary operator.
Sebastian Redlc13f2682008-12-09 20:22:58 +0000317 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000318 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000319 if (Tok.isNot(tok::colon)) {
Chris Lattner244b96b2009-12-10 02:02:58 +0000320 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
321 ColonProtectionRAIIObject X(*this);
322
Chris Lattner96c3deb2006-08-12 17:13:08 +0000323 // Handle this production specially:
324 // logical-OR-expression '?' expression ':' conditional-expression
325 // In particular, the RHS of the '?' is 'expression', not
326 // 'logical-OR-expression' as we might expect.
327 TernaryMiddle = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000328 if (TernaryMiddle.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000329 return move(TernaryMiddle);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000330 } else {
331 // Special case handling of "X ? Y : Z" where Y is empty:
332 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000333 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000334 Diag(Tok, diag::ext_gnu_conditional_expr);
335 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000336
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000337 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000338 Diag(Tok, diag::err_expected_colon);
Chris Lattner03c40412008-11-23 23:17:07 +0000339 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redl90893182008-12-11 22:33:27 +0000340 return ExprError();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000341 }
Sebastian Redl90893182008-12-11 22:33:27 +0000342
Chris Lattner96c3deb2006-08-12 17:13:08 +0000343 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000344 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000345 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000346
Chris Lattner96c3deb2006-08-12 17:13:08 +0000347 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000348 // ParseCastExpression works here because all RHS expressions in C have it
349 // as a prefix, at least. However, in C++, an assignment-expression could
350 // be a throw-expression, which is not a valid cast-expression.
351 // Therefore we need some special-casing here.
352 // Also note that the third operand of the conditional operator is
353 // an assignment-expression in C++.
354 OwningExprResult RHS(Actions);
355 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
356 RHS = ParseAssignmentExpression();
357 else
358 RHS = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000359 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000360 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000361
362 // Remember the precedence of this operator and get the precedence of the
363 // operator immediately to the right of the RHS.
364 unsigned ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000365 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
366 getLang().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000367
368 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000369 bool isRightAssoc = ThisPrec == prec::Conditional ||
370 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000371
372 // Get the precedence of the operator to the right of the RHS. If it binds
373 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000374 if (ThisPrec < NextTokPrec ||
375 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000376 // If this is left-associative, only parse things on the RHS that bind
377 // more tightly than the current operator. If it is left-associative, it
378 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
379 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000380 // The function takes ownership of the RHS.
Sebastian Redl90893182008-12-11 22:33:27 +0000381 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000382 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000383 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000384
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000385 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
386 getLang().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000387 }
388 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000389
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000390 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000391 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000392 if (TernaryMiddle.isInvalid()) {
393 // If we're using '>>' as an operator within a template
394 // argument list (in C++98), suggest the addition of
395 // parentheses so that the code remains well-formed in C++0x.
396 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
397 SuggestParentheses(OpToken.getLocation(),
398 diag::warn_cxx0x_right_shift_in_template_arg,
399 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
400 Actions.getExprRange(RHS.get()).getEnd()));
401
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000402 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000403 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor87f95b02009-02-26 21:00:50 +0000404 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000405 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000406 move(LHS), move(TernaryMiddle),
407 move(RHS));
Chris Lattner319079c2007-08-31 05:01:50 +0000408 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000409 }
410}
411
Chris Lattnereaf06592006-08-11 02:02:23 +0000412/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000413/// true, parse a unary-expression. isAddressOfOperand exists because an
414/// id-expression that is the operand of address-of gets special treatment
415/// due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000416///
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000417Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000418 bool isAddressOfOperand,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000419 TypeTy *TypeOfCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000420 bool NotCastExpr;
421 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
422 isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000423 NotCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000424 TypeOfCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000425 if (NotCastExpr)
426 Diag(Tok, diag::err_expected_expression);
427 return move(Res);
428}
429
430/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
431/// true, parse a unary-expression. isAddressOfOperand exists because an
432/// id-expression that is the operand of address-of gets special treatment
433/// due to member pointers. NotCastExpr is set to true if the token is not the
434/// start of a cast-expression, and no diagnostic is emitted in this case.
435///
Chris Lattner4564bc12006-08-10 23:14:52 +0000436/// cast-expression: [C99 6.5.4]
437/// unary-expression
438/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000439///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000440/// unary-expression: [C99 6.5.3]
441/// postfix-expression
442/// '++' unary-expression
443/// '--' unary-expression
444/// unary-operator cast-expression
445/// 'sizeof' unary-expression
446/// 'sizeof' '(' type-name ')'
447/// [GNU] '__alignof' unary-expression
448/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000449/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000450/// [GNU] '&&' identifier
Sebastian Redlbd150f42008-11-21 19:14:01 +0000451/// [C++] new-expression
452/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000453///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000454/// unary-operator: one of
455/// '&' '*' '+' '-' '~' '!'
456/// [GNU] '__extension__' '__real' '__imag'
457///
Chris Lattner52a99e52006-08-10 20:56:00 +0000458/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000459/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000460/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000461/// constant
462/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000463/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl576fd422009-05-10 18:38:11 +0000464/// [C++0x] 'nullptr' [C++0x 2.14.7]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000465/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000466/// '__func__' [C99 6.4.2.2]
467/// [GNU] '__FUNCTION__'
468/// [GNU] '__PRETTY_FUNCTION__'
469/// [GNU] '(' compound-statement ')'
470/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
471/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
472/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
473/// assign-expr ')'
474/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000475/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000476/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000477/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump11289f42009-09-09 15:08:12 +0000478/// [OBJC] '@protocol' '(' identifier ')'
479/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000480/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000481/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
482/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000483/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
484/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
485/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
486/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000487/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
488/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000489/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000490/// [G++] unary-type-trait '(' type-id ')'
491/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff0ac012832008-08-28 19:20:44 +0000492/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000493///
494/// constant: [C99 6.4.4]
495/// integer-constant
496/// floating-constant
497/// enumeration-constant -> identifier
498/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000499///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000500/// id-expression: [C++ 5.1]
501/// unqualified-id
502/// qualified-id [TODO]
503///
504/// unqualified-id: [C++ 5.1]
505/// identifier
506/// operator-function-id
507/// conversion-function-id [TODO]
508/// '~' class-name [TODO]
509/// template-id [TODO]
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000510///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000511/// new-expression: [C++ 5.3.4]
512/// '::'[opt] 'new' new-placement[opt] new-type-id
513/// new-initializer[opt]
514/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
515/// new-initializer[opt]
516///
517/// delete-expression: [C++ 5.3.5]
518/// '::'[opt] 'delete' cast-expression
519/// '::'[opt] 'delete' '[' ']' cast-expression
520///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000521/// [GNU] unary-type-trait:
522/// '__has_nothrow_assign' [TODO]
523/// '__has_nothrow_copy' [TODO]
524/// '__has_nothrow_constructor' [TODO]
525/// '__has_trivial_assign' [TODO]
526/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000527/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000528/// '__has_trivial_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000529/// '__has_virtual_destructor' [TODO]
530/// '__is_abstract' [TODO]
531/// '__is_class'
532/// '__is_empty' [TODO]
533/// '__is_enum'
534/// '__is_pod'
535/// '__is_polymorphic'
536/// '__is_union'
537///
538/// [GNU] binary-type-trait:
539/// '__is_base_of' [TODO]
540///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000541Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000542 bool isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000543 bool &NotCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000544 TypeTy *TypeOfCast) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000545 OwningExprResult Res(Actions);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000546 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000547 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner81b576e2006-08-11 02:13:20 +0000549 // This handles all of cast-expression, unary-expression, postfix-expression,
550 // and primary-expression. We handle them together like this for efficiency
551 // and to simplify handling of an expression starting with a '(' token: which
552 // may be one of a parenthesized expression, cast-expression, compound literal
553 // expression, or statement expression.
554 //
555 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000556 // call ParsePostfixExpressionSuffix to handle the postfix expression
557 // suffixes. Cases that cannot be followed by postfix exprs should
558 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000559 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000560 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000561 // If this expression is limited to being a unary-expression, the parent can
562 // not start a cast expression.
563 ParenParseOption ParenExprType =
564 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000565 TypeTy *CastTy;
566 SourceLocation LParenLoc = Tok.getLocation();
567 SourceLocation RParenLoc;
Chris Lattner3c674cf2009-12-10 02:08:07 +0000568
569 {
570 // The inside of the parens don't need to be a colon protected scope.
571 ColonProtectionRAIIObject X(*this, false);
572
573 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
574 TypeOfCast, CastTy, RParenLoc);
575 if (Res.isInvalid()) return move(Res);
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner81b576e2006-08-11 02:13:20 +0000578 switch (ParenExprType) {
579 case SimpleExpr: break; // Nothing else to do.
580 case CompoundStmt: break; // Nothing else to do.
581 case CompoundLiteral:
582 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
583 // postfix-expression exist, parse them now.
584 break;
585 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000586 // We have parsed the cast-expression and no postfix-expr pieces are
587 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000588 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000589 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000590
Chris Lattner20c6a452006-08-12 17:40:43 +0000591 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000592 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnere550a4e2006-08-24 06:37:51 +0000593 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000594
Chris Lattner52a99e52006-08-10 20:56:00 +0000595 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000596 case tok::numeric_constant:
597 // constant: integer-constant
598 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000599
Steve Naroff83895f72007-09-16 03:34:24 +0000600 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000601 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000602
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000603 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000604 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000605
Bill Wendling4073ed52007-02-13 01:51:42 +0000606 case tok::kw_true:
607 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000608 return ParseCXXBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000609
Sebastian Redl576fd422009-05-10 18:38:11 +0000610 case tok::kw_nullptr:
611 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
612
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000613 case tok::identifier: { // primary-expression: identifier
614 // unqualified-id: identifier
615 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000616 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000617 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner9a8968b2009-01-04 23:23:14 +0000618 if (getLang().CPlusPlus) {
Chris Lattner1f69ebb2009-01-04 23:46:59 +0000619 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
620 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000621 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000622 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000623
Chris Lattner55662902009-10-25 17:04:48 +0000624 // Consume the identifier so that we can see if it is followed by a '(' or
625 // '.'.
626 IdentifierInfo &II = *Tok.getIdentifierInfo();
627 SourceLocation ILoc = ConsumeToken();
628
629 // Support 'Class.property' notation. We don't use
630 // isTokObjCMessageIdentifierReceiver(), since it allows 'super' (which is
631 // inappropriate here).
632 if (getLang().ObjC1 && Tok.is(tok::period) &&
633 Actions.getTypeName(II, ILoc, CurScope)) {
Steve Naroff9527bbf2009-03-09 21:12:44 +0000634 SourceLocation DotLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000635
Steve Naroff9527bbf2009-03-09 21:12:44 +0000636 if (Tok.isNot(tok::identifier)) {
Chris Lattner55662902009-10-25 17:04:48 +0000637 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000638 return ExprError();
639 }
640 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
641 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000642
643 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
644 ILoc, PropertyLoc);
Steve Naroffd5ca2d02009-04-02 18:37:59 +0000645 // These can be followed by postfix-expr pieces.
646 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff9527bbf2009-03-09 21:12:44 +0000647 }
Chris Lattner55662902009-10-25 17:04:48 +0000648
Chris Lattnerac18be92006-11-20 06:49:47 +0000649 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
650 // need to know whether or not this identifier is a function designator or
651 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000652 UnqualifiedId Name;
653 CXXScopeSpec ScopeSpec;
654 Name.setIdentifier(&II, ILoc);
655 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name,
656 Tok.is(tok::l_paren), false);
Chris Lattner17ed4872006-11-20 04:58:19 +0000657 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000658 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerac18be92006-11-20 06:49:47 +0000659 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000660 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000661 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000662 ConsumeToken();
663 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000664 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000665 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
666 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
667 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000668 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000669 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000670 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000671 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000672 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000673 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000674 Res = ParseStringLiteralExpression();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000675 if (Res.isInvalid()) return move(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +0000676 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl59b5e512008-12-11 21:36:32 +0000677 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerf8339772006-08-10 22:01:51 +0000678 case tok::kw___builtin_va_arg:
679 case tok::kw___builtin_offsetof:
680 case tok::kw___builtin_choose_expr:
681 case tok::kw___builtin_types_compatible_p:
Sebastian Redl90893182008-12-11 22:33:27 +0000682 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000683 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000684 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor3be4b122008-11-29 04:51:27 +0000685 break;
Chris Lattner81b576e2006-08-11 02:13:20 +0000686 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000687 case tok::minusminus: { // unary-expression: '--' unary-expression
688 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000689 Res = ParseCastExpression(true);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000690 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000691 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000692 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000693 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000694 case tok::amp: { // unary-expression: '&' cast-expression
695 // Special treatment because of member pointers
696 SourceLocation SavedLoc = ConsumeToken();
697 Res = ParseCastExpression(false, true);
698 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000699 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000700 return move(Res);
701 }
702
Chris Lattner81b576e2006-08-11 02:13:20 +0000703 case tok::star: // unary-expression: '*' cast-expression
704 case tok::plus: // unary-expression: '+' cast-expression
705 case tok::minus: // unary-expression: '-' cast-expression
706 case tok::tilde: // unary-expression: '~' cast-expression
707 case tok::exclaim: // unary-expression: '!' cast-expression
708 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000709 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000710 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000711 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000712 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000713 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000714 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000715 }
716
Chris Lattnerc43926f2008-02-02 20:20:10 +0000717 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
718 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000719 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000720 SourceLocation SavedLoc = ConsumeToken();
721 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000722 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000723 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000724 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000725 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000726 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
727 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000728 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000729 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
730 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000731 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +0000732 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000733 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000734 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000735 if (Tok.isNot(tok::identifier))
736 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000737
Chris Lattnereefa10e2007-05-28 06:56:27 +0000738 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000739 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000740 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000741 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000742 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000743 }
Chris Lattner29375652006-12-04 18:06:35 +0000744 case tok::kw_const_cast:
745 case tok::kw_dynamic_cast:
746 case tok::kw_reinterpret_cast:
747 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000748 Res = ParseCXXCasts();
749 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000750 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc4704762008-11-11 11:37:55 +0000751 case tok::kw_typeid:
752 Res = ParseCXXTypeid();
753 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000754 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000755 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000756 Res = ParseCXXThis();
757 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000758 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000759
760 case tok::kw_char:
761 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000762 case tok::kw_char16_t:
763 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000764 case tok::kw_bool:
765 case tok::kw_short:
766 case tok::kw_int:
767 case tok::kw_long:
768 case tok::kw_signed:
769 case tok::kw_unsigned:
770 case tok::kw_float:
771 case tok::kw_double:
772 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000773 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000774 case tok::kw_typeof:
Chris Lattnera8a3f732009-01-06 05:06:21 +0000775 case tok::annot_typename: {
Chris Lattner8a38aa82009-01-04 22:28:21 +0000776 if (!getLang().CPlusPlus) {
777 Diag(Tok, diag::err_expected_expression);
778 return ExprError();
779 }
Eli Friedman6d692cc2009-06-11 00:33:41 +0000780
781 if (SavedKind == tok::kw_typename) {
782 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
783 if (!TryAnnotateTypeOrScopeToken())
784 return ExprError();
785 }
786
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000787 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
788 //
789 DeclSpec DS;
790 ParseCXXSimpleTypeSpecifier(DS);
791 if (Tok.isNot(tok::l_paren))
Sebastian Redl59b5e512008-12-11 21:36:32 +0000792 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
793 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000794
795 Res = ParseCXXTypeConstructExpression(DS);
796 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000797 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000798 }
799
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000800 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
801 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000802 case tok::annot_template_id: // [C++] template-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000803 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000804 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000805
Chris Lattner122db262009-01-04 22:52:14 +0000806 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000807 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
808 // annotates the token, tail recurse.
809 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000810 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
811
Chris Lattner122db262009-01-04 22:52:14 +0000812 // ::new -> [C++] new-expression
813 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000814 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +0000815 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000816 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +0000817 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000818 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000819
Chris Lattner9a8968b2009-01-04 23:23:14 +0000820 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000821 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000822 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +0000823 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +0000824
Sebastian Redlbd150f42008-11-21 19:14:01 +0000825 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000826 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000827
828 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000829 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000830
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000831 case tok::kw___is_pod: // [GNU] unary-type-trait
832 case tok::kw___is_class:
833 case tok::kw___is_enum:
834 case tok::kw___is_union:
Eli Friedmanc96d4962009-08-15 21:55:26 +0000835 case tok::kw___is_empty:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000836 case tok::kw___is_polymorphic:
Anders Carlsson7cbd8fb2009-03-22 01:52:17 +0000837 case tok::kw___is_abstract:
Sebastian Redl79eba1c2009-12-03 00:13:20 +0000838 case tok::kw___is_literal:
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000839 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000840 case tok::kw___has_trivial_copy:
841 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +0000842 case tok::kw___has_trivial_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000843 return ParseUnaryTypeTrait();
844
Chris Lattner644e1b72007-10-03 22:03:06 +0000845 case tok::at: {
846 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000847 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000848 }
Steve Naroff0ac012832008-08-28 19:20:44 +0000849 case tok::caret:
Chris Lattner9eac9312009-03-27 04:18:06 +0000850 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattner6bf1db12008-12-12 19:20:14 +0000851 case tok::l_square:
852 // These can be followed by postfix-expr pieces.
853 if (getLang().ObjC1)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000854 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000855 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +0000856 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000857 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000858 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +0000859 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000860
Chris Lattner20c6a452006-08-12 17:40:43 +0000861 // unreachable.
862 abort();
863}
864
865/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
866/// is parsed, this method parses any suffixes that apply.
867///
868/// postfix-expression: [C99 6.5.2]
869/// primary-expression
870/// postfix-expression '[' expression ']'
871/// postfix-expression '(' argument-expression-list[opt] ')'
872/// postfix-expression '.' identifier
873/// postfix-expression '->' identifier
874/// postfix-expression '++'
875/// postfix-expression '--'
876/// '(' type-name ')' '{' initializer-list '}'
877/// '(' type-name ')' '{' initializer-list ',' '}'
878///
879/// argument-expression-list: [C99 6.5.2]
880/// argument-expression
881/// argument-expression-list ',' assignment-expression
882///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000883Parser::OwningExprResult
884Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +0000885 // Now that the primary-expression piece of the postfix-expression has been
886 // parsed, see if there are any postfix-expression pieces here.
887 SourceLocation Loc;
888 while (1) {
889 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000890 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000891 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000892 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000893 Loc = ConsumeBracket();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000894 OwningExprResult Idx(ParseExpression());
Sebastian Redl511ed552008-11-25 22:21:31 +0000895
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000896 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000897
898 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl726a0d92009-02-05 15:02:23 +0000899 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
900 move(Idx), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000901 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +0000902 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000903
Chris Lattner89c50c62006-08-11 06:41:18 +0000904 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000905 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000906 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000907 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000908
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000909 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl511ed552008-11-25 22:21:31 +0000910 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000911 CommaLocsTy CommaLocs;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000912
Chris Lattner04132372006-10-16 06:12:55 +0000913 Loc = ConsumeParen();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000914
Douglas Gregorcabea402009-09-22 15:41:20 +0000915 if (Tok.is(tok::code_completion)) {
916 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
917 ConsumeToken();
918 }
919
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000920 if (Tok.isNot(tok::r_paren)) {
Douglas Gregorcabea402009-09-22 15:41:20 +0000921 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
922 LHS.get())) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000923 SkipUntil(tok::r_paren);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000924 return ExprError();
Chris Lattner0c6c0342006-08-12 18:12:45 +0000925 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000926 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000927
Chris Lattner89c50c62006-08-11 06:41:18 +0000928 // Match the ')'.
Chris Lattner0d6c0612009-04-13 00:10:38 +0000929 if (Tok.isNot(tok::r_paren)) {
930 MatchRHSPunctuation(tok::r_paren, Loc);
931 return ExprError();
932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner0d6c0612009-04-13 00:10:38 +0000934 if (!LHS.isInvalid()) {
Chris Lattnere165d942006-08-24 04:40:38 +0000935 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
936 "Unexpected number of commas!");
Sebastian Redl726a0d92009-02-05 15:02:23 +0000937 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000938 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redl511ed552008-11-25 22:21:31 +0000939 Tok.getLocation());
Chris Lattnere165d942006-08-24 04:40:38 +0000940 }
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner0d6c0612009-04-13 00:10:38 +0000942 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000943 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000944 }
Douglas Gregor308047d2009-09-09 00:23:06 +0000945 case tok::arrow:
946 case tok::period: {
947 // postfix-expression: p-e '->' template[opt] id-expression
948 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000949 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000950 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000951
Douglas Gregord8061562009-08-06 03:17:00 +0000952 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000953 Action::TypeTy *ObjectType = 0;
Douglas Gregord8061562009-08-06 03:17:00 +0000954 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000955 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
956 OpLoc, OpKind, ObjectType);
Douglas Gregord8061562009-08-06 03:17:00 +0000957 if (LHS.isInvalid())
958 break;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000959 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false);
Douglas Gregord8061562009-08-06 03:17:00 +0000960 }
961
Douglas Gregor2436e712009-09-17 21:32:03 +0000962 if (Tok.is(tok::code_completion)) {
963 // Code completion for a member access expression.
964 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
965 OpLoc, OpKind == tok::arrow);
966
967 ConsumeToken();
968 }
969
Douglas Gregor30d60cb2009-11-03 19:44:04 +0000970 UnqualifiedId Name;
971 if (ParseUnqualifiedId(SS,
972 /*EnteringContext=*/false,
973 /*AllowDestructorName=*/true,
974 /*AllowConstructorName=*/false,
975 ObjectType,
976 Name))
977 return ExprError();
978
979 if (!LHS.isInvalid())
980 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc, OpKind,
981 SS, Name, ObjCImpDecl,
982 Tok.is(tok::l_paren));
983
Chris Lattner89c50c62006-08-11 06:41:18 +0000984 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000985 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000986 case tok::plusplus: // postfix-expression: postfix-expression '++'
987 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000988 if (!LHS.isInvalid()) {
Mike Stump11289f42009-09-09 15:08:12 +0000989 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000990 Tok.getKind(), move(LHS));
Sebastian Redl511ed552008-11-25 22:21:31 +0000991 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000992 ConsumeToken();
993 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000994 }
995 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000996}
997
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +0000998/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
999/// we are at the start of an expression or a parenthesized type-id.
1000/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
1001/// (isCastExpr == false) or the type (isCastExpr == true).
1002///
1003/// unary-expression: [C99 6.5.3]
1004/// 'sizeof' unary-expression
1005/// 'sizeof' '(' type-name ')'
1006/// [GNU] '__alignof' unary-expression
1007/// [GNU] '__alignof' '(' type-name ')'
1008/// [C++0x] 'alignof' '(' type-id ')'
1009///
1010/// [GNU] typeof-specifier:
1011/// typeof ( expressions )
1012/// typeof ( type-name )
1013/// [GNU/C++] typeof unary-expression
1014///
1015Parser::OwningExprResult
1016Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1017 bool &isCastExpr,
1018 TypeTy *&CastTy,
1019 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001020
1021 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001022 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1023 "Not a typeof/sizeof/alignof expression!");
1024
1025 OwningExprResult Operand(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001026
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001027 // If the operand doesn't start with an '(', it must be an expression.
1028 if (Tok.isNot(tok::l_paren)) {
1029 isCastExpr = false;
1030 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1031 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1032 return ExprError();
1033 }
Mike Stump11289f42009-09-09 15:08:12 +00001034
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001035 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001036 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001037 // operand (Clause 5) [...]
1038 //
1039 // The GNU typeof and alignof extensions also behave as unevaluated
1040 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001041 EnterExpressionEvaluationContext Unevaluated(Actions,
1042 Action::Unevaluated);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001043 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001044 } else {
1045 // If it starts with a '(', we know that it is either a parenthesized
1046 // type-name, or it is a unary-expression that starts with a compound
1047 // literal, or starts with a primary-expression that is a parenthesized
1048 // expression.
1049 ParenParseOption ExprType = CastExpr;
1050 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001051
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001052 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001053 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001054 // operand (Clause 5) [...]
1055 //
1056 // The GNU typeof and alignof extensions also behave as unevaluated
1057 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001058 EnterExpressionEvaluationContext Unevaluated(Actions,
1059 Action::Unevaluated);
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001060 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1061 0/*TypeOfCast*/,
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001062 CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001063 CastRange = SourceRange(LParenLoc, RParenLoc);
1064
1065 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1066 // a type.
1067 if (ExprType == CastExpr) {
1068 isCastExpr = true;
1069 return ExprEmpty();
1070 }
1071
Mike Stump11289f42009-09-09 15:08:12 +00001072 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001073 // unary-expression, but doesn't include any postfix pieces. Parse these
1074 // now if present.
1075 Operand = ParsePostfixExpressionSuffix(move(Operand));
1076 }
1077
1078 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1079 isCastExpr = false;
1080 return move(Operand);
1081}
1082
Chris Lattner20c6a452006-08-12 17:40:43 +00001083
Chris Lattner81b576e2006-08-11 02:13:20 +00001084/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1085/// unary-expression: [C99 6.5.3]
1086/// 'sizeof' unary-expression
1087/// 'sizeof' '(' type-name ')'
1088/// [GNU] '__alignof' unary-expression
1089/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001090/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +00001091Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001092 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1093 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +00001094 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001095 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001096 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001097
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001098 bool isCastExpr;
1099 TypeTy *CastTy;
1100 SourceRange CastRange;
1101 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1102 isCastExpr,
1103 CastTy,
1104 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001105
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001106 if (isCastExpr)
1107 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1108 OpTok.is(tok::kw_sizeof),
1109 /*isType=*/true, CastTy,
1110 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001111
Chris Lattner26115ac2006-08-24 06:10:04 +00001112 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001113 if (!Operand.isInvalid())
Sebastian Redl6f282892008-11-11 17:56:53 +00001114 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1115 OpTok.is(tok::kw_sizeof),
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001116 /*isType=*/false,
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001117 Operand.release(), CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001118 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001119}
1120
Chris Lattner11124352006-08-12 19:16:08 +00001121/// ParseBuiltinPrimaryExpression
1122///
1123/// primary-expression: [C99 6.5.1]
1124/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1125/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1126/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1127/// assign-expr ')'
1128/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001129///
Chris Lattner11124352006-08-12 19:16:08 +00001130/// [GNU] offsetof-member-designator:
1131/// [GNU] identifier
1132/// [GNU] offsetof-member-designator '.' identifier
1133/// [GNU] offsetof-member-designator '[' expression ']'
1134///
Sebastian Redl90893182008-12-11 22:33:27 +00001135Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001136 OwningExprResult Res(Actions);
Chris Lattner11124352006-08-12 19:16:08 +00001137 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1138
1139 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001140 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001141
1142 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001143 if (Tok.isNot(tok::l_paren))
1144 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1145 << BuiltinII);
1146
Chris Lattner04132372006-10-16 06:12:55 +00001147 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001148 // TODO: Build AST.
1149
Chris Lattner11124352006-08-12 19:16:08 +00001150 switch (T) {
1151 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001152 case tok::kw___builtin_va_arg: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001153 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001154 if (Expr.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001155 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001156 return ExprError();
Chris Lattner11124352006-08-12 19:16:08 +00001157 }
Chris Lattner0be454e2006-08-12 19:30:51 +00001158
Chris Lattner6d7e6342006-08-15 03:41:14 +00001159 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001160 return ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001161
Douglas Gregor220cac52009-02-18 17:45:20 +00001162 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001163
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001164 if (Tok.isNot(tok::r_paren)) {
1165 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001166 return ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001167 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001168 if (Ty.isInvalid())
1169 Res = ExprError();
1170 else
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001171 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001172 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001173 }
Chris Lattner687d6092007-08-30 15:51:11 +00001174 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001175 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001176 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001177 if (Ty.isInvalid()) {
1178 SkipUntil(tok::r_paren);
1179 return ExprError();
1180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Chris Lattner6d7e6342006-08-15 03:41:14 +00001182 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001183 return ExprError();
1184
Chris Lattner11124352006-08-12 19:16:08 +00001185 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001186 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001187 Diag(Tok, diag::err_expected_ident);
1188 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001189 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001190 }
Sebastian Redl90893182008-12-11 22:33:27 +00001191
Chris Lattner687d6092007-08-30 15:51:11 +00001192 // Keep track of the various subcomponents we see.
1193 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001194
Chris Lattner687d6092007-08-30 15:51:11 +00001195 Comps.push_back(Action::OffsetOfComponent());
1196 Comps.back().isBrackets = false;
1197 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1198 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001199
Sebastian Redl511ed552008-11-25 22:21:31 +00001200 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001201 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001202 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001203 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +00001204 Comps.push_back(Action::OffsetOfComponent());
1205 Comps.back().isBrackets = false;
1206 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001207
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001208 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001209 Diag(Tok, diag::err_expected_ident);
1210 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001211 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001212 }
1213 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1214 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001215
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001216 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +00001217 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +00001218 Comps.push_back(Action::OffsetOfComponent());
1219 Comps.back().isBrackets = true;
1220 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +00001221 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001222 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001223 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001224 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001225 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001226 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001227
Chris Lattner687d6092007-08-30 15:51:11 +00001228 Comps.back().LocEnd =
1229 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman5e774b12009-06-27 20:38:33 +00001230 } else {
1231 if (Tok.isNot(tok::r_paren)) {
1232 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor220cac52009-02-18 17:45:20 +00001233 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001234 } else if (Ty.isInvalid()) {
1235 Res = ExprError();
1236 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001237 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1238 Ty.get(), &Comps[0],
Douglas Gregor220cac52009-02-18 17:45:20 +00001239 Comps.size(), ConsumeParen());
Eli Friedman5e774b12009-06-27 20:38:33 +00001240 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001241 break;
Chris Lattner11124352006-08-12 19:16:08 +00001242 }
1243 }
1244 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001245 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001246 case tok::kw___builtin_choose_expr: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001247 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001248 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001249 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001250 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001251 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001252 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001253 return ExprError();
1254
Sebastian Redl59b5e512008-12-11 21:36:32 +00001255 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001256 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001257 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001258 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001259 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001260 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001261 return ExprError();
1262
Sebastian Redl59b5e512008-12-11 21:36:32 +00001263 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001264 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001265 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001266 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001267 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001268 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001269 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001270 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001271 }
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001272 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1273 move(Expr2), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001274 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001275 }
Chris Lattner11124352006-08-12 19:16:08 +00001276 case tok::kw___builtin_types_compatible_p:
Douglas Gregor220cac52009-02-18 17:45:20 +00001277 TypeResult Ty1 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001278
Chris Lattner6d7e6342006-08-15 03:41:14 +00001279 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001280 return ExprError();
1281
Douglas Gregor220cac52009-02-18 17:45:20 +00001282 TypeResult Ty2 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001283
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001284 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +00001285 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001286 return ExprError();
Steve Naroff788d8642007-08-01 23:45:51 +00001287 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001288
1289 if (Ty1.isInvalid() || Ty2.isInvalid())
1290 Res = ExprError();
1291 else
1292 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1293 ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001294 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001295 }
1296
Chris Lattner11124352006-08-12 19:16:08 +00001297 // These can be followed by postfix-expr pieces because they are
1298 // primary-expressions.
Sebastian Redl90893182008-12-11 22:33:27 +00001299 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner11124352006-08-12 19:16:08 +00001300}
1301
Chris Lattner4add4e62006-08-11 01:33:00 +00001302/// ParseParenExpression - This parses the unit that starts with a '(' token,
1303/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001304/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1305/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001306///
1307/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001308/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001309/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1310/// postfix-expression: [C99 6.5.2]
1311/// '(' type-name ')' '{' initializer-list '}'
1312/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001313/// cast-expression: [C99 6.5.4]
1314/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +00001315///
Sebastian Redl90893182008-12-11 22:33:27 +00001316Parser::OwningExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001317Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001318 TypeTy *TypeOfCast, TypeTy *&CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001319 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001320 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor0db4ccd2009-02-09 21:04:56 +00001321 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner04132372006-10-16 06:12:55 +00001322 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001323 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001324 bool isAmbiguousTypeId;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001325 CastTy = 0;
Sebastian Redl90893182008-12-11 22:33:27 +00001326
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001327 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001328 Diag(Tok, diag::ext_gnu_statement_expr);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001329 OwningStmtResult Stmt(ParseCompoundStatement(0, true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001330 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001331
Chris Lattner366727f2007-07-24 16:58:17 +00001332 // If the substmt parsed correctly, build the AST node.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001333 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001334 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001335
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001336 } else if (ExprType >= CompoundLiteral &&
1337 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001338
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001339 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001340
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001341 // In C++, if the type-id is ambiguous we disambiguate based on context.
1342 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1343 // in which case we should treat it as type-id.
1344 // if stopIfCastExpr is false, we need to determine the context past the
1345 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1346 if (isAmbiguousTypeId && !stopIfCastExpr)
1347 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1348 OpenLoc, RParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001349
Douglas Gregor220cac52009-02-18 17:45:20 +00001350 TypeResult Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001351
1352 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001353 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001354 RParenLoc = ConsumeParen();
1355 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001356 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001357
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001358 if (Tok.is(tok::l_brace)) {
Chris Lattner4add4e62006-08-11 01:33:00 +00001359 ExprType = CompoundLiteral;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001360 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnerd8980502008-12-12 06:00:12 +00001361 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001362
Chris Lattnerd8980502008-12-12 06:00:12 +00001363 if (ExprType == CastExpr) {
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001364 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor220cac52009-02-18 17:45:20 +00001365
1366 if (Ty.isInvalid())
1367 return ExprError();
1368
1369 CastTy = Ty.get();
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001370
1371 if (stopIfCastExpr) {
1372 // Note that this doesn't parse the subsequent cast-expression, it just
1373 // returns the parsed type to the callee.
1374 return OwningExprResult(Actions);
1375 }
1376
1377 // Parse the cast-expression that follows it next.
1378 // TODO: For cast expression with CastTy.
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001379 Result = ParseCastExpression(false, false, CastTy);
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001380 if (!Result.isInvalid())
Nate Begeman5ec4b312009-08-10 23:49:36 +00001381 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1382 move(Result));
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001383 return move(Result);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001384 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001385
Chris Lattnerd8980502008-12-12 06:00:12 +00001386 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1387 return ExprError();
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001388 } else if (TypeOfCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00001389 // Parse the expression-list.
1390 ExprVector ArgExprs(Actions);
1391 CommaLocsTy CommaLocs;
1392
1393 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1394 ExprType = SimpleExpr;
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001395 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
1396 move_arg(ArgExprs), TypeOfCast);
Nate Begeman5ec4b312009-08-10 23:49:36 +00001397 }
Chris Lattner4add4e62006-08-11 01:33:00 +00001398 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001399 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001400 ExprType = SimpleExpr;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001401 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl726a0d92009-02-05 15:02:23 +00001402 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattnerf8339772006-08-10 22:01:51 +00001403 }
Sebastian Redl90893182008-12-11 22:33:27 +00001404
Chris Lattner4564bc12006-08-10 23:14:52 +00001405 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00001406 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00001407 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00001408 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00001409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
Chris Lattnerd8980502008-12-12 06:00:12 +00001411 if (Tok.is(tok::r_paren))
1412 RParenLoc = ConsumeParen();
1413 else
1414 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001415
1416 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001417}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001418
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001419/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1420/// and we are at the left brace.
1421///
1422/// postfix-expression: [C99 6.5.2]
1423/// '(' type-name ')' '{' initializer-list '}'
1424/// '(' type-name ')' '{' initializer-list ',' '}'
1425///
1426Parser::OwningExprResult
1427Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1428 SourceLocation LParenLoc,
1429 SourceLocation RParenLoc) {
1430 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1431 if (!getLang().C99) // Compound literals don't exist in C90.
1432 Diag(LParenLoc, diag::ext_c99_compound_literal);
1433 OwningExprResult Result = ParseInitializer();
1434 if (!Result.isInvalid() && Ty)
1435 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1436 return move(Result);
1437}
1438
Chris Lattnerd3e98952006-10-06 05:22:26 +00001439/// ParseStringLiteralExpression - This handles the various token types that
1440/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1441/// translation phase #6].
1442///
1443/// primary-expression: [C99 6.5.1]
1444/// string-literal
Sebastian Redld65cea82008-12-11 22:51:44 +00001445Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001446 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00001447
Chris Lattnerd3e98952006-10-06 05:22:26 +00001448 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1449 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001450 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00001451
Chris Lattnerd3e98952006-10-06 05:22:26 +00001452 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001453 StringToks.push_back(Tok);
1454 ConsumeStringToken();
1455 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001456
1457 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlffbcf962009-01-18 18:53:16 +00001458 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001459}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001460
1461/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1462///
1463/// argument-expression-list:
1464/// assignment-expression
1465/// argument-expression-list , assignment-expression
1466///
1467/// [C++] expression-list:
1468/// [C++] assignment-expression
1469/// [C++] expression-list , assignment-expression
1470///
Douglas Gregorcabea402009-09-22 15:41:20 +00001471bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1472 void (Action::*Completer)(Scope *S,
1473 void *Data,
1474 ExprTy **Args,
1475 unsigned NumArgs),
1476 void *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001477 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00001478 if (Tok.is(tok::code_completion)) {
1479 if (Completer)
1480 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1481 ConsumeToken();
1482 }
1483
Sebastian Redl59b5e512008-12-11 21:36:32 +00001484 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001485 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001486 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001487
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001488 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001489
1490 if (Tok.isNot(tok::comma))
1491 return false;
1492 // Move to the next argument, remember where the comma was.
1493 CommaLocs.push_back(ConsumeToken());
1494 }
1495}
Steve Naroff0ac012832008-08-28 19:20:44 +00001496
Mike Stump82f071f2009-02-04 22:31:32 +00001497/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1498///
1499/// [clang] block-id:
1500/// [clang] specifier-qualifier-list block-declarator
1501///
1502void Parser::ParseBlockId() {
1503 // Parse the specifier-qualifier-list piece.
1504 DeclSpec DS;
1505 ParseSpecifierQualifierList(DS);
1506
1507 // Parse the block-declarator.
1508 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1509 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00001510
Mike Stump56ed2ea2009-04-29 21:40:37 +00001511 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1512 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1513 SourceLocation());
1514
Mike Stump88788fe2009-04-29 19:03:13 +00001515 if (Tok.is(tok::kw___attribute)) {
1516 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001517 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001518 DeclaratorInfo.AddAttributes(AttrList, Loc);
1519 }
1520
Mike Stump82f071f2009-02-04 22:31:32 +00001521 // Inform sema that we are starting a block.
1522 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1523}
1524
Steve Naroff0ac012832008-08-28 19:20:44 +00001525/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001526/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001527///
1528/// block-literal:
1529/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00001530/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001531/// [clang] block-args:
1532/// [clang] '(' parameter-list ')'
1533///
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001534Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00001535 assert(Tok.is(tok::caret) && "block literal starts with ^");
1536 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001537
Chris Lattnerf6801202009-03-05 07:32:12 +00001538 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1539 "block literal parsing");
1540
Mike Stump11289f42009-09-09 15:08:12 +00001541 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00001542 // argument decls, decls within the compound expression, etc. This also
1543 // allows determining whether a variable reference inside the block is
1544 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001545 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1546 Scope::BreakScope | Scope::ContinueScope |
1547 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001548
1549 // Inform sema that we are starting a block.
1550 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump11289f42009-09-09 15:08:12 +00001551
Steve Naroff0ac012832008-08-28 19:20:44 +00001552 // Parse the return type if present.
1553 DeclSpec DS;
Mike Stump82f071f2009-02-04 22:31:32 +00001554 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001555 // FIXME: Since the return type isn't actually parsed, it can't be used to
1556 // fill ParamInfo with an initial valid range, so do it manually.
1557 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001558
Steve Naroff0ac012832008-08-28 19:20:44 +00001559 // If this block has arguments, parse them. There is no ambiguity here with
1560 // the expression case, because the expression case requires a parameter list.
1561 if (Tok.is(tok::l_paren)) {
1562 ParseParenDeclarator(ParamInfo);
1563 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001564 // SetIdentifier sets the source range end, but in this case we're past
1565 // that location.
1566 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00001567 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001568 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001569 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00001570 // If there was an error parsing the arguments, they may have
1571 // tried to use ^(x+y) which requires an argument list. Just
1572 // skip the whole block literal.
Chris Lattnerf95894c2009-04-18 20:05:34 +00001573 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001574 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00001575 }
Mike Stump88788fe2009-04-29 19:03:13 +00001576
1577 if (Tok.is(tok::kw___attribute)) {
1578 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001579 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001580 ParamInfo.AddAttributes(AttrList, Loc);
1581 }
1582
Mike Stump82f071f2009-02-04 22:31:32 +00001583 // Inform sema that we are starting a block.
1584 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpd73e4412009-04-14 18:24:37 +00001585 } else if (!Tok.is(tok::l_brace)) {
Mike Stump82f071f2009-02-04 22:31:32 +00001586 ParseBlockId();
Steve Naroff0ac012832008-08-28 19:20:44 +00001587 } else {
1588 // Otherwise, pretend we saw (void).
Mike Stump11289f42009-09-09 15:08:12 +00001589 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00001590 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001591 0, 0, 0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00001592 false, SourceLocation(),
1593 false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00001594 CaretLoc, CaretLoc,
1595 ParamInfo),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001596 CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00001597
1598 if (Tok.is(tok::kw___attribute)) {
1599 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001600 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001601 ParamInfo.AddAttributes(AttrList, Loc);
1602 }
1603
Mike Stump82f071f2009-02-04 22:31:32 +00001604 // Inform sema that we are starting a block.
1605 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001606 }
1607
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001608
Sebastian Redlc13f2682008-12-09 20:22:58 +00001609 OwningExprResult Result(Actions, true);
Chris Lattner9eac9312009-03-27 04:18:06 +00001610 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001611 // Saw something like: ^expr
1612 Diag(Tok, diag::err_expected_expression);
Chris Lattnerf95894c2009-04-18 20:05:34 +00001613 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001614 return ExprError();
1615 }
Mike Stump11289f42009-09-09 15:08:12 +00001616
Chris Lattner9eac9312009-03-27 04:18:06 +00001617 OwningStmtResult Stmt(ParseCompoundStatementBody());
1618 if (!Stmt.isInvalid())
1619 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1620 else
1621 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001622 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00001623}