blob: 4a07d05650bd1c1ce1ec6893b02aa7b4cbc611e9 [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) [...].
282 EnterUnevaluatedOperand Unevaluated(Actions);
283
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000284 OwningExprResult LHS(ParseCastExpression(false));
285 if (LHS.isInvalid()) return move(LHS);
286
Sebastian Redld8c4e152008-12-11 22:33:27 +0000287 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
291/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000292Parser::OwningExprResult
293Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000294 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
295 GreaterThanIsOperator,
296 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 SourceLocation ColonLoc;
298
299 while (1) {
300 // If this token has a lower precedence than we are allowed to parse (e.g.
301 // because we are called recursively, or because the token is not a binop),
302 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000303 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000304 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000305
306 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000307 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000311 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000313 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Handle this production specially:
315 // logical-OR-expression '?' expression ':' conditional-expression
316 // In particular, the RHS of the '?' is 'expression', not
317 // 'logical-OR-expression' as we might expect.
318 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000319 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000320 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 } else {
322 // Special case handling of "X ? Y : Z" where Y is empty:
323 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000324 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 Diag(Tok, diag::ext_gnu_conditional_expr);
326 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000327
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000328 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000330 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000331 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Eat the colon.
335 ColonLoc = ConsumeToken();
336 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000337
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000339 // ParseCastExpression works here because all RHS expressions in C have it
340 // as a prefix, at least. However, in C++, an assignment-expression could
341 // be a throw-expression, which is not a valid cast-expression.
342 // Therefore we need some special-casing here.
343 // Also note that the third operand of the conditional operator is
344 // an assignment-expression in C++.
345 OwningExprResult RHS(Actions);
346 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
347 RHS = ParseAssignmentExpression();
348 else
349 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000350 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000351 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000352
353 // Remember the precedence of this operator and get the precedence of the
354 // operator immediately to the right of the RHS.
355 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000356 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
357 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358
359 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000360 bool isRightAssoc = ThisPrec == prec::Conditional ||
361 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000362
363 // Get the precedence of the operator to the right of the RHS. If it binds
364 // more tightly with RHS than we do, evaluate it completely first.
365 if (ThisPrec < NextTokPrec ||
366 (ThisPrec == NextTokPrec && isRightAssoc)) {
367 // If this is left-associative, only parse things on the RHS that bind
368 // more tightly than the current operator. If it is left-associative, it
369 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
370 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000371 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000372 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000373 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000374 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000376 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
377 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 }
379 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000380
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000381 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000382 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000383 if (TernaryMiddle.isInvalid()) {
384 // If we're using '>>' as an operator within a template
385 // argument list (in C++98), suggest the addition of
386 // parentheses so that the code remains well-formed in C++0x.
387 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
388 SuggestParentheses(OpToken.getLocation(),
389 diag::warn_cxx0x_right_shift_in_template_arg,
390 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
391 Actions.getExprRange(RHS.get()).getEnd()));
392
Sebastian Redleffa8d12008-12-10 00:02:53 +0000393 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000394 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000395 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000396 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000397 move(LHS), move(TernaryMiddle),
398 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000399 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
401}
402
403/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000404/// true, parse a unary-expression. isAddressOfOperand exists because an
405/// id-expression that is the operand of address-of gets special treatment
406/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000407///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000408Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
409 bool isAddressOfOperand) {
410 bool NotCastExpr;
411 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
412 isAddressOfOperand,
413 NotCastExpr);
414 if (NotCastExpr)
415 Diag(Tok, diag::err_expected_expression);
416 return move(Res);
417}
418
419/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
420/// true, parse a unary-expression. isAddressOfOperand exists because an
421/// id-expression that is the operand of address-of gets special treatment
422/// due to member pointers. NotCastExpr is set to true if the token is not the
423/// start of a cast-expression, and no diagnostic is emitted in this case.
424///
Reid Spencer5f016e22007-07-11 17:01:13 +0000425/// cast-expression: [C99 6.5.4]
426/// unary-expression
427/// '(' type-name ')' cast-expression
428///
429/// unary-expression: [C99 6.5.3]
430/// postfix-expression
431/// '++' unary-expression
432/// '--' unary-expression
433/// unary-operator cast-expression
434/// 'sizeof' unary-expression
435/// 'sizeof' '(' type-name ')'
436/// [GNU] '__alignof' unary-expression
437/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000438/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000439/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000440/// [C++] new-expression
441/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000442///
443/// unary-operator: one of
444/// '&' '*' '+' '-' '~' '!'
445/// [GNU] '__extension__' '__real' '__imag'
446///
447/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000448/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000449/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000450/// constant
451/// string-literal
452/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000453/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000454/// '(' expression ')'
455/// '__func__' [C99 6.4.2.2]
456/// [GNU] '__FUNCTION__'
457/// [GNU] '__PRETTY_FUNCTION__'
458/// [GNU] '(' compound-statement ')'
459/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
460/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
461/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
462/// assign-expr ')'
463/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000464/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000465/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000466/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000467/// [OBJC] '@protocol' '(' identifier ')'
468/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000469/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000470/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
471/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000472/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
473/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
474/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
475/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000476/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
477/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000478/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000479/// [G++] unary-type-trait '(' type-id ')'
480/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000481/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000482///
483/// constant: [C99 6.4.4]
484/// integer-constant
485/// floating-constant
486/// enumeration-constant -> identifier
487/// character-constant
488///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000489/// id-expression: [C++ 5.1]
490/// unqualified-id
491/// qualified-id [TODO]
492///
493/// unqualified-id: [C++ 5.1]
494/// identifier
495/// operator-function-id
496/// conversion-function-id [TODO]
497/// '~' class-name [TODO]
498/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000499///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000500/// new-expression: [C++ 5.3.4]
501/// '::'[opt] 'new' new-placement[opt] new-type-id
502/// new-initializer[opt]
503/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
504/// new-initializer[opt]
505///
506/// delete-expression: [C++ 5.3.5]
507/// '::'[opt] 'delete' cast-expression
508/// '::'[opt] 'delete' '[' ']' cast-expression
509///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000510/// [GNU] unary-type-trait:
511/// '__has_nothrow_assign' [TODO]
512/// '__has_nothrow_copy' [TODO]
513/// '__has_nothrow_constructor' [TODO]
514/// '__has_trivial_assign' [TODO]
515/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000516/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000517/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000518/// '__has_virtual_destructor' [TODO]
519/// '__is_abstract' [TODO]
520/// '__is_class'
521/// '__is_empty' [TODO]
522/// '__is_enum'
523/// '__is_pod'
524/// '__is_polymorphic'
525/// '__is_union'
526///
527/// [GNU] binary-type-trait:
528/// '__is_base_of' [TODO]
529///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000530Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000531 bool isAddressOfOperand,
532 bool &NotCastExpr) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000533 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000535 NotCastExpr = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000536
537 // This handles all of cast-expression, unary-expression, postfix-expression,
538 // and primary-expression. We handle them together like this for efficiency
539 // and to simplify handling of an expression starting with a '(' token: which
540 // may be one of a parenthesized expression, cast-expression, compound literal
541 // expression, or statement expression.
542 //
543 // If the parsed tokens consist of a primary-expression, the cases below
544 // call ParsePostfixExpressionSuffix to handle the postfix expression
545 // suffixes. Cases that cannot be followed by postfix exprs should
546 // return without invoking ParsePostfixExpressionSuffix.
547 switch (SavedKind) {
548 case tok::l_paren: {
549 // If this expression is limited to being a unary-expression, the parent can
550 // not start a cast expression.
551 ParenParseOption ParenExprType =
552 isUnaryExpression ? CompoundLiteral : CastExpr;
553 TypeTy *CastTy;
554 SourceLocation LParenLoc = Tok.getLocation();
555 SourceLocation RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000556 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
557 CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000558 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559
560 switch (ParenExprType) {
561 case SimpleExpr: break; // Nothing else to do.
562 case CompoundStmt: break; // Nothing else to do.
563 case CompoundLiteral:
564 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
565 // postfix-expression exist, parse them now.
566 break;
567 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000568 // We have parsed the cast-expression and no postfix-expr pieces are
569 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000570 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000572
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000574 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000576
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 // primary-expression
578 case tok::numeric_constant:
579 // constant: integer-constant
580 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000581
Steve Narofff69936d2007-09-16 03:34:24 +0000582 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000586 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000587
588 case tok::kw_true:
589 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000590 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000591
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000592 case tok::kw_nullptr:
593 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
594
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000595 case tok::identifier: { // primary-expression: identifier
596 // unqualified-id: identifier
597 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000598 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000599 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000600 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000601 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
602 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000603 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000604 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000605
Steve Naroff61f72cb2009-03-09 21:12:44 +0000606 // Support 'Class.property' notation.
607 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
608 // 'super' (which is inappropriate here).
609 if (getLang().ObjC1 &&
610 Actions.getTypeName(*Tok.getIdentifierInfo(),
611 Tok.getLocation(), CurScope) &&
612 NextToken().is(tok::period)) {
613 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
614 SourceLocation IdentLoc = ConsumeToken();
615 SourceLocation DotLoc = ConsumeToken();
616
617 if (Tok.isNot(tok::identifier)) {
618 Diag(Tok, diag::err_expected_ident);
619 return ExprError();
620 }
621 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
622 SourceLocation PropertyLoc = ConsumeToken();
623
624 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
625 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000626 // These can be followed by postfix-expr pieces.
627 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000628 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 // Consume the identifier so that we can see if it is followed by a '('.
630 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
631 // need to know whether or not this identifier is a function designator or
632 // not.
633 IdentifierInfo &II = *Tok.getIdentifierInfo();
634 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000635 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000637 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 }
639 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000640 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 ConsumeToken();
642 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000643 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
645 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
646 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000647 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 ConsumeToken();
649 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000650 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 case tok::string_literal: // primary-expression: string-literal
652 case tok::wide_string_literal:
653 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000654 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000656 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 case tok::kw___builtin_va_arg:
658 case tok::kw___builtin_offsetof:
659 case tok::kw___builtin_choose_expr:
660 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000661 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000662 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000663 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000664 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 case tok::plusplus: // unary-expression: '++' unary-expression
666 case tok::minusminus: { // unary-expression: '--' unary-expression
667 SourceLocation SavedLoc = ConsumeToken();
668 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000669 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000670 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000671 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000673 case tok::amp: { // unary-expression: '&' cast-expression
674 // Special treatment because of member pointers
675 SourceLocation SavedLoc = ConsumeToken();
676 Res = ParseCastExpression(false, true);
677 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000678 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000679 return move(Res);
680 }
681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 case tok::star: // unary-expression: '*' cast-expression
683 case tok::plus: // unary-expression: '+' cast-expression
684 case tok::minus: // unary-expression: '-' cast-expression
685 case tok::tilde: // unary-expression: '~' cast-expression
686 case tok::exclaim: // unary-expression: '!' cast-expression
687 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000688 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 SourceLocation SavedLoc = ConsumeToken();
690 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000691 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000692 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000693 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000694 }
695
Chris Lattner35080842008-02-02 20:20:10 +0000696 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
697 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000698 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000699 SourceLocation SavedLoc = ConsumeToken();
700 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000701 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000702 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000703 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 }
705 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
706 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000707 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
709 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000710 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000711 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 case tok::ampamp: { // unary-expression: '&&' identifier
713 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000714 if (Tok.isNot(tok::identifier))
715 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000716
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000718 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 Tok.getIdentifierInfo());
720 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000721 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 }
723 case tok::kw_const_cast:
724 case tok::kw_dynamic_cast:
725 case tok::kw_reinterpret_cast:
726 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000727 Res = ParseCXXCasts();
728 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000729 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000730 case tok::kw_typeid:
731 Res = ParseCXXTypeid();
732 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000733 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000734 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000735 Res = ParseCXXThis();
736 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000737 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000738
739 case tok::kw_char:
740 case tok::kw_wchar_t:
741 case tok::kw_bool:
742 case tok::kw_short:
743 case tok::kw_int:
744 case tok::kw_long:
745 case tok::kw_signed:
746 case tok::kw_unsigned:
747 case tok::kw_float:
748 case tok::kw_double:
749 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000750 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000751 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000752 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000753 if (!getLang().CPlusPlus) {
754 Diag(Tok, diag::err_expected_expression);
755 return ExprError();
756 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000757
758 if (SavedKind == tok::kw_typename) {
759 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
760 if (!TryAnnotateTypeOrScopeToken())
761 return ExprError();
762 }
763
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000764 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
765 //
766 DeclSpec DS;
767 ParseCXXSimpleTypeSpecifier(DS);
768 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000769 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
770 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000771
772 Res = ParseCXXTypeConstructExpression(DS);
773 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000774 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000775 }
776
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000777 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
778 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
779 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000780 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000781 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000782
Chris Lattner74ba4102009-01-04 22:52:14 +0000783 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000784 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
785 // annotates the token, tail recurse.
786 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000787 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
788
Chris Lattner74ba4102009-01-04 22:52:14 +0000789 // ::new -> [C++] new-expression
790 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000791 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000792 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000793 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000794 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000795 return ParseCXXDeleteExpression(true, CCLoc);
796
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000797 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000798 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000799 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000800 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000801
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000802 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000803 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000804
805 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000806 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000807
Sebastian Redl64b45f72009-01-05 20:52:13 +0000808 case tok::kw___is_pod: // [GNU] unary-type-trait
809 case tok::kw___is_class:
810 case tok::kw___is_enum:
811 case tok::kw___is_union:
812 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000813 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000814 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000815 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000816 return ParseUnaryTypeTrait();
817
Chris Lattnerc97c2042007-10-03 22:03:06 +0000818 case tok::at: {
819 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000820 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000821 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000822 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000823 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000824 case tok::l_square:
825 // These can be followed by postfix-expr pieces.
826 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000827 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000828 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000830 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000831 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 // unreachable.
835 abort();
836}
837
838/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
839/// is parsed, this method parses any suffixes that apply.
840///
841/// postfix-expression: [C99 6.5.2]
842/// primary-expression
843/// postfix-expression '[' expression ']'
844/// postfix-expression '(' argument-expression-list[opt] ')'
845/// postfix-expression '.' identifier
846/// postfix-expression '->' identifier
847/// postfix-expression '++'
848/// postfix-expression '--'
849/// '(' type-name ')' '{' initializer-list '}'
850/// '(' type-name ')' '{' initializer-list ',' '}'
851///
852/// argument-expression-list: [C99 6.5.2]
853/// argument-expression
854/// argument-expression-list ',' assignment-expression
855///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000856Parser::OwningExprResult
857Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // Now that the primary-expression piece of the postfix-expression has been
859 // parsed, see if there are any postfix-expression pieces here.
860 SourceLocation Loc;
861 while (1) {
862 switch (Tok.getKind()) {
863 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000864 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
866 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000867 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000868
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000870
871 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000872 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
873 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000874 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000875 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000876
877 // Match the ']'.
878 MatchRHSPunctuation(tok::r_square, Loc);
879 break;
880 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000883 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000884 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000887
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000888 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000889 if (ParseExpressionList(ArgExprs, CommaLocs)) {
890 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000891 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 }
893 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000896 if (Tok.isNot(tok::r_paren)) {
897 MatchRHSPunctuation(tok::r_paren, Loc);
898 return ExprError();
899 }
900
901 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
903 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000904 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000905 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000906 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000908
909 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 break;
911 }
912 case tok::arrow: // postfix-expression: p-e '->' identifier
913 case tok::period: { // postfix-expression: p-e '.' identifier
914 tok::TokenKind OpKind = Tok.getKind();
915 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000916
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000917 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000919 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000921
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000922 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000923 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000924 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000925 *Tok.getIdentifierInfo(),
926 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000927 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 ConsumeToken();
929 break;
930 }
931 case tok::plusplus: // postfix-expression: postfix-expression '++'
932 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000933 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000934 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000935 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000936 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 ConsumeToken();
938 break;
939 }
940 }
941}
942
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000943/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
944/// we are at the start of an expression or a parenthesized type-id.
945/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
946/// (isCastExpr == false) or the type (isCastExpr == true).
947///
948/// unary-expression: [C99 6.5.3]
949/// 'sizeof' unary-expression
950/// 'sizeof' '(' type-name ')'
951/// [GNU] '__alignof' unary-expression
952/// [GNU] '__alignof' '(' type-name ')'
953/// [C++0x] 'alignof' '(' type-id ')'
954///
955/// [GNU] typeof-specifier:
956/// typeof ( expressions )
957/// typeof ( type-name )
958/// [GNU/C++] typeof unary-expression
959///
960Parser::OwningExprResult
961Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
962 bool &isCastExpr,
963 TypeTy *&CastTy,
964 SourceRange &CastRange) {
965
966 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
967 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
968 "Not a typeof/sizeof/alignof expression!");
969
970 OwningExprResult Operand(Actions);
971
972 // If the operand doesn't start with an '(', it must be an expression.
973 if (Tok.isNot(tok::l_paren)) {
974 isCastExpr = false;
975 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
976 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
977 return ExprError();
978 }
Douglas Gregore0762c92009-06-19 23:52:42 +0000979
980 // C++0x [expr.sizeof]p1:
981 // [...] The operand is either an expression, which is an unevaluated
982 // operand (Clause 5) [...]
983 //
984 // The GNU typeof and alignof extensions also behave as unevaluated
985 // operands.
986 EnterUnevaluatedOperand Unevaluated(Actions);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000987 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000988 } else {
989 // If it starts with a '(', we know that it is either a parenthesized
990 // type-name, or it is a unary-expression that starts with a compound
991 // literal, or starts with a primary-expression that is a parenthesized
992 // expression.
993 ParenParseOption ExprType = CastExpr;
994 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Douglas Gregore0762c92009-06-19 23:52:42 +0000995
996 // C++0x [expr.sizeof]p1:
997 // [...] The operand is either an expression, which is an unevaluated
998 // operand (Clause 5) [...]
999 //
1000 // The GNU typeof and alignof extensions also behave as unevaluated
1001 // operands.
1002 EnterUnevaluatedOperand Unevaluated(Actions);
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001003 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1004 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001005 CastRange = SourceRange(LParenLoc, RParenLoc);
1006
1007 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1008 // a type.
1009 if (ExprType == CastExpr) {
1010 isCastExpr = true;
1011 return ExprEmpty();
1012 }
1013
1014 // If this is a parenthesized expression, it is the start of a
1015 // unary-expression, but doesn't include any postfix pieces. Parse these
1016 // now if present.
1017 Operand = ParsePostfixExpressionSuffix(move(Operand));
1018 }
1019
1020 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1021 isCastExpr = false;
1022 return move(Operand);
1023}
1024
Reid Spencer5f016e22007-07-11 17:01:13 +00001025
1026/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1027/// unary-expression: [C99 6.5.3]
1028/// 'sizeof' unary-expression
1029/// 'sizeof' '(' type-name ')'
1030/// [GNU] '__alignof' unary-expression
1031/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001032/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +00001033Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001034 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1035 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001037 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 ConsumeToken();
1039
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001040 bool isCastExpr;
1041 TypeTy *CastTy;
1042 SourceRange CastRange;
1043 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1044 isCastExpr,
1045 CastTy,
1046 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001047
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001048 if (isCastExpr)
1049 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1050 OpTok.is(tok::kw_sizeof),
1051 /*isType=*/true, CastTy,
1052 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001055 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001056 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1057 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001058 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001059 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001060 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001061}
1062
1063/// ParseBuiltinPrimaryExpression
1064///
1065/// primary-expression: [C99 6.5.1]
1066/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1067/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1068/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1069/// assign-expr ')'
1070/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1071///
1072/// [GNU] offsetof-member-designator:
1073/// [GNU] identifier
1074/// [GNU] offsetof-member-designator '.' identifier
1075/// [GNU] offsetof-member-designator '[' expression ']'
1076///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001077Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001078 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1080
1081 tok::TokenKind T = Tok.getKind();
1082 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1083
1084 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001085 if (Tok.isNot(tok::l_paren))
1086 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1087 << BuiltinII);
1088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 SourceLocation LParenLoc = ConsumeParen();
1090 // TODO: Build AST.
1091
1092 switch (T) {
1093 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001094 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001095 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001096 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001098 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 }
1100
1101 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001102 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001103
Douglas Gregor809070a2009-02-18 17:45:20 +00001104 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001105
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001106 if (Tok.isNot(tok::r_paren)) {
1107 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001108 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001109 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001110 if (Ty.isInvalid())
1111 Res = ExprError();
1112 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001113 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001115 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001116 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001117 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001118 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001119 if (Ty.isInvalid()) {
1120 SkipUntil(tok::r_paren);
1121 return ExprError();
1122 }
1123
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001125 return ExprError();
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001128 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001129 Diag(Tok, diag::err_expected_ident);
1130 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001131 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001132 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001133
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001134 // Keep track of the various subcomponents we see.
1135 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001136
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001137 Comps.push_back(Action::OffsetOfComponent());
1138 Comps.back().isBrackets = false;
1139 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1140 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001141
Sebastian Redla55e52c2008-11-25 22:21:31 +00001142 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001144 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001146 Comps.push_back(Action::OffsetOfComponent());
1147 Comps.back().isBrackets = false;
1148 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001149
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001150 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001151 Diag(Tok, diag::err_expected_ident);
1152 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001153 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001154 }
1155 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1156 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001157
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001158 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001160 Comps.push_back(Action::OffsetOfComponent());
1161 Comps.back().isBrackets = true;
1162 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001164 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001166 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001168 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001169
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001170 Comps.back().LocEnd =
1171 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001172 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001173 if (Ty.isInvalid())
1174 Res = ExprError();
1175 else
1176 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1177 Ty.get(), &Comps[0],
1178 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001179 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001181 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001182 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
1184 }
1185 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001186 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001187 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001188 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001189 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001190 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001191 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001192 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001194 return ExprError();
1195
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001196 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001197 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001198 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001199 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001200 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001202 return ExprError();
1203
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001204 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001205 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001206 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001207 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001208 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001209 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001210 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001211 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001212 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001213 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1214 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001215 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001216 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001218 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001221 return ExprError();
1222
Douglas Gregor809070a2009-02-18 17:45:20 +00001223 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001224
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001225 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001226 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001227 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001228 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001229
1230 if (Ty1.isInvalid() || Ty2.isInvalid())
1231 Res = ExprError();
1232 else
1233 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1234 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001235 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001236 }
1237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 // These can be followed by postfix-expr pieces because they are
1239 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001240 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001241}
1242
1243/// ParseParenExpression - This parses the unit that starts with a '(' token,
1244/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001245/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1246/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001247///
1248/// primary-expression: [C99 6.5.1]
1249/// '(' expression ')'
1250/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1251/// postfix-expression: [C99 6.5.2]
1252/// '(' type-name ')' '{' initializer-list '}'
1253/// '(' type-name ')' '{' initializer-list ',' '}'
1254/// cast-expression: [C99 6.5.4]
1255/// '(' type-name ')' cast-expression
1256///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001257Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001258Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Sebastian Redld8c4e152008-12-11 22:33:27 +00001259 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001260 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001261 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001263 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001264 bool isAmbiguousTypeId;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001266
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001267 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001269 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001271
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001272 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001273 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001274 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001275
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001276 } else if (ExprType >= CompoundLiteral &&
1277 isTypeIdInParens(isAmbiguousTypeId)) {
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001278
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // Otherwise, this is a compound literal expression or cast expression.
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001280
1281 // In C++, if the type-id is ambiguous we disambiguate based on context.
1282 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1283 // in which case we should treat it as type-id.
1284 // if stopIfCastExpr is false, we need to determine the context past the
1285 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1286 if (isAmbiguousTypeId && !stopIfCastExpr)
1287 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1288 OpenLoc, RParenLoc);
1289
Douglas Gregor809070a2009-02-18 17:45:20 +00001290 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001291
1292 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001293 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 RParenLoc = ConsumeParen();
1295 else
1296 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001297
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001298 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001300 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001301 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001302
Chris Lattner42ece642008-12-12 06:00:12 +00001303 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001304 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001305
1306 if (Ty.isInvalid())
1307 return ExprError();
1308
1309 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001310
1311 if (stopIfCastExpr) {
1312 // Note that this doesn't parse the subsequent cast-expression, it just
1313 // returns the parsed type to the callee.
1314 return OwningExprResult(Actions);
1315 }
1316
1317 // Parse the cast-expression that follows it next.
1318 // TODO: For cast expression with CastTy.
1319 Result = ParseCastExpression(false);
1320 if (!Result.isInvalid())
1321 Result = Actions.ActOnCastExpr(OpenLoc, CastTy, RParenLoc,move(Result));
1322 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001324
Chris Lattner42ece642008-12-12 06:00:12 +00001325 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1326 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 } else {
1328 Result = ParseExpression();
1329 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001330 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001331 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001332 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001333
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001335 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001337 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 }
Chris Lattner42ece642008-12-12 06:00:12 +00001339
1340 if (Tok.is(tok::r_paren))
1341 RParenLoc = ConsumeParen();
1342 else
1343 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001344
1345 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346}
1347
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001348/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1349/// and we are at the left brace.
1350///
1351/// postfix-expression: [C99 6.5.2]
1352/// '(' type-name ')' '{' initializer-list '}'
1353/// '(' type-name ')' '{' initializer-list ',' '}'
1354///
1355Parser::OwningExprResult
1356Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1357 SourceLocation LParenLoc,
1358 SourceLocation RParenLoc) {
1359 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1360 if (!getLang().C99) // Compound literals don't exist in C90.
1361 Diag(LParenLoc, diag::ext_c99_compound_literal);
1362 OwningExprResult Result = ParseInitializer();
1363 if (!Result.isInvalid() && Ty)
1364 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1365 return move(Result);
1366}
1367
Reid Spencer5f016e22007-07-11 17:01:13 +00001368/// ParseStringLiteralExpression - This handles the various token types that
1369/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1370/// translation phase #6].
1371///
1372/// primary-expression: [C99 6.5.1]
1373/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001374Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001376
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1378 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001379 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001380
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 do {
1382 StringToks.push_back(Tok);
1383 ConsumeStringToken();
1384 } while (isTokenStringLiteral());
1385
1386 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001387 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001388}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001389
1390/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1391///
1392/// argument-expression-list:
1393/// assignment-expression
1394/// argument-expression-list , assignment-expression
1395///
1396/// [C++] expression-list:
1397/// [C++] assignment-expression
1398/// [C++] expression-list , assignment-expression
1399///
1400bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1401 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001402 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001404 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001405
Sebastian Redleffa8d12008-12-10 00:02:53 +00001406 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001407
1408 if (Tok.isNot(tok::comma))
1409 return false;
1410 // Move to the next argument, remember where the comma was.
1411 CommaLocs.push_back(ConsumeToken());
1412 }
1413}
Steve Naroff296e8d52008-08-28 19:20:44 +00001414
Mike Stump98eb8a72009-02-04 22:31:32 +00001415/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1416///
1417/// [clang] block-id:
1418/// [clang] specifier-qualifier-list block-declarator
1419///
1420void Parser::ParseBlockId() {
1421 // Parse the specifier-qualifier-list piece.
1422 DeclSpec DS;
1423 ParseSpecifierQualifierList(DS);
1424
1425 // Parse the block-declarator.
1426 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1427 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001428
Mike Stump6c92fa72009-04-29 21:40:37 +00001429 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1430 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1431 SourceLocation());
1432
Mike Stump19c30c02009-04-29 19:03:13 +00001433 if (Tok.is(tok::kw___attribute)) {
1434 SourceLocation Loc;
1435 AttributeList *AttrList = ParseAttributes(&Loc);
1436 DeclaratorInfo.AddAttributes(AttrList, Loc);
1437 }
1438
Mike Stump98eb8a72009-02-04 22:31:32 +00001439 // Inform sema that we are starting a block.
1440 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1441}
1442
Steve Naroff296e8d52008-08-28 19:20:44 +00001443/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001444/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001445///
1446/// block-literal:
1447/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001448/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001449/// [clang] block-args:
1450/// [clang] '(' parameter-list ')'
1451///
Sebastian Redl1d922962008-12-13 15:32:12 +00001452Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001453 assert(Tok.is(tok::caret) && "block literal starts with ^");
1454 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001455
Chris Lattner6b91f002009-03-05 07:32:12 +00001456 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1457 "block literal parsing");
1458
Steve Naroff296e8d52008-08-28 19:20:44 +00001459 // Enter a scope to hold everything within the block. This includes the
1460 // argument decls, decls within the compound expression, etc. This also
1461 // allows determining whether a variable reference inside the block is
1462 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001463 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1464 Scope::BreakScope | Scope::ContinueScope |
1465 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001466
1467 // Inform sema that we are starting a block.
1468 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001469
Steve Naroff296e8d52008-08-28 19:20:44 +00001470 // Parse the return type if present.
1471 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001472 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001473 // FIXME: Since the return type isn't actually parsed, it can't be used to
1474 // fill ParamInfo with an initial valid range, so do it manually.
1475 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001476
Steve Naroff296e8d52008-08-28 19:20:44 +00001477 // If this block has arguments, parse them. There is no ambiguity here with
1478 // the expression case, because the expression case requires a parameter list.
1479 if (Tok.is(tok::l_paren)) {
1480 ParseParenDeclarator(ParamInfo);
1481 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001482 // SetIdentifier sets the source range end, but in this case we're past
1483 // that location.
1484 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001485 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001486 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001487 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001488 // If there was an error parsing the arguments, they may have
1489 // tried to use ^(x+y) which requires an argument list. Just
1490 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001491 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001492 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001493 }
Mike Stump19c30c02009-04-29 19:03:13 +00001494
1495 if (Tok.is(tok::kw___attribute)) {
1496 SourceLocation Loc;
1497 AttributeList *AttrList = ParseAttributes(&Loc);
1498 ParamInfo.AddAttributes(AttrList, Loc);
1499 }
1500
Mike Stump98eb8a72009-02-04 22:31:32 +00001501 // Inform sema that we are starting a block.
1502 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001503 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001504 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001505 } else {
1506 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001507 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1508 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001509 0, 0, 0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00001510 false, SourceLocation(),
1511 false, 0, 0, 0,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001512 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001513 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001514
1515 if (Tok.is(tok::kw___attribute)) {
1516 SourceLocation Loc;
1517 AttributeList *AttrList = ParseAttributes(&Loc);
1518 ParamInfo.AddAttributes(AttrList, Loc);
1519 }
1520
Mike Stump98eb8a72009-02-04 22:31:32 +00001521 // Inform sema that we are starting a block.
1522 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001523 }
1524
Sebastian Redl1d922962008-12-13 15:32:12 +00001525
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001526 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001527 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001528 // Saw something like: ^expr
1529 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001530 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001531 return ExprError();
1532 }
Chris Lattner9af55002009-03-27 04:18:06 +00001533
1534 OwningStmtResult Stmt(ParseCompoundStatementBody());
1535 if (!Stmt.isInvalid())
1536 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1537 else
1538 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001539 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001540}