blob: 3fee78bb719f674aafd5743f48cb854c741e9be2 [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 }
Eli Friedmanc8afd2d2009-06-11 00:33:41 +0000752
753 if (SavedKind == tok::kw_typename) {
754 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
755 if (!TryAnnotateTypeOrScopeToken())
756 return ExprError();
757 }
758
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000759 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
760 //
761 DeclSpec DS;
762 ParseCXXSimpleTypeSpecifier(DS);
763 if (Tok.isNot(tok::l_paren))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000764 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
765 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000766
767 Res = ParseCXXTypeConstructExpression(DS);
768 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000769 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000770 }
771
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000772 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
773 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
774 // template-id
Sebastian Redl0c9da212009-02-03 20:19:35 +0000775 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000776 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000777
Chris Lattner68751c42009-01-04 22:52:14 +0000778 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000779 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
780 // annotates the token, tail recurse.
781 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000782 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
783
Chris Lattner68751c42009-01-04 22:52:14 +0000784 // ::new -> [C++] new-expression
785 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000786 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000787 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000788 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000789 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000790 return ParseCXXDeleteExpression(true, CCLoc);
791
Chris Lattner1e015942009-01-04 23:23:14 +0000792 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000793 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000794 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000795 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000796
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000797 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000798 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000799
800 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000801 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000802
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000803 case tok::kw___is_pod: // [GNU] unary-type-trait
804 case tok::kw___is_class:
805 case tok::kw___is_enum:
806 case tok::kw___is_union:
807 case tok::kw___is_polymorphic:
Anders Carlsson1dae87f2009-03-22 01:52:17 +0000808 case tok::kw___is_abstract:
Anders Carlssonc6363712009-04-16 00:08:20 +0000809 case tok::kw___has_trivial_constructor:
Anders Carlsson39a10db2009-04-17 02:34:54 +0000810 case tok::kw___has_trivial_destructor:
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000811 return ParseUnaryTypeTrait();
812
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000813 case tok::at: {
814 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000815 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000816 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000817 case tok::caret:
Chris Lattnerc14c7f02009-03-27 04:18:06 +0000818 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000819 case tok::l_square:
820 // These can be followed by postfix-expr pieces.
821 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000822 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000823 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000824 default:
Argiris Kirtzidis785299b2009-05-22 10:24:42 +0000825 NotCastExpr = true;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000826 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000827 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000828
Chris Lattner4b009652007-07-25 00:24:17 +0000829 // unreachable.
830 abort();
831}
832
833/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
834/// is parsed, this method parses any suffixes that apply.
835///
836/// postfix-expression: [C99 6.5.2]
837/// primary-expression
838/// postfix-expression '[' expression ']'
839/// postfix-expression '(' argument-expression-list[opt] ')'
840/// postfix-expression '.' identifier
841/// postfix-expression '->' identifier
842/// postfix-expression '++'
843/// postfix-expression '--'
844/// '(' type-name ')' '{' initializer-list '}'
845/// '(' type-name ')' '{' initializer-list ',' '}'
846///
847/// argument-expression-list: [C99 6.5.2]
848/// argument-expression
849/// argument-expression-list ',' assignment-expression
850///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000851Parser::OwningExprResult
852Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000853 // Now that the primary-expression piece of the postfix-expression has been
854 // parsed, see if there are any postfix-expression pieces here.
855 SourceLocation Loc;
856 while (1) {
857 switch (Tok.getKind()) {
858 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000859 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000860 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
861 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000862 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000863
Chris Lattner4b009652007-07-25 00:24:17 +0000864 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000865
866 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000867 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
868 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000869 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000870 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000871
872 // Match the ']'.
873 MatchRHSPunctuation(tok::r_square, Loc);
874 break;
875 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000876
Chris Lattner4b009652007-07-25 00:24:17 +0000877 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000878 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000879 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000880
Chris Lattner4b009652007-07-25 00:24:17 +0000881 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000882
Chris Lattner4d7d2342007-10-09 17:41:39 +0000883 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000884 if (ParseExpressionList(ArgExprs, CommaLocs)) {
885 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000886 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000887 }
888 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000889
Chris Lattner4b009652007-07-25 00:24:17 +0000890 // Match the ')'.
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000891 if (Tok.isNot(tok::r_paren)) {
892 MatchRHSPunctuation(tok::r_paren, Loc);
893 return ExprError();
894 }
895
896 if (!LHS.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000897 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
898 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000899 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foad9e6bef42009-05-21 09:52:38 +0000900 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redl6008ac32008-11-25 22:21:31 +0000901 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000902 }
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000903
904 ConsumeParen();
Chris Lattner4b009652007-07-25 00:24:17 +0000905 break;
906 }
907 case tok::arrow: // postfix-expression: p-e '->' identifier
908 case tok::period: { // postfix-expression: p-e '.' identifier
909 tok::TokenKind OpKind = Tok.getKind();
910 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000911
Chris Lattner4d7d2342007-10-09 17:41:39 +0000912 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000913 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000914 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000915 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000916
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000917 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000918 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000919 OpKind, Tok.getLocation(),
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +0000920 *Tok.getIdentifierInfo(),
921 ObjCImpDecl);
Sebastian Redl6008ac32008-11-25 22:21:31 +0000922 }
Chris Lattner4b009652007-07-25 00:24:17 +0000923 ConsumeToken();
924 break;
925 }
926 case tok::plusplus: // postfix-expression: postfix-expression '++'
927 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000928 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000929 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000930 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000931 }
Chris Lattner4b009652007-07-25 00:24:17 +0000932 ConsumeToken();
933 break;
934 }
935 }
936}
937
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +0000938/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
939/// we are at the start of an expression or a parenthesized type-id.
940/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
941/// (isCastExpr == false) or the type (isCastExpr == true).
942///
943/// unary-expression: [C99 6.5.3]
944/// 'sizeof' unary-expression
945/// 'sizeof' '(' type-name ')'
946/// [GNU] '__alignof' unary-expression
947/// [GNU] '__alignof' '(' type-name ')'
948/// [C++0x] 'alignof' '(' type-id ')'
949///
950/// [GNU] typeof-specifier:
951/// typeof ( expressions )
952/// typeof ( type-name )
953/// [GNU/C++] typeof unary-expression
954///
955Parser::OwningExprResult
956Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
957 bool &isCastExpr,
958 TypeTy *&CastTy,
959 SourceRange &CastRange) {
960
961 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
962 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
963 "Not a typeof/sizeof/alignof expression!");
964
965 OwningExprResult Operand(Actions);
966
967 // If the operand doesn't start with an '(', it must be an expression.
968 if (Tok.isNot(tok::l_paren)) {
969 isCastExpr = false;
970 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
971 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
972 return ExprError();
973 }
974 Operand = ParseCastExpression(true/*isUnaryExpression*/);
975
976 } else {
977 // If it starts with a '(', we know that it is either a parenthesized
978 // type-name, or it is a unary-expression that starts with a compound
979 // literal, or starts with a primary-expression that is a parenthesized
980 // expression.
981 ParenParseOption ExprType = CastExpr;
982 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +0000983 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
984 CastTy, RParenLoc);
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +0000985 CastRange = SourceRange(LParenLoc, RParenLoc);
986
987 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
988 // a type.
989 if (ExprType == CastExpr) {
990 isCastExpr = true;
991 return ExprEmpty();
992 }
993
994 // If this is a parenthesized expression, it is the start of a
995 // unary-expression, but doesn't include any postfix pieces. Parse these
996 // now if present.
997 Operand = ParsePostfixExpressionSuffix(move(Operand));
998 }
999
1000 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1001 isCastExpr = false;
1002 return move(Operand);
1003}
1004
Chris Lattner4b009652007-07-25 00:24:17 +00001005
1006/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1007/// unary-expression: [C99 6.5.3]
1008/// 'sizeof' unary-expression
1009/// 'sizeof' '(' type-name ')'
1010/// [GNU] '__alignof' unary-expression
1011/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +00001012/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +00001013Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +00001014 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1015 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001016 "Not a sizeof/alignof expression!");
1017 Token OpTok = Tok;
1018 ConsumeToken();
1019
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001020 bool isCastExpr;
1021 TypeTy *CastTy;
1022 SourceRange CastRange;
1023 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1024 isCastExpr,
1025 CastTy,
1026 CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001027
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001028 if (isCastExpr)
1029 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1030 OpTok.is(tok::kw_sizeof),
1031 /*isType=*/true, CastTy,
1032 CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001033
Chris Lattner4b009652007-07-25 00:24:17 +00001034 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001035 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001036 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1037 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001038 /*isType=*/false,
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001039 Operand.release(), CastRange);
Sebastian Redla6817a02008-12-11 22:33:27 +00001040 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +00001041}
1042
1043/// ParseBuiltinPrimaryExpression
1044///
1045/// primary-expression: [C99 6.5.1]
1046/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1047/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1048/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1049/// assign-expr ')'
1050/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
1051///
1052/// [GNU] offsetof-member-designator:
1053/// [GNU] identifier
1054/// [GNU] offsetof-member-designator '.' identifier
1055/// [GNU] offsetof-member-designator '[' expression ']'
1056///
Sebastian Redla6817a02008-12-11 22:33:27 +00001057Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +00001058 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001059 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1060
1061 tok::TokenKind T = Tok.getKind();
1062 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1063
1064 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +00001065 if (Tok.isNot(tok::l_paren))
1066 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1067 << BuiltinII);
1068
Chris Lattner4b009652007-07-25 00:24:17 +00001069 SourceLocation LParenLoc = ConsumeParen();
1070 // TODO: Build AST.
1071
1072 switch (T) {
1073 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +00001074 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001075 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001076 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001077 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001078 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001079 }
1080
1081 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001082 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001083
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001084 TypeResult Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001085
Anders Carlsson36760332007-10-15 20:28:48 +00001086 if (Tok.isNot(tok::r_paren)) {
1087 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001088 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +00001089 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001090 if (Ty.isInvalid())
1091 Res = ExprError();
1092 else
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001093 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +00001094 break;
Anders Carlsson36760332007-10-15 20:28:48 +00001095 }
Chris Lattner69638b12007-08-30 15:51:11 +00001096 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +00001097 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001098 TypeResult Ty = ParseTypeName();
Chris Lattner7d5caf22009-03-24 17:21:43 +00001099 if (Ty.isInvalid()) {
1100 SkipUntil(tok::r_paren);
1101 return ExprError();
1102 }
1103
Chris Lattner4b009652007-07-25 00:24:17 +00001104 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001105 return ExprError();
1106
Chris Lattner4b009652007-07-25 00:24:17 +00001107 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001108 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001109 Diag(Tok, diag::err_expected_ident);
1110 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001111 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001112 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001113
Chris Lattner69638b12007-08-30 15:51:11 +00001114 // Keep track of the various subcomponents we see.
1115 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +00001116
Chris Lattner69638b12007-08-30 15:51:11 +00001117 Comps.push_back(Action::OffsetOfComponent());
1118 Comps.back().isBrackets = false;
1119 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1120 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001121
Sebastian Redl6008ac32008-11-25 22:21:31 +00001122 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +00001123 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001124 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001125 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +00001126 Comps.push_back(Action::OffsetOfComponent());
1127 Comps.back().isBrackets = false;
1128 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001129
Chris Lattner4d7d2342007-10-09 17:41:39 +00001130 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001131 Diag(Tok, diag::err_expected_ident);
1132 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001133 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001134 }
1135 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1136 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001137
Chris Lattner4d7d2342007-10-09 17:41:39 +00001138 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001139 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +00001140 Comps.push_back(Action::OffsetOfComponent());
1141 Comps.back().isBrackets = true;
1142 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +00001143 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001144 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001145 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001146 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001147 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001148 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +00001149
Chris Lattner69638b12007-08-30 15:51:11 +00001150 Comps.back().LocEnd =
1151 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001152 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001153 if (Ty.isInvalid())
1154 Res = ExprError();
1155 else
1156 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1157 Ty.get(), &Comps[0],
1158 Comps.size(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001159 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001160 } else {
Chris Lattner69638b12007-08-30 15:51:11 +00001161 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +00001162 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001163 }
1164 }
1165 break;
Chris Lattner69638b12007-08-30 15:51:11 +00001166 }
Steve Naroff93c53012007-08-03 21:21:27 +00001167 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001168 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001169 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001170 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001171 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001172 }
Chris Lattner4b009652007-07-25 00:24:17 +00001173 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001174 return ExprError();
1175
Sebastian Redl14ca7412008-12-11 21:36:32 +00001176 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001177 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001178 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001179 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001180 }
Chris Lattner4b009652007-07-25 00:24:17 +00001181 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001182 return ExprError();
1183
Sebastian Redl14ca7412008-12-11 21:36:32 +00001184 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001185 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001186 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001187 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001188 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001189 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001190 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001191 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001192 }
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001193 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1194 move(Expr2), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001195 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001196 }
Chris Lattner4b009652007-07-25 00:24:17 +00001197 case tok::kw___builtin_types_compatible_p:
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001198 TypeResult Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001199
Chris Lattner4b009652007-07-25 00:24:17 +00001200 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001201 return ExprError();
1202
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001203 TypeResult Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001204
Chris Lattner4d7d2342007-10-09 17:41:39 +00001205 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001206 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001207 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001208 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001209
1210 if (Ty1.isInvalid() || Ty2.isInvalid())
1211 Res = ExprError();
1212 else
1213 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1214 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001215 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001216 }
1217
Chris Lattner4b009652007-07-25 00:24:17 +00001218 // These can be followed by postfix-expr pieces because they are
1219 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001220 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001221}
1222
1223/// ParseParenExpression - This parses the unit that starts with a '(' token,
1224/// based on what is allowed by ExprType. The actual thing parsed is returned
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001225/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1226/// not the parsed cast-expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001227///
1228/// primary-expression: [C99 6.5.1]
1229/// '(' expression ')'
1230/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1231/// postfix-expression: [C99 6.5.2]
1232/// '(' type-name ')' '{' initializer-list '}'
1233/// '(' type-name ')' '{' initializer-list ',' '}'
1234/// cast-expression: [C99 6.5.4]
1235/// '(' type-name ')' cast-expression
1236///
Sebastian Redla6817a02008-12-11 22:33:27 +00001237Parser::OwningExprResult
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001238Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Sebastian Redla6817a02008-12-11 22:33:27 +00001239 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001240 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregoraf0d0092009-02-09 21:04:56 +00001241 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001242 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001243 OwningExprResult Result(Actions, true);
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001244 bool isAmbiguousTypeId;
Chris Lattner4b009652007-07-25 00:24:17 +00001245 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001246
Chris Lattner4d7d2342007-10-09 17:41:39 +00001247 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001248 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001249 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001250 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001251
Chris Lattner4b009652007-07-25 00:24:17 +00001252 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001253 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001254 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001255
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001256 } else if (ExprType >= CompoundLiteral &&
1257 isTypeIdInParens(isAmbiguousTypeId)) {
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00001258
Chris Lattner4b009652007-07-25 00:24:17 +00001259 // Otherwise, this is a compound literal expression or cast expression.
Argiris Kirtzidis785299b2009-05-22 10:24:42 +00001260
1261 // In C++, if the type-id is ambiguous we disambiguate based on context.
1262 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1263 // in which case we should treat it as type-id.
1264 // if stopIfCastExpr is false, we need to determine the context past the
1265 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1266 if (isAmbiguousTypeId && !stopIfCastExpr)
1267 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1268 OpenLoc, RParenLoc);
1269
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001270 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +00001271
1272 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001273 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001274 RParenLoc = ConsumeParen();
1275 else
1276 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001277
Chris Lattner4d7d2342007-10-09 17:41:39 +00001278 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001279 ExprType = CompoundLiteral;
Argiris Kirtzidis70c95822009-05-22 10:24:05 +00001280 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001281 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001282
Chris Lattnercde12fd2008-12-12 06:00:12 +00001283 if (ExprType == CastExpr) {
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001284 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001285
1286 if (Ty.isInvalid())
1287 return ExprError();
1288
1289 CastTy = Ty.get();
Argiris Kirtzidis32d6d102009-05-22 10:23:40 +00001290
1291 if (stopIfCastExpr) {
1292 // Note that this doesn't parse the subsequent cast-expression, it just
1293 // returns the parsed type to the callee.
1294 return OwningExprResult(Actions);
1295 }
1296
1297 // Parse the cast-expression that follows it next.
1298 // TODO: For cast expression with CastTy.
1299 Result = ParseCastExpression(false);
1300 if (!Result.isInvalid())
1301 Result = Actions.ActOnCastExpr(OpenLoc, CastTy, RParenLoc,move(Result));
1302 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001303 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001304
Chris Lattnercde12fd2008-12-12 06:00:12 +00001305 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1306 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001307 } else {
1308 Result = ParseExpression();
1309 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001310 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001311 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001312 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001313
Chris Lattner4b009652007-07-25 00:24:17 +00001314 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001315 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001316 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001317 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001318 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001319
1320 if (Tok.is(tok::r_paren))
1321 RParenLoc = ConsumeParen();
1322 else
1323 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001324
1325 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001326}
1327
Argiris Kirtzidis70c95822009-05-22 10:24:05 +00001328/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1329/// and we are at the left brace.
1330///
1331/// postfix-expression: [C99 6.5.2]
1332/// '(' type-name ')' '{' initializer-list '}'
1333/// '(' type-name ')' '{' initializer-list ',' '}'
1334///
1335Parser::OwningExprResult
1336Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1337 SourceLocation LParenLoc,
1338 SourceLocation RParenLoc) {
1339 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1340 if (!getLang().C99) // Compound literals don't exist in C90.
1341 Diag(LParenLoc, diag::ext_c99_compound_literal);
1342 OwningExprResult Result = ParseInitializer();
1343 if (!Result.isInvalid() && Ty)
1344 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1345 return move(Result);
1346}
1347
Chris Lattner4b009652007-07-25 00:24:17 +00001348/// ParseStringLiteralExpression - This handles the various token types that
1349/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1350/// translation phase #6].
1351///
1352/// primary-expression: [C99 6.5.1]
1353/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001354Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001355 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001356
Chris Lattner4b009652007-07-25 00:24:17 +00001357 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1358 // considered to be strings for concatenation purposes.
1359 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001360
Chris Lattner4b009652007-07-25 00:24:17 +00001361 do {
1362 StringToks.push_back(Tok);
1363 ConsumeStringToken();
1364 } while (isTokenStringLiteral());
1365
1366 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001367 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001368}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001369
1370/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1371///
1372/// argument-expression-list:
1373/// assignment-expression
1374/// argument-expression-list , assignment-expression
1375///
1376/// [C++] expression-list:
1377/// [C++] assignment-expression
1378/// [C++] expression-list , assignment-expression
1379///
1380bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1381 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001382 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001383 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001384 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001385
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001386 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001387
1388 if (Tok.isNot(tok::comma))
1389 return false;
1390 // Move to the next argument, remember where the comma was.
1391 CommaLocs.push_back(ConsumeToken());
1392 }
1393}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001394
Mike Stumpc1fddff2009-02-04 22:31:32 +00001395/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1396///
1397/// [clang] block-id:
1398/// [clang] specifier-qualifier-list block-declarator
1399///
1400void Parser::ParseBlockId() {
1401 // Parse the specifier-qualifier-list piece.
1402 DeclSpec DS;
1403 ParseSpecifierQualifierList(DS);
1404
1405 // Parse the block-declarator.
1406 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1407 ParseDeclarator(DeclaratorInfo);
Mike Stump115a0722009-04-29 19:03:13 +00001408
Mike Stump9e439c92009-04-29 21:40:37 +00001409 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1410 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1411 SourceLocation());
1412
Mike Stump115a0722009-04-29 19:03:13 +00001413 if (Tok.is(tok::kw___attribute)) {
1414 SourceLocation Loc;
1415 AttributeList *AttrList = ParseAttributes(&Loc);
1416 DeclaratorInfo.AddAttributes(AttrList, Loc);
1417 }
1418
Mike Stumpc1fddff2009-02-04 22:31:32 +00001419 // Inform sema that we are starting a block.
1420 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1421}
1422
Steve Narofffd5b19d2008-08-28 19:20:44 +00001423/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001424/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001425///
1426/// block-literal:
1427/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001428/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001429/// [clang] block-args:
1430/// [clang] '(' parameter-list ')'
1431///
Sebastian Redla2deb432008-12-13 15:32:12 +00001432Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001433 assert(Tok.is(tok::caret) && "block literal starts with ^");
1434 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001435
Chris Lattnere533c7d2009-03-05 07:32:12 +00001436 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1437 "block literal parsing");
1438
Steve Narofffd5b19d2008-08-28 19:20:44 +00001439 // Enter a scope to hold everything within the block. This includes the
1440 // argument decls, decls within the compound expression, etc. This also
1441 // allows determining whether a variable reference inside the block is
1442 // within or outside of the block.
Sebastian Redl0c986032009-02-09 18:23:29 +00001443 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1444 Scope::BreakScope | Scope::ContinueScope |
1445 Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001446
1447 // Inform sema that we are starting a block.
1448 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattnere533c7d2009-03-05 07:32:12 +00001449
Steve Narofffd5b19d2008-08-28 19:20:44 +00001450 // Parse the return type if present.
1451 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001452 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redl0c986032009-02-09 18:23:29 +00001453 // FIXME: Since the return type isn't actually parsed, it can't be used to
1454 // fill ParamInfo with an initial valid range, so do it manually.
1455 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redla2deb432008-12-13 15:32:12 +00001456
Steve Narofffd5b19d2008-08-28 19:20:44 +00001457 // If this block has arguments, parse them. There is no ambiguity here with
1458 // the expression case, because the expression case requires a parameter list.
1459 if (Tok.is(tok::l_paren)) {
1460 ParseParenDeclarator(ParamInfo);
1461 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redl0c986032009-02-09 18:23:29 +00001462 // SetIdentifier sets the source range end, but in this case we're past
1463 // that location.
1464 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001465 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001466 ParamInfo.SetRangeEnd(Tmp);
Chris Lattner34c61332009-04-25 08:06:05 +00001467 if (ParamInfo.isInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001468 // If there was an error parsing the arguments, they may have
1469 // tried to use ^(x+y) which requires an argument list. Just
1470 // skip the whole block literal.
Chris Lattnerd860cbd2009-04-18 20:05:34 +00001471 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001472 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001473 }
Mike Stump115a0722009-04-29 19:03:13 +00001474
1475 if (Tok.is(tok::kw___attribute)) {
1476 SourceLocation Loc;
1477 AttributeList *AttrList = ParseAttributes(&Loc);
1478 ParamInfo.AddAttributes(AttrList, Loc);
1479 }
1480
Mike Stumpc1fddff2009-02-04 22:31:32 +00001481 // Inform sema that we are starting a block.
1482 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stump1214b582009-04-14 18:24:37 +00001483 } else if (!Tok.is(tok::l_brace)) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001484 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001485 } else {
1486 // Otherwise, pretend we saw (void).
Douglas Gregor88a25f82009-02-18 07:07:28 +00001487 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1488 SourceLocation(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001489 0, 0, 0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00001490 false, SourceLocation(),
1491 false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001492 CaretLoc, ParamInfo),
Sebastian Redl0c986032009-02-09 18:23:29 +00001493 CaretLoc);
Mike Stump115a0722009-04-29 19:03:13 +00001494
1495 if (Tok.is(tok::kw___attribute)) {
1496 SourceLocation Loc;
1497 AttributeList *AttrList = ParseAttributes(&Loc);
1498 ParamInfo.AddAttributes(AttrList, Loc);
1499 }
1500
Mike Stumpc1fddff2009-02-04 22:31:32 +00001501 // Inform sema that we are starting a block.
1502 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001503 }
1504
Sebastian Redla2deb432008-12-13 15:32:12 +00001505
Sebastian Redl62261042008-12-09 20:22:58 +00001506 OwningExprResult Result(Actions, true);
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001507 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001508 // Saw something like: ^expr
1509 Diag(Tok, diag::err_expected_expression);
Chris Lattnerd860cbd2009-04-18 20:05:34 +00001510 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001511 return ExprError();
1512 }
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001513
1514 OwningStmtResult Stmt(ParseCompoundStatementBody());
1515 if (!Stmt.isInvalid())
1516 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1517 else
1518 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001519 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001520}