blob: 7d056fdebb9165cb4bb59b0e93b1ddee1d76e292 [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 Lattnerf02ef3e2008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.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
344 if ((OpToken.is(tok::periodstar) || OpToken.is(tok::arrowstar))
345 && Tok.is(tok::identifier)) {
346 CXXScopeSpec SS;
347 if (Actions.getTypeName(*Tok.getIdentifierInfo(),
348 Tok.getLocation(), CurScope, &SS)) {
349 const char *Opc = OpToken.is(tok::periodstar) ? "'.*'" : "'->*'";
350 Diag(OpToken, diag::err_pointer_to_member_type) << Opc;
351 return ExprError();
352 }
353
354 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000355 // Parse another leaf here for the RHS of the operator.
Sebastian Redl1a99f442009-04-16 17:51:27 +0000356 // ParseCastExpression works here because all RHS expressions in C have it
357 // as a prefix, at least. However, in C++, an assignment-expression could
358 // be a throw-expression, which is not a valid cast-expression.
359 // Therefore we need some special-casing here.
360 // Also note that the third operand of the conditional operator is
361 // an assignment-expression in C++.
362 OwningExprResult RHS(Actions);
363 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
364 RHS = ParseAssignmentExpression();
365 else
366 RHS = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000367 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000368 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000369
370 // Remember the precedence of this operator and get the precedence of the
371 // operator immediately to the right of the RHS.
372 unsigned ThisPrec = NextTokPrec;
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000373 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
374 getLang().CPlusPlus0x);
Chris Lattner89d53752006-08-12 17:18:19 +0000375
376 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000377 bool isRightAssoc = ThisPrec == prec::Conditional ||
378 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000379
380 // Get the precedence of the operator to the right of the RHS. If it binds
381 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000382 if (ThisPrec < NextTokPrec ||
383 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000384 // If this is left-associative, only parse things on the RHS that bind
385 // more tightly than the current operator. If it is left-associative, it
386 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
387 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000388 // The function takes ownership of the RHS.
Sebastian Redl90893182008-12-11 22:33:27 +0000389 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000390 if (RHS.isInvalid())
Sebastian Redl90893182008-12-11 22:33:27 +0000391 return move(RHS);
Chris Lattnercde626a2006-08-12 08:13:25 +0000392
Douglas Gregorcbb45d02009-02-25 23:02:36 +0000393 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
394 getLang().CPlusPlus0x);
Chris Lattnercde626a2006-08-12 08:13:25 +0000395 }
396 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000397
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000398 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000399 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor87f95b02009-02-26 21:00:50 +0000400 if (TernaryMiddle.isInvalid()) {
401 // If we're using '>>' as an operator within a template
402 // argument list (in C++98), suggest the addition of
403 // parentheses so that the code remains well-formed in C++0x.
404 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
405 SuggestParentheses(OpToken.getLocation(),
406 diag::warn_cxx0x_right_shift_in_template_arg,
407 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
408 Actions.getExprRange(RHS.get()).getEnd()));
409
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000410 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +0000411 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor87f95b02009-02-26 21:00:50 +0000412 } else
Steve Naroff83895f72007-09-16 03:34:24 +0000413 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +0000414 move(LHS), move(TernaryMiddle),
415 move(RHS));
Chris Lattner319079c2007-08-31 05:01:50 +0000416 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000417 }
418}
419
Chris Lattnereaf06592006-08-11 02:02:23 +0000420/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000421/// true, parse a unary-expression. isAddressOfOperand exists because an
422/// id-expression that is the operand of address-of gets special treatment
423/// due to member pointers.
Chris Lattnereaf06592006-08-11 02:02:23 +0000424///
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000425Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000426 bool isAddressOfOperand,
427 bool parseParenAsExprList){
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000428 bool NotCastExpr;
429 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
430 isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000431 NotCastExpr,
432 parseParenAsExprList);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000433 if (NotCastExpr)
434 Diag(Tok, diag::err_expected_expression);
435 return move(Res);
436}
437
438/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
439/// true, parse a unary-expression. isAddressOfOperand exists because an
440/// id-expression that is the operand of address-of gets special treatment
441/// due to member pointers. NotCastExpr is set to true if the token is not the
442/// start of a cast-expression, and no diagnostic is emitted in this case.
443///
Chris Lattner4564bc12006-08-10 23:14:52 +0000444/// cast-expression: [C99 6.5.4]
445/// unary-expression
446/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000447///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000448/// unary-expression: [C99 6.5.3]
449/// postfix-expression
450/// '++' unary-expression
451/// '--' unary-expression
452/// unary-operator cast-expression
453/// 'sizeof' unary-expression
454/// 'sizeof' '(' type-name ')'
455/// [GNU] '__alignof' unary-expression
456/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000457/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000458/// [GNU] '&&' identifier
Sebastian Redlbd150f42008-11-21 19:14:01 +0000459/// [C++] new-expression
460/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000461///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000462/// unary-operator: one of
463/// '&' '*' '+' '-' '~' '!'
464/// [GNU] '__extension__' '__real' '__imag'
465///
Chris Lattner52a99e52006-08-10 20:56:00 +0000466/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000467/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000468/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000469/// constant
470/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000471/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl576fd422009-05-10 18:38:11 +0000472/// [C++0x] 'nullptr' [C++0x 2.14.7]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000473/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000474/// '__func__' [C99 6.4.2.2]
475/// [GNU] '__FUNCTION__'
476/// [GNU] '__PRETTY_FUNCTION__'
477/// [GNU] '(' compound-statement ')'
478/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
479/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
480/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
481/// assign-expr ')'
482/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000483/// [GNU] '__null'
Mike Stump11289f42009-09-09 15:08:12 +0000484/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000485/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump11289f42009-09-09 15:08:12 +0000486/// [OBJC] '@protocol' '(' identifier ')'
487/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000488/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000489/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
490/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000491/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
492/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
493/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
494/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000495/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
496/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000497/// [C++] 'this' [C++ 9.3.2]
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000498/// [G++] unary-type-trait '(' type-id ')'
499/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff0ac012832008-08-28 19:20:44 +0000500/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000501///
502/// constant: [C99 6.4.4]
503/// integer-constant
504/// floating-constant
505/// enumeration-constant -> identifier
506/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000507///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000508/// id-expression: [C++ 5.1]
509/// unqualified-id
510/// qualified-id [TODO]
511///
512/// unqualified-id: [C++ 5.1]
513/// identifier
514/// operator-function-id
515/// conversion-function-id [TODO]
516/// '~' class-name [TODO]
517/// template-id [TODO]
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000518///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000519/// new-expression: [C++ 5.3.4]
520/// '::'[opt] 'new' new-placement[opt] new-type-id
521/// new-initializer[opt]
522/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
523/// new-initializer[opt]
524///
525/// delete-expression: [C++ 5.3.5]
526/// '::'[opt] 'delete' cast-expression
527/// '::'[opt] 'delete' '[' ']' cast-expression
528///
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000529/// [GNU] unary-type-trait:
530/// '__has_nothrow_assign' [TODO]
531/// '__has_nothrow_copy' [TODO]
532/// '__has_nothrow_constructor' [TODO]
533/// '__has_trivial_assign' [TODO]
534/// '__has_trivial_copy' [TODO]
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000535/// '__has_trivial_constructor'
Anders Carlsson6dc35752009-04-17 02:34:54 +0000536/// '__has_trivial_destructor'
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000537/// '__has_virtual_destructor' [TODO]
538/// '__is_abstract' [TODO]
539/// '__is_class'
540/// '__is_empty' [TODO]
541/// '__is_enum'
542/// '__is_pod'
543/// '__is_polymorphic'
544/// '__is_union'
545///
546/// [GNU] binary-type-trait:
547/// '__is_base_of' [TODO]
548///
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000549Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000550 bool isAddressOfOperand,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000551 bool &NotCastExpr,
552 bool parseParenAsExprList){
Sebastian Redlc13f2682008-12-09 20:22:58 +0000553 OwningExprResult Res(Actions);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000554 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000555 NotCastExpr = false;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattner81b576e2006-08-11 02:13:20 +0000557 // This handles all of cast-expression, unary-expression, postfix-expression,
558 // and primary-expression. We handle them together like this for efficiency
559 // and to simplify handling of an expression starting with a '(' token: which
560 // may be one of a parenthesized expression, cast-expression, compound literal
561 // expression, or statement expression.
562 //
563 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000564 // call ParsePostfixExpressionSuffix to handle the postfix expression
565 // suffixes. Cases that cannot be followed by postfix exprs should
566 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000567 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000568 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000569 // If this expression is limited to being a unary-expression, the parent can
570 // not start a cast expression.
571 ParenParseOption ParenExprType =
572 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000573 TypeTy *CastTy;
574 SourceLocation LParenLoc = Tok.getLocation();
575 SourceLocation RParenLoc;
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000576 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Nate Begeman5ec4b312009-08-10 23:49:36 +0000577 parseParenAsExprList, CastTy, RParenLoc);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000578 if (Res.isInvalid()) return move(Res);
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner81b576e2006-08-11 02:13:20 +0000580 switch (ParenExprType) {
581 case SimpleExpr: break; // Nothing else to do.
582 case CompoundStmt: break; // Nothing else to do.
583 case CompoundLiteral:
584 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
585 // postfix-expression exist, parse them now.
586 break;
587 case CastExpr:
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +0000588 // We have parsed the cast-expression and no postfix-expr pieces are
589 // following.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000590 return move(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000591 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000592
Chris Lattner20c6a452006-08-12 17:40:43 +0000593 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000594 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnere550a4e2006-08-24 06:37:51 +0000595 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000596
Chris Lattner52a99e52006-08-10 20:56:00 +0000597 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000598 case tok::numeric_constant:
599 // constant: integer-constant
600 // constant: floating-constant
Sebastian Redl59b5e512008-12-11 21:36:32 +0000601
Steve Naroff83895f72007-09-16 03:34:24 +0000602 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000603 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000604
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000605 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000606 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000607
Bill Wendling4073ed52007-02-13 01:51:42 +0000608 case tok::kw_true:
609 case tok::kw_false:
Sebastian Redld65cea82008-12-11 22:51:44 +0000610 return ParseCXXBoolLiteral();
Bill Wendling4073ed52007-02-13 01:51:42 +0000611
Sebastian Redl576fd422009-05-10 18:38:11 +0000612 case tok::kw_nullptr:
613 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
614
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000615 case tok::identifier: { // primary-expression: identifier
616 // unqualified-id: identifier
617 // constant: enumeration-constant
Chris Lattnera8a3f732009-01-06 05:06:21 +0000618 // Turn a potentially qualified name into a annot_typename or
Chris Lattner122db262009-01-04 22:52:14 +0000619 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner9a8968b2009-01-04 23:23:14 +0000620 if (getLang().CPlusPlus) {
Chris Lattner1f69ebb2009-01-04 23:46:59 +0000621 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
622 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000623 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000624 }
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000625
Steve Naroff9527bbf2009-03-09 21:12:44 +0000626 // Support 'Class.property' notation.
Mike Stump11289f42009-09-09 15:08:12 +0000627 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
Steve Naroff9527bbf2009-03-09 21:12:44 +0000628 // 'super' (which is inappropriate here).
Mike Stump11289f42009-09-09 15:08:12 +0000629 if (getLang().ObjC1 &&
630 Actions.getTypeName(*Tok.getIdentifierInfo(),
Steve Naroff9527bbf2009-03-09 21:12:44 +0000631 Tok.getLocation(), CurScope) &&
632 NextToken().is(tok::period)) {
633 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
634 SourceLocation IdentLoc = ConsumeToken();
635 SourceLocation DotLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000636
Steve Naroff9527bbf2009-03-09 21:12:44 +0000637 if (Tok.isNot(tok::identifier)) {
638 Diag(Tok, diag::err_expected_ident);
639 return ExprError();
640 }
641 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
642 SourceLocation PropertyLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000643
Steve Naroff9527bbf2009-03-09 21:12:44 +0000644 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
645 IdentLoc, PropertyLoc);
Steve Naroffd5ca2d02009-04-02 18:37:59 +0000646 // These can be followed by postfix-expr pieces.
647 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff9527bbf2009-03-09 21:12:44 +0000648 }
Chris Lattnerac18be92006-11-20 06:49:47 +0000649 // Consume the identifier so that we can see if it is followed by a '('.
650 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
651 // need to know whether or not this identifier is a function designator or
652 // not.
653 IdentifierInfo &II = *Tok.getIdentifierInfo();
654 SourceLocation L = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000655 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner17ed4872006-11-20 04:58:19 +0000656 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000657 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerac18be92006-11-20 06:49:47 +0000658 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000659 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000660 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000661 ConsumeToken();
662 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000663 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000664 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
665 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
666 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000667 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000668 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000669 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000670 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner52a99e52006-08-10 20:56:00 +0000671 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000672 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000673 Res = ParseStringLiteralExpression();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000674 if (Res.isInvalid()) return move(Res);
Chris Lattner20c6a452006-08-12 17:40:43 +0000675 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl59b5e512008-12-11 21:36:32 +0000676 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattnerf8339772006-08-10 22:01:51 +0000677 case tok::kw___builtin_va_arg:
678 case tok::kw___builtin_offsetof:
679 case tok::kw___builtin_choose_expr:
680 case tok::kw___builtin_types_compatible_p:
Sebastian Redl90893182008-12-11 22:33:27 +0000681 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000682 case tok::kw___null:
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000683 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor3be4b122008-11-29 04:51:27 +0000684 break;
Chris Lattner81b576e2006-08-11 02:13:20 +0000685 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000686 case tok::minusminus: { // unary-expression: '--' unary-expression
687 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000688 Res = ParseCastExpression(true);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000689 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000690 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000691 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000692 }
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000693 case tok::amp: { // unary-expression: '&' cast-expression
694 // Special treatment because of member pointers
695 SourceLocation SavedLoc = ConsumeToken();
696 Res = ParseCastExpression(false, true);
697 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000698 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000699 return move(Res);
700 }
701
Chris Lattner81b576e2006-08-11 02:13:20 +0000702 case tok::star: // unary-expression: '*' cast-expression
703 case tok::plus: // unary-expression: '+' cast-expression
704 case tok::minus: // unary-expression: '-' cast-expression
705 case tok::tilde: // unary-expression: '~' cast-expression
706 case tok::exclaim: // unary-expression: '!' cast-expression
707 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000708 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000709 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000710 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000711 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000712 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000713 return move(Res);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000714 }
715
Chris Lattnerc43926f2008-02-02 20:20:10 +0000716 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
717 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000718 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000719 SourceLocation SavedLoc = ConsumeToken();
720 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000721 if (!Res.isInvalid())
Sebastian Redl726a0d92009-02-05 15:02:23 +0000722 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl59b5e512008-12-11 21:36:32 +0000723 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000724 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000725 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
726 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000727 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000728 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
729 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000730 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +0000731 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000732 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000733 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000734 if (Tok.isNot(tok::identifier))
735 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000736
Chris Lattnereefa10e2007-05-28 06:56:27 +0000737 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000738 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000739 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000740 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000741 return move(Res);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000742 }
Chris Lattner29375652006-12-04 18:06:35 +0000743 case tok::kw_const_cast:
744 case tok::kw_dynamic_cast:
745 case tok::kw_reinterpret_cast:
746 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000747 Res = ParseCXXCasts();
748 // These can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000749 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc4704762008-11-11 11:37:55 +0000750 case tok::kw_typeid:
751 Res = ParseCXXTypeid();
752 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000753 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000754 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000755 Res = ParseCXXThis();
756 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000757 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000758
759 case tok::kw_char:
760 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000761 case tok::kw_char16_t:
762 case tok::kw_char32_t:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000763 case tok::kw_bool:
764 case tok::kw_short:
765 case tok::kw_int:
766 case tok::kw_long:
767 case tok::kw_signed:
768 case tok::kw_unsigned:
769 case tok::kw_float:
770 case tok::kw_double:
771 case tok::kw_void:
Douglas Gregor333489b2009-03-27 23:10:48 +0000772 case tok::kw_typename:
Chris Lattner8a38aa82009-01-04 22:28:21 +0000773 case tok::kw_typeof:
Chris Lattnera8a3f732009-01-06 05:06:21 +0000774 case tok::annot_typename: {
Chris Lattner8a38aa82009-01-04 22:28:21 +0000775 if (!getLang().CPlusPlus) {
776 Diag(Tok, diag::err_expected_expression);
777 return ExprError();
778 }
Eli Friedman6d692cc2009-06-11 00:33:41 +0000779
780 if (SavedKind == tok::kw_typename) {
781 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
782 if (!TryAnnotateTypeOrScopeToken())
783 return ExprError();
784 }
785
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000786 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
787 //
788 DeclSpec DS;
789 ParseCXXSimpleTypeSpecifier(DS);
790 if (Tok.isNot(tok::l_paren))
Sebastian Redl59b5e512008-12-11 21:36:32 +0000791 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
792 << DS.getSourceRange());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000793
794 Res = ParseCXXTypeConstructExpression(DS);
795 // This can be followed by postfix-expr pieces.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000796 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000797 }
798
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000799 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
800 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Douglas Gregora727cb92009-06-30 22:34:41 +0000801 case tok::annot_template_id: // [C++] template-id
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000802 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000803 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000804
Chris Lattner122db262009-01-04 22:52:14 +0000805 case tok::coloncolon: {
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000806 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
807 // annotates the token, tail recurse.
808 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl3d3f75a2009-02-03 20:19:35 +0000809 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
810
Chris Lattner122db262009-01-04 22:52:14 +0000811 // ::new -> [C++] new-expression
812 // ::delete -> [C++] delete-expression
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000813 SourceLocation CCLoc = ConsumeToken();
Chris Lattner109faf22009-01-04 21:25:24 +0000814 if (Tok.is(tok::kw_new))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000815 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner122db262009-01-04 22:52:14 +0000816 if (Tok.is(tok::kw_delete))
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000817 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000818
Chris Lattner9a8968b2009-01-04 23:23:14 +0000819 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner8a7d10d2009-01-05 03:55:46 +0000820 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner9a8968b2009-01-04 23:23:14 +0000821 return ExprError();
Chris Lattner109faf22009-01-04 21:25:24 +0000822 }
Sebastian Redldb36b9b2008-12-02 16:35:44 +0000823
Sebastian Redlbd150f42008-11-21 19:14:01 +0000824 case tok::kw_new: // [C++] new-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000825 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000826
827 case tok::kw_delete: // [C++] delete-expression
Chris Lattner109faf22009-01-04 21:25:24 +0000828 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redlbd150f42008-11-21 19:14:01 +0000829
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000830 case tok::kw___is_pod: // [GNU] unary-type-trait
831 case tok::kw___is_class:
832 case tok::kw___is_enum:
833 case tok::kw___is_union:
Eli Friedmanc96d4962009-08-15 21:55:26 +0000834 case tok::kw___is_empty:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000835 case tok::kw___is_polymorphic:
Anders Carlsson7cbd8fb2009-03-22 01:52:17 +0000836 case tok::kw___is_abstract:
Anders Carlssonfe63dc52009-04-16 00:08:20 +0000837 case tok::kw___has_trivial_constructor:
Douglas Gregor79f83ed2009-07-23 23:49:00 +0000838 case tok::kw___has_trivial_copy:
839 case tok::kw___has_trivial_assign:
Anders Carlsson6dc35752009-04-17 02:34:54 +0000840 case tok::kw___has_trivial_destructor:
Sebastian Redlbaad4e72009-01-05 20:52:13 +0000841 return ParseUnaryTypeTrait();
842
Chris Lattner644e1b72007-10-03 22:03:06 +0000843 case tok::at: {
844 SourceLocation AtLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000845 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000846 }
Steve Naroff0ac012832008-08-28 19:20:44 +0000847 case tok::caret:
Chris Lattner9eac9312009-03-27 04:18:06 +0000848 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattner6bf1db12008-12-12 19:20:14 +0000849 case tok::l_square:
850 // These can be followed by postfix-expr pieces.
851 if (getLang().ObjC1)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +0000852 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000853 // FALL THROUGH.
Chris Lattner52a99e52006-08-10 20:56:00 +0000854 default:
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +0000855 NotCastExpr = true;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000856 return ExprError();
Chris Lattnerf8339772006-08-10 22:01:51 +0000857 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000858
Chris Lattner20c6a452006-08-12 17:40:43 +0000859 // unreachable.
860 abort();
861}
862
863/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
864/// is parsed, this method parses any suffixes that apply.
865///
866/// postfix-expression: [C99 6.5.2]
867/// primary-expression
868/// postfix-expression '[' expression ']'
869/// postfix-expression '(' argument-expression-list[opt] ')'
870/// postfix-expression '.' identifier
871/// postfix-expression '->' identifier
872/// postfix-expression '++'
873/// postfix-expression '--'
874/// '(' type-name ')' '{' initializer-list '}'
875/// '(' type-name ')' '{' initializer-list ',' '}'
876///
877/// argument-expression-list: [C99 6.5.2]
878/// argument-expression
879/// argument-expression-list ',' assignment-expression
880///
Sebastian Redl59b5e512008-12-11 21:36:32 +0000881Parser::OwningExprResult
882Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattnerf8339772006-08-10 22:01:51 +0000883 // Now that the primary-expression piece of the postfix-expression has been
884 // parsed, see if there are any postfix-expression pieces here.
885 SourceLocation Loc;
886 while (1) {
887 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000888 default: // Not a postfix-expression suffix.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000889 return move(LHS);
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000890 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000891 Loc = ConsumeBracket();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000892 OwningExprResult Idx(ParseExpression());
Sebastian Redl511ed552008-11-25 22:21:31 +0000893
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000894 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000895
896 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl726a0d92009-02-05 15:02:23 +0000897 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
898 move(Idx), RLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000899 } else
Sebastian Redl59b5e512008-12-11 21:36:32 +0000900 LHS = ExprError();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000901
Chris Lattner89c50c62006-08-11 06:41:18 +0000902 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000903 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000904 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000905 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000906
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000907 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl511ed552008-11-25 22:21:31 +0000908 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000909 CommaLocsTy CommaLocs;
Sebastian Redl59b5e512008-12-11 21:36:32 +0000910
Chris Lattner04132372006-10-16 06:12:55 +0000911 Loc = ConsumeParen();
Sebastian Redl59b5e512008-12-11 21:36:32 +0000912
Douglas Gregorcabea402009-09-22 15:41:20 +0000913 if (Tok.is(tok::code_completion)) {
914 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
915 ConsumeToken();
916 }
917
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000918 if (Tok.isNot(tok::r_paren)) {
Douglas Gregorcabea402009-09-22 15:41:20 +0000919 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
920 LHS.get())) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000921 SkipUntil(tok::r_paren);
Sebastian Redl59b5e512008-12-11 21:36:32 +0000922 return ExprError();
Chris Lattner0c6c0342006-08-12 18:12:45 +0000923 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000924 }
Sebastian Redl59b5e512008-12-11 21:36:32 +0000925
Chris Lattner89c50c62006-08-11 06:41:18 +0000926 // Match the ')'.
Chris Lattner0d6c0612009-04-13 00:10:38 +0000927 if (Tok.isNot(tok::r_paren)) {
928 MatchRHSPunctuation(tok::r_paren, Loc);
929 return ExprError();
930 }
Mike Stump11289f42009-09-09 15:08:12 +0000931
Chris Lattner0d6c0612009-04-13 00:10:38 +0000932 if (!LHS.isInvalid()) {
Chris Lattnere165d942006-08-24 04:40:38 +0000933 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
934 "Unexpected number of commas!");
Sebastian Redl726a0d92009-02-05 15:02:23 +0000935 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000936 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redl511ed552008-11-25 22:21:31 +0000937 Tok.getLocation());
Chris Lattnere165d942006-08-24 04:40:38 +0000938 }
Mike Stump11289f42009-09-09 15:08:12 +0000939
Chris Lattner0d6c0612009-04-13 00:10:38 +0000940 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000941 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000942 }
Douglas Gregor308047d2009-09-09 00:23:06 +0000943 case tok::arrow:
944 case tok::period: {
945 // postfix-expression: p-e '->' template[opt] id-expression
946 // postfix-expression: p-e '.' template[opt] id-expression
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000947 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000948 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl59b5e512008-12-11 21:36:32 +0000949
Douglas Gregord8061562009-08-06 03:17:00 +0000950 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000951 Action::TypeTy *ObjectType = 0;
Douglas Gregord8061562009-08-06 03:17:00 +0000952 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000953 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
954 OpLoc, OpKind, ObjectType);
Douglas Gregord8061562009-08-06 03:17:00 +0000955 if (LHS.isInvalid())
956 break;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000957 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false);
Douglas Gregord8061562009-08-06 03:17:00 +0000958 }
959
Douglas Gregor2436e712009-09-17 21:32:03 +0000960 if (Tok.is(tok::code_completion)) {
961 // Code completion for a member access expression.
962 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
963 OpLoc, OpKind == tok::arrow);
964
965 ConsumeToken();
966 }
967
Anders Carlsson7e3f0e42009-08-25 23:46:41 +0000968 if (Tok.is(tok::identifier)) {
969 if (!LHS.isInvalid())
970 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
971 OpKind, Tok.getLocation(),
972 *Tok.getIdentifierInfo(),
973 ObjCImpDecl, &SS);
Douglas Gregor522fbc42009-08-31 19:52:13 +0000974 ConsumeToken();
Anders Carlsson7e3f0e42009-08-25 23:46:41 +0000975 } else if (getLang().CPlusPlus && Tok.is(tok::tilde)) {
Douglas Gregorfbc18232009-08-31 21:16:32 +0000976 // We have a C++ pseudo-destructor or a destructor call, e.g., t.~T()
Mike Stump11289f42009-09-09 15:08:12 +0000977
Anders Carlsson7e3f0e42009-08-25 23:46:41 +0000978 // Consume the tilde.
979 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000980
Anders Carlsson7e3f0e42009-08-25 23:46:41 +0000981 if (!Tok.is(tok::identifier)) {
982 Diag(Tok, diag::err_expected_ident);
983 return ExprError();
984 }
Mike Stump11289f42009-09-09 15:08:12 +0000985
Douglas Gregorc59e5612009-10-19 22:04:39 +0000986 if (NextToken().is(tok::less)) {
987 // class-name:
988 // ~ simple-template-id
989 TemplateTy Template
990 = Actions.ActOnDependentTemplateName(SourceLocation(),
991 *Tok.getIdentifierInfo(),
992 Tok.getLocation(),
993 SS,
994 ObjectType);
995 if (AnnotateTemplateIdToken(Template, TNK_Type_template, &SS,
996 SourceLocation(), true))
997 return ExprError();
998
999 assert(Tok.is(tok::annot_typename) &&
1000 "AnnotateTemplateIdToken didn't work?");
1001 if (!LHS.isInvalid())
1002 LHS = Actions.ActOnDestructorReferenceExpr(CurScope, move(LHS),
1003 OpLoc, OpKind,
1004 Tok.getAnnotationRange(),
1005 Tok.getAnnotationValue(),
1006 SS,
1007 NextToken().is(tok::l_paren));
1008 } else {
1009 // class-name:
1010 // ~ identifier
1011 if (!LHS.isInvalid())
1012 LHS = Actions.ActOnDestructorReferenceExpr(CurScope, move(LHS),
1013 OpLoc, OpKind,
1014 Tok.getLocation(),
1015 Tok.getIdentifierInfo(),
1016 SS,
1017 NextToken().is(tok::l_paren));
1018 }
1019
1020 // Consume the identifier or template-id token.
Douglas Gregor522fbc42009-08-31 19:52:13 +00001021 ConsumeToken();
1022 } else if (getLang().CPlusPlus && Tok.is(tok::kw_operator)) {
Douglas Gregorfbc18232009-08-31 21:16:32 +00001023 // We have a reference to a member operator, e.g., t.operator int or
1024 // t.operator+.
Anders Carlsson8523d202009-10-13 21:02:07 +00001025 SourceLocation OperatorLoc = Tok.getLocation();
1026
Douglas Gregor522fbc42009-08-31 19:52:13 +00001027 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1028 if (!LHS.isInvalid())
1029 LHS = Actions.ActOnOverloadedOperatorReferenceExpr(CurScope,
1030 move(LHS), OpLoc,
1031 OpKind,
Anders Carlsson8523d202009-10-13 21:02:07 +00001032 OperatorLoc,
Douglas Gregor522fbc42009-08-31 19:52:13 +00001033 Op, &SS);
1034 // TryParseOperatorFunctionId already consumed our token, so
1035 // don't bother
1036 } else if (TypeTy *ConvType = ParseConversionFunctionId()) {
1037 if (!LHS.isInvalid())
1038 LHS = Actions.ActOnConversionOperatorReferenceExpr(CurScope,
1039 move(LHS), OpLoc,
1040 OpKind,
Anders Carlsson8523d202009-10-13 21:02:07 +00001041 OperatorLoc,
Douglas Gregor522fbc42009-08-31 19:52:13 +00001042 ConvType, &SS);
1043 } else {
Douglas Gregor522fbc42009-08-31 19:52:13 +00001044 // Don't emit a diagnostic; ParseConversionFunctionId does it for us
1045 return ExprError();
1046 }
Douglas Gregorfbc18232009-08-31 21:16:32 +00001047 } else if (getLang().CPlusPlus && Tok.is(tok::annot_template_id)) {
1048 // We have a reference to a member template along with explicitly-
1049 // specified template arguments, e.g., t.f<int>.
Mike Stump11289f42009-09-09 15:08:12 +00001050 TemplateIdAnnotation *TemplateId
Douglas Gregorfbc18232009-08-31 21:16:32 +00001051 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1052 if (!LHS.isInvalid()) {
Mike Stump11289f42009-09-09 15:08:12 +00001053 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregorfbc18232009-08-31 21:16:32 +00001054 TemplateId->getTemplateArgs(),
1055 TemplateId->getTemplateArgIsType(),
1056 TemplateId->NumArgs);
Mike Stump11289f42009-09-09 15:08:12 +00001057
Douglas Gregorfbc18232009-08-31 21:16:32 +00001058 LHS = Actions.ActOnMemberTemplateIdReferenceExpr(CurScope, move(LHS),
1059 OpLoc, OpKind, SS,
1060 TemplateTy::make(TemplateId->Template),
1061 TemplateId->TemplateNameLoc,
1062 TemplateId->LAngleLoc,
1063 TemplateArgsPtr,
1064 TemplateId->getTemplateArgLocations(),
1065 TemplateId->RAngleLoc);
1066 }
1067 ConsumeToken();
Anders Carlsson7e3f0e42009-08-25 23:46:41 +00001068 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001069 Diag(Tok, diag::err_expected_ident);
Sebastian Redl59b5e512008-12-11 21:36:32 +00001070 return ExprError();
Chris Lattner89c50c62006-08-11 06:41:18 +00001071 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001072 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001073 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001074 case tok::plusplus: // postfix-expression: postfix-expression '++'
1075 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001076 if (!LHS.isInvalid()) {
Mike Stump11289f42009-09-09 15:08:12 +00001077 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001078 Tok.getKind(), move(LHS));
Sebastian Redl511ed552008-11-25 22:21:31 +00001079 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001080 ConsumeToken();
1081 break;
Chris Lattnerf8339772006-08-10 22:01:51 +00001082 }
1083 }
Chris Lattner52a99e52006-08-10 20:56:00 +00001084}
1085
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001086/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
1087/// we are at the start of an expression or a parenthesized type-id.
1088/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
1089/// (isCastExpr == false) or the type (isCastExpr == true).
1090///
1091/// unary-expression: [C99 6.5.3]
1092/// 'sizeof' unary-expression
1093/// 'sizeof' '(' type-name ')'
1094/// [GNU] '__alignof' unary-expression
1095/// [GNU] '__alignof' '(' type-name ')'
1096/// [C++0x] 'alignof' '(' type-id ')'
1097///
1098/// [GNU] typeof-specifier:
1099/// typeof ( expressions )
1100/// typeof ( type-name )
1101/// [GNU/C++] typeof unary-expression
1102///
1103Parser::OwningExprResult
1104Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1105 bool &isCastExpr,
1106 TypeTy *&CastTy,
1107 SourceRange &CastRange) {
Mike Stump11289f42009-09-09 15:08:12 +00001108
1109 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001110 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1111 "Not a typeof/sizeof/alignof expression!");
1112
1113 OwningExprResult Operand(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001115 // If the operand doesn't start with an '(', it must be an expression.
1116 if (Tok.isNot(tok::l_paren)) {
1117 isCastExpr = false;
1118 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1119 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1120 return ExprError();
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001123 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001124 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001125 // operand (Clause 5) [...]
1126 //
1127 // The GNU typeof and alignof extensions also behave as unevaluated
1128 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001129 EnterExpressionEvaluationContext Unevaluated(Actions,
1130 Action::Unevaluated);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001131 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001132 } else {
1133 // If it starts with a '(', we know that it is either a parenthesized
1134 // type-name, or it is a unary-expression that starts with a compound
1135 // literal, or starts with a primary-expression that is a parenthesized
1136 // expression.
1137 ParenParseOption ExprType = CastExpr;
1138 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001139
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001140 // C++0x [expr.sizeof]p1:
Mike Stump11289f42009-09-09 15:08:12 +00001141 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00001142 // operand (Clause 5) [...]
1143 //
1144 // The GNU typeof and alignof extensions also behave as unevaluated
1145 // operands.
Douglas Gregor0b6a6242009-06-22 20:57:11 +00001146 EnterExpressionEvaluationContext Unevaluated(Actions,
1147 Action::Unevaluated);
Nate Begeman5ec4b312009-08-10 23:49:36 +00001148 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, false,
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001149 CastTy, RParenLoc);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001150 CastRange = SourceRange(LParenLoc, RParenLoc);
1151
1152 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1153 // a type.
1154 if (ExprType == CastExpr) {
1155 isCastExpr = true;
1156 return ExprEmpty();
1157 }
1158
Mike Stump11289f42009-09-09 15:08:12 +00001159 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001160 // unary-expression, but doesn't include any postfix pieces. Parse these
1161 // now if present.
1162 Operand = ParsePostfixExpressionSuffix(move(Operand));
1163 }
1164
1165 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1166 isCastExpr = false;
1167 return move(Operand);
1168}
1169
Chris Lattner20c6a452006-08-12 17:40:43 +00001170
Chris Lattner81b576e2006-08-11 02:13:20 +00001171/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1172/// unary-expression: [C99 6.5.3]
1173/// 'sizeof' unary-expression
1174/// 'sizeof' '(' type-name ')'
1175/// [GNU] '__alignof' unary-expression
1176/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +00001177/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redl90893182008-12-11 22:33:27 +00001178Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +00001179 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1180 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +00001181 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +00001182 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +00001183 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001184
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001185 bool isCastExpr;
1186 TypeTy *CastTy;
1187 SourceRange CastRange;
1188 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1189 isCastExpr,
1190 CastTy,
1191 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001192
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001193 if (isCastExpr)
1194 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1195 OpTok.is(tok::kw_sizeof),
1196 /*isType=*/true, CastTy,
1197 CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001198
Chris Lattner26115ac2006-08-24 06:10:04 +00001199 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001200 if (!Operand.isInvalid())
Sebastian Redl6f282892008-11-11 17:56:53 +00001201 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1202 OpTok.is(tok::kw_sizeof),
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001203 /*isType=*/false,
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00001204 Operand.release(), CastRange);
Sebastian Redl90893182008-12-11 22:33:27 +00001205 return move(Operand);
Chris Lattner81b576e2006-08-11 02:13:20 +00001206}
1207
Chris Lattner11124352006-08-12 19:16:08 +00001208/// ParseBuiltinPrimaryExpression
1209///
1210/// primary-expression: [C99 6.5.1]
1211/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1212/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1213/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1214/// assign-expr ')'
1215/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump11289f42009-09-09 15:08:12 +00001216///
Chris Lattner11124352006-08-12 19:16:08 +00001217/// [GNU] offsetof-member-designator:
1218/// [GNU] identifier
1219/// [GNU] offsetof-member-designator '.' identifier
1220/// [GNU] offsetof-member-designator '[' expression ']'
1221///
Sebastian Redl90893182008-12-11 22:33:27 +00001222Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001223 OwningExprResult Res(Actions);
Chris Lattner11124352006-08-12 19:16:08 +00001224 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1225
1226 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +00001227 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +00001228
1229 // All of these start with an open paren.
Sebastian Redl90893182008-12-11 22:33:27 +00001230 if (Tok.isNot(tok::l_paren))
1231 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1232 << BuiltinII);
1233
Chris Lattner04132372006-10-16 06:12:55 +00001234 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +00001235 // TODO: Build AST.
1236
Chris Lattner11124352006-08-12 19:16:08 +00001237 switch (T) {
1238 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001239 case tok::kw___builtin_va_arg: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001240 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001241 if (Expr.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001242 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001243 return ExprError();
Chris Lattner11124352006-08-12 19:16:08 +00001244 }
Chris Lattner0be454e2006-08-12 19:30:51 +00001245
Chris Lattner6d7e6342006-08-15 03:41:14 +00001246 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001247 return ExprError();
Chris Lattner0be454e2006-08-12 19:30:51 +00001248
Douglas Gregor220cac52009-02-18 17:45:20 +00001249 TypeResult Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001250
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001251 if (Tok.isNot(tok::r_paren)) {
1252 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001253 return ExprError();
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001254 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001255 if (Ty.isInvalid())
1256 Res = ExprError();
1257 else
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001258 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +00001259 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +00001260 }
Chris Lattner687d6092007-08-30 15:51:11 +00001261 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +00001262 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor220cac52009-02-18 17:45:20 +00001263 TypeResult Ty = ParseTypeName();
Chris Lattnerf37e09e2009-03-24 17:21:43 +00001264 if (Ty.isInvalid()) {
1265 SkipUntil(tok::r_paren);
1266 return ExprError();
1267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Chris Lattner6d7e6342006-08-15 03:41:14 +00001269 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001270 return ExprError();
1271
Chris Lattner11124352006-08-12 19:16:08 +00001272 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001273 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001274 Diag(Tok, diag::err_expected_ident);
1275 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001276 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001277 }
Sebastian Redl90893182008-12-11 22:33:27 +00001278
Chris Lattner687d6092007-08-30 15:51:11 +00001279 // Keep track of the various subcomponents we see.
1280 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redl90893182008-12-11 22:33:27 +00001281
Chris Lattner687d6092007-08-30 15:51:11 +00001282 Comps.push_back(Action::OffsetOfComponent());
1283 Comps.back().isBrackets = false;
1284 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1285 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +00001286
Sebastian Redl511ed552008-11-25 22:21:31 +00001287 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +00001288 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001289 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +00001290 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +00001291 Comps.push_back(Action::OffsetOfComponent());
1292 Comps.back().isBrackets = false;
1293 Comps.back().LocStart = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001294
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001295 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +00001296 Diag(Tok, diag::err_expected_ident);
1297 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001298 return ExprError();
Chris Lattner687d6092007-08-30 15:51:11 +00001299 }
1300 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1301 Comps.back().LocEnd = ConsumeToken();
Sebastian Redl90893182008-12-11 22:33:27 +00001302
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001303 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +00001304 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +00001305 Comps.push_back(Action::OffsetOfComponent());
1306 Comps.back().isBrackets = true;
1307 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +00001308 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001309 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +00001310 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001311 return move(Res);
Chris Lattner11124352006-08-12 19:16:08 +00001312 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001313 Comps.back().U.E = Res.release();
Chris Lattner11124352006-08-12 19:16:08 +00001314
Chris Lattner687d6092007-08-30 15:51:11 +00001315 Comps.back().LocEnd =
1316 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman5e774b12009-06-27 20:38:33 +00001317 } else {
1318 if (Tok.isNot(tok::r_paren)) {
1319 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor220cac52009-02-18 17:45:20 +00001320 Res = ExprError();
Eli Friedman5e774b12009-06-27 20:38:33 +00001321 } else if (Ty.isInvalid()) {
1322 Res = ExprError();
1323 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001324 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1325 Ty.get(), &Comps[0],
Douglas Gregor220cac52009-02-18 17:45:20 +00001326 Comps.size(), ConsumeParen());
Eli Friedman5e774b12009-06-27 20:38:33 +00001327 }
Chris Lattner5ad4f462007-08-30 15:52:49 +00001328 break;
Chris Lattner11124352006-08-12 19:16:08 +00001329 }
1330 }
1331 break;
Chris Lattner687d6092007-08-30 15:51:11 +00001332 }
Steve Naroff9efdabc2007-08-03 21:21:27 +00001333 case tok::kw___builtin_choose_expr: {
Sebastian Redl59b5e512008-12-11 21:36:32 +00001334 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001335 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001336 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001337 return move(Cond);
Steve Naroff9efdabc2007-08-03 21:21:27 +00001338 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001339 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001340 return ExprError();
1341
Sebastian Redl59b5e512008-12-11 21:36:32 +00001342 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001343 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001344 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001345 return move(Expr1);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001346 }
Chris Lattner6d7e6342006-08-15 03:41:14 +00001347 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001348 return ExprError();
1349
Sebastian Redl59b5e512008-12-11 21:36:32 +00001350 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001351 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001352 SkipUntil(tok::r_paren);
Sebastian Redl90893182008-12-11 22:33:27 +00001353 return move(Expr2);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001354 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001355 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +00001356 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001357 return ExprError();
Steve Naroff9efdabc2007-08-03 21:21:27 +00001358 }
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001359 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1360 move(Expr2), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001361 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +00001362 }
Chris Lattner11124352006-08-12 19:16:08 +00001363 case tok::kw___builtin_types_compatible_p:
Douglas Gregor220cac52009-02-18 17:45:20 +00001364 TypeResult Ty1 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001365
Chris Lattner6d7e6342006-08-15 03:41:14 +00001366 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redl90893182008-12-11 22:33:27 +00001367 return ExprError();
1368
Douglas Gregor220cac52009-02-18 17:45:20 +00001369 TypeResult Ty2 = ParseTypeName();
Sebastian Redl90893182008-12-11 22:33:27 +00001370
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001371 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +00001372 Diag(Tok, diag::err_expected_rparen);
Sebastian Redl90893182008-12-11 22:33:27 +00001373 return ExprError();
Steve Naroff788d8642007-08-01 23:45:51 +00001374 }
Douglas Gregor220cac52009-02-18 17:45:20 +00001375
1376 if (Ty1.isInvalid() || Ty2.isInvalid())
1377 Res = ExprError();
1378 else
1379 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1380 ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001381 break;
Sebastian Redl59b5e512008-12-11 21:36:32 +00001382 }
1383
Chris Lattner11124352006-08-12 19:16:08 +00001384 // These can be followed by postfix-expr pieces because they are
1385 // primary-expressions.
Sebastian Redl90893182008-12-11 22:33:27 +00001386 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner11124352006-08-12 19:16:08 +00001387}
1388
Chris Lattner4add4e62006-08-11 01:33:00 +00001389/// ParseParenExpression - This parses the unit that starts with a '(' token,
1390/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001391/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1392/// not the parsed cast-expression.
Chris Lattner4add4e62006-08-11 01:33:00 +00001393///
1394/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001395/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001396/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1397/// postfix-expression: [C99 6.5.2]
1398/// '(' type-name ')' '{' initializer-list '}'
1399/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001400/// cast-expression: [C99 6.5.4]
1401/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +00001402///
Sebastian Redl90893182008-12-11 22:33:27 +00001403Parser::OwningExprResult
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001404Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Mike Stump11289f42009-09-09 15:08:12 +00001405 bool parseAsExprList, TypeTy *&CastTy,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001406 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001407 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregor0db4ccd2009-02-09 21:04:56 +00001408 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner04132372006-10-16 06:12:55 +00001409 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001410 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001411 bool isAmbiguousTypeId;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001412 CastTy = 0;
Sebastian Redl90893182008-12-11 22:33:27 +00001413
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001414 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001415 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl042ad952008-12-11 19:30:53 +00001416 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001417 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001418
Chris Lattner366727f2007-07-24 16:58:17 +00001419 // If the substmt parsed correctly, build the AST node.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001420 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl6d4256c2009-03-15 17:47:39 +00001421 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001422
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001423 } else if (ExprType >= CompoundLiteral &&
1424 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump11289f42009-09-09 15:08:12 +00001425
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001426 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump11289f42009-09-09 15:08:12 +00001427
Argyrios Kyrtzidis12179bc2009-05-22 10:24:42 +00001428 // In C++, if the type-id is ambiguous we disambiguate based on context.
1429 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1430 // in which case we should treat it as type-id.
1431 // if stopIfCastExpr is false, we need to determine the context past the
1432 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1433 if (isAmbiguousTypeId && !stopIfCastExpr)
1434 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1435 OpenLoc, RParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001436
Douglas Gregor220cac52009-02-18 17:45:20 +00001437 TypeResult Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001438
1439 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001440 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001441 RParenLoc = ConsumeParen();
1442 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001443 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001444
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001445 if (Tok.is(tok::l_brace)) {
Chris Lattner4add4e62006-08-11 01:33:00 +00001446 ExprType = CompoundLiteral;
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001447 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnerd8980502008-12-12 06:00:12 +00001448 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001449
Chris Lattnerd8980502008-12-12 06:00:12 +00001450 if (ExprType == CastExpr) {
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001451 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor220cac52009-02-18 17:45:20 +00001452
1453 if (Ty.isInvalid())
1454 return ExprError();
1455
1456 CastTy = Ty.get();
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001457
1458 if (stopIfCastExpr) {
1459 // Note that this doesn't parse the subsequent cast-expression, it just
1460 // returns the parsed type to the callee.
1461 return OwningExprResult(Actions);
1462 }
1463
1464 // Parse the cast-expression that follows it next.
1465 // TODO: For cast expression with CastTy.
Nate Begeman5ec4b312009-08-10 23:49:36 +00001466 Result = ParseCastExpression(false, false, true);
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001467 if (!Result.isInvalid())
Nate Begeman5ec4b312009-08-10 23:49:36 +00001468 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1469 move(Result));
Argyrios Kyrtzidis9a9c0f42009-05-22 10:23:40 +00001470 return move(Result);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001471 }
Sebastian Redlb5d49352009-01-19 22:31:54 +00001472
Chris Lattnerd8980502008-12-12 06:00:12 +00001473 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1474 return ExprError();
Nate Begeman5ec4b312009-08-10 23:49:36 +00001475 } else if (parseAsExprList) {
1476 // Parse the expression-list.
1477 ExprVector ArgExprs(Actions);
1478 CommaLocsTy CommaLocs;
1479
1480 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1481 ExprType = SimpleExpr;
Mike Stump11289f42009-09-09 15:08:12 +00001482 Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(),
Nate Begeman5ec4b312009-08-10 23:49:36 +00001483 move_arg(ArgExprs));
1484 }
Chris Lattner4add4e62006-08-11 01:33:00 +00001485 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001486 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001487 ExprType = SimpleExpr;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001488 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl726a0d92009-02-05 15:02:23 +00001489 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattnerf8339772006-08-10 22:01:51 +00001490 }
Sebastian Redl90893182008-12-11 22:33:27 +00001491
Chris Lattner4564bc12006-08-10 23:14:52 +00001492 // Match the ')'.
Chris Lattnerd8980502008-12-12 06:00:12 +00001493 if (Result.isInvalid()) {
Chris Lattner89c50c62006-08-11 06:41:18 +00001494 SkipUntil(tok::r_paren);
Chris Lattnerd8980502008-12-12 06:00:12 +00001495 return ExprError();
Chris Lattnere550a4e2006-08-24 06:37:51 +00001496 }
Mike Stump11289f42009-09-09 15:08:12 +00001497
Chris Lattnerd8980502008-12-12 06:00:12 +00001498 if (Tok.is(tok::r_paren))
1499 RParenLoc = ConsumeParen();
1500 else
1501 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001502
1503 return move(Result);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001504}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001505
Argyrios Kyrtzidis5da1f082009-05-22 10:24:05 +00001506/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1507/// and we are at the left brace.
1508///
1509/// postfix-expression: [C99 6.5.2]
1510/// '(' type-name ')' '{' initializer-list '}'
1511/// '(' type-name ')' '{' initializer-list ',' '}'
1512///
1513Parser::OwningExprResult
1514Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1515 SourceLocation LParenLoc,
1516 SourceLocation RParenLoc) {
1517 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1518 if (!getLang().C99) // Compound literals don't exist in C90.
1519 Diag(LParenLoc, diag::ext_c99_compound_literal);
1520 OwningExprResult Result = ParseInitializer();
1521 if (!Result.isInvalid() && Ty)
1522 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1523 return move(Result);
1524}
1525
Chris Lattnerd3e98952006-10-06 05:22:26 +00001526/// ParseStringLiteralExpression - This handles the various token types that
1527/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1528/// translation phase #6].
1529///
1530/// primary-expression: [C99 6.5.1]
1531/// string-literal
Sebastian Redld65cea82008-12-11 22:51:44 +00001532Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001533 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redld65cea82008-12-11 22:51:44 +00001534
Chris Lattnerd3e98952006-10-06 05:22:26 +00001535 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1536 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001537 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redld65cea82008-12-11 22:51:44 +00001538
Chris Lattnerd3e98952006-10-06 05:22:26 +00001539 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001540 StringToks.push_back(Tok);
1541 ConsumeStringToken();
1542 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001543
1544 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlffbcf962009-01-18 18:53:16 +00001545 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001546}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001547
1548/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1549///
1550/// argument-expression-list:
1551/// assignment-expression
1552/// argument-expression-list , assignment-expression
1553///
1554/// [C++] expression-list:
1555/// [C++] assignment-expression
1556/// [C++] expression-list , assignment-expression
1557///
Douglas Gregorcabea402009-09-22 15:41:20 +00001558bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1559 void (Action::*Completer)(Scope *S,
1560 void *Data,
1561 ExprTy **Args,
1562 unsigned NumArgs),
1563 void *Data) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001564 while (1) {
Douglas Gregorcabea402009-09-22 15:41:20 +00001565 if (Tok.is(tok::code_completion)) {
1566 if (Completer)
1567 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1568 ConsumeToken();
1569 }
1570
Sebastian Redl59b5e512008-12-11 21:36:32 +00001571 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001572 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001573 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001574
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001575 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001576
1577 if (Tok.isNot(tok::comma))
1578 return false;
1579 // Move to the next argument, remember where the comma was.
1580 CommaLocs.push_back(ConsumeToken());
1581 }
1582}
Steve Naroff0ac012832008-08-28 19:20:44 +00001583
Mike Stump82f071f2009-02-04 22:31:32 +00001584/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1585///
1586/// [clang] block-id:
1587/// [clang] specifier-qualifier-list block-declarator
1588///
1589void Parser::ParseBlockId() {
1590 // Parse the specifier-qualifier-list piece.
1591 DeclSpec DS;
1592 ParseSpecifierQualifierList(DS);
1593
1594 // Parse the block-declarator.
1595 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1596 ParseDeclarator(DeclaratorInfo);
Mike Stump88788fe2009-04-29 19:03:13 +00001597
Mike Stump56ed2ea2009-04-29 21:40:37 +00001598 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1599 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1600 SourceLocation());
1601
Mike Stump88788fe2009-04-29 19:03:13 +00001602 if (Tok.is(tok::kw___attribute)) {
1603 SourceLocation Loc;
1604 AttributeList *AttrList = ParseAttributes(&Loc);
1605 DeclaratorInfo.AddAttributes(AttrList, Loc);
1606 }
1607
Mike Stump82f071f2009-02-04 22:31:32 +00001608 // Inform sema that we are starting a block.
1609 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1610}
1611
Steve Naroff0ac012832008-08-28 19:20:44 +00001612/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001613/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001614///
1615/// block-literal:
1616/// [clang] '^' block-args[opt] compound-statement
Mike Stump82f071f2009-02-04 22:31:32 +00001617/// [clang] '^' block-id compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001618/// [clang] block-args:
1619/// [clang] '(' parameter-list ')'
1620///
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001621Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff0ac012832008-08-28 19:20:44 +00001622 assert(Tok.is(tok::caret) && "block literal starts with ^");
1623 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001624
Chris Lattnerf6801202009-03-05 07:32:12 +00001625 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1626 "block literal parsing");
1627
Mike Stump11289f42009-09-09 15:08:12 +00001628 // Enter a scope to hold everything within the block. This includes the
Steve Naroff0ac012832008-08-28 19:20:44 +00001629 // argument decls, decls within the compound expression, etc. This also
1630 // allows determining whether a variable reference inside the block is
1631 // within or outside of the block.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001632 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1633 Scope::BreakScope | Scope::ContinueScope |
1634 Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001635
1636 // Inform sema that we are starting a block.
1637 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump11289f42009-09-09 15:08:12 +00001638
Steve Naroff0ac012832008-08-28 19:20:44 +00001639 // Parse the return type if present.
1640 DeclSpec DS;
Mike Stump82f071f2009-02-04 22:31:32 +00001641 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001642 // FIXME: Since the return type isn't actually parsed, it can't be used to
1643 // fill ParamInfo with an initial valid range, so do it manually.
1644 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001645
Steve Naroff0ac012832008-08-28 19:20:44 +00001646 // If this block has arguments, parse them. There is no ambiguity here with
1647 // the expression case, because the expression case requires a parameter list.
1648 if (Tok.is(tok::l_paren)) {
1649 ParseParenDeclarator(ParamInfo);
1650 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001651 // SetIdentifier sets the source range end, but in this case we're past
1652 // that location.
1653 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff0ac012832008-08-28 19:20:44 +00001654 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001655 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +00001656 if (ParamInfo.isInvalidType()) {
Mike Stump82f071f2009-02-04 22:31:32 +00001657 // If there was an error parsing the arguments, they may have
1658 // tried to use ^(x+y) which requires an argument list. Just
1659 // skip the whole block literal.
Chris Lattnerf95894c2009-04-18 20:05:34 +00001660 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001661 return ExprError();
Steve Naroff0ac012832008-08-28 19:20:44 +00001662 }
Mike Stump88788fe2009-04-29 19:03:13 +00001663
1664 if (Tok.is(tok::kw___attribute)) {
1665 SourceLocation Loc;
1666 AttributeList *AttrList = ParseAttributes(&Loc);
1667 ParamInfo.AddAttributes(AttrList, Loc);
1668 }
1669
Mike Stump82f071f2009-02-04 22:31:32 +00001670 // Inform sema that we are starting a block.
1671 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpd73e4412009-04-14 18:24:37 +00001672 } else if (!Tok.is(tok::l_brace)) {
Mike Stump82f071f2009-02-04 22:31:32 +00001673 ParseBlockId();
Steve Naroff0ac012832008-08-28 19:20:44 +00001674 } else {
1675 // Otherwise, pretend we saw (void).
Mike Stump11289f42009-09-09 15:08:12 +00001676 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00001677 SourceLocation(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00001678 0, 0, 0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00001679 false, SourceLocation(),
1680 false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00001681 CaretLoc, CaretLoc,
1682 ParamInfo),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001683 CaretLoc);
Mike Stump88788fe2009-04-29 19:03:13 +00001684
1685 if (Tok.is(tok::kw___attribute)) {
1686 SourceLocation Loc;
1687 AttributeList *AttrList = ParseAttributes(&Loc);
1688 ParamInfo.AddAttributes(AttrList, Loc);
1689 }
1690
Mike Stump82f071f2009-02-04 22:31:32 +00001691 // Inform sema that we are starting a block.
1692 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001693 }
1694
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001695
Sebastian Redlc13f2682008-12-09 20:22:58 +00001696 OwningExprResult Result(Actions, true);
Chris Lattner9eac9312009-03-27 04:18:06 +00001697 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001698 // Saw something like: ^expr
1699 Diag(Tok, diag::err_expected_expression);
Chris Lattnerf95894c2009-04-18 20:05:34 +00001700 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianf2a31202009-01-14 19:39:53 +00001701 return ExprError();
1702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
Chris Lattner9eac9312009-03-27 04:18:06 +00001704 OwningStmtResult Stmt(ParseCompoundStatementBody());
1705 if (!Stmt.isInvalid())
1706 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1707 else
1708 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001709 return move(Result);
Steve Naroff0ac012832008-08-28 19:20:44 +00001710}