blob: 13b32acd5b299196c4811cdfbe90965bd34522d8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// 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 Redl22460502009-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 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor55f6b142009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000059 bool GreaterThanIsOperator,
60 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000062 case tok::greater:
Douglas Gregor3965b7b2009-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 Gregor55f6b142009-02-09 18:46:07 +000067 if (GreaterThanIsOperator)
68 return prec::Relational;
69 return prec::Unknown;
70
Douglas Gregor3965b7b2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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;
101 case tok::exclaimequal:
102 case tok::equalequal: return prec::Equality;
103 case tok::lessequal:
104 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000105 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000106 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl22460502009-02-07 00:15:38 +0000112 case tok::periodstar:
113 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
117
118/// ParseExpression - Simple precedence-based parser for binary/ternary
119/// operators.
120///
121/// 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 Redl22460502009-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///
Reid Spencer5f016e22007-07-11 17:01:13 +0000135/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000136/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl3201f6b2009-04-16 17:51:27 +0000188/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000189///
190/// assignment-expression: [C99 6.5.16]
191/// conditional-expression
192/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000193/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000194///
195/// assignment-operator: one of
196/// = *= /= %= += -= <<= >>= &= ^= |=
197///
198/// expression: [C99 6.5.17]
199/// assignment-expression
200/// expression ',' assignment-expression
201///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000202Parser::OwningExprResult Parser::ParseExpression() {
Mike Stump6ce0c392009-05-15 21:47:08 +0000203 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000204 if (LHS.isInvalid()) return move(LHS);
205
Sebastian Redld8c4e152008-12-11 22:33:27 +0000206 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207}
208
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000209/// This routine is called when the '@' is seen and consumed.
210/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000211/// routine is necessary to disambiguate @try-statement from,
212/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000213///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000214Parser::OwningExprResult
215Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000216 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000217 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000218
Sebastian Redld8c4e152008-12-11 22:33:27 +0000219 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000220}
221
Eli Friedmanadf077f2009-01-27 08:43:38 +0000222/// This routine is called when a leading '__extension__' is seen and
223/// consumed. This is necessary because the token gets consumed in the
224/// process of disambiguating between an expression and a declaration.
225Parser::OwningExprResult
226Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000227 OwningExprResult LHS(Actions, true);
228 {
229 // Silence extension warnings in the sub-expression
230 ExtensionRAIIObject O(Diags);
231
232 LHS = ParseCastExpression(false);
233 if (LHS.isInvalid()) return move(LHS);
234 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000235
236 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000237 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000238 if (LHS.isInvalid()) return move(LHS);
239
240 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
241}
242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
244///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000245Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000246 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000247 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000248
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000249 OwningExprResult LHS(ParseCastExpression(false));
250 if (LHS.isInvalid()) return move(LHS);
251
Sebastian Redld8c4e152008-12-11 22:33:27 +0000252 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
Chris Lattnerb93fb492008-06-02 21:31:07 +0000255/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
256/// where part of an objc message send has already been parsed. In this case
257/// LBracLoc indicates the location of the '[' of the message send, and either
258/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
259/// message.
260///
261/// Since this handles full assignment-expression's, it handles postfix
262/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000263Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000264Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000265 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000266 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000267 ExprArg ReceiverExpr) {
268 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
269 ReceiverName,
270 move(ReceiverExpr)));
271 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000272 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000273 if (R.isInvalid()) return move(R);
274 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000275}
276
277
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000278Parser::OwningExprResult Parser::ParseConstantExpression() {
Douglas Gregore0762c92009-06-19 23:52:42 +0000279 // C++ [basic.def.odr]p2:
280 // An expression is potentially evaluated unless it appears where an
281 // integral constant expression is required (see 5.19) [...].
Douglas Gregorac7610d2009-06-22 20:57:11 +0000282 EnterExpressionEvaluationContext Unevaluated(Actions,
283 Action::Unevaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +0000284
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000285 OwningExprResult LHS(ParseCastExpression(false));
286 if (LHS.isInvalid()) return move(LHS);
287
Sebastian Redld8c4e152008-12-11 22:33:27 +0000288 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}
290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
292/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000293Parser::OwningExprResult
294Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000295 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
296 GreaterThanIsOperator,
297 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 SourceLocation ColonLoc;
299
300 while (1) {
301 // If this token has a lower precedence than we are allowed to parse (e.g.
302 // because we are called recursively, or because the token is not a binop),
303 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000304 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000305 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
307 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000308 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000312 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000314 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 // Handle this production specially:
316 // logical-OR-expression '?' expression ':' conditional-expression
317 // In particular, the RHS of the '?' is 'expression', not
318 // 'logical-OR-expression' as we might expect.
319 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000320 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000321 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 } else {
323 // Special case handling of "X ? Y : Z" where Y is empty:
324 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000325 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 Diag(Tok, diag::ext_gnu_conditional_expr);
327 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000328
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000329 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000331 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000332 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // Eat the colon.
336 ColonLoc = ConsumeToken();
337 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000338
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000340 // ParseCastExpression works here because all RHS expressions in C have it
341 // as a prefix, at least. However, in C++, an assignment-expression could
342 // be a throw-expression, which is not a valid cast-expression.
343 // Therefore we need some special-casing here.
344 // Also note that the third operand of the conditional operator is
345 // an assignment-expression in C++.
346 OwningExprResult RHS(Actions);
347 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
348 RHS = ParseAssignmentExpression();
349 else
350 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000351 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000352 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
354 // Remember the precedence of this operator and get the precedence of the
355 // operator immediately to the right of the RHS.
356 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000357 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
358 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
360 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000361 bool isRightAssoc = ThisPrec == prec::Conditional ||
362 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000363
364 // Get the precedence of the operator to the right of the RHS. If it binds
365 // more tightly with RHS than we do, evaluate it completely first.
366 if (ThisPrec < NextTokPrec ||
367 (ThisPrec == NextTokPrec && isRightAssoc)) {
368 // If this is left-associative, only parse things on the RHS that bind
369 // more tightly than the current operator. If it is left-associative, it
370 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
371 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000372 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000373 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000374 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000375 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000377 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
378 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 }
380 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000381
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000382 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000383 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000384 if (TernaryMiddle.isInvalid()) {
385 // If we're using '>>' as an operator within a template
386 // argument list (in C++98), suggest the addition of
387 // parentheses so that the code remains well-formed in C++0x.
388 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
389 SuggestParentheses(OpToken.getLocation(),
390 diag::warn_cxx0x_right_shift_in_template_arg,
391 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
392 Actions.getExprRange(RHS.get()).getEnd()));
393
Sebastian Redleffa8d12008-12-10 00:02:53 +0000394 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000395 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000396 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000397 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000398 move(LHS), move(TernaryMiddle),
399 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000400 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 }
402}
403
404/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000405/// true, parse a unary-expression. isAddressOfOperand exists because an
406/// id-expression that is the operand of address-of gets special treatment
407/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000408///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000409Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
410 bool isAddressOfOperand) {
411 bool NotCastExpr;
412 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
413 isAddressOfOperand,
414 NotCastExpr);
415 if (NotCastExpr)
416 Diag(Tok, diag::err_expected_expression);
417 return move(Res);
418}
419
420/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
421/// 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. NotCastExpr is set to true if the token is not the
424/// start of a cast-expression, and no diagnostic is emitted in this case.
425///
Reid Spencer5f016e22007-07-11 17:01:13 +0000426/// cast-expression: [C99 6.5.4]
427/// unary-expression
428/// '(' type-name ')' cast-expression
429///
430/// unary-expression: [C99 6.5.3]
431/// postfix-expression
432/// '++' unary-expression
433/// '--' unary-expression
434/// unary-operator cast-expression
435/// 'sizeof' unary-expression
436/// 'sizeof' '(' type-name ')'
437/// [GNU] '__alignof' unary-expression
438/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000439/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000440/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000441/// [C++] new-expression
442/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000443///
444/// unary-operator: one of
445/// '&' '*' '+' '-' '~' '!'
446/// [GNU] '__extension__' '__real' '__imag'
447///
448/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000449/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000450/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000451/// constant
452/// string-literal
453/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000454/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000455/// '(' expression ')'
456/// '__func__' [C99 6.4.2.2]
457/// [GNU] '__FUNCTION__'
458/// [GNU] '__PRETTY_FUNCTION__'
459/// [GNU] '(' compound-statement ')'
460/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
461/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
462/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
463/// assign-expr ')'
464/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000465/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000466/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000467/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000468/// [OBJC] '@protocol' '(' identifier ')'
469/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000470/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000471/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
472/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000473/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
474/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
475/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
476/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000477/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
478/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000479/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000480/// [G++] unary-type-trait '(' type-id ')'
481/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000482/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000483///
484/// constant: [C99 6.4.4]
485/// integer-constant
486/// floating-constant
487/// enumeration-constant -> identifier
488/// character-constant
489///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000490/// id-expression: [C++ 5.1]
491/// unqualified-id
492/// qualified-id [TODO]
493///
494/// unqualified-id: [C++ 5.1]
495/// identifier
496/// operator-function-id
497/// conversion-function-id [TODO]
498/// '~' class-name [TODO]
499/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000500///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000501/// new-expression: [C++ 5.3.4]
502/// '::'[opt] 'new' new-placement[opt] new-type-id
503/// new-initializer[opt]
504/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
505/// new-initializer[opt]
506///
507/// delete-expression: [C++ 5.3.5]
508/// '::'[opt] 'delete' cast-expression
509/// '::'[opt] 'delete' '[' ']' cast-expression
510///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000511/// [GNU] unary-type-trait:
512/// '__has_nothrow_assign' [TODO]
513/// '__has_nothrow_copy' [TODO]
514/// '__has_nothrow_constructor' [TODO]
515/// '__has_trivial_assign' [TODO]
516/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000517/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000518/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000519/// '__has_virtual_destructor' [TODO]
520/// '__is_abstract' [TODO]
521/// '__is_class'
522/// '__is_empty' [TODO]
523/// '__is_enum'
524/// '__is_pod'
525/// '__is_polymorphic'
526/// '__is_union'
527///
528/// [GNU] binary-type-trait:
529/// '__is_base_of' [TODO]
530///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000531Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000532 bool isAddressOfOperand,
533 bool &NotCastExpr) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000534 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000536 NotCastExpr = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000537
538 // This handles all of cast-expression, unary-expression, postfix-expression,
539 // and primary-expression. We handle them together like this for efficiency
540 // and to simplify handling of an expression starting with a '(' token: which
541 // may be one of a parenthesized expression, cast-expression, compound literal
542 // expression, or statement expression.
543 //
544 // If the parsed tokens consist of a primary-expression, the cases below
545 // call ParsePostfixExpressionSuffix to handle the postfix expression
546 // suffixes. Cases that cannot be followed by postfix exprs should
547 // return without invoking ParsePostfixExpressionSuffix.
548 switch (SavedKind) {
549 case tok::l_paren: {
550 // If this expression is limited to being a unary-expression, the parent can
551 // not start a cast expression.
552 ParenParseOption ParenExprType =
553 isUnaryExpression ? CompoundLiteral : CastExpr;
554 TypeTy *CastTy;
555 SourceLocation LParenLoc = Tok.getLocation();
556 SourceLocation RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000557 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
558 CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000559 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000560
561 switch (ParenExprType) {
562 case SimpleExpr: break; // Nothing else to do.
563 case CompoundStmt: break; // Nothing else to do.
564 case CompoundLiteral:
565 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
566 // postfix-expression exist, parse them now.
567 break;
568 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000569 // We have parsed the cast-expression and no postfix-expr pieces are
570 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000571 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000575 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000577
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 // primary-expression
579 case tok::numeric_constant:
580 // constant: integer-constant
581 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000582
Steve Narofff69936d2007-09-16 03:34:24 +0000583 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000587 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000588
589 case tok::kw_true:
590 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000591 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000592
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000593 case tok::kw_nullptr:
594 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
595
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000596 case tok::identifier: { // primary-expression: identifier
597 // unqualified-id: identifier
598 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000599 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000600 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000601 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000602 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
603 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000604 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000605 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000606
Steve Naroff61f72cb2009-03-09 21:12:44 +0000607 // Support 'Class.property' notation.
608 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
609 // 'super' (which is inappropriate here).
610 if (getLang().ObjC1 &&
611 Actions.getTypeName(*Tok.getIdentifierInfo(),
612 Tok.getLocation(), CurScope) &&
613 NextToken().is(tok::period)) {
614 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
615 SourceLocation IdentLoc = ConsumeToken();
616 SourceLocation DotLoc = ConsumeToken();
617
618 if (Tok.isNot(tok::identifier)) {
619 Diag(Tok, diag::err_expected_ident);
620 return ExprError();
621 }
622 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
623 SourceLocation PropertyLoc = ConsumeToken();
624
625 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
626 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000627 // These can be followed by postfix-expr pieces.
628 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000629 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 // Consume the identifier so that we can see if it is followed by a '('.
631 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
632 // need to know whether or not this identifier is a function designator or
633 // not.
634 IdentifierInfo &II = *Tok.getIdentifierInfo();
635 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000636 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000638 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 }
640 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000641 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 ConsumeToken();
643 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000644 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
646 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
647 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000648 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 ConsumeToken();
650 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000651 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 case tok::string_literal: // primary-expression: string-literal
653 case tok::wide_string_literal:
654 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000655 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000657 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 case tok::kw___builtin_va_arg:
659 case tok::kw___builtin_offsetof:
660 case tok::kw___builtin_choose_expr:
661 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000662 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000663 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000664 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000665 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 case tok::plusplus: // unary-expression: '++' unary-expression
667 case tok::minusminus: { // unary-expression: '--' unary-expression
668 SourceLocation SavedLoc = ConsumeToken();
669 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000670 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000671 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000672 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000674 case tok::amp: { // unary-expression: '&' cast-expression
675 // Special treatment because of member pointers
676 SourceLocation SavedLoc = ConsumeToken();
677 Res = ParseCastExpression(false, true);
678 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000679 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000680 return move(Res);
681 }
682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 case tok::star: // unary-expression: '*' cast-expression
684 case tok::plus: // unary-expression: '+' cast-expression
685 case tok::minus: // unary-expression: '-' cast-expression
686 case tok::tilde: // unary-expression: '~' cast-expression
687 case tok::exclaim: // unary-expression: '!' cast-expression
688 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000689 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 SourceLocation SavedLoc = ConsumeToken();
691 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000692 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000693 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000694 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000695 }
696
Chris Lattner35080842008-02-02 20:20:10 +0000697 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
698 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000699 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000700 SourceLocation SavedLoc = ConsumeToken();
701 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000702 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000703 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000704 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 }
706 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
707 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000708 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
710 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000711 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000712 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 case tok::ampamp: { // unary-expression: '&&' identifier
714 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000715 if (Tok.isNot(tok::identifier))
716 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000719 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 Tok.getIdentifierInfo());
721 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000722 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 }
724 case tok::kw_const_cast:
725 case tok::kw_dynamic_cast:
726 case tok::kw_reinterpret_cast:
727 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000728 Res = ParseCXXCasts();
729 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000730 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000731 case tok::kw_typeid:
732 Res = ParseCXXTypeid();
733 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000734 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000735 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000736 Res = ParseCXXThis();
737 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000738 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000739
740 case tok::kw_char:
741 case tok::kw_wchar_t:
742 case tok::kw_bool:
743 case tok::kw_short:
744 case tok::kw_int:
745 case tok::kw_long:
746 case tok::kw_signed:
747 case tok::kw_unsigned:
748 case tok::kw_float:
749 case tok::kw_double:
750 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000751 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000752 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000753 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000754 if (!getLang().CPlusPlus) {
755 Diag(Tok, diag::err_expected_expression);
756 return ExprError();
757 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000758
759 if (SavedKind == tok::kw_typename) {
760 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
761 if (!TryAnnotateTypeOrScopeToken())
762 return ExprError();
763 }
764
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000765 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
766 //
767 DeclSpec DS;
768 ParseCXXSimpleTypeSpecifier(DS);
769 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000770 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
771 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000772
773 Res = ParseCXXTypeConstructExpression(DS);
774 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000775 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000776 }
777
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000778 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
779 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
780 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000781 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000782 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000783
Chris Lattner74ba4102009-01-04 22:52:14 +0000784 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000785 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
786 // annotates the token, tail recurse.
787 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000788 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
789
Chris Lattner74ba4102009-01-04 22:52:14 +0000790 // ::new -> [C++] new-expression
791 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000792 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000793 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000794 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000795 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000796 return ParseCXXDeleteExpression(true, CCLoc);
797
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000798 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000799 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000800 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000801 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000802
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000803 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000804 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000805
806 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000807 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000808
Sebastian Redl64b45f72009-01-05 20:52:13 +0000809 case tok::kw___is_pod: // [GNU] unary-type-trait
810 case tok::kw___is_class:
811 case tok::kw___is_enum:
812 case tok::kw___is_union:
813 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000814 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000815 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000816 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000817 return ParseUnaryTypeTrait();
818
Chris Lattnerc97c2042007-10-03 22:03:06 +0000819 case tok::at: {
820 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000821 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000822 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000823 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000824 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000825 case tok::l_square:
826 // These can be followed by postfix-expr pieces.
827 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000828 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000829 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000831 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000832 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000834
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 // unreachable.
836 abort();
837}
838
839/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
840/// is parsed, this method parses any suffixes that apply.
841///
842/// postfix-expression: [C99 6.5.2]
843/// primary-expression
844/// postfix-expression '[' expression ']'
845/// postfix-expression '(' argument-expression-list[opt] ')'
846/// postfix-expression '.' identifier
847/// postfix-expression '->' identifier
848/// postfix-expression '++'
849/// postfix-expression '--'
850/// '(' type-name ')' '{' initializer-list '}'
851/// '(' type-name ')' '{' initializer-list ',' '}'
852///
853/// argument-expression-list: [C99 6.5.2]
854/// argument-expression
855/// argument-expression-list ',' assignment-expression
856///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000857Parser::OwningExprResult
858Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 // Now that the primary-expression piece of the postfix-expression has been
860 // parsed, see if there are any postfix-expression pieces here.
861 SourceLocation Loc;
862 while (1) {
863 switch (Tok.getKind()) {
864 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000865 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
867 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000868 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000871
872 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000873 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
874 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000875 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000876 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
878 // Match the ']'.
879 MatchRHSPunctuation(tok::r_square, Loc);
880 break;
881 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000882
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000884 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000885 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000888
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000889 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000890 if (ParseExpressionList(ArgExprs, CommaLocs)) {
891 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000892 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 }
894 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000895
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000897 if (Tok.isNot(tok::r_paren)) {
898 MatchRHSPunctuation(tok::r_paren, Loc);
899 return ExprError();
900 }
901
902 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
904 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000905 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000906 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000907 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000909
910 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 break;
912 }
913 case tok::arrow: // postfix-expression: p-e '->' identifier
914 case tok::period: { // postfix-expression: p-e '.' identifier
915 tok::TokenKind OpKind = Tok.getKind();
916 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000917
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000918 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000920 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000922
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000923 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000924 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000925 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000926 *Tok.getIdentifierInfo(),
927 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000928 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 ConsumeToken();
930 break;
931 }
932 case tok::plusplus: // postfix-expression: postfix-expression '++'
933 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000934 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000935 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000936 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000937 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 ConsumeToken();
939 break;
940 }
941 }
942}
943
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000944/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
945/// we are at the start of an expression or a parenthesized type-id.
946/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
947/// (isCastExpr == false) or the type (isCastExpr == true).
948///
949/// unary-expression: [C99 6.5.3]
950/// 'sizeof' unary-expression
951/// 'sizeof' '(' type-name ')'
952/// [GNU] '__alignof' unary-expression
953/// [GNU] '__alignof' '(' type-name ')'
954/// [C++0x] 'alignof' '(' type-id ')'
955///
956/// [GNU] typeof-specifier:
957/// typeof ( expressions )
958/// typeof ( type-name )
959/// [GNU/C++] typeof unary-expression
960///
961Parser::OwningExprResult
962Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
963 bool &isCastExpr,
964 TypeTy *&CastTy,
965 SourceRange &CastRange) {
966
967 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
968 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
969 "Not a typeof/sizeof/alignof expression!");
970
971 OwningExprResult Operand(Actions);
972
973 // If the operand doesn't start with an '(', it must be an expression.
974 if (Tok.isNot(tok::l_paren)) {
975 isCastExpr = false;
976 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
977 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
978 return ExprError();
979 }
Douglas Gregore0762c92009-06-19 23:52:42 +0000980
981 // C++0x [expr.sizeof]p1:
982 // [...] The operand is either an expression, which is an unevaluated
983 // operand (Clause 5) [...]
984 //
985 // The GNU typeof and alignof extensions also behave as unevaluated
986 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +0000987 EnterExpressionEvaluationContext Unevaluated(Actions,
988 Action::Unevaluated);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000989 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000990 } else {
991 // If it starts with a '(', we know that it is either a parenthesized
992 // type-name, or it is a unary-expression that starts with a compound
993 // literal, or starts with a primary-expression that is a parenthesized
994 // expression.
995 ParenParseOption ExprType = CastExpr;
996 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Douglas Gregore0762c92009-06-19 23:52:42 +0000997
998 // C++0x [expr.sizeof]p1:
999 // [...] The operand is either an expression, which is an unevaluated
1000 // operand (Clause 5) [...]
1001 //
1002 // The GNU typeof and alignof extensions also behave as unevaluated
1003 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001004 EnterExpressionEvaluationContext Unevaluated(Actions,
1005 Action::Unevaluated);
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001006 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1007 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001008 CastRange = SourceRange(LParenLoc, RParenLoc);
1009
1010 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1011 // a type.
1012 if (ExprType == CastExpr) {
1013 isCastExpr = true;
1014 return ExprEmpty();
1015 }
1016
1017 // If this is a parenthesized expression, it is the start of a
1018 // unary-expression, but doesn't include any postfix pieces. Parse these
1019 // now if present.
1020 Operand = ParsePostfixExpressionSuffix(move(Operand));
1021 }
1022
1023 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1024 isCastExpr = false;
1025 return move(Operand);
1026}
1027
Reid Spencer5f016e22007-07-11 17:01:13 +00001028
1029/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1030/// unary-expression: [C99 6.5.3]
1031/// 'sizeof' unary-expression
1032/// 'sizeof' '(' type-name ')'
1033/// [GNU] '__alignof' unary-expression
1034/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001035/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +00001036Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001037 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1038 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001040 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 ConsumeToken();
1042
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001043 bool isCastExpr;
1044 TypeTy *CastTy;
1045 SourceRange CastRange;
1046 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1047 isCastExpr,
1048 CastTy,
1049 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001050
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001051 if (isCastExpr)
1052 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1053 OpTok.is(tok::kw_sizeof),
1054 /*isType=*/true, CastTy,
1055 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001058 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001059 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1060 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001061 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001062 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001063 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001064}
1065
1066/// ParseBuiltinPrimaryExpression
1067///
1068/// primary-expression: [C99 6.5.1]
1069/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1070/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1071/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1072/// assign-expr ')'
1073/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1074///
1075/// [GNU] offsetof-member-designator:
1076/// [GNU] identifier
1077/// [GNU] offsetof-member-designator '.' identifier
1078/// [GNU] offsetof-member-designator '[' expression ']'
1079///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001080Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001081 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1083
1084 tok::TokenKind T = Tok.getKind();
1085 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1086
1087 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001088 if (Tok.isNot(tok::l_paren))
1089 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1090 << BuiltinII);
1091
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 SourceLocation LParenLoc = ConsumeParen();
1093 // TODO: Build AST.
1094
1095 switch (T) {
1096 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001097 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001098 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001099 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001101 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 }
1103
1104 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001105 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001106
Douglas Gregor809070a2009-02-18 17:45:20 +00001107 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001108
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001109 if (Tok.isNot(tok::r_paren)) {
1110 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001111 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001112 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001113 if (Ty.isInvalid())
1114 Res = ExprError();
1115 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001116 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001118 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001119 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001120 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001121 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001122 if (Ty.isInvalid()) {
1123 SkipUntil(tok::r_paren);
1124 return ExprError();
1125 }
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001128 return ExprError();
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001131 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001132 Diag(Tok, diag::err_expected_ident);
1133 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001134 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001135 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001136
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001137 // Keep track of the various subcomponents we see.
1138 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001139
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001140 Comps.push_back(Action::OffsetOfComponent());
1141 Comps.back().isBrackets = false;
1142 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1143 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001144
Sebastian Redla55e52c2008-11-25 22:21:31 +00001145 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001147 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001149 Comps.push_back(Action::OffsetOfComponent());
1150 Comps.back().isBrackets = false;
1151 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001152
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001153 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001154 Diag(Tok, diag::err_expected_ident);
1155 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001156 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001157 }
1158 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1159 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001160
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001161 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001163 Comps.push_back(Action::OffsetOfComponent());
1164 Comps.back().isBrackets = true;
1165 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001167 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001169 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001171 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001172
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001173 Comps.back().LocEnd =
1174 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001175 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001176 if (Ty.isInvalid())
1177 Res = ExprError();
1178 else
1179 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1180 Ty.get(), &Comps[0],
1181 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001182 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001184 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001185 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
1187 }
1188 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001189 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001190 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001191 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001192 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001193 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001194 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001195 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001197 return ExprError();
1198
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001199 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001200 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001201 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001202 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001203 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001205 return ExprError();
1206
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001207 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001208 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001209 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001210 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001211 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001212 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001213 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001214 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001215 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001216 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1217 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001218 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001219 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001221 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001222
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001224 return ExprError();
1225
Douglas Gregor809070a2009-02-18 17:45:20 +00001226 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001227
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001228 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001229 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001230 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001231 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001232
1233 if (Ty1.isInvalid() || Ty2.isInvalid())
1234 Res = ExprError();
1235 else
1236 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1237 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001238 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001239 }
1240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 // These can be followed by postfix-expr pieces because they are
1242 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001243 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001244}
1245
1246/// ParseParenExpression - This parses the unit that starts with a '(' token,
1247/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001248/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1249/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001250///
1251/// primary-expression: [C99 6.5.1]
1252/// '(' expression ')'
1253/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1254/// postfix-expression: [C99 6.5.2]
1255/// '(' type-name ')' '{' initializer-list '}'
1256/// '(' type-name ')' '{' initializer-list ',' '}'
1257/// cast-expression: [C99 6.5.4]
1258/// '(' type-name ')' cast-expression
1259///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001260Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001261Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Sebastian Redld8c4e152008-12-11 22:33:27 +00001262 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001263 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001264 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001266 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001267 bool isAmbiguousTypeId;
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001269
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001270 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001272 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001274
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001275 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001276 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001277 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001278
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001279 } else if (ExprType >= CompoundLiteral &&
1280 isTypeIdInParens(isAmbiguousTypeId)) {
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001281
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 // Otherwise, this is a compound literal expression or cast expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001283
1284 // In C++, if the type-id is ambiguous we disambiguate based on context.
1285 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1286 // in which case we should treat it as type-id.
1287 // if stopIfCastExpr is false, we need to determine the context past the
1288 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1289 if (isAmbiguousTypeId && !stopIfCastExpr)
1290 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1291 OpenLoc, RParenLoc);
1292
Douglas Gregor809070a2009-02-18 17:45:20 +00001293 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001294
1295 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001296 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 RParenLoc = ConsumeParen();
1298 else
1299 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001300
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001301 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001303 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001304 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001305
Chris Lattner42ece642008-12-12 06:00:12 +00001306 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001307 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001308
1309 if (Ty.isInvalid())
1310 return ExprError();
1311
1312 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001313
1314 if (stopIfCastExpr) {
1315 // Note that this doesn't parse the subsequent cast-expression, it just
1316 // returns the parsed type to the callee.
1317 return OwningExprResult(Actions);
1318 }
1319
1320 // Parse the cast-expression that follows it next.
1321 // TODO: For cast expression with CastTy.
1322 Result = ParseCastExpression(false);
1323 if (!Result.isInvalid())
1324 Result = Actions.ActOnCastExpr(OpenLoc, CastTy, RParenLoc,move(Result));
1325 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001327
Chris Lattner42ece642008-12-12 06:00:12 +00001328 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1329 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 } else {
1331 Result = ParseExpression();
1332 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001333 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001334 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001338 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001340 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 }
Chris Lattner42ece642008-12-12 06:00:12 +00001342
1343 if (Tok.is(tok::r_paren))
1344 RParenLoc = ConsumeParen();
1345 else
1346 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001347
1348 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001349}
1350
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001351/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1352/// and we are at the left brace.
1353///
1354/// postfix-expression: [C99 6.5.2]
1355/// '(' type-name ')' '{' initializer-list '}'
1356/// '(' type-name ')' '{' initializer-list ',' '}'
1357///
1358Parser::OwningExprResult
1359Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1360 SourceLocation LParenLoc,
1361 SourceLocation RParenLoc) {
1362 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1363 if (!getLang().C99) // Compound literals don't exist in C90.
1364 Diag(LParenLoc, diag::ext_c99_compound_literal);
1365 OwningExprResult Result = ParseInitializer();
1366 if (!Result.isInvalid() && Ty)
1367 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1368 return move(Result);
1369}
1370
Reid Spencer5f016e22007-07-11 17:01:13 +00001371/// ParseStringLiteralExpression - This handles the various token types that
1372/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1373/// translation phase #6].
1374///
1375/// primary-expression: [C99 6.5.1]
1376/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001377Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001379
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1381 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001382 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 do {
1385 StringToks.push_back(Tok);
1386 ConsumeStringToken();
1387 } while (isTokenStringLiteral());
1388
1389 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001390 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001391}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001392
1393/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1394///
1395/// argument-expression-list:
1396/// assignment-expression
1397/// argument-expression-list , assignment-expression
1398///
1399/// [C++] expression-list:
1400/// [C++] assignment-expression
1401/// [C++] expression-list , assignment-expression
1402///
1403bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1404 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001405 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001406 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001407 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001408
Sebastian Redleffa8d12008-12-10 00:02:53 +00001409 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001410
1411 if (Tok.isNot(tok::comma))
1412 return false;
1413 // Move to the next argument, remember where the comma was.
1414 CommaLocs.push_back(ConsumeToken());
1415 }
1416}
Steve Naroff296e8d52008-08-28 19:20:44 +00001417
Mike Stump98eb8a72009-02-04 22:31:32 +00001418/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1419///
1420/// [clang] block-id:
1421/// [clang] specifier-qualifier-list block-declarator
1422///
1423void Parser::ParseBlockId() {
1424 // Parse the specifier-qualifier-list piece.
1425 DeclSpec DS;
1426 ParseSpecifierQualifierList(DS);
1427
1428 // Parse the block-declarator.
1429 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1430 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001431
Mike Stump6c92fa72009-04-29 21:40:37 +00001432 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1433 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1434 SourceLocation());
1435
Mike Stump19c30c02009-04-29 19:03:13 +00001436 if (Tok.is(tok::kw___attribute)) {
1437 SourceLocation Loc;
1438 AttributeList *AttrList = ParseAttributes(&Loc);
1439 DeclaratorInfo.AddAttributes(AttrList, Loc);
1440 }
1441
Mike Stump98eb8a72009-02-04 22:31:32 +00001442 // Inform sema that we are starting a block.
1443 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1444}
1445
Steve Naroff296e8d52008-08-28 19:20:44 +00001446/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001447/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001448///
1449/// block-literal:
1450/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001451/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001452/// [clang] block-args:
1453/// [clang] '(' parameter-list ')'
1454///
Sebastian Redl1d922962008-12-13 15:32:12 +00001455Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001456 assert(Tok.is(tok::caret) && "block literal starts with ^");
1457 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001458
Chris Lattner6b91f002009-03-05 07:32:12 +00001459 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1460 "block literal parsing");
1461
Steve Naroff296e8d52008-08-28 19:20:44 +00001462 // Enter a scope to hold everything within the block. This includes the
1463 // argument decls, decls within the compound expression, etc. This also
1464 // allows determining whether a variable reference inside the block is
1465 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001466 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1467 Scope::BreakScope | Scope::ContinueScope |
1468 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001469
1470 // Inform sema that we are starting a block.
1471 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001472
Steve Naroff296e8d52008-08-28 19:20:44 +00001473 // Parse the return type if present.
1474 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001475 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001476 // FIXME: Since the return type isn't actually parsed, it can't be used to
1477 // fill ParamInfo with an initial valid range, so do it manually.
1478 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001479
Steve Naroff296e8d52008-08-28 19:20:44 +00001480 // If this block has arguments, parse them. There is no ambiguity here with
1481 // the expression case, because the expression case requires a parameter list.
1482 if (Tok.is(tok::l_paren)) {
1483 ParseParenDeclarator(ParamInfo);
1484 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001485 // SetIdentifier sets the source range end, but in this case we're past
1486 // that location.
1487 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001488 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001489 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001490 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001491 // If there was an error parsing the arguments, they may have
1492 // tried to use ^(x+y) which requires an argument list. Just
1493 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001494 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001495 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001496 }
Mike Stump19c30c02009-04-29 19:03:13 +00001497
1498 if (Tok.is(tok::kw___attribute)) {
1499 SourceLocation Loc;
1500 AttributeList *AttrList = ParseAttributes(&Loc);
1501 ParamInfo.AddAttributes(AttrList, Loc);
1502 }
1503
Mike Stump98eb8a72009-02-04 22:31:32 +00001504 // Inform sema that we are starting a block.
1505 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001506 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001507 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001508 } else {
1509 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001510 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1511 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001512 0, 0, 0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00001513 false, SourceLocation(),
1514 false, 0, 0, 0,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001515 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001516 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001517
1518 if (Tok.is(tok::kw___attribute)) {
1519 SourceLocation Loc;
1520 AttributeList *AttrList = ParseAttributes(&Loc);
1521 ParamInfo.AddAttributes(AttrList, Loc);
1522 }
1523
Mike Stump98eb8a72009-02-04 22:31:32 +00001524 // Inform sema that we are starting a block.
1525 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001526 }
1527
Sebastian Redl1d922962008-12-13 15:32:12 +00001528
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001529 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001530 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001531 // Saw something like: ^expr
1532 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001533 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001534 return ExprError();
1535 }
Chris Lattner9af55002009-03-27 04:18:06 +00001536
1537 OwningStmtResult Stmt(ParseCompoundStatementBody());
1538 if (!Stmt.isInvalid())
1539 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1540 else
1541 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001542 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001543}