blob: 12ba001f0ea5c5bc0a5aa412501f73402e4d80f0 [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() {
279 OwningExprResult LHS(ParseCastExpression(false));
280 if (LHS.isInvalid()) return move(LHS);
281
Sebastian Redld8c4e152008-12-11 22:33:27 +0000282 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
286/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000287Parser::OwningExprResult
288Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000289 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
290 GreaterThanIsOperator,
291 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 SourceLocation ColonLoc;
293
294 while (1) {
295 // If this token has a lower precedence than we are allowed to parse (e.g.
296 // because we are called recursively, or because the token is not a binop),
297 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000298 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000299 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000300
301 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000302 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000306 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000308 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // Handle this production specially:
310 // logical-OR-expression '?' expression ':' conditional-expression
311 // In particular, the RHS of the '?' is 'expression', not
312 // 'logical-OR-expression' as we might expect.
313 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000314 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000315 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 } else {
317 // Special case handling of "X ? Y : Z" where Y is empty:
318 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000319 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 Diag(Tok, diag::ext_gnu_conditional_expr);
321 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000322
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000323 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000325 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000326 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // Eat the colon.
330 ColonLoc = ConsumeToken();
331 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000334 // ParseCastExpression works here because all RHS expressions in C have it
335 // as a prefix, at least. However, in C++, an assignment-expression could
336 // be a throw-expression, which is not a valid cast-expression.
337 // Therefore we need some special-casing here.
338 // Also note that the third operand of the conditional operator is
339 // an assignment-expression in C++.
340 OwningExprResult RHS(Actions);
341 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
342 RHS = ParseAssignmentExpression();
343 else
344 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000345 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000346 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000347
348 // Remember the precedence of this operator and get the precedence of the
349 // operator immediately to the right of the RHS.
350 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000351 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
352 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000353
354 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000355 bool isRightAssoc = ThisPrec == prec::Conditional ||
356 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000357
358 // Get the precedence of the operator to the right of the RHS. If it binds
359 // more tightly with RHS than we do, evaluate it completely first.
360 if (ThisPrec < NextTokPrec ||
361 (ThisPrec == NextTokPrec && isRightAssoc)) {
362 // If this is left-associative, only parse things on the RHS that bind
363 // more tightly than the current operator. If it is left-associative, it
364 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
365 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000366 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000367 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000368 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000369 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000370
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000371 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
372 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
374 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000375
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000376 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000377 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000378 if (TernaryMiddle.isInvalid()) {
379 // If we're using '>>' as an operator within a template
380 // argument list (in C++98), suggest the addition of
381 // parentheses so that the code remains well-formed in C++0x.
382 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
383 SuggestParentheses(OpToken.getLocation(),
384 diag::warn_cxx0x_right_shift_in_template_arg,
385 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
386 Actions.getExprRange(RHS.get()).getEnd()));
387
Sebastian Redleffa8d12008-12-10 00:02:53 +0000388 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000389 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000390 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000391 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000392 move(LHS), move(TernaryMiddle),
393 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000394 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
396}
397
398/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000399/// true, parse a unary-expression. isAddressOfOperand exists because an
400/// id-expression that is the operand of address-of gets special treatment
401/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000402///
403/// cast-expression: [C99 6.5.4]
404/// unary-expression
405/// '(' type-name ')' cast-expression
406///
407/// unary-expression: [C99 6.5.3]
408/// postfix-expression
409/// '++' unary-expression
410/// '--' unary-expression
411/// unary-operator cast-expression
412/// 'sizeof' unary-expression
413/// 'sizeof' '(' type-name ')'
414/// [GNU] '__alignof' unary-expression
415/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000416/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000418/// [C++] new-expression
419/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000420///
421/// unary-operator: one of
422/// '&' '*' '+' '-' '~' '!'
423/// [GNU] '__extension__' '__real' '__imag'
424///
425/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000426/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000427/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000428/// constant
429/// string-literal
430/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000431/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000432/// '(' expression ')'
433/// '__func__' [C99 6.4.2.2]
434/// [GNU] '__FUNCTION__'
435/// [GNU] '__PRETTY_FUNCTION__'
436/// [GNU] '(' compound-statement ')'
437/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
438/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
439/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
440/// assign-expr ')'
441/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000442/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000443/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000444/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000445/// [OBJC] '@protocol' '(' identifier ')'
446/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000447/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000448/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
449/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000450/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
451/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
452/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
453/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000454/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
455/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000456/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000457/// [G++] unary-type-trait '(' type-id ')'
458/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000459/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000460///
461/// constant: [C99 6.4.4]
462/// integer-constant
463/// floating-constant
464/// enumeration-constant -> identifier
465/// character-constant
466///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000467/// id-expression: [C++ 5.1]
468/// unqualified-id
469/// qualified-id [TODO]
470///
471/// unqualified-id: [C++ 5.1]
472/// identifier
473/// operator-function-id
474/// conversion-function-id [TODO]
475/// '~' class-name [TODO]
476/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000477///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000478/// new-expression: [C++ 5.3.4]
479/// '::'[opt] 'new' new-placement[opt] new-type-id
480/// new-initializer[opt]
481/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
482/// new-initializer[opt]
483///
484/// delete-expression: [C++ 5.3.5]
485/// '::'[opt] 'delete' cast-expression
486/// '::'[opt] 'delete' '[' ']' cast-expression
487///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000488/// [GNU] unary-type-trait:
489/// '__has_nothrow_assign' [TODO]
490/// '__has_nothrow_copy' [TODO]
491/// '__has_nothrow_constructor' [TODO]
492/// '__has_trivial_assign' [TODO]
493/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000494/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000495/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000496/// '__has_virtual_destructor' [TODO]
497/// '__is_abstract' [TODO]
498/// '__is_class'
499/// '__is_empty' [TODO]
500/// '__is_enum'
501/// '__is_pod'
502/// '__is_polymorphic'
503/// '__is_union'
504///
505/// [GNU] binary-type-trait:
506/// '__is_base_of' [TODO]
507///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000508Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
509 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000510 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 tok::TokenKind SavedKind = Tok.getKind();
512
513 // This handles all of cast-expression, unary-expression, postfix-expression,
514 // and primary-expression. We handle them together like this for efficiency
515 // and to simplify handling of an expression starting with a '(' token: which
516 // may be one of a parenthesized expression, cast-expression, compound literal
517 // expression, or statement expression.
518 //
519 // If the parsed tokens consist of a primary-expression, the cases below
520 // call ParsePostfixExpressionSuffix to handle the postfix expression
521 // suffixes. Cases that cannot be followed by postfix exprs should
522 // return without invoking ParsePostfixExpressionSuffix.
523 switch (SavedKind) {
524 case tok::l_paren: {
525 // If this expression is limited to being a unary-expression, the parent can
526 // not start a cast expression.
527 ParenParseOption ParenExprType =
528 isUnaryExpression ? CompoundLiteral : CastExpr;
529 TypeTy *CastTy;
530 SourceLocation LParenLoc = Tok.getLocation();
531 SourceLocation RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000532 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
533 CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000534 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535
536 switch (ParenExprType) {
537 case SimpleExpr: break; // Nothing else to do.
538 case CompoundStmt: break; // Nothing else to do.
539 case CompoundLiteral:
540 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
541 // postfix-expression exist, parse them now.
542 break;
543 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000544 // We have parsed the cast-expression and no postfix-expr pieces are
545 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000546 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000550 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 // primary-expression
554 case tok::numeric_constant:
555 // constant: integer-constant
556 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000557
Steve Narofff69936d2007-09-16 03:34:24 +0000558 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000562 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
564 case tok::kw_true:
565 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000566 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000567
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000568 case tok::kw_nullptr:
569 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
570
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000571 case tok::identifier: { // primary-expression: identifier
572 // unqualified-id: identifier
573 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000574 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000575 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000576 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000577 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
578 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000579 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000580 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000581
Steve Naroff61f72cb2009-03-09 21:12:44 +0000582 // Support 'Class.property' notation.
583 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
584 // 'super' (which is inappropriate here).
585 if (getLang().ObjC1 &&
586 Actions.getTypeName(*Tok.getIdentifierInfo(),
587 Tok.getLocation(), CurScope) &&
588 NextToken().is(tok::period)) {
589 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
590 SourceLocation IdentLoc = ConsumeToken();
591 SourceLocation DotLoc = ConsumeToken();
592
593 if (Tok.isNot(tok::identifier)) {
594 Diag(Tok, diag::err_expected_ident);
595 return ExprError();
596 }
597 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
598 SourceLocation PropertyLoc = ConsumeToken();
599
600 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
601 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000602 // These can be followed by postfix-expr pieces.
603 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000604 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 // Consume the identifier so that we can see if it is followed by a '('.
606 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
607 // need to know whether or not this identifier is a function designator or
608 // not.
609 IdentifierInfo &II = *Tok.getIdentifierInfo();
610 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000611 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000613 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
615 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000616 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 ConsumeToken();
618 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000619 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
621 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
622 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000623 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 ConsumeToken();
625 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000626 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 case tok::string_literal: // primary-expression: string-literal
628 case tok::wide_string_literal:
629 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000630 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000632 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 case tok::kw___builtin_va_arg:
634 case tok::kw___builtin_offsetof:
635 case tok::kw___builtin_choose_expr:
636 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000637 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000638 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000639 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000640 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 case tok::plusplus: // unary-expression: '++' unary-expression
642 case tok::minusminus: { // unary-expression: '--' unary-expression
643 SourceLocation SavedLoc = ConsumeToken();
644 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000645 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000646 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000647 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000649 case tok::amp: { // unary-expression: '&' cast-expression
650 // Special treatment because of member pointers
651 SourceLocation SavedLoc = ConsumeToken();
652 Res = ParseCastExpression(false, true);
653 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000654 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000655 return move(Res);
656 }
657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 case tok::star: // unary-expression: '*' cast-expression
659 case tok::plus: // unary-expression: '+' cast-expression
660 case tok::minus: // unary-expression: '-' cast-expression
661 case tok::tilde: // unary-expression: '~' cast-expression
662 case tok::exclaim: // unary-expression: '!' cast-expression
663 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000664 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 SourceLocation SavedLoc = ConsumeToken();
666 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000668 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000669 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000670 }
671
Chris Lattner35080842008-02-02 20:20:10 +0000672 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
673 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000674 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000675 SourceLocation SavedLoc = ConsumeToken();
676 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000677 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000678 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000679 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 }
681 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
682 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000683 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
685 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000686 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000687 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 case tok::ampamp: { // unary-expression: '&&' identifier
689 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000690 if (Tok.isNot(tok::identifier))
691 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000694 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 Tok.getIdentifierInfo());
696 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000697 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 }
699 case tok::kw_const_cast:
700 case tok::kw_dynamic_cast:
701 case tok::kw_reinterpret_cast:
702 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000703 Res = ParseCXXCasts();
704 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000705 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000706 case tok::kw_typeid:
707 Res = ParseCXXTypeid();
708 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000709 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000710 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000711 Res = ParseCXXThis();
712 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000713 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000714
715 case tok::kw_char:
716 case tok::kw_wchar_t:
717 case tok::kw_bool:
718 case tok::kw_short:
719 case tok::kw_int:
720 case tok::kw_long:
721 case tok::kw_signed:
722 case tok::kw_unsigned:
723 case tok::kw_float:
724 case tok::kw_double:
725 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000726 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000727 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000728 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000729 if (!getLang().CPlusPlus) {
730 Diag(Tok, diag::err_expected_expression);
731 return ExprError();
732 }
733
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000734 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
735 //
736 DeclSpec DS;
737 ParseCXXSimpleTypeSpecifier(DS);
738 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000739 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
740 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000741
742 Res = ParseCXXTypeConstructExpression(DS);
743 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000744 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000745 }
746
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000747 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
748 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
749 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000750 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000751 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000752
Chris Lattner74ba4102009-01-04 22:52:14 +0000753 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000754 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
755 // annotates the token, tail recurse.
756 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000757 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
758
Chris Lattner74ba4102009-01-04 22:52:14 +0000759 // ::new -> [C++] new-expression
760 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000761 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000762 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000763 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000764 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000765 return ParseCXXDeleteExpression(true, CCLoc);
766
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000767 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000768 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000769 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000770 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000771
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000772 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000773 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000774
775 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000776 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000777
Sebastian Redl64b45f72009-01-05 20:52:13 +0000778 case tok::kw___is_pod: // [GNU] unary-type-trait
779 case tok::kw___is_class:
780 case tok::kw___is_enum:
781 case tok::kw___is_union:
782 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000783 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000784 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000785 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000786 return ParseUnaryTypeTrait();
787
Chris Lattnerc97c2042007-10-03 22:03:06 +0000788 case tok::at: {
789 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000790 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000791 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000792 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000793 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000794 case tok::l_square:
795 // These can be followed by postfix-expr pieces.
796 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000797 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000798 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 default:
800 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000801 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 // unreachable.
805 abort();
806}
807
808/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
809/// is parsed, this method parses any suffixes that apply.
810///
811/// postfix-expression: [C99 6.5.2]
812/// primary-expression
813/// postfix-expression '[' expression ']'
814/// postfix-expression '(' argument-expression-list[opt] ')'
815/// postfix-expression '.' identifier
816/// postfix-expression '->' identifier
817/// postfix-expression '++'
818/// postfix-expression '--'
819/// '(' type-name ')' '{' initializer-list '}'
820/// '(' type-name ')' '{' initializer-list ',' '}'
821///
822/// argument-expression-list: [C99 6.5.2]
823/// argument-expression
824/// argument-expression-list ',' assignment-expression
825///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000826Parser::OwningExprResult
827Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 // Now that the primary-expression piece of the postfix-expression has been
829 // parsed, see if there are any postfix-expression pieces here.
830 SourceLocation Loc;
831 while (1) {
832 switch (Tok.getKind()) {
833 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000834 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
836 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000837 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000840
841 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000842 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
843 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000844 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000845 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000846
847 // Match the ']'.
848 MatchRHSPunctuation(tok::r_square, Loc);
849 break;
850 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000853 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000854 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000857
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000858 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000859 if (ParseExpressionList(ArgExprs, CommaLocs)) {
860 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000861 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 }
863 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000864
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000866 if (Tok.isNot(tok::r_paren)) {
867 MatchRHSPunctuation(tok::r_paren, Loc);
868 return ExprError();
869 }
870
871 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
873 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000874 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000875 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000876 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000878
879 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 break;
881 }
882 case tok::arrow: // postfix-expression: p-e '->' identifier
883 case tok::period: { // postfix-expression: p-e '.' identifier
884 tok::TokenKind OpKind = Tok.getKind();
885 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000886
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000887 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000889 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000891
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000892 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000893 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000894 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000895 *Tok.getIdentifierInfo(),
896 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000897 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 ConsumeToken();
899 break;
900 }
901 case tok::plusplus: // postfix-expression: postfix-expression '++'
902 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000903 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000904 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000905 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000906 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 ConsumeToken();
908 break;
909 }
910 }
911}
912
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000913/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
914/// we are at the start of an expression or a parenthesized type-id.
915/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
916/// (isCastExpr == false) or the type (isCastExpr == true).
917///
918/// unary-expression: [C99 6.5.3]
919/// 'sizeof' unary-expression
920/// 'sizeof' '(' type-name ')'
921/// [GNU] '__alignof' unary-expression
922/// [GNU] '__alignof' '(' type-name ')'
923/// [C++0x] 'alignof' '(' type-id ')'
924///
925/// [GNU] typeof-specifier:
926/// typeof ( expressions )
927/// typeof ( type-name )
928/// [GNU/C++] typeof unary-expression
929///
930Parser::OwningExprResult
931Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
932 bool &isCastExpr,
933 TypeTy *&CastTy,
934 SourceRange &CastRange) {
935
936 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
937 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
938 "Not a typeof/sizeof/alignof expression!");
939
940 OwningExprResult Operand(Actions);
941
942 // If the operand doesn't start with an '(', it must be an expression.
943 if (Tok.isNot(tok::l_paren)) {
944 isCastExpr = false;
945 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
946 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
947 return ExprError();
948 }
949 Operand = ParseCastExpression(true/*isUnaryExpression*/);
950
951 } else {
952 // If it starts with a '(', we know that it is either a parenthesized
953 // type-name, or it is a unary-expression that starts with a compound
954 // literal, or starts with a primary-expression that is a parenthesized
955 // expression.
956 ParenParseOption ExprType = CastExpr;
957 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000958 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
959 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000960 CastRange = SourceRange(LParenLoc, RParenLoc);
961
962 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
963 // a type.
964 if (ExprType == CastExpr) {
965 isCastExpr = true;
966 return ExprEmpty();
967 }
968
969 // If this is a parenthesized expression, it is the start of a
970 // unary-expression, but doesn't include any postfix pieces. Parse these
971 // now if present.
972 Operand = ParsePostfixExpressionSuffix(move(Operand));
973 }
974
975 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
976 isCastExpr = false;
977 return move(Operand);
978}
979
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
981/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
982/// unary-expression: [C99 6.5.3]
983/// 'sizeof' unary-expression
984/// 'sizeof' '(' type-name ')'
985/// [GNU] '__alignof' unary-expression
986/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000987/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000988Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000989 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
990 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000992 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 ConsumeToken();
994
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000995 bool isCastExpr;
996 TypeTy *CastTy;
997 SourceRange CastRange;
998 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
999 isCastExpr,
1000 CastTy,
1001 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001002
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001003 if (isCastExpr)
1004 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1005 OpTok.is(tok::kw_sizeof),
1006 /*isType=*/true, CastTy,
1007 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001010 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001011 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1012 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001013 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001014 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001015 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016}
1017
1018/// ParseBuiltinPrimaryExpression
1019///
1020/// primary-expression: [C99 6.5.1]
1021/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1022/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1023/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1024/// assign-expr ')'
1025/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1026///
1027/// [GNU] offsetof-member-designator:
1028/// [GNU] identifier
1029/// [GNU] offsetof-member-designator '.' identifier
1030/// [GNU] offsetof-member-designator '[' expression ']'
1031///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001032Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001033 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1035
1036 tok::TokenKind T = Tok.getKind();
1037 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1038
1039 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001040 if (Tok.isNot(tok::l_paren))
1041 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1042 << BuiltinII);
1043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 SourceLocation LParenLoc = ConsumeParen();
1045 // TODO: Build AST.
1046
1047 switch (T) {
1048 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001049 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001050 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001051 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001053 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 }
1055
1056 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001057 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
Douglas Gregor809070a2009-02-18 17:45:20 +00001059 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001061 if (Tok.isNot(tok::r_paren)) {
1062 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001063 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001064 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001065 if (Ty.isInvalid())
1066 Res = ExprError();
1067 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001068 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001070 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001071 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001072 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001073 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001074 if (Ty.isInvalid()) {
1075 SkipUntil(tok::r_paren);
1076 return ExprError();
1077 }
1078
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001080 return ExprError();
1081
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001083 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001084 Diag(Tok, diag::err_expected_ident);
1085 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001086 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001087 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001088
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001089 // Keep track of the various subcomponents we see.
1090 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001091
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001092 Comps.push_back(Action::OffsetOfComponent());
1093 Comps.back().isBrackets = false;
1094 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1095 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001096
Sebastian Redla55e52c2008-11-25 22:21:31 +00001097 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001099 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001101 Comps.push_back(Action::OffsetOfComponent());
1102 Comps.back().isBrackets = false;
1103 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001104
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001105 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001106 Diag(Tok, diag::err_expected_ident);
1107 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001108 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001109 }
1110 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1111 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001112
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001113 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001115 Comps.push_back(Action::OffsetOfComponent());
1116 Comps.back().isBrackets = true;
1117 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001119 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001121 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001123 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001124
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001125 Comps.back().LocEnd =
1126 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001127 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001128 if (Ty.isInvalid())
1129 Res = ExprError();
1130 else
1131 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1132 Ty.get(), &Comps[0],
1133 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001134 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001136 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001137 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 }
1139 }
1140 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001141 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001142 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001143 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001144 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001145 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001146 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001147 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001149 return ExprError();
1150
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001151 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001152 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001153 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001154 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001155 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001157 return ExprError();
1158
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001159 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001160 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001161 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001162 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001163 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001164 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001165 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001166 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001167 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001168 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1169 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001170 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001171 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001173 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001176 return ExprError();
1177
Douglas Gregor809070a2009-02-18 17:45:20 +00001178 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001179
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001180 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001181 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001182 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001183 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001184
1185 if (Ty1.isInvalid() || Ty2.isInvalid())
1186 Res = ExprError();
1187 else
1188 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1189 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001190 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001191 }
1192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 // These can be followed by postfix-expr pieces because they are
1194 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001195 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001196}
1197
1198/// ParseParenExpression - This parses the unit that starts with a '(' token,
1199/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001200/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1201/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001202///
1203/// primary-expression: [C99 6.5.1]
1204/// '(' expression ')'
1205/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1206/// postfix-expression: [C99 6.5.2]
1207/// '(' type-name ')' '{' initializer-list '}'
1208/// '(' type-name ')' '{' initializer-list ',' '}'
1209/// cast-expression: [C99 6.5.4]
1210/// '(' type-name ')' cast-expression
1211///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001212Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001213Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Sebastian Redld8c4e152008-12-11 22:33:27 +00001214 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001215 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001216 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001218 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001220
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001221 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001223 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001225
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001226 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001227 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001228 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001229
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001230 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001233 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001234
1235 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001236 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 RParenLoc = ConsumeParen();
1238 else
1239 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001240
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001241 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001243 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001244 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001245
Chris Lattner42ece642008-12-12 06:00:12 +00001246 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001247 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001248
1249 if (Ty.isInvalid())
1250 return ExprError();
1251
1252 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001253
1254 if (stopIfCastExpr) {
1255 // Note that this doesn't parse the subsequent cast-expression, it just
1256 // returns the parsed type to the callee.
1257 return OwningExprResult(Actions);
1258 }
1259
1260 // Parse the cast-expression that follows it next.
1261 // TODO: For cast expression with CastTy.
1262 Result = ParseCastExpression(false);
1263 if (!Result.isInvalid())
1264 Result = Actions.ActOnCastExpr(OpenLoc, CastTy, RParenLoc,move(Result));
1265 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001267
Chris Lattner42ece642008-12-12 06:00:12 +00001268 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1269 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 } else {
1271 Result = ParseExpression();
1272 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001273 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001274 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001276
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001278 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001280 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 }
Chris Lattner42ece642008-12-12 06:00:12 +00001282
1283 if (Tok.is(tok::r_paren))
1284 RParenLoc = ConsumeParen();
1285 else
1286 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001287
1288 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289}
1290
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001291/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1292/// and we are at the left brace.
1293///
1294/// postfix-expression: [C99 6.5.2]
1295/// '(' type-name ')' '{' initializer-list '}'
1296/// '(' type-name ')' '{' initializer-list ',' '}'
1297///
1298Parser::OwningExprResult
1299Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1300 SourceLocation LParenLoc,
1301 SourceLocation RParenLoc) {
1302 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1303 if (!getLang().C99) // Compound literals don't exist in C90.
1304 Diag(LParenLoc, diag::ext_c99_compound_literal);
1305 OwningExprResult Result = ParseInitializer();
1306 if (!Result.isInvalid() && Ty)
1307 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1308 return move(Result);
1309}
1310
Reid Spencer5f016e22007-07-11 17:01:13 +00001311/// ParseStringLiteralExpression - This handles the various token types that
1312/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1313/// translation phase #6].
1314///
1315/// primary-expression: [C99 6.5.1]
1316/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001317Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001319
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1321 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001322 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001323
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 do {
1325 StringToks.push_back(Tok);
1326 ConsumeStringToken();
1327 } while (isTokenStringLiteral());
1328
1329 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001330 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001332
1333/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1334///
1335/// argument-expression-list:
1336/// assignment-expression
1337/// argument-expression-list , assignment-expression
1338///
1339/// [C++] expression-list:
1340/// [C++] assignment-expression
1341/// [C++] expression-list , assignment-expression
1342///
1343bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1344 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001345 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001346 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001347 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001348
Sebastian Redleffa8d12008-12-10 00:02:53 +00001349 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001350
1351 if (Tok.isNot(tok::comma))
1352 return false;
1353 // Move to the next argument, remember where the comma was.
1354 CommaLocs.push_back(ConsumeToken());
1355 }
1356}
Steve Naroff296e8d52008-08-28 19:20:44 +00001357
Mike Stump98eb8a72009-02-04 22:31:32 +00001358/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1359///
1360/// [clang] block-id:
1361/// [clang] specifier-qualifier-list block-declarator
1362///
1363void Parser::ParseBlockId() {
1364 // Parse the specifier-qualifier-list piece.
1365 DeclSpec DS;
1366 ParseSpecifierQualifierList(DS);
1367
1368 // Parse the block-declarator.
1369 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1370 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001371
Mike Stump6c92fa72009-04-29 21:40:37 +00001372 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1373 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1374 SourceLocation());
1375
Mike Stump19c30c02009-04-29 19:03:13 +00001376 if (Tok.is(tok::kw___attribute)) {
1377 SourceLocation Loc;
1378 AttributeList *AttrList = ParseAttributes(&Loc);
1379 DeclaratorInfo.AddAttributes(AttrList, Loc);
1380 }
1381
Mike Stump98eb8a72009-02-04 22:31:32 +00001382 // Inform sema that we are starting a block.
1383 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1384}
1385
Steve Naroff296e8d52008-08-28 19:20:44 +00001386/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001387/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001388///
1389/// block-literal:
1390/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001391/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001392/// [clang] block-args:
1393/// [clang] '(' parameter-list ')'
1394///
Sebastian Redl1d922962008-12-13 15:32:12 +00001395Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001396 assert(Tok.is(tok::caret) && "block literal starts with ^");
1397 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001398
Chris Lattner6b91f002009-03-05 07:32:12 +00001399 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1400 "block literal parsing");
1401
Steve Naroff296e8d52008-08-28 19:20:44 +00001402 // Enter a scope to hold everything within the block. This includes the
1403 // argument decls, decls within the compound expression, etc. This also
1404 // allows determining whether a variable reference inside the block is
1405 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001406 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1407 Scope::BreakScope | Scope::ContinueScope |
1408 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001409
1410 // Inform sema that we are starting a block.
1411 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001412
Steve Naroff296e8d52008-08-28 19:20:44 +00001413 // Parse the return type if present.
1414 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001415 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001416 // FIXME: Since the return type isn't actually parsed, it can't be used to
1417 // fill ParamInfo with an initial valid range, so do it manually.
1418 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001419
Steve Naroff296e8d52008-08-28 19:20:44 +00001420 // If this block has arguments, parse them. There is no ambiguity here with
1421 // the expression case, because the expression case requires a parameter list.
1422 if (Tok.is(tok::l_paren)) {
1423 ParseParenDeclarator(ParamInfo);
1424 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001425 // SetIdentifier sets the source range end, but in this case we're past
1426 // that location.
1427 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001428 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001429 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001430 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001431 // If there was an error parsing the arguments, they may have
1432 // tried to use ^(x+y) which requires an argument list. Just
1433 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001434 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001435 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001436 }
Mike Stump19c30c02009-04-29 19:03:13 +00001437
1438 if (Tok.is(tok::kw___attribute)) {
1439 SourceLocation Loc;
1440 AttributeList *AttrList = ParseAttributes(&Loc);
1441 ParamInfo.AddAttributes(AttrList, Loc);
1442 }
1443
Mike Stump98eb8a72009-02-04 22:31:32 +00001444 // Inform sema that we are starting a block.
1445 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001446 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001447 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001448 } else {
1449 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001450 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1451 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001452 0, 0, 0,
1453 false, false, 0, 0,
1454 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001455 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001456
1457 if (Tok.is(tok::kw___attribute)) {
1458 SourceLocation Loc;
1459 AttributeList *AttrList = ParseAttributes(&Loc);
1460 ParamInfo.AddAttributes(AttrList, Loc);
1461 }
1462
Mike Stump98eb8a72009-02-04 22:31:32 +00001463 // Inform sema that we are starting a block.
1464 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001465 }
1466
Sebastian Redl1d922962008-12-13 15:32:12 +00001467
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001468 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001469 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001470 // Saw something like: ^expr
1471 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001472 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001473 return ExprError();
1474 }
Chris Lattner9af55002009-03-27 04:18:06 +00001475
1476 OwningStmtResult Stmt(ParseCompoundStatementBody());
1477 if (!Stmt.isInvalid())
1478 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1479 else
1480 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001481 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001482}