blob: 54131e0ccc5065ed487d0a1ce52627a6fd94b0e7 [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 Lattner96c3deb2006-08-12 17:13:08 +0000320 // Handle this production specially:
321 // logical-OR-expression '?' expression ':' conditional-expression
322 // In particular, the RHS of the '?' is 'expression', not
323 // 'logical-OR-expression' as we might expect.
324 TernaryMiddle = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000325 if (TernaryMiddle.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000326 return move(TernaryMiddle);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000327 } else {
328 // Special case handling of "X ? Y : Z" where Y is empty:
329 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redlc13f2682008-12-09 20:22:58 +0000330 TernaryMiddle = 0;
Chris Lattner96c3deb2006-08-12 17:13:08 +0000331 Diag(Tok, diag::ext_gnu_conditional_expr);
332 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000333
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000334 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000335 Diag(Tok, diag::err_expected_colon);
Chris Lattner03c40412008-11-23 23:17:07 +0000336 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redl90893182008-12-11 22:33:27 +0000337 return ExprError();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000338 }
Sebastian Redl90893182008-12-11 22:33:27 +0000339
Chris Lattner96c3deb2006-08-12 17:13:08 +0000340 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000341 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000342 }
Fariborz Jahanian9a14b842009-10-23 21:01:39 +0000343
Chris Lattner96c3deb2006-08-12 17:13:08 +0000344 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000345 // ParseCastExpression works here because all RHS expressions in C have it
346 // as a prefix, at least. However, in C++, an assignment-expression could
347 // be a throw-expression, which is not a valid cast-expression.
348 // Therefore we need some special-casing here.
349 // Also note that the third operand of the conditional operator is
350 // an assignment-expression in C++.
351 OwningExprResult RHS(Actions);
352 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
353 RHS = ParseAssignmentExpression();
354 else
355 RHS = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000356 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000357 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000358
359 // Remember the precedence of this operator and get the precedence of the
360 // operator immediately to the right of the RHS.
361 unsigned ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000362 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
363 getLang().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000364
365 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000366 bool isRightAssoc = ThisPrec == prec::Conditional ||
367 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000368
369 // Get the precedence of the operator to the right of the RHS. If it binds
370 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000371 if (ThisPrec < NextTokPrec ||
372 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000373 // If this is left-associative, only parse things on the RHS that bind
374 // more tightly than the current operator. If it is left-associative, it
375 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
376 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000377 // The function takes ownership of the RHS.
Sebastian Redl90893182008-12-11 22:33:27 +0000378 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000379 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000380 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000381
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000382 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
383 getLang().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000384 }
385 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000386
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000387 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000388 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000389 if (TernaryMiddle.isInvalid()) {
390 // If we're using '>>' as an operator within a template
391 // argument list (in C++98), suggest the addition of
392 // parentheses so that the code remains well-formed in C++0x.
393 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
394 SuggestParentheses(OpToken.getLocation(),
395 diag::warn_cxx0x_right_shift_in_template_arg,
396 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
397 Actions.getExprRange(RHS.get()).getEnd()));
398
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000399 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000400 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor87f95b02009-02-26 21:00:50 +0000401 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000402 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000403 move(LHS), move(TernaryMiddle),
404 move(RHS));
Chris Lattner319079c2007-08-31 05:01:50 +0000405 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000406 }
407}
408
Chris Lattnereaf06592006-08-11 02:02:23 +0000409/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000410/// true, parse a unary-expression. isAddressOfOperand exists because an
411/// id-expression that is the operand of address-of gets special treatment
412/// due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000413///
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000414Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000415 bool isAddressOfOperand,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000416 TypeTy *TypeOfCast) {
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000417 bool NotCastExpr;
418 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
419 isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000420 NotCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000421 TypeOfCast);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000422 if (NotCastExpr)
423 Diag(Tok, diag::err_expected_expression);
424 return move(Res);
425}
426
427/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
428/// true, parse a unary-expression. isAddressOfOperand exists because an
429/// id-expression that is the operand of address-of gets special treatment
430/// due to member pointers. NotCastExpr is set to true if the token is not the
431/// start of a cast-expression, and no diagnostic is emitted in this case.
432///
Chris Lattner4564bc12006-08-10 23:14:52 +0000433/// cast-expression: [C99 6.5.4]
434/// unary-expression
435/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000436///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000437/// unary-expression: [C99 6.5.3]
438/// postfix-expression
439/// '++' unary-expression
440/// '--' unary-expression
441/// unary-operator cast-expression
442/// 'sizeof' unary-expression
443/// 'sizeof' '(' type-name ')'
444/// [GNU] '__alignof' unary-expression
445/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000446/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000447/// [GNU] '&&' identifier
Sebastian Redlbd150f42008-11-21 19:14:01 +0000448/// [C++] new-expression
449/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000450///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000451/// unary-operator: one of
452/// '&' '*' '+' '-' '~' '!'
453/// [GNU] '__extension__' '__real' '__imag'
454///
Chris Lattner52a99e52006-08-10 20:56:00 +0000455/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000456/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000457/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000458/// constant
459/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000460/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl576fd422009-05-10 18:38:11 +0000461/// [C++0x] 'nullptr' [C++0x 2.14.7]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000462/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000463/// '__func__' [C99 6.4.2.2]
464/// [GNU] '__FUNCTION__'
465/// [GNU] '__PRETTY_FUNCTION__'
466/// [GNU] '(' compound-statement ')'
467/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
468/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
469/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
470/// assign-expr ')'
471/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000472/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000473/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000474/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump11289f42009-09-09 15:08:12 +0000475/// [OBJC] '@protocol' '(' identifier ')'
476/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000477/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000478/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
479/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000480/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
481/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
482/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
483/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000484/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
485/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000486/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000487/// [G++] unary-type-trait '(' type-id ')'
488/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff0ac012832008-08-28 19:20:44 +0000489/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000490///
491/// constant: [C99 6.4.4]
492/// integer-constant
493/// floating-constant
494/// enumeration-constant -> identifier
495/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000496///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000497/// id-expression: [C++ 5.1]
498/// unqualified-id
499/// qualified-id [TODO]
500///
501/// unqualified-id: [C++ 5.1]
502/// identifier
503/// operator-function-id
504/// conversion-function-id [TODO]
505/// '~' class-name [TODO]
506/// template-id [TODO]
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000507///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000508/// new-expression: [C++ 5.3.4]
509/// '::'[opt] 'new' new-placement[opt] new-type-id
510/// new-initializer[opt]
511/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
512/// new-initializer[opt]
513///
514/// delete-expression: [C++ 5.3.5]
515/// '::'[opt] 'delete' cast-expression
516/// '::'[opt] 'delete' '[' ']' cast-expression
517///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000518/// [GNU] unary-type-trait:
519/// '__has_nothrow_assign' [TODO]
520/// '__has_nothrow_copy' [TODO]
521/// '__has_nothrow_constructor' [TODO]
522/// '__has_trivial_assign' [TODO]
523/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000524/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000525/// '__has_trivial_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000526/// '__has_virtual_destructor' [TODO]
527/// '__is_abstract' [TODO]
528/// '__is_class'
529/// '__is_empty' [TODO]
530/// '__is_enum'
531/// '__is_pod'
532/// '__is_polymorphic'
533/// '__is_union'
534///
535/// [GNU] binary-type-trait:
536/// '__is_base_of' [TODO]
537///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000538Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000539 bool isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000540 bool &NotCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000541 TypeTy *TypeOfCast) {
Sebastian Redlc13f2682008-12-09 20:22:58 +0000542 OwningExprResult Res(Actions);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000543 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000544 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner81b576e2006-08-11 02:13:20 +0000546 // This handles all of cast-expression, unary-expression, postfix-expression,
547 // and primary-expression. We handle them together like this for efficiency
548 // and to simplify handling of an expression starting with a '(' token: which
549 // may be one of a parenthesized expression, cast-expression, compound literal
550 // expression, or statement expression.
551 //
552 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000553 // call ParsePostfixExpressionSuffix to handle the postfix expression
554 // suffixes. Cases that cannot be followed by postfix exprs should
555 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000556 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000557 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000558 // If this expression is limited to being a unary-expression, the parent can
559 // not start a cast expression.
560 ParenParseOption ParenExprType =
561 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000562 TypeTy *CastTy;
563 SourceLocation LParenLoc = Tok.getLocation();
564 SourceLocation RParenLoc;
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000565 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Fariborz Jahanian906d8712009-11-25 01:26:41 +0000566 TypeOfCast, CastTy, RParenLoc);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000567 if (Res.isInvalid()) return move(Res);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner81b576e2006-08-11 02:13:20 +0000569 switch (ParenExprType) {
570 case SimpleExpr: break; // Nothing else to do.
571 case CompoundStmt: break; // Nothing else to do.
572 case CompoundLiteral:
573 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
574 // postfix-expression exist, parse them now.
575 break;
576 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000577 // We have parsed the cast-expression and no postfix-expr pieces are
578 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000579 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000580 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000581
Chris Lattner20c6a452006-08-12 17:40:43 +0000582 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000583 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnere550a4e2006-08-24 06:37:51 +0000584 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000585
Chris Lattner52a99e52006-08-10 20:56:00 +0000586 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000587 case tok::numeric_constant:
588 // constant: integer-constant
589 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000590
Steve Naroff83895f72007-09-16 03:34:24 +0000591 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000592 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000593
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000594 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000595 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000596
Bill Wendling4073ed52007-02-13 01:51:42 +0000597 case tok::kw_true:
598 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000599 return ParseCXXBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000600
Sebastian Redl576fd422009-05-10 18:38:11 +0000601 case tok::kw_nullptr:
602 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
603
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000604 case tok::identifier: { // primary-expression: identifier
605 // unqualified-id: identifier
606 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000607 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000608 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner9a8968b2009-01-04 23:23:14 +0000609 if (getLang().CPlusPlus) {
Chris Lattner1f69ebb2009-01-04 23:46:59 +0000610 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
611 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000612 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000613 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000614
Chris Lattner55662902009-10-25 17:04:48 +0000615 // Consume the identifier so that we can see if it is followed by a '(' or
616 // '.'.
617 IdentifierInfo &II = *Tok.getIdentifierInfo();
618 SourceLocation ILoc = ConsumeToken();
619
620 // Support 'Class.property' notation. We don't use
621 // isTokObjCMessageIdentifierReceiver(), since it allows 'super' (which is
622 // inappropriate here).
623 if (getLang().ObjC1 && Tok.is(tok::period) &&
624 Actions.getTypeName(II, ILoc, CurScope)) {
Steve Naroff9527bbf2009-03-09 21:12:44 +0000625 SourceLocation DotLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000626
Steve Naroff9527bbf2009-03-09 21:12:44 +0000627 if (Tok.isNot(tok::identifier)) {
Chris Lattner55662902009-10-25 17:04:48 +0000628 Diag(Tok, diag::err_expected_property_name);
Steve Naroff9527bbf2009-03-09 21:12:44 +0000629 return ExprError();
630 }
631 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
632 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattner55662902009-10-25 17:04:48 +0000633
634 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
635 ILoc, PropertyLoc);
Steve Naroffd5ca2d02009-04-02 18:37:59 +0000636 // These can be followed by postfix-expr pieces.
637 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff9527bbf2009-03-09 21:12:44 +0000638 }
Chris Lattner55662902009-10-25 17:04:48 +0000639
Chris Lattnerac18be92006-11-20 06:49:47 +0000640 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
641 // need to know whether or not this identifier is a function designator or
642 // not.
Douglas Gregora121b752009-11-03 16:56:39 +0000643 UnqualifiedId Name;
644 CXXScopeSpec ScopeSpec;
645 Name.setIdentifier(&II, ILoc);
646 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name,
647 Tok.is(tok::l_paren), false);
Chris Lattner17ed4872006-11-20 04:58:19 +0000648 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000649 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerac18be92006-11-20 06:49:47 +0000650 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000651 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000652 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000653 ConsumeToken();
654 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000655 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000656 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
657 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
658 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000659 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000660 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000661 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000662 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000663 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000664 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000665 Res = ParseStringLiteralExpression();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000666 if (Res.isInvalid()) return move(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +0000667 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl59b5e512008-12-11 21:36:32 +0000668 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerf8339772006-08-10 22:01:51 +0000669 case tok::kw___builtin_va_arg:
670 case tok::kw___builtin_offsetof:
671 case tok::kw___builtin_choose_expr:
672 case tok::kw___builtin_types_compatible_p:
Sebastian Redl90893182008-12-11 22:33:27 +0000673 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000674 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000675 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor3be4b122008-11-29 04:51:27 +0000676 break;
Chris Lattner81b576e2006-08-11 02:13:20 +0000677 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000678 case tok::minusminus: { // unary-expression: '--' unary-expression
679 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000680 Res = ParseCastExpression(true);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000681 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000682 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000683 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000684 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000685 case tok::amp: { // unary-expression: '&' cast-expression
686 // Special treatment because of member pointers
687 SourceLocation SavedLoc = ConsumeToken();
688 Res = ParseCastExpression(false, true);
689 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000690 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000691 return move(Res);
692 }
693
Chris Lattner81b576e2006-08-11 02:13:20 +0000694 case tok::star: // unary-expression: '*' cast-expression
695 case tok::plus: // unary-expression: '+' cast-expression
696 case tok::minus: // unary-expression: '-' cast-expression
697 case tok::tilde: // unary-expression: '~' cast-expression
698 case tok::exclaim: // unary-expression: '!' cast-expression
699 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000700 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000701 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000702 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000704 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000705 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000706 }
707
Chris Lattnerc43926f2008-02-02 20:20:10 +0000708 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
709 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000710 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000711 SourceLocation SavedLoc = ConsumeToken();
712 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000713 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000714 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000715 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000716 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000717 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
718 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000719 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000720 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
721 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000722 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +0000723 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000724 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000725 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000726 if (Tok.isNot(tok::identifier))
727 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000728
Chris Lattnereefa10e2007-05-28 06:56:27 +0000729 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000730 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000731 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000732 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000733 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000734 }
Chris Lattner29375652006-12-04 18:06:35 +0000735 case tok::kw_const_cast:
736 case tok::kw_dynamic_cast:
737 case tok::kw_reinterpret_cast:
738 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000739 Res = ParseCXXCasts();
740 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000741 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc4704762008-11-11 11:37:55 +0000742 case tok::kw_typeid:
743 Res = ParseCXXTypeid();
744 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000745 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000746 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000747 Res = ParseCXXThis();
748 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000749 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000750
751 case tok::kw_char:
752 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000753 case tok::kw_char16_t:
754 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000755 case tok::kw_bool:
756 case tok::kw_short:
757 case tok::kw_int:
758 case tok::kw_long:
759 case tok::kw_signed:
760 case tok::kw_unsigned:
761 case tok::kw_float:
762 case tok::kw_double:
763 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000764 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000765 case tok::kw_typeof:
Chris Lattnera8a3f732009-01-06 05:06:21 +0000766 case tok::annot_typename: {
Chris Lattner8a38aa82009-01-04 22:28:21 +0000767 if (!getLang().CPlusPlus) {
768 Diag(Tok, diag::err_expected_expression);
769 return ExprError();
770 }
Eli Friedman6d692cc2009-06-11 00:33:41 +0000771
772 if (SavedKind == tok::kw_typename) {
773 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
774 if (!TryAnnotateTypeOrScopeToken())
775 return ExprError();
776 }
777
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000778 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
779 //
780 DeclSpec DS;
781 ParseCXXSimpleTypeSpecifier(DS);
782 if (Tok.isNot(tok::l_paren))
Sebastian Redl59b5e512008-12-11 21:36:32 +0000783 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
784 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000785
786 Res = ParseCXXTypeConstructExpression(DS);
787 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000788 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000789 }
790
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000791 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
792 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000793 case tok::annot_template_id: // [C++] template-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000794 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000795 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000796
Chris Lattner122db262009-01-04 22:52:14 +0000797 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000798 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
799 // annotates the token, tail recurse.
800 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000801 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
802
Chris Lattner122db262009-01-04 22:52:14 +0000803 // ::new -> [C++] new-expression
804 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000805 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +0000806 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000807 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +0000808 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000809 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000810
Chris Lattner9a8968b2009-01-04 23:23:14 +0000811 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000812 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000813 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +0000814 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +0000815
Sebastian Redlbd150f42008-11-21 19:14:01 +0000816 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000817 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000818
819 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000820 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000821
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000822 case tok::kw___is_pod: // [GNU] unary-type-trait
823 case tok::kw___is_class:
824 case tok::kw___is_enum:
825 case tok::kw___is_union:
Eli Friedmanc96d4962009-08-15 21:55:26 +0000826 case tok::kw___is_empty:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000827 case tok::kw___is_polymorphic:
Anders Carlsson7cbd8fb2009-03-22 01:52:17 +0000828 case tok::kw___is_abstract:
Sebastian Redl79eba1c2009-12-03 00:13:20 +0000829 case tok::kw___is_literal:
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000830 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000831 case tok::kw___has_trivial_copy:
832 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +0000833 case tok::kw___has_trivial_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000834 return ParseUnaryTypeTrait();
835
Chris Lattner644e1b72007-10-03 22:03:06 +0000836 case tok::at: {
837 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000838 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000839 }
Steve Naroff0ac012832008-08-28 19:20:44 +0000840 case tok::caret:
Chris Lattner9eac9312009-03-27 04:18:06 +0000841 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattner6bf1db12008-12-12 19:20:14 +0000842 case tok::l_square:
843 // These can be followed by postfix-expr pieces.
844 if (getLang().ObjC1)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000845 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000846 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +0000847 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000848 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000849 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +0000850 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000851
Chris Lattner20c6a452006-08-12 17:40:43 +0000852 // unreachable.
853 abort();
854}
855
856/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
857/// is parsed, this method parses any suffixes that apply.
858///
859/// postfix-expression: [C99 6.5.2]
860/// primary-expression
861/// postfix-expression '[' expression ']'
862/// postfix-expression '(' argument-expression-list[opt] ')'
863/// postfix-expression '.' identifier
864/// postfix-expression '->' identifier
865/// postfix-expression '++'
866/// postfix-expression '--'
867/// '(' type-name ')' '{' initializer-list '}'
868/// '(' type-name ')' '{' initializer-list ',' '}'
869///
870/// argument-expression-list: [C99 6.5.2]
871/// argument-expression
872/// argument-expression-list ',' assignment-expression
873///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000874Parser::OwningExprResult
875Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +0000876 // Now that the primary-expression piece of the postfix-expression has been
877 // parsed, see if there are any postfix-expression pieces here.
878 SourceLocation Loc;
879 while (1) {
880 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000881 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000882 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000883 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000884 Loc = ConsumeBracket();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000885 OwningExprResult Idx(ParseExpression());
Sebastian Redl511ed552008-11-25 22:21:31 +0000886
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000887 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000888
889 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl726a0d92009-02-05 15:02:23 +0000890 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
891 move(Idx), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000892 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +0000893 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000894
Chris Lattner89c50c62006-08-11 06:41:18 +0000895 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000896 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000897 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000898 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000899
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000900 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl511ed552008-11-25 22:21:31 +0000901 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000902 CommaLocsTy CommaLocs;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000903
Chris Lattner04132372006-10-16 06:12:55 +0000904 Loc = ConsumeParen();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000905
Douglas Gregorcabea402009-09-22 15:41:20 +0000906 if (Tok.is(tok::code_completion)) {
907 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
908 ConsumeToken();
909 }
910
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000911 if (Tok.isNot(tok::r_paren)) {
Douglas Gregorcabea402009-09-22 15:41:20 +0000912 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
913 LHS.get())) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000914 SkipUntil(tok::r_paren);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000915 return ExprError();
Chris Lattner0c6c0342006-08-12 18:12:45 +0000916 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000917 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000918
Chris Lattner89c50c62006-08-11 06:41:18 +0000919 // Match the ')'.
Chris Lattner0d6c0612009-04-13 00:10:38 +0000920 if (Tok.isNot(tok::r_paren)) {
921 MatchRHSPunctuation(tok::r_paren, Loc);
922 return ExprError();
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner0d6c0612009-04-13 00:10:38 +0000925 if (!LHS.isInvalid()) {
Chris Lattnere165d942006-08-24 04:40:38 +0000926 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
927 "Unexpected number of commas!");
Sebastian Redl726a0d92009-02-05 15:02:23 +0000928 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000929 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redl511ed552008-11-25 22:21:31 +0000930 Tok.getLocation());
Chris Lattnere165d942006-08-24 04:40:38 +0000931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner0d6c0612009-04-13 00:10:38 +0000933 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000934 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000935 }
Douglas Gregor308047d2009-09-09 00:23:06 +0000936 case tok::arrow:
937 case tok::period: {
938 // postfix-expression: p-e '->' template[opt] id-expression
939 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000940 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000941 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000942
Douglas Gregord8061562009-08-06 03:17:00 +0000943 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000944 Action::TypeTy *ObjectType = 0;
Douglas Gregord8061562009-08-06 03:17:00 +0000945 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000946 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
947 OpLoc, OpKind, ObjectType);
Douglas Gregord8061562009-08-06 03:17:00 +0000948 if (LHS.isInvalid())
949 break;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000950 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false);
Douglas Gregord8061562009-08-06 03:17:00 +0000951 }
952
Douglas Gregor2436e712009-09-17 21:32:03 +0000953 if (Tok.is(tok::code_completion)) {
954 // Code completion for a member access expression.
955 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
956 OpLoc, OpKind == tok::arrow);
957
958 ConsumeToken();
959 }
960
Douglas Gregor30d60cb2009-11-03 19:44:04 +0000961 UnqualifiedId Name;
962 if (ParseUnqualifiedId(SS,
963 /*EnteringContext=*/false,
964 /*AllowDestructorName=*/true,
965 /*AllowConstructorName=*/false,
966 ObjectType,
967 Name))
968 return ExprError();
969
970 if (!LHS.isInvalid())
971 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc, OpKind,
972 SS, Name, ObjCImpDecl,
973 Tok.is(tok::l_paren));
974
Chris Lattner89c50c62006-08-11 06:41:18 +0000975 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000976 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000977 case tok::plusplus: // postfix-expression: postfix-expression '++'
978 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000979 if (!LHS.isInvalid()) {
Mike Stump11289f42009-09-09 15:08:12 +0000980 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000981 Tok.getKind(), move(LHS));
Sebastian Redl511ed552008-11-25 22:21:31 +0000982 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000983 ConsumeToken();
984 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000985 }
986 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000987}
988
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +0000989/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
990/// we are at the start of an expression or a parenthesized type-id.
991/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
992/// (isCastExpr == false) or the type (isCastExpr == true).
993///
994/// unary-expression: [C99 6.5.3]
995/// 'sizeof' unary-expression
996/// 'sizeof' '(' type-name ')'
997/// [GNU] '__alignof' unary-expression
998/// [GNU] '__alignof' '(' type-name ')'
999/// [C++0x] 'alignof' '(' type-id ')'
1000///
1001/// [GNU] typeof-specifier:
1002/// typeof ( expressions )
1003/// typeof ( type-name )
1004/// [GNU/C++] typeof unary-expression
1005///
1006Parser::OwningExprResult
1007Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1008 bool &isCastExpr,
1009 TypeTy *&CastTy,
1010 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001011
1012 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001013 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1014 "Not a typeof/sizeof/alignof expression!");
1015
1016 OwningExprResult Operand(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001017
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001018 // If the operand doesn't start with an '(', it must be an expression.
1019 if (Tok.isNot(tok::l_paren)) {
1020 isCastExpr = false;
1021 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1022 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1023 return ExprError();
1024 }
Mike Stump11289f42009-09-09 15:08:12 +00001025
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001026 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001027 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001028 // operand (Clause 5) [...]
1029 //
1030 // The GNU typeof and alignof extensions also behave as unevaluated
1031 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001032 EnterExpressionEvaluationContext Unevaluated(Actions,
1033 Action::Unevaluated);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001034 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001035 } else {
1036 // If it starts with a '(', we know that it is either a parenthesized
1037 // type-name, or it is a unary-expression that starts with a compound
1038 // literal, or starts with a primary-expression that is a parenthesized
1039 // expression.
1040 ParenParseOption ExprType = CastExpr;
1041 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001042
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001043 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001044 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001045 // operand (Clause 5) [...]
1046 //
1047 // The GNU typeof and alignof extensions also behave as unevaluated
1048 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001049 EnterExpressionEvaluationContext Unevaluated(Actions,
1050 Action::Unevaluated);
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001051 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1052 0/*TypeOfCast*/,
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001053 CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001054 CastRange = SourceRange(LParenLoc, RParenLoc);
1055
1056 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1057 // a type.
1058 if (ExprType == CastExpr) {
1059 isCastExpr = true;
1060 return ExprEmpty();
1061 }
1062
Mike Stump11289f42009-09-09 15:08:12 +00001063 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001064 // unary-expression, but doesn't include any postfix pieces. Parse these
1065 // now if present.
1066 Operand = ParsePostfixExpressionSuffix(move(Operand));
1067 }
1068
1069 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1070 isCastExpr = false;
1071 return move(Operand);
1072}
1073
Chris Lattner20c6a452006-08-12 17:40:43 +00001074
Chris Lattner81b576e2006-08-11 02:13:20 +00001075/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1076/// unary-expression: [C99 6.5.3]
1077/// 'sizeof' unary-expression
1078/// 'sizeof' '(' type-name ')'
1079/// [GNU] '__alignof' unary-expression
1080/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001081/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +00001082Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001083 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1084 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +00001085 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001086 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001087 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001088
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001089 bool isCastExpr;
1090 TypeTy *CastTy;
1091 SourceRange CastRange;
1092 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1093 isCastExpr,
1094 CastTy,
1095 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001096
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001097 if (isCastExpr)
1098 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1099 OpTok.is(tok::kw_sizeof),
1100 /*isType=*/true, CastTy,
1101 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001102
Chris Lattner26115ac2006-08-24 06:10:04 +00001103 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001104 if (!Operand.isInvalid())
Sebastian Redl6f282892008-11-11 17:56:53 +00001105 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1106 OpTok.is(tok::kw_sizeof),
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001107 /*isType=*/false,
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001108 Operand.release(), CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001109 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001110}
1111
Chris Lattner11124352006-08-12 19:16:08 +00001112/// ParseBuiltinPrimaryExpression
1113///
1114/// primary-expression: [C99 6.5.1]
1115/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1116/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1117/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1118/// assign-expr ')'
1119/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001120///
Chris Lattner11124352006-08-12 19:16:08 +00001121/// [GNU] offsetof-member-designator:
1122/// [GNU] identifier
1123/// [GNU] offsetof-member-designator '.' identifier
1124/// [GNU] offsetof-member-designator '[' expression ']'
1125///
Sebastian Redl90893182008-12-11 22:33:27 +00001126Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001127 OwningExprResult Res(Actions);
Chris Lattner11124352006-08-12 19:16:08 +00001128 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1129
1130 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001131 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001132
1133 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001134 if (Tok.isNot(tok::l_paren))
1135 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1136 << BuiltinII);
1137
Chris Lattner04132372006-10-16 06:12:55 +00001138 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001139 // TODO: Build AST.
1140
Chris Lattner11124352006-08-12 19:16:08 +00001141 switch (T) {
1142 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001143 case tok::kw___builtin_va_arg: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001144 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001145 if (Expr.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001146 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001147 return ExprError();
Chris Lattner11124352006-08-12 19:16:08 +00001148 }
Chris Lattner0be454e2006-08-12 19:30:51 +00001149
Chris Lattner6d7e6342006-08-15 03:41:14 +00001150 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001151 return ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001152
Douglas Gregor220cac52009-02-18 17:45:20 +00001153 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001154
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001155 if (Tok.isNot(tok::r_paren)) {
1156 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001157 return ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001158 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001159 if (Ty.isInvalid())
1160 Res = ExprError();
1161 else
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001162 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001163 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001164 }
Chris Lattner687d6092007-08-30 15:51:11 +00001165 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001166 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001167 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001168 if (Ty.isInvalid()) {
1169 SkipUntil(tok::r_paren);
1170 return ExprError();
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattner6d7e6342006-08-15 03:41:14 +00001173 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001174 return ExprError();
1175
Chris Lattner11124352006-08-12 19:16:08 +00001176 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001177 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001178 Diag(Tok, diag::err_expected_ident);
1179 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001180 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001181 }
Sebastian Redl90893182008-12-11 22:33:27 +00001182
Chris Lattner687d6092007-08-30 15:51:11 +00001183 // Keep track of the various subcomponents we see.
1184 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001185
Chris Lattner687d6092007-08-30 15:51:11 +00001186 Comps.push_back(Action::OffsetOfComponent());
1187 Comps.back().isBrackets = false;
1188 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1189 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001190
Sebastian Redl511ed552008-11-25 22:21:31 +00001191 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001192 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001193 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001194 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +00001195 Comps.push_back(Action::OffsetOfComponent());
1196 Comps.back().isBrackets = false;
1197 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001198
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001199 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001200 Diag(Tok, diag::err_expected_ident);
1201 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001202 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001203 }
1204 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1205 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001206
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001207 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +00001208 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +00001209 Comps.push_back(Action::OffsetOfComponent());
1210 Comps.back().isBrackets = true;
1211 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +00001212 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001213 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001214 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001215 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001216 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001217 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001218
Chris Lattner687d6092007-08-30 15:51:11 +00001219 Comps.back().LocEnd =
1220 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman5e774b12009-06-27 20:38:33 +00001221 } else {
1222 if (Tok.isNot(tok::r_paren)) {
1223 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor220cac52009-02-18 17:45:20 +00001224 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001225 } else if (Ty.isInvalid()) {
1226 Res = ExprError();
1227 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001228 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1229 Ty.get(), &Comps[0],
Douglas Gregor220cac52009-02-18 17:45:20 +00001230 Comps.size(), ConsumeParen());
Eli Friedman5e774b12009-06-27 20:38:33 +00001231 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001232 break;
Chris Lattner11124352006-08-12 19:16:08 +00001233 }
1234 }
1235 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001236 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001237 case tok::kw___builtin_choose_expr: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001238 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001239 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001240 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001241 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001242 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001243 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001244 return ExprError();
1245
Sebastian Redl59b5e512008-12-11 21:36:32 +00001246 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001247 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001248 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001249 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001250 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001251 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001252 return ExprError();
1253
Sebastian Redl59b5e512008-12-11 21:36:32 +00001254 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001255 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001256 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001257 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001259 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001260 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001261 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001262 }
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001263 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1264 move(Expr2), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001265 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001266 }
Chris Lattner11124352006-08-12 19:16:08 +00001267 case tok::kw___builtin_types_compatible_p:
Douglas Gregor220cac52009-02-18 17:45:20 +00001268 TypeResult Ty1 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001269
Chris Lattner6d7e6342006-08-15 03:41:14 +00001270 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001271 return ExprError();
1272
Douglas Gregor220cac52009-02-18 17:45:20 +00001273 TypeResult Ty2 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001274
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001275 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +00001276 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001277 return ExprError();
Steve Naroff788d8642007-08-01 23:45:51 +00001278 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001279
1280 if (Ty1.isInvalid() || Ty2.isInvalid())
1281 Res = ExprError();
1282 else
1283 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1284 ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001285 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001286 }
1287
Chris Lattner11124352006-08-12 19:16:08 +00001288 // These can be followed by postfix-expr pieces because they are
1289 // primary-expressions.
Sebastian Redl90893182008-12-11 22:33:27 +00001290 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner11124352006-08-12 19:16:08 +00001291}
1292
Chris Lattner4add4e62006-08-11 01:33:00 +00001293/// ParseParenExpression - This parses the unit that starts with a '(' token,
1294/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001295/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1296/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001297///
1298/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001299/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001300/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1301/// postfix-expression: [C99 6.5.2]
1302/// '(' type-name ')' '{' initializer-list '}'
1303/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001304/// cast-expression: [C99 6.5.4]
1305/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +00001306///
Sebastian Redl90893182008-12-11 22:33:27 +00001307Parser::OwningExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001308Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001309 TypeTy *TypeOfCast, TypeTy *&CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001310 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001311 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor0db4ccd2009-02-09 21:04:56 +00001312 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner04132372006-10-16 06:12:55 +00001313 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001314 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001315 bool isAmbiguousTypeId;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001316 CastTy = 0;
Sebastian Redl90893182008-12-11 22:33:27 +00001317
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001318 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001319 Diag(Tok, diag::ext_gnu_statement_expr);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001320 OwningStmtResult Stmt(ParseCompoundStatement(0, true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001321 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001322
Chris Lattner366727f2007-07-24 16:58:17 +00001323 // If the substmt parsed correctly, build the AST node.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001324 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001325 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001326
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001327 } else if (ExprType >= CompoundLiteral &&
1328 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001329
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001330 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001331
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001332 // In C++, if the type-id is ambiguous we disambiguate based on context.
1333 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1334 // in which case we should treat it as type-id.
1335 // if stopIfCastExpr is false, we need to determine the context past the
1336 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1337 if (isAmbiguousTypeId && !stopIfCastExpr)
1338 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1339 OpenLoc, RParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Douglas Gregor220cac52009-02-18 17:45:20 +00001341 TypeResult Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001342
1343 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001344 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001345 RParenLoc = ConsumeParen();
1346 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001347 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001348
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001349 if (Tok.is(tok::l_brace)) {
Chris Lattner4add4e62006-08-11 01:33:00 +00001350 ExprType = CompoundLiteral;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001351 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnerd8980502008-12-12 06:00:12 +00001352 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001353
Chris Lattnerd8980502008-12-12 06:00:12 +00001354 if (ExprType == CastExpr) {
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001355 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor220cac52009-02-18 17:45:20 +00001356
1357 if (Ty.isInvalid())
1358 return ExprError();
1359
1360 CastTy = Ty.get();
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001361
1362 if (stopIfCastExpr) {
1363 // Note that this doesn't parse the subsequent cast-expression, it just
1364 // returns the parsed type to the callee.
1365 return OwningExprResult(Actions);
1366 }
1367
1368 // Parse the cast-expression that follows it next.
1369 // TODO: For cast expression with CastTy.
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001370 Result = ParseCastExpression(false, false, CastTy);
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001371 if (!Result.isInvalid())
Nate Begeman5ec4b312009-08-10 23:49:36 +00001372 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1373 move(Result));
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001374 return move(Result);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001375 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001376
Chris Lattnerd8980502008-12-12 06:00:12 +00001377 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1378 return ExprError();
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001379 } else if (TypeOfCast) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00001380 // Parse the expression-list.
1381 ExprVector ArgExprs(Actions);
1382 CommaLocsTy CommaLocs;
1383
1384 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1385 ExprType = SimpleExpr;
Fariborz Jahanian906d8712009-11-25 01:26:41 +00001386 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
1387 move_arg(ArgExprs), TypeOfCast);
Nate Begeman5ec4b312009-08-10 23:49:36 +00001388 }
Chris Lattner4add4e62006-08-11 01:33:00 +00001389 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001390 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001391 ExprType = SimpleExpr;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001392 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl726a0d92009-02-05 15:02:23 +00001393 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattnerf8339772006-08-10 22:01:51 +00001394 }
Sebastian Redl90893182008-12-11 22:33:27 +00001395
Chris Lattner4564bc12006-08-10 23:14:52 +00001396 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00001397 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00001398 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00001399 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00001400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Chris Lattnerd8980502008-12-12 06:00:12 +00001402 if (Tok.is(tok::r_paren))
1403 RParenLoc = ConsumeParen();
1404 else
1405 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001406
1407 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001408}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001409
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001410/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1411/// and we are at the left brace.
1412///
1413/// postfix-expression: [C99 6.5.2]
1414/// '(' type-name ')' '{' initializer-list '}'
1415/// '(' type-name ')' '{' initializer-list ',' '}'
1416///
1417Parser::OwningExprResult
1418Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1419 SourceLocation LParenLoc,
1420 SourceLocation RParenLoc) {
1421 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1422 if (!getLang().C99) // Compound literals don't exist in C90.
1423 Diag(LParenLoc, diag::ext_c99_compound_literal);
1424 OwningExprResult Result = ParseInitializer();
1425 if (!Result.isInvalid() && Ty)
1426 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1427 return move(Result);
1428}
1429
Chris Lattnerd3e98952006-10-06 05:22:26 +00001430/// ParseStringLiteralExpression - This handles the various token types that
1431/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1432/// translation phase #6].
1433///
1434/// primary-expression: [C99 6.5.1]
1435/// string-literal
Sebastian Redld65cea82008-12-11 22:51:44 +00001436Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001437 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00001438
Chris Lattnerd3e98952006-10-06 05:22:26 +00001439 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1440 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001441 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00001442
Chris Lattnerd3e98952006-10-06 05:22:26 +00001443 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001444 StringToks.push_back(Tok);
1445 ConsumeStringToken();
1446 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001447
1448 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlffbcf962009-01-18 18:53:16 +00001449 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001450}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001451
1452/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1453///
1454/// argument-expression-list:
1455/// assignment-expression
1456/// argument-expression-list , assignment-expression
1457///
1458/// [C++] expression-list:
1459/// [C++] assignment-expression
1460/// [C++] expression-list , assignment-expression
1461///
Douglas Gregorcabea402009-09-22 15:41:20 +00001462bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1463 void (Action::*Completer)(Scope *S,
1464 void *Data,
1465 ExprTy **Args,
1466 unsigned NumArgs),
1467 void *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001468 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00001469 if (Tok.is(tok::code_completion)) {
1470 if (Completer)
1471 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1472 ConsumeToken();
1473 }
1474
Sebastian Redl59b5e512008-12-11 21:36:32 +00001475 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001476 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001477 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001478
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001479 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001480
1481 if (Tok.isNot(tok::comma))
1482 return false;
1483 // Move to the next argument, remember where the comma was.
1484 CommaLocs.push_back(ConsumeToken());
1485 }
1486}
Steve Naroff0ac012832008-08-28 19:20:44 +00001487
Mike Stump82f071f2009-02-04 22:31:32 +00001488/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1489///
1490/// [clang] block-id:
1491/// [clang] specifier-qualifier-list block-declarator
1492///
1493void Parser::ParseBlockId() {
1494 // Parse the specifier-qualifier-list piece.
1495 DeclSpec DS;
1496 ParseSpecifierQualifierList(DS);
1497
1498 // Parse the block-declarator.
1499 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1500 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00001501
Mike Stump56ed2ea2009-04-29 21:40:37 +00001502 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1503 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1504 SourceLocation());
1505
Mike Stump88788fe2009-04-29 19:03:13 +00001506 if (Tok.is(tok::kw___attribute)) {
1507 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001508 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001509 DeclaratorInfo.AddAttributes(AttrList, Loc);
1510 }
1511
Mike Stump82f071f2009-02-04 22:31:32 +00001512 // Inform sema that we are starting a block.
1513 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1514}
1515
Steve Naroff0ac012832008-08-28 19:20:44 +00001516/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001517/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001518///
1519/// block-literal:
1520/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00001521/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001522/// [clang] block-args:
1523/// [clang] '(' parameter-list ')'
1524///
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001525Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00001526 assert(Tok.is(tok::caret) && "block literal starts with ^");
1527 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001528
Chris Lattnerf6801202009-03-05 07:32:12 +00001529 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1530 "block literal parsing");
1531
Mike Stump11289f42009-09-09 15:08:12 +00001532 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00001533 // argument decls, decls within the compound expression, etc. This also
1534 // allows determining whether a variable reference inside the block is
1535 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001536 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1537 Scope::BreakScope | Scope::ContinueScope |
1538 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001539
1540 // Inform sema that we are starting a block.
1541 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump11289f42009-09-09 15:08:12 +00001542
Steve Naroff0ac012832008-08-28 19:20:44 +00001543 // Parse the return type if present.
1544 DeclSpec DS;
Mike Stump82f071f2009-02-04 22:31:32 +00001545 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001546 // FIXME: Since the return type isn't actually parsed, it can't be used to
1547 // fill ParamInfo with an initial valid range, so do it manually.
1548 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001549
Steve Naroff0ac012832008-08-28 19:20:44 +00001550 // If this block has arguments, parse them. There is no ambiguity here with
1551 // the expression case, because the expression case requires a parameter list.
1552 if (Tok.is(tok::l_paren)) {
1553 ParseParenDeclarator(ParamInfo);
1554 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001555 // SetIdentifier sets the source range end, but in this case we're past
1556 // that location.
1557 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00001558 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001559 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001560 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00001561 // If there was an error parsing the arguments, they may have
1562 // tried to use ^(x+y) which requires an argument list. Just
1563 // skip the whole block literal.
Chris Lattnerf95894c2009-04-18 20:05:34 +00001564 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001565 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00001566 }
Mike Stump88788fe2009-04-29 19:03:13 +00001567
1568 if (Tok.is(tok::kw___attribute)) {
1569 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001570 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001571 ParamInfo.AddAttributes(AttrList, Loc);
1572 }
1573
Mike Stump82f071f2009-02-04 22:31:32 +00001574 // Inform sema that we are starting a block.
1575 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpd73e4412009-04-14 18:24:37 +00001576 } else if (!Tok.is(tok::l_brace)) {
Mike Stump82f071f2009-02-04 22:31:32 +00001577 ParseBlockId();
Steve Naroff0ac012832008-08-28 19:20:44 +00001578 } else {
1579 // Otherwise, pretend we saw (void).
Mike Stump11289f42009-09-09 15:08:12 +00001580 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00001581 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001582 0, 0, 0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00001583 false, SourceLocation(),
1584 false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00001585 CaretLoc, CaretLoc,
1586 ParamInfo),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001587 CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00001588
1589 if (Tok.is(tok::kw___attribute)) {
1590 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001591 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump88788fe2009-04-29 19:03:13 +00001592 ParamInfo.AddAttributes(AttrList, Loc);
1593 }
1594
Mike Stump82f071f2009-02-04 22:31:32 +00001595 // Inform sema that we are starting a block.
1596 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001597 }
1598
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001599
Sebastian Redlc13f2682008-12-09 20:22:58 +00001600 OwningExprResult Result(Actions, true);
Chris Lattner9eac9312009-03-27 04:18:06 +00001601 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001602 // Saw something like: ^expr
1603 Diag(Tok, diag::err_expected_expression);
Chris Lattnerf95894c2009-04-18 20:05:34 +00001604 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001605 return ExprError();
1606 }
Mike Stump11289f42009-09-09 15:08:12 +00001607
Chris Lattner9eac9312009-03-27 04:18:06 +00001608 OwningStmtResult Stmt(ParseCompoundStatementBody());
1609 if (!Stmt.isInvalid())
1610 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1611 else
1612 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001613 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00001614}