blob: 8b8d4e1924936eda74972af64cc746bc2b5a0382 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Narofffd5b19d2008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnere533c7d2009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Chris Lattner4b009652007-07-25 00:24:17 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor8e458f42009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorf2d87392009-02-25 23:02:36 +000059 bool GreaterThanIsOperator,
60 bool CPlusPlus0x) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 switch (Kind) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000062 case tok::greater:
Douglas Gregorf2d87392009-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 Gregor8e458f42009-02-09 18:46:07 +000067 if (GreaterThanIsOperator)
68 return prec::Relational;
69 return prec::Unknown;
70
Douglas Gregorf2d87392009-02-25 23:02:36 +000071 case tok::greatergreater:
72 // C++0x [temp.names]p3:
73 //
74 // [...] Similarly, the first non-nested >> is treated as two
75 // consecutive but distinct > tokens, the first of which is
76 // taken as the end of the template-argument-list and completes
77 // the template-id. [...]
78 if (GreaterThanIsOperator || !CPlusPlus0x)
79 return prec::Shift;
80 return prec::Unknown;
81
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor8e458f42009-02-09 18:46:07 +0000105 case tok::greaterequal: return prec::Relational;
Douglas Gregorf2d87392009-02-25 23:02:36 +0000106 case tok::lessless: return prec::Shift;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-02-07 00:15:38 +0000112 case tok::periodstar:
113 case tok::arrowstar: return prec::PointerToMember;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-02-07 00:15:38 +0000130/// pm-expression: [C++ 5.5]
131/// cast-expression
132/// pm-expression '.*' cast-expression
133/// pm-expression '->*' cast-expression
134///
Chris Lattner4b009652007-07-25 00:24:17 +0000135/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl95216a62009-02-07 00:15:38 +0000136/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbd261962009-04-16 17:51:27 +0000188/// [C++] the third operand is an assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000189///
190/// assignment-expression: [C99 6.5.16]
191/// conditional-expression
192/// unary-expression assignment-operator assignment-expression
Chris Lattnera7447ba2008-02-26 00:51:44 +0000193/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000194///
195/// assignment-operator: one of
196/// = *= /= %= += -= <<= >>= &= ^= |=
197///
198/// expression: [C99 6.5.17]
199/// assignment-expression
200/// expression ',' assignment-expression
201///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000202Parser::OwningExprResult Parser::ParseExpression() {
Mike Stumpf3c0e8c2009-05-15 21:47:08 +0000203 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl14ca7412008-12-11 21:36:32 +0000204 if (LHS.isInvalid()) return move(LHS);
205
Sebastian Redla6817a02008-12-11 22:33:27 +0000206 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000207}
208
Fariborz Jahanian64b864e2007-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 Lattnerb82d6ef2007-10-03 22:03:06 +0000211/// routine is necessary to disambiguate @try-statement from,
212/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000213///
Sebastian Redla6817a02008-12-11 22:33:27 +0000214Parser::OwningExprResult
215Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redla2deb432008-12-13 15:32:12 +0000216 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000217 if (LHS.isInvalid()) return move(LHS);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000218
Sebastian Redla6817a02008-12-11 22:33:27 +0000219 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000220}
221
Eli Friedmanc4772072009-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 Friedman7bffa932009-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 Friedmanc4772072009-01-27 08:43:38 +0000235
236 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl81db6682009-02-05 15:02:23 +0000237 move(LHS));
Eli Friedmanc4772072009-01-27 08:43:38 +0000238 if (LHS.isInvalid()) return move(LHS);
239
240 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
241}
242
Chris Lattner4b009652007-07-25 00:24:17 +0000243/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
244///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000245Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000246 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000247 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000248
Sebastian Redl14ca7412008-12-11 21:36:32 +0000249 OwningExprResult LHS(ParseCastExpression(false));
250 if (LHS.isInvalid()) return move(LHS);
251
Sebastian Redla6817a02008-12-11 22:33:27 +0000252 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000253}
254
Chris Lattnerbfcf4772008-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 Redla2deb432008-12-13 15:32:12 +0000263Parser::OwningExprResult
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000264Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000265 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000266 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-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 Redl14ca7412008-12-11 21:36:32 +0000272 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redla2deb432008-12-13 15:32:12 +0000273 if (R.isInvalid()) return move(R);
274 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000275}
276
277
Sebastian Redl14ca7412008-12-11 21:36:32 +0000278Parser::OwningExprResult Parser::ParseConstantExpression() {
279 OwningExprResult LHS(ParseCastExpression(false));
280 if (LHS.isInvalid()) return move(LHS);
281
Sebastian Redla6817a02008-12-11 22:33:27 +0000282 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000283}
284
Chris Lattner4b009652007-07-25 00:24:17 +0000285/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
286/// LHS and has a precedence of at least MinPrec.
Sebastian Redla6817a02008-12-11 22:33:27 +0000287Parser::OwningExprResult
288Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregorf2d87392009-02-25 23:02:36 +0000289 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
290 GreaterThanIsOperator,
291 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000298 if (NextTokPrec < MinPrec)
Sebastian Redla6817a02008-12-11 22:33:27 +0000299 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000300
301 // Consume the operator, saving the operator token for error reporting.
302 Token OpToken = Tok;
303 ConsumeToken();
Sebastian Redl95216a62009-02-07 00:15:38 +0000304
Chris Lattner4b009652007-07-25 00:24:17 +0000305 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000306 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000307 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000308 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000314 if (TernaryMiddle.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000315 return move(TernaryMiddle);
Chris Lattner4b009652007-07-25 00:24:17 +0000316 } else {
317 // Special case handling of "X ? Y : Z" where Y is empty:
318 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000319 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000320 Diag(Tok, diag::ext_gnu_conditional_expr);
321 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000322
Chris Lattner4d7d2342007-10-09 17:41:39 +0000323 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000324 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000325 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redla6817a02008-12-11 22:33:27 +0000326 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000327 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000328
Chris Lattner4b009652007-07-25 00:24:17 +0000329 // Eat the colon.
330 ColonLoc = ConsumeToken();
331 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000332
Chris Lattner4b009652007-07-25 00:24:17 +0000333 // Parse another leaf here for the RHS of the operator.
Sebastian Redlbd261962009-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 Redlbb4dae72008-12-09 13:15:23 +0000345 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000346 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorf2d87392009-02-25 23:02:36 +0000351 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
352 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +0000353
354 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000355 bool isRightAssoc = ThisPrec == prec::Conditional ||
356 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl6008ac32008-11-25 22:21:31 +0000366 // The function takes ownership of the RHS.
Sebastian Redla6817a02008-12-11 22:33:27 +0000367 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000368 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000369 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000370
Douglas Gregorf2d87392009-02-25 23:02:36 +0000371 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
372 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +0000373 }
374 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000375
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000376 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000377 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor3bb30002009-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 Redl6f1ee232008-12-10 00:02:53 +0000388 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000389 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor3bb30002009-02-26 21:00:50 +0000390 } else
Steve Naroff87d58b42007-09-16 03:34:24 +0000391 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000392 move(LHS), move(TernaryMiddle),
393 move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000394 }
Chris Lattner4b009652007-07-25 00:24:17 +0000395 }
396}
397
398/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl0c9da212009-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.
Chris Lattner4b009652007-07-25 00:24:17 +0000402///
Argiris Kirtzidis785299b2009-05-22 10:24:42 +0000403Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
404 bool isAddressOfOperand) {
405 bool NotCastExpr;
406 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
407 isAddressOfOperand,
408 NotCastExpr);
409 if (NotCastExpr)
410 Diag(Tok, diag::err_expected_expression);
411 return move(Res);
412}
413
414/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
415/// true, parse a unary-expression. isAddressOfOperand exists because an
416/// id-expression that is the operand of address-of gets special treatment
417/// due to member pointers. NotCastExpr is set to true if the token is not the
418/// start of a cast-expression, and no diagnostic is emitted in this case.
419///
Chris Lattner4b009652007-07-25 00:24:17 +0000420/// cast-expression: [C99 6.5.4]
421/// unary-expression
422/// '(' type-name ')' cast-expression
423///
424/// unary-expression: [C99 6.5.3]
425/// postfix-expression
426/// '++' unary-expression
427/// '--' unary-expression
428/// unary-operator cast-expression
429/// 'sizeof' unary-expression
430/// 'sizeof' '(' type-name ')'
431/// [GNU] '__alignof' unary-expression
432/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000433/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000434/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000435/// [C++] new-expression
436/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000437///
438/// unary-operator: one of
439/// '&' '*' '+' '-' '~' '!'
440/// [GNU] '__extension__' '__real' '__imag'
441///
442/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000443/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000444/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000445/// constant
446/// string-literal
447/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000448/// [C++0x] 'nullptr' [C++0x 2.14.7]
Chris Lattner4b009652007-07-25 00:24:17 +0000449/// '(' expression ')'
450/// '__func__' [C99 6.4.2.2]
451/// [GNU] '__FUNCTION__'
452/// [GNU] '__PRETTY_FUNCTION__'
453/// [GNU] '(' compound-statement ')'
454/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
455/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
456/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
457/// assign-expr ')'
458/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregorad4b3792008-11-29 04:51:27 +0000459/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000460/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000461/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000462/// [OBJC] '@protocol' '(' identifier ')'
463/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000464/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000465/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
466/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000467/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
468/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
469/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
470/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000471/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
472/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000473/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000474/// [G++] unary-type-trait '(' type-id ')'
475/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000476/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000477///
478/// constant: [C99 6.4.4]
479/// integer-constant
480/// floating-constant
481/// enumeration-constant -> identifier
482/// character-constant
483///
Douglas Gregore60e5d32008-11-06 22:13:31 +0000484/// id-expression: [C++ 5.1]
485/// unqualified-id
486/// qualified-id [TODO]
487///
488/// unqualified-id: [C++ 5.1]
489/// identifier
490/// operator-function-id
491/// conversion-function-id [TODO]
492/// '~' class-name [TODO]
493/// template-id [TODO]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000494///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000495/// new-expression: [C++ 5.3.4]
496/// '::'[opt] 'new' new-placement[opt] new-type-id
497/// new-initializer[opt]
498/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
499/// new-initializer[opt]
500///
501/// delete-expression: [C++ 5.3.5]
502/// '::'[opt] 'delete' cast-expression
503/// '::'[opt] 'delete' '[' ']' cast-expression
504///
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000505/// [GNU] unary-type-trait:
506/// '__has_nothrow_assign' [TODO]
507/// '__has_nothrow_copy' [TODO]
508/// '__has_nothrow_constructor' [TODO]
509/// '__has_trivial_assign' [TODO]
510/// '__has_trivial_copy' [TODO]
Anders Carlssonc6363712009-04-16 00:08:20 +0000511/// '__has_trivial_constructor'
Anders Carlsson39a10db2009-04-17 02:34:54 +0000512/// '__has_trivial_destructor'
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000513/// '__has_virtual_destructor' [TODO]
514/// '__is_abstract' [TODO]
515/// '__is_class'
516/// '__is_empty' [TODO]
517/// '__is_enum'
518/// '__is_pod'
519/// '__is_polymorphic'
520/// '__is_union'
521///
522/// [GNU] binary-type-trait:
523/// '__is_base_of' [TODO]
524///
Sebastian Redl0c9da212009-02-03 20:19:35 +0000525Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argiris Kirtzidis785299b2009-05-22 10:24:42 +0000526 bool isAddressOfOperand,
527 bool &NotCastExpr) {
Sebastian Redl62261042008-12-09 20:22:58 +0000528 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000529 tok::TokenKind SavedKind = Tok.getKind();
Argiris Kirtzidis785299b2009-05-22 10:24:42 +0000530 NotCastExpr = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000531
532 // This handles all of cast-expression, unary-expression, postfix-expression,
533 // and primary-expression. We handle them together like this for efficiency
534 // and to simplify handling of an expression starting with a '(' token: which
535 // may be one of a parenthesized expression, cast-expression, compound literal
536 // expression, or statement expression.
537 //
538 // If the parsed tokens consist of a primary-expression, the cases below
539 // call ParsePostfixExpressionSuffix to handle the postfix expression
540 // suffixes. Cases that cannot be followed by postfix exprs should
541 // return without invoking ParsePostfixExpressionSuffix.
542 switch (SavedKind) {
543 case tok::l_paren: {
544 // If this expression is limited to being a unary-expression, the parent can
545 // not start a cast expression.
546 ParenParseOption ParenExprType =
547 isUnaryExpression ? CompoundLiteral : CastExpr;
548 TypeTy *CastTy;
549 SourceLocation LParenLoc = Tok.getLocation();
550 SourceLocation RParenLoc;
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +0000551 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
552 CastTy, RParenLoc);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000553 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000554
555 switch (ParenExprType) {
556 case SimpleExpr: break; // Nothing else to do.
557 case CompoundStmt: break; // Nothing else to do.
558 case CompoundLiteral:
559 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
560 // postfix-expression exist, parse them now.
561 break;
562 case CastExpr:
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +0000563 // We have parsed the cast-expression and no postfix-expr pieces are
564 // following.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000565 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000566 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000567
Chris Lattner4b009652007-07-25 00:24:17 +0000568 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000569 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000570 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000571
Chris Lattner4b009652007-07-25 00:24:17 +0000572 // primary-expression
573 case tok::numeric_constant:
574 // constant: integer-constant
575 // constant: floating-constant
Sebastian Redl14ca7412008-12-11 21:36:32 +0000576
Steve Naroff87d58b42007-09-16 03:34:24 +0000577 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000578 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000579
Chris Lattner4b009652007-07-25 00:24:17 +0000580 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000581 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000582
583 case tok::kw_true:
584 case tok::kw_false:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000585 return ParseCXXBoolLiteral();
Chris Lattner4b009652007-07-25 00:24:17 +0000586
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000587 case tok::kw_nullptr:
588 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
589
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000590 case tok::identifier: { // primary-expression: identifier
591 // unqualified-id: identifier
592 // constant: enumeration-constant
Chris Lattner5d7eace2009-01-06 05:06:21 +0000593 // Turn a potentially qualified name into a annot_typename or
Chris Lattner68751c42009-01-04 22:52:14 +0000594 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner1e015942009-01-04 23:23:14 +0000595 if (getLang().CPlusPlus) {
Chris Lattner914660b2009-01-04 23:46:59 +0000596 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
597 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000598 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner1e015942009-01-04 23:23:14 +0000599 }
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000600
Steve Naroff73ec9322009-03-09 21:12:44 +0000601 // Support 'Class.property' notation.
602 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
603 // 'super' (which is inappropriate here).
604 if (getLang().ObjC1 &&
605 Actions.getTypeName(*Tok.getIdentifierInfo(),
606 Tok.getLocation(), CurScope) &&
607 NextToken().is(tok::period)) {
608 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
609 SourceLocation IdentLoc = ConsumeToken();
610 SourceLocation DotLoc = ConsumeToken();
611
612 if (Tok.isNot(tok::identifier)) {
613 Diag(Tok, diag::err_expected_ident);
614 return ExprError();
615 }
616 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
617 SourceLocation PropertyLoc = ConsumeToken();
618
619 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
620 IdentLoc, PropertyLoc);
Steve Naroffc5ba83c2009-04-02 18:37:59 +0000621 // These can be followed by postfix-expr pieces.
622 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff73ec9322009-03-09 21:12:44 +0000623 }
Chris Lattner4b009652007-07-25 00:24:17 +0000624 // Consume the identifier so that we can see if it is followed by a '('.
625 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
626 // need to know whether or not this identifier is a function designator or
627 // not.
628 IdentifierInfo &II = *Tok.getIdentifierInfo();
629 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000630 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000631 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000632 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000633 }
634 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000635 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000636 ConsumeToken();
637 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000638 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000639 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
640 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
641 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner69909292008-08-10 01:53:14 +0000642 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000643 ConsumeToken();
644 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000645 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000646 case tok::string_literal: // primary-expression: string-literal
647 case tok::wide_string_literal:
648 Res = ParseStringLiteralExpression();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000649 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000650 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl14ca7412008-12-11 21:36:32 +0000651 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000652 case tok::kw___builtin_va_arg:
653 case tok::kw___builtin_offsetof:
654 case tok::kw___builtin_choose_expr:
655 case tok::kw___builtin_types_compatible_p:
Sebastian Redla6817a02008-12-11 22:33:27 +0000656 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000657 case tok::kw___null:
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000658 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregorad4b3792008-11-29 04:51:27 +0000659 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000660 case tok::plusplus: // unary-expression: '++' unary-expression
661 case tok::minusminus: { // unary-expression: '--' unary-expression
662 SourceLocation SavedLoc = ConsumeToken();
663 Res = ParseCastExpression(true);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000664 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000665 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000666 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000667 }
Sebastian Redl0c9da212009-02-03 20:19:35 +0000668 case tok::amp: { // unary-expression: '&' cast-expression
669 // Special treatment because of member pointers
670 SourceLocation SavedLoc = ConsumeToken();
671 Res = ParseCastExpression(false, true);
672 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000673 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl0c9da212009-02-03 20:19:35 +0000674 return move(Res);
675 }
676
Chris Lattner4b009652007-07-25 00:24:17 +0000677 case tok::star: // unary-expression: '*' cast-expression
678 case tok::plus: // unary-expression: '+' cast-expression
679 case tok::minus: // unary-expression: '-' cast-expression
680 case tok::tilde: // unary-expression: '~' cast-expression
681 case tok::exclaim: // unary-expression: '!' cast-expression
682 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000683 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000684 SourceLocation SavedLoc = ConsumeToken();
685 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000686 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000687 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000688 return move(Res);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000689 }
690
Chris Lattner6cf92942008-02-02 20:20:10 +0000691 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
692 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000693 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000694 SourceLocation SavedLoc = ConsumeToken();
695 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000696 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000697 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000698 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000699 }
700 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
701 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000702 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000703 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
704 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000705 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000706 return ParseSizeofAlignofExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000707 case tok::ampamp: { // unary-expression: '&&' identifier
708 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000709 if (Tok.isNot(tok::identifier))
710 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000711
Chris Lattner4b009652007-07-25 00:24:17 +0000712 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000713 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000714 Tok.getIdentifierInfo());
715 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000716 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000717 }
718 case tok::kw_const_cast:
719 case tok::kw_dynamic_cast:
720 case tok::kw_reinterpret_cast:
721 case tok::kw_static_cast:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000722 Res = ParseCXXCasts();
723 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000724 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000725 case tok::kw_typeid:
726 Res = ParseCXXTypeid();
727 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000728 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000729 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000730 Res = ParseCXXThis();
731 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000732 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000733
734 case tok::kw_char:
735 case tok::kw_wchar_t:
736 case tok::kw_bool:
737 case tok::kw_short:
738 case tok::kw_int:
739 case tok::kw_long:
740 case tok::kw_signed:
741 case tok::kw_unsigned:
742 case tok::kw_float:
743 case tok::kw_double:
744 case tok::kw_void:
Douglas Gregord3022602009-03-27 23:10:48 +0000745 case tok::kw_typename:
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000746 case tok::kw_typeof:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000747 case tok::annot_typename: {
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000748 if (!getLang().CPlusPlus) {
749 Diag(Tok, diag::err_expected_expression);
750 return ExprError();
751 }
752
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000753 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
754 //
755 DeclSpec DS;
756 ParseCXXSimpleTypeSpecifier(DS);
757 if (Tok.isNot(tok::l_paren))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000758 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
759 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000760
761 Res = ParseCXXTypeConstructExpression(DS);
762 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000763 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000764 }
765
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000766 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
767 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
768 // template-id
Sebastian Redl0c9da212009-02-03 20:19:35 +0000769 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000770 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000771
Chris Lattner68751c42009-01-04 22:52:14 +0000772 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000773 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
774 // annotates the token, tail recurse.
775 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000776 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
777
Chris Lattner68751c42009-01-04 22:52:14 +0000778 // ::new -> [C++] new-expression
779 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000780 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000781 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000782 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000783 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000784 return ParseCXXDeleteExpression(true, CCLoc);
785
Chris Lattner1e015942009-01-04 23:23:14 +0000786 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000787 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000788 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000789 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000790
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000791 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000792 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000793
794 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000795 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000796
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000797 case tok::kw___is_pod: // [GNU] unary-type-trait
798 case tok::kw___is_class:
799 case tok::kw___is_enum:
800 case tok::kw___is_union:
801 case tok::kw___is_polymorphic:
Anders Carlsson1dae87f2009-03-22 01:52:17 +0000802 case tok::kw___is_abstract:
Anders Carlssonc6363712009-04-16 00:08:20 +0000803 case tok::kw___has_trivial_constructor:
Anders Carlsson39a10db2009-04-17 02:34:54 +0000804 case tok::kw___has_trivial_destructor:
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000805 return ParseUnaryTypeTrait();
806
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000807 case tok::at: {
808 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000809 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000810 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000811 case tok::caret:
Chris Lattnerc14c7f02009-03-27 04:18:06 +0000812 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000813 case tok::l_square:
814 // These can be followed by postfix-expr pieces.
815 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000816 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000817 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000818 default:
Argiris Kirtzidis785299b2009-05-22 10:24:42 +0000819 NotCastExpr = true;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000820 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000821 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000822
Chris Lattner4b009652007-07-25 00:24:17 +0000823 // unreachable.
824 abort();
825}
826
827/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
828/// is parsed, this method parses any suffixes that apply.
829///
830/// postfix-expression: [C99 6.5.2]
831/// primary-expression
832/// postfix-expression '[' expression ']'
833/// postfix-expression '(' argument-expression-list[opt] ')'
834/// postfix-expression '.' identifier
835/// postfix-expression '->' identifier
836/// postfix-expression '++'
837/// postfix-expression '--'
838/// '(' type-name ')' '{' initializer-list '}'
839/// '(' type-name ')' '{' initializer-list ',' '}'
840///
841/// argument-expression-list: [C99 6.5.2]
842/// argument-expression
843/// argument-expression-list ',' assignment-expression
844///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000845Parser::OwningExprResult
846Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000847 // Now that the primary-expression piece of the postfix-expression has been
848 // parsed, see if there are any postfix-expression pieces here.
849 SourceLocation Loc;
850 while (1) {
851 switch (Tok.getKind()) {
852 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000853 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000854 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
855 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000856 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000857
Chris Lattner4b009652007-07-25 00:24:17 +0000858 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000859
860 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000861 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
862 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000863 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000864 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000865
866 // Match the ']'.
867 MatchRHSPunctuation(tok::r_square, Loc);
868 break;
869 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000870
Chris Lattner4b009652007-07-25 00:24:17 +0000871 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000872 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000873 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000874
Chris Lattner4b009652007-07-25 00:24:17 +0000875 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000876
Chris Lattner4d7d2342007-10-09 17:41:39 +0000877 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000878 if (ParseExpressionList(ArgExprs, CommaLocs)) {
879 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000880 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000881 }
882 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000883
Chris Lattner4b009652007-07-25 00:24:17 +0000884 // Match the ')'.
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000885 if (Tok.isNot(tok::r_paren)) {
886 MatchRHSPunctuation(tok::r_paren, Loc);
887 return ExprError();
888 }
889
890 if (!LHS.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000891 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
892 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000893 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000894 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redl6008ac32008-11-25 22:21:31 +0000895 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000896 }
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000897
898 ConsumeParen();
Chris Lattner4b009652007-07-25 00:24:17 +0000899 break;
900 }
901 case tok::arrow: // postfix-expression: p-e '->' identifier
902 case tok::period: { // postfix-expression: p-e '.' identifier
903 tok::TokenKind OpKind = Tok.getKind();
904 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000905
Chris Lattner4d7d2342007-10-09 17:41:39 +0000906 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000907 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000908 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000909 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000910
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000911 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000912 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000913 OpKind, Tok.getLocation(),
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +0000914 *Tok.getIdentifierInfo(),
915 ObjCImpDecl);
Sebastian Redl6008ac32008-11-25 22:21:31 +0000916 }
Chris Lattner4b009652007-07-25 00:24:17 +0000917 ConsumeToken();
918 break;
919 }
920 case tok::plusplus: // postfix-expression: postfix-expression '++'
921 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000922 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000923 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000924 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000925 }
Chris Lattner4b009652007-07-25 00:24:17 +0000926 ConsumeToken();
927 break;
928 }
929 }
930}
931
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +0000932/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
933/// we are at the start of an expression or a parenthesized type-id.
934/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
935/// (isCastExpr == false) or the type (isCastExpr == true).
936///
937/// unary-expression: [C99 6.5.3]
938/// 'sizeof' unary-expression
939/// 'sizeof' '(' type-name ')'
940/// [GNU] '__alignof' unary-expression
941/// [GNU] '__alignof' '(' type-name ')'
942/// [C++0x] 'alignof' '(' type-id ')'
943///
944/// [GNU] typeof-specifier:
945/// typeof ( expressions )
946/// typeof ( type-name )
947/// [GNU/C++] typeof unary-expression
948///
949Parser::OwningExprResult
950Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
951 bool &isCastExpr,
952 TypeTy *&CastTy,
953 SourceRange &CastRange) {
954
955 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
956 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
957 "Not a typeof/sizeof/alignof expression!");
958
959 OwningExprResult Operand(Actions);
960
961 // If the operand doesn't start with an '(', it must be an expression.
962 if (Tok.isNot(tok::l_paren)) {
963 isCastExpr = false;
964 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
965 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
966 return ExprError();
967 }
968 Operand = ParseCastExpression(true/*isUnaryExpression*/);
969
970 } else {
971 // If it starts with a '(', we know that it is either a parenthesized
972 // type-name, or it is a unary-expression that starts with a compound
973 // literal, or starts with a primary-expression that is a parenthesized
974 // expression.
975 ParenParseOption ExprType = CastExpr;
976 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +0000977 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
978 CastTy, RParenLoc);
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +0000979 CastRange = SourceRange(LParenLoc, RParenLoc);
980
981 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
982 // a type.
983 if (ExprType == CastExpr) {
984 isCastExpr = true;
985 return ExprEmpty();
986 }
987
988 // If this is a parenthesized expression, it is the start of a
989 // unary-expression, but doesn't include any postfix pieces. Parse these
990 // now if present.
991 Operand = ParsePostfixExpressionSuffix(move(Operand));
992 }
993
994 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
995 isCastExpr = false;
996 return move(Operand);
997}
998
Chris Lattner4b009652007-07-25 00:24:17 +0000999
1000/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1001/// unary-expression: [C99 6.5.3]
1002/// 'sizeof' unary-expression
1003/// 'sizeof' '(' type-name ')'
1004/// [GNU] '__alignof' unary-expression
1005/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +00001006/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +00001007Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +00001008 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1009 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001010 "Not a sizeof/alignof expression!");
1011 Token OpTok = Tok;
1012 ConsumeToken();
1013
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001014 bool isCastExpr;
1015 TypeTy *CastTy;
1016 SourceRange CastRange;
1017 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1018 isCastExpr,
1019 CastTy,
1020 CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001021
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001022 if (isCastExpr)
1023 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1024 OpTok.is(tok::kw_sizeof),
1025 /*isType=*/true, CastTy,
1026 CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001027
Chris Lattner4b009652007-07-25 00:24:17 +00001028 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001029 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001030 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1031 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001032 /*isType=*/false,
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001033 Operand.release(), CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001034 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +00001035}
1036
1037/// ParseBuiltinPrimaryExpression
1038///
1039/// primary-expression: [C99 6.5.1]
1040/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1041/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1042/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1043/// assign-expr ')'
1044/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1045///
1046/// [GNU] offsetof-member-designator:
1047/// [GNU] identifier
1048/// [GNU] offsetof-member-designator '.' identifier
1049/// [GNU] offsetof-member-designator '[' expression ']'
1050///
Sebastian Redla6817a02008-12-11 22:33:27 +00001051Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +00001052 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001053 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1054
1055 tok::TokenKind T = Tok.getKind();
1056 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1057
1058 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +00001059 if (Tok.isNot(tok::l_paren))
1060 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1061 << BuiltinII);
1062
Chris Lattner4b009652007-07-25 00:24:17 +00001063 SourceLocation LParenLoc = ConsumeParen();
1064 // TODO: Build AST.
1065
1066 switch (T) {
1067 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +00001068 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001069 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001070 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001071 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001072 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001073 }
1074
1075 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001076 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001077
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001078 TypeResult Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001079
Anders Carlsson36760332007-10-15 20:28:48 +00001080 if (Tok.isNot(tok::r_paren)) {
1081 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001082 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +00001083 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001084 if (Ty.isInvalid())
1085 Res = ExprError();
1086 else
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001087 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +00001088 break;
Anders Carlsson36760332007-10-15 20:28:48 +00001089 }
Chris Lattner69638b12007-08-30 15:51:11 +00001090 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +00001091 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001092 TypeResult Ty = ParseTypeName();
Chris Lattner7d5caf22009-03-24 17:21:43 +00001093 if (Ty.isInvalid()) {
1094 SkipUntil(tok::r_paren);
1095 return ExprError();
1096 }
1097
Chris Lattner4b009652007-07-25 00:24:17 +00001098 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001099 return ExprError();
1100
Chris Lattner4b009652007-07-25 00:24:17 +00001101 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001102 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001103 Diag(Tok, diag::err_expected_ident);
1104 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001105 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001106 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001107
Chris Lattner69638b12007-08-30 15:51:11 +00001108 // Keep track of the various subcomponents we see.
1109 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +00001110
Chris Lattner69638b12007-08-30 15:51:11 +00001111 Comps.push_back(Action::OffsetOfComponent());
1112 Comps.back().isBrackets = false;
1113 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1114 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001115
Sebastian Redl6008ac32008-11-25 22:21:31 +00001116 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +00001117 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001118 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001119 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +00001120 Comps.push_back(Action::OffsetOfComponent());
1121 Comps.back().isBrackets = false;
1122 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001123
Chris Lattner4d7d2342007-10-09 17:41:39 +00001124 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001125 Diag(Tok, diag::err_expected_ident);
1126 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001127 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001128 }
1129 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1130 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001131
Chris Lattner4d7d2342007-10-09 17:41:39 +00001132 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001133 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +00001134 Comps.push_back(Action::OffsetOfComponent());
1135 Comps.back().isBrackets = true;
1136 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +00001137 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001138 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001139 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001140 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001141 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001142 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +00001143
Chris Lattner69638b12007-08-30 15:51:11 +00001144 Comps.back().LocEnd =
1145 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001146 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001147 if (Ty.isInvalid())
1148 Res = ExprError();
1149 else
1150 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1151 Ty.get(), &Comps[0],
1152 Comps.size(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001153 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001154 } else {
Chris Lattner69638b12007-08-30 15:51:11 +00001155 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +00001156 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001157 }
1158 }
1159 break;
Chris Lattner69638b12007-08-30 15:51:11 +00001160 }
Steve Naroff93c53012007-08-03 21:21:27 +00001161 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001162 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001163 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001164 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001165 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001166 }
Chris Lattner4b009652007-07-25 00:24:17 +00001167 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001168 return ExprError();
1169
Sebastian Redl14ca7412008-12-11 21:36:32 +00001170 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001171 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001172 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001173 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001174 }
Chris Lattner4b009652007-07-25 00:24:17 +00001175 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001176 return ExprError();
1177
Sebastian Redl14ca7412008-12-11 21:36:32 +00001178 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001179 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001180 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001181 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001182 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001183 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001184 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001185 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001186 }
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001187 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1188 move(Expr2), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001189 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001190 }
Chris Lattner4b009652007-07-25 00:24:17 +00001191 case tok::kw___builtin_types_compatible_p:
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001192 TypeResult Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001193
Chris Lattner4b009652007-07-25 00:24:17 +00001194 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001195 return ExprError();
1196
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001197 TypeResult Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001198
Chris Lattner4d7d2342007-10-09 17:41:39 +00001199 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001200 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001201 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001202 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001203
1204 if (Ty1.isInvalid() || Ty2.isInvalid())
1205 Res = ExprError();
1206 else
1207 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1208 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001209 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001210 }
1211
Chris Lattner4b009652007-07-25 00:24:17 +00001212 // These can be followed by postfix-expr pieces because they are
1213 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001214 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001215}
1216
1217/// ParseParenExpression - This parses the unit that starts with a '(' token,
1218/// based on what is allowed by ExprType. The actual thing parsed is returned
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001219/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1220/// not the parsed cast-expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001221///
1222/// primary-expression: [C99 6.5.1]
1223/// '(' expression ')'
1224/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1225/// postfix-expression: [C99 6.5.2]
1226/// '(' type-name ')' '{' initializer-list '}'
1227/// '(' type-name ')' '{' initializer-list ',' '}'
1228/// cast-expression: [C99 6.5.4]
1229/// '(' type-name ')' cast-expression
1230///
Sebastian Redla6817a02008-12-11 22:33:27 +00001231Parser::OwningExprResult
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001232Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Sebastian Redla6817a02008-12-11 22:33:27 +00001233 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001234 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregoraf0d0092009-02-09 21:04:56 +00001235 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001236 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001237 OwningExprResult Result(Actions, true);
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001238 bool isAmbiguousTypeId;
Chris Lattner4b009652007-07-25 00:24:17 +00001239 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001240
Chris Lattner4d7d2342007-10-09 17:41:39 +00001241 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001242 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001243 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001244 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001245
Chris Lattner4b009652007-07-25 00:24:17 +00001246 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001247 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001248 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001249
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001250 } else if (ExprType >= CompoundLiteral &&
1251 isTypeIdInParens(isAmbiguousTypeId)) {
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001252
Chris Lattner4b009652007-07-25 00:24:17 +00001253 // Otherwise, this is a compound literal expression or cast expression.
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001254
1255 // In C++, if the type-id is ambiguous we disambiguate based on context.
1256 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1257 // in which case we should treat it as type-id.
1258 // if stopIfCastExpr is false, we need to determine the context past the
1259 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1260 if (isAmbiguousTypeId && !stopIfCastExpr)
1261 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1262 OpenLoc, RParenLoc);
1263
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001264 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +00001265
1266 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001267 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001268 RParenLoc = ConsumeParen();
1269 else
1270 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001271
Chris Lattner4d7d2342007-10-09 17:41:39 +00001272 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001273 ExprType = CompoundLiteral;
Argiris Kirtzidis70c95822009-05-22 10:24:05 +00001274 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001275 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001276
Chris Lattnercde12fd2008-12-12 06:00:12 +00001277 if (ExprType == CastExpr) {
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001278 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001279
1280 if (Ty.isInvalid())
1281 return ExprError();
1282
1283 CastTy = Ty.get();
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001284
1285 if (stopIfCastExpr) {
1286 // Note that this doesn't parse the subsequent cast-expression, it just
1287 // returns the parsed type to the callee.
1288 return OwningExprResult(Actions);
1289 }
1290
1291 // Parse the cast-expression that follows it next.
1292 // TODO: For cast expression with CastTy.
1293 Result = ParseCastExpression(false);
1294 if (!Result.isInvalid())
1295 Result = Actions.ActOnCastExpr(OpenLoc, CastTy, RParenLoc,move(Result));
1296 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001297 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001298
Chris Lattnercde12fd2008-12-12 06:00:12 +00001299 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1300 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001301 } else {
1302 Result = ParseExpression();
1303 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001304 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001305 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001306 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001307
Chris Lattner4b009652007-07-25 00:24:17 +00001308 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001309 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001310 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001311 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001312 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001313
1314 if (Tok.is(tok::r_paren))
1315 RParenLoc = ConsumeParen();
1316 else
1317 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001318
1319 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001320}
1321
Argiris Kirtzidis70c95822009-05-22 10:24:05 +00001322/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1323/// and we are at the left brace.
1324///
1325/// postfix-expression: [C99 6.5.2]
1326/// '(' type-name ')' '{' initializer-list '}'
1327/// '(' type-name ')' '{' initializer-list ',' '}'
1328///
1329Parser::OwningExprResult
1330Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1331 SourceLocation LParenLoc,
1332 SourceLocation RParenLoc) {
1333 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1334 if (!getLang().C99) // Compound literals don't exist in C90.
1335 Diag(LParenLoc, diag::ext_c99_compound_literal);
1336 OwningExprResult Result = ParseInitializer();
1337 if (!Result.isInvalid() && Ty)
1338 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1339 return move(Result);
1340}
1341
Chris Lattner4b009652007-07-25 00:24:17 +00001342/// ParseStringLiteralExpression - This handles the various token types that
1343/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1344/// translation phase #6].
1345///
1346/// primary-expression: [C99 6.5.1]
1347/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001348Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001349 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001350
Chris Lattner4b009652007-07-25 00:24:17 +00001351 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1352 // considered to be strings for concatenation purposes.
1353 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001354
Chris Lattner4b009652007-07-25 00:24:17 +00001355 do {
1356 StringToks.push_back(Tok);
1357 ConsumeStringToken();
1358 } while (isTokenStringLiteral());
1359
1360 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001361 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001362}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001363
1364/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1365///
1366/// argument-expression-list:
1367/// assignment-expression
1368/// argument-expression-list , assignment-expression
1369///
1370/// [C++] expression-list:
1371/// [C++] assignment-expression
1372/// [C++] expression-list , assignment-expression
1373///
1374bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1375 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001376 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001377 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001378 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001379
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001380 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001381
1382 if (Tok.isNot(tok::comma))
1383 return false;
1384 // Move to the next argument, remember where the comma was.
1385 CommaLocs.push_back(ConsumeToken());
1386 }
1387}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001388
Mike Stumpc1fddff2009-02-04 22:31:32 +00001389/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1390///
1391/// [clang] block-id:
1392/// [clang] specifier-qualifier-list block-declarator
1393///
1394void Parser::ParseBlockId() {
1395 // Parse the specifier-qualifier-list piece.
1396 DeclSpec DS;
1397 ParseSpecifierQualifierList(DS);
1398
1399 // Parse the block-declarator.
1400 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1401 ParseDeclarator(DeclaratorInfo);
Mike Stump115a0722009-04-29 19:03:13 +00001402
Mike Stump9e439c92009-04-29 21:40:37 +00001403 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1404 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1405 SourceLocation());
1406
Mike Stump115a0722009-04-29 19:03:13 +00001407 if (Tok.is(tok::kw___attribute)) {
1408 SourceLocation Loc;
1409 AttributeList *AttrList = ParseAttributes(&Loc);
1410 DeclaratorInfo.AddAttributes(AttrList, Loc);
1411 }
1412
Mike Stumpc1fddff2009-02-04 22:31:32 +00001413 // Inform sema that we are starting a block.
1414 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1415}
1416
Steve Narofffd5b19d2008-08-28 19:20:44 +00001417/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001418/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001419///
1420/// block-literal:
1421/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001422/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001423/// [clang] block-args:
1424/// [clang] '(' parameter-list ')'
1425///
Sebastian Redla2deb432008-12-13 15:32:12 +00001426Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001427 assert(Tok.is(tok::caret) && "block literal starts with ^");
1428 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001429
Chris Lattnere533c7d2009-03-05 07:32:12 +00001430 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1431 "block literal parsing");
1432
Steve Narofffd5b19d2008-08-28 19:20:44 +00001433 // Enter a scope to hold everything within the block. This includes the
1434 // argument decls, decls within the compound expression, etc. This also
1435 // allows determining whether a variable reference inside the block is
1436 // within or outside of the block.
Sebastian Redl0c986032009-02-09 18:23:29 +00001437 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1438 Scope::BreakScope | Scope::ContinueScope |
1439 Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001440
1441 // Inform sema that we are starting a block.
1442 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattnere533c7d2009-03-05 07:32:12 +00001443
Steve Narofffd5b19d2008-08-28 19:20:44 +00001444 // Parse the return type if present.
1445 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001446 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redl0c986032009-02-09 18:23:29 +00001447 // FIXME: Since the return type isn't actually parsed, it can't be used to
1448 // fill ParamInfo with an initial valid range, so do it manually.
1449 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redla2deb432008-12-13 15:32:12 +00001450
Steve Narofffd5b19d2008-08-28 19:20:44 +00001451 // If this block has arguments, parse them. There is no ambiguity here with
1452 // the expression case, because the expression case requires a parameter list.
1453 if (Tok.is(tok::l_paren)) {
1454 ParseParenDeclarator(ParamInfo);
1455 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redl0c986032009-02-09 18:23:29 +00001456 // SetIdentifier sets the source range end, but in this case we're past
1457 // that location.
1458 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001459 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001460 ParamInfo.SetRangeEnd(Tmp);
Chris Lattner34c61332009-04-25 08:06:05 +00001461 if (ParamInfo.isInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001462 // If there was an error parsing the arguments, they may have
1463 // tried to use ^(x+y) which requires an argument list. Just
1464 // skip the whole block literal.
Chris Lattnerd860cbd2009-04-18 20:05:34 +00001465 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001466 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001467 }
Mike Stump115a0722009-04-29 19:03:13 +00001468
1469 if (Tok.is(tok::kw___attribute)) {
1470 SourceLocation Loc;
1471 AttributeList *AttrList = ParseAttributes(&Loc);
1472 ParamInfo.AddAttributes(AttrList, Loc);
1473 }
1474
Mike Stumpc1fddff2009-02-04 22:31:32 +00001475 // Inform sema that we are starting a block.
1476 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stump1214b582009-04-14 18:24:37 +00001477 } else if (!Tok.is(tok::l_brace)) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001478 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001479 } else {
1480 // Otherwise, pretend we saw (void).
Douglas Gregor88a25f82009-02-18 07:07:28 +00001481 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1482 SourceLocation(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001483 0, 0, 0,
Sebastian Redlaaacda92009-05-29 18:02:33 +00001484 false, false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001485 CaretLoc, ParamInfo),
Sebastian Redl0c986032009-02-09 18:23:29 +00001486 CaretLoc);
Mike Stump115a0722009-04-29 19:03:13 +00001487
1488 if (Tok.is(tok::kw___attribute)) {
1489 SourceLocation Loc;
1490 AttributeList *AttrList = ParseAttributes(&Loc);
1491 ParamInfo.AddAttributes(AttrList, Loc);
1492 }
1493
Mike Stumpc1fddff2009-02-04 22:31:32 +00001494 // Inform sema that we are starting a block.
1495 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001496 }
1497
Sebastian Redla2deb432008-12-13 15:32:12 +00001498
Sebastian Redl62261042008-12-09 20:22:58 +00001499 OwningExprResult Result(Actions, true);
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001500 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001501 // Saw something like: ^expr
1502 Diag(Tok, diag::err_expected_expression);
Chris Lattnerd860cbd2009-04-18 20:05:34 +00001503 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001504 return ExprError();
1505 }
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001506
1507 OwningStmtResult Stmt(ParseCompoundStatementBody());
1508 if (!Stmt.isInvalid())
1509 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1510 else
1511 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001512 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001513}