blob: 49c28eec75a6dc280ee7a7044f7c95c295e5ce97 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercde626a2006-08-12 08:13:25 +000010// 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
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff0ac012832008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000027#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000028using namespace clang;
29
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000030/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000031/// the C99 grammar. These have been named to relate with the C99 grammar
32/// productions. Low precedences numbers bind more weakly than high numbers.
33namespace prec {
34 enum Level {
35 Unknown = 0, // Not binary operator.
36 Comma = 1, // ,
37 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
38 Conditional = 3, // ?
39 LogicalOr = 4, // ||
40 LogicalAnd = 5, // &&
41 InclusiveOr = 6, // |
42 ExclusiveOr = 7, // ^
43 And = 8, // &
Chris Lattner9916c5c2006-10-27 05:24:37 +000044 Equality = 9, // ==, !=
45 Relational = 10, // >=, <=, >, <
46 Shift = 11, // <<, >>
47 Additive = 12, // -, +
48 Multiplicative = 13 // *, /, %
Chris Lattnercde626a2006-08-12 08:13:25 +000049 };
50}
51
52
53/// getBinOpPrecedence - Return the precedence of the specified binary operator
54/// token. This returns:
55///
56static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
57 switch (Kind) {
58 default: return prec::Unknown;
59 case tok::comma: return prec::Comma;
60 case tok::equal:
61 case tok::starequal:
62 case tok::slashequal:
63 case tok::percentequal:
64 case tok::plusequal:
65 case tok::minusequal:
66 case tok::lesslessequal:
67 case tok::greatergreaterequal:
68 case tok::ampequal:
69 case tok::caretequal:
70 case tok::pipeequal: return prec::Assignment;
71 case tok::question: return prec::Conditional;
72 case tok::pipepipe: return prec::LogicalOr;
73 case tok::ampamp: return prec::LogicalAnd;
74 case tok::pipe: return prec::InclusiveOr;
75 case tok::caret: return prec::ExclusiveOr;
76 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000077 case tok::exclaimequal:
78 case tok::equalequal: return prec::Equality;
79 case tok::lessequal:
80 case tok::less:
81 case tok::greaterequal:
82 case tok::greater: return prec::Relational;
83 case tok::lessless:
84 case tok::greatergreater: return prec::Shift;
85 case tok::plus:
86 case tok::minus: return prec::Additive;
87 case tok::percent:
88 case tok::slash:
89 case tok::star: return prec::Multiplicative;
90 }
91}
92
93
Chris Lattnerce7e21d2006-08-12 17:22:40 +000094/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000095/// operators.
96///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000097/// Note: we diverge from the C99 grammar when parsing the assignment-expression
98/// production. C99 specifies that the LHS of an assignment operator should be
99/// parsed as a unary-expression, but consistency dictates that it be a
100/// conditional-expession. In practice, the important thing here is that the
101/// LHS of an assignment has to be an l-value, which productions between
102/// unary-expression and conditional-expression don't produce. Because we want
103/// consistency, we parse the LHS as a conditional-expression, then check for
104/// l-value-ness in semantic analysis stages.
105///
Chris Lattnercde626a2006-08-12 08:13:25 +0000106/// multiplicative-expression: [C99 6.5.5]
107/// cast-expression
108/// multiplicative-expression '*' cast-expression
109/// multiplicative-expression '/' cast-expression
110/// multiplicative-expression '%' cast-expression
111///
112/// additive-expression: [C99 6.5.6]
113/// multiplicative-expression
114/// additive-expression '+' multiplicative-expression
115/// additive-expression '-' multiplicative-expression
116///
117/// shift-expression: [C99 6.5.7]
118/// additive-expression
119/// shift-expression '<<' additive-expression
120/// shift-expression '>>' additive-expression
121///
122/// relational-expression: [C99 6.5.8]
123/// shift-expression
124/// relational-expression '<' shift-expression
125/// relational-expression '>' shift-expression
126/// relational-expression '<=' shift-expression
127/// relational-expression '>=' shift-expression
128///
129/// equality-expression: [C99 6.5.9]
130/// relational-expression
131/// equality-expression '==' relational-expression
132/// equality-expression '!=' relational-expression
133///
134/// AND-expression: [C99 6.5.10]
135/// equality-expression
136/// AND-expression '&' equality-expression
137///
138/// exclusive-OR-expression: [C99 6.5.11]
139/// AND-expression
140/// exclusive-OR-expression '^' AND-expression
141///
142/// inclusive-OR-expression: [C99 6.5.12]
143/// exclusive-OR-expression
144/// inclusive-OR-expression '|' exclusive-OR-expression
145///
146/// logical-AND-expression: [C99 6.5.13]
147/// inclusive-OR-expression
148/// logical-AND-expression '&&' inclusive-OR-expression
149///
150/// logical-OR-expression: [C99 6.5.14]
151/// logical-AND-expression
152/// logical-OR-expression '||' logical-AND-expression
153///
154/// conditional-expression: [C99 6.5.15]
155/// logical-OR-expression
156/// logical-OR-expression '?' expression ':' conditional-expression
157/// [GNU] logical-OR-expression '?' ':' conditional-expression
158///
159/// assignment-expression: [C99 6.5.16]
160/// conditional-expression
161/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000162/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000163///
164/// assignment-operator: one of
165/// = *= /= %= += -= <<= >>= &= ^= |=
166///
167/// expression: [C99 6.5.17]
168/// assignment-expression
169/// expression ',' assignment-expression
170///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000171Parser::ExprResult Parser::ParseExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000172 if (Tok.is(tok::kw_throw))
173 return ParseThrowExpression();
174
Chris Lattnercde626a2006-08-12 08:13:25 +0000175 ExprResult LHS = ParseCastExpression(false);
176 if (LHS.isInvalid) return LHS;
177
178 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
179}
180
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000181/// This routine is called when the '@' is seen and consumed.
182/// Current token is an Identifier and is not a 'try'. This
Chris Lattner644e1b72007-10-03 22:03:06 +0000183/// routine is necessary to disambiguate @try-statement from,
184/// for example, @encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000185///
Fariborz Jahanian90814572007-10-04 20:19:06 +0000186Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Steve Naroff126b4d82007-10-15 20:55:58 +0000187 ExprResult LHS = ParseObjCAtExpression(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000188 if (LHS.isInvalid) return LHS;
189
190 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
191}
192
Chris Lattner0c6c0342006-08-12 18:12:45 +0000193/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
194///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000195Parser::ExprResult Parser::ParseAssignmentExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000196 if (Tok.is(tok::kw_throw))
197 return ParseThrowExpression();
198
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000199 ExprResult LHS = ParseCastExpression(false);
200 if (LHS.isInvalid) return LHS;
201
202 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
203}
204
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000205/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
206/// where part of an objc message send has already been parsed. In this case
207/// LBracLoc indicates the location of the '[' of the message send, and either
208/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
209/// message.
210///
211/// Since this handles full assignment-expression's, it handles postfix
212/// expressions and other binary operators for these expressions as well.
213Parser::ExprResult
214Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
215 IdentifierInfo *ReceiverName,
216 ExprTy *ReceiverExpr) {
217 ExprResult R = ParseObjCMessageExpressionBody(LBracLoc, ReceiverName,
218 ReceiverExpr);
219 if (R.isInvalid) return R;
220 R = ParsePostfixExpressionSuffix(R);
221 if (R.isInvalid) return R;
222 return ParseRHSOfBinaryExpression(R, 2);
223}
224
225
Chris Lattner3b561a32006-08-13 00:12:11 +0000226Parser::ExprResult Parser::ParseConstantExpression() {
227 ExprResult LHS = ParseCastExpression(false);
228 if (LHS.isInvalid) return LHS;
229
Chris Lattner3b561a32006-08-13 00:12:11 +0000230 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
231}
232
Chris Lattnercde626a2006-08-12 08:13:25 +0000233/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
234/// LHS and has a precedence of at least MinPrec.
235Parser::ExprResult
236Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
237 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000238 SourceLocation ColonLoc;
239
Chris Lattnercde626a2006-08-12 08:13:25 +0000240 while (1) {
241 // If this token has a lower precedence than we are allowed to parse (e.g.
242 // because we are called recursively, or because the token is not a binop),
243 // then we are done!
244 if (NextTokPrec < MinPrec)
245 return LHS;
246
247 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000248 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000249 ConsumeToken();
250
Chris Lattner96c3deb2006-08-12 17:13:08 +0000251 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000252 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000253 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000254 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000255 // Handle this production specially:
256 // logical-OR-expression '?' expression ':' conditional-expression
257 // In particular, the RHS of the '?' is 'expression', not
258 // 'logical-OR-expression' as we might expect.
259 TernaryMiddle = ParseExpression();
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000260 if (TernaryMiddle.isInvalid) {
261 Actions.DeleteExpr(LHS.Val);
262 return TernaryMiddle;
263 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000264 } else {
265 // Special case handling of "X ? Y : Z" where Y is empty:
266 // logical-OR-expression '?' ':' conditional-expression [GNU]
267 TernaryMiddle = ExprResult(false);
268 Diag(Tok, diag::ext_gnu_conditional_expr);
269 }
270
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000271 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000272 Diag(Tok, diag::err_expected_colon);
273 Diag(OpToken, diag::err_matching, "?");
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000274 Actions.DeleteExpr(LHS.Val);
275 Actions.DeleteExpr(TernaryMiddle.Val);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000276 return ExprResult(true);
277 }
278
279 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000280 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000281 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000282
283 // Parse another leaf here for the RHS of the operator.
284 ExprResult RHS = ParseCastExpression(false);
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000285 if (RHS.isInvalid) {
286 Actions.DeleteExpr(LHS.Val);
287 Actions.DeleteExpr(TernaryMiddle.Val);
288 return RHS;
289 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000290
291 // Remember the precedence of this operator and get the precedence of the
292 // operator immediately to the right of the RHS.
293 unsigned ThisPrec = NextTokPrec;
294 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000295
296 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000297 bool isRightAssoc = ThisPrec == prec::Conditional ||
298 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000299
300 // Get the precedence of the operator to the right of the RHS. If it binds
301 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000302 if (ThisPrec < NextTokPrec ||
303 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000304 // If this is left-associative, only parse things on the RHS that bind
305 // more tightly than the current operator. If it is left-associative, it
306 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
307 // A=(B=(C=D)), where each paren is a level of recursion here.
308 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000309 if (RHS.isInvalid) {
310 Actions.DeleteExpr(LHS.Val);
311 Actions.DeleteExpr(TernaryMiddle.Val);
312 return RHS;
313 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000314
315 NextTokPrec = getBinOpPrecedence(Tok.getKind());
316 }
317 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
318
Chris Lattner319079c2007-08-31 05:01:50 +0000319 if (!LHS.isInvalid) {
320 // Combine the LHS and RHS into the LHS (e.g. build AST).
321 if (TernaryMiddle.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000322 LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(),
Chris Lattner319079c2007-08-31 05:01:50 +0000323 LHS.Val, RHS.Val);
324 else
Steve Naroff83895f72007-09-16 03:34:24 +0000325 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Chris Lattner319079c2007-08-31 05:01:50 +0000326 LHS.Val, TernaryMiddle.Val, RHS.Val);
327 } else {
328 // We had a semantic error on the LHS. Just free the RHS and continue.
329 Actions.DeleteExpr(TernaryMiddle.Val);
330 Actions.DeleteExpr(RHS.Val);
331 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000332 }
333}
334
Chris Lattnereaf06592006-08-11 02:02:23 +0000335/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
336/// true, parse a unary-expression.
337///
Chris Lattner4564bc12006-08-10 23:14:52 +0000338/// cast-expression: [C99 6.5.4]
339/// unary-expression
340/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000341///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000342/// unary-expression: [C99 6.5.3]
343/// postfix-expression
344/// '++' unary-expression
345/// '--' unary-expression
346/// unary-operator cast-expression
347/// 'sizeof' unary-expression
348/// 'sizeof' '(' type-name ')'
349/// [GNU] '__alignof' unary-expression
350/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000351/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000352/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000353///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000354/// unary-operator: one of
355/// '&' '*' '+' '-' '~' '!'
356/// [GNU] '__extension__' '__real' '__imag'
357///
Chris Lattner52a99e52006-08-10 20:56:00 +0000358/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000359/// [C99] identifier
360// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000361/// constant
362/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000363/// [C++] boolean-literal [C++ 2.13.5]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000364/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000365/// '__func__' [C99 6.4.2.2]
366/// [GNU] '__FUNCTION__'
367/// [GNU] '__PRETTY_FUNCTION__'
368/// [GNU] '(' compound-statement ')'
369/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
370/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
371/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
372/// assign-expr ')'
373/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000374/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000375/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000376/// [OBJC] '@protocol' '(' identifier ')'
377/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000378/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000379/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
380/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000381/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
382/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
383/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
384/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000385/// [C++] 'this' [C++ 9.3.2]
Steve Naroff0ac012832008-08-28 19:20:44 +0000386/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000387///
388/// constant: [C99 6.4.4]
389/// integer-constant
390/// floating-constant
391/// enumeration-constant -> identifier
392/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000393///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000394/// id-expression: [C++ 5.1]
395/// unqualified-id
396/// qualified-id [TODO]
397///
398/// unqualified-id: [C++ 5.1]
399/// identifier
400/// operator-function-id
401/// conversion-function-id [TODO]
402/// '~' class-name [TODO]
403/// template-id [TODO]
Chris Lattner89c50c62006-08-11 06:41:18 +0000404Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
405 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000406 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000407
Chris Lattner81b576e2006-08-11 02:13:20 +0000408 // This handles all of cast-expression, unary-expression, postfix-expression,
409 // and primary-expression. We handle them together like this for efficiency
410 // and to simplify handling of an expression starting with a '(' token: which
411 // may be one of a parenthesized expression, cast-expression, compound literal
412 // expression, or statement expression.
413 //
414 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000415 // call ParsePostfixExpressionSuffix to handle the postfix expression
416 // suffixes. Cases that cannot be followed by postfix exprs should
417 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000418 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000419 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000420 // If this expression is limited to being a unary-expression, the parent can
421 // not start a cast expression.
422 ParenParseOption ParenExprType =
423 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000424 TypeTy *CastTy;
425 SourceLocation LParenLoc = Tok.getLocation();
426 SourceLocation RParenLoc;
427 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000428 if (Res.isInvalid) return Res;
429
Chris Lattner81b576e2006-08-11 02:13:20 +0000430 switch (ParenExprType) {
431 case SimpleExpr: break; // Nothing else to do.
432 case CompoundStmt: break; // Nothing else to do.
433 case CompoundLiteral:
434 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
435 // postfix-expression exist, parse them now.
436 break;
437 case CastExpr:
438 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
439 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000440 // TODO: For cast expression with CastTy.
441 Res = ParseCastExpression(false);
442 if (!Res.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000443 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000444 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000445 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000446
447 // These can be followed by postfix-expr pieces.
448 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000449 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000450
Chris Lattner52a99e52006-08-10 20:56:00 +0000451 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000452 case tok::numeric_constant:
453 // constant: integer-constant
454 // constant: floating-constant
455
Steve Naroff83895f72007-09-16 03:34:24 +0000456 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000457 ConsumeToken();
458
459 // These can be followed by postfix-expr pieces.
460 return ParsePostfixExpressionSuffix(Res);
461
Bill Wendling4073ed52007-02-13 01:51:42 +0000462 case tok::kw_true:
463 case tok::kw_false:
464 return ParseCXXBoolLiteral();
465
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000466 case tok::identifier: {
467 if (getLang().CPlusPlus &&
468 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
469 // Handle C++ function-style cast, e.g. "T(4.5)" where T is a typedef for
470 // double.
471 goto HandleType;
472 }
473
474 // primary-expression: identifier
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000475 // unqualified-id: identifier
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000476 // constant: enumeration-constant
477
Chris Lattnerac18be92006-11-20 06:49:47 +0000478 // Consume the identifier so that we can see if it is followed by a '('.
479 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
480 // need to know whether or not this identifier is a function designator or
481 // not.
482 IdentifierInfo &II = *Tok.getIdentifierInfo();
483 SourceLocation L = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000484 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner17ed4872006-11-20 04:58:19 +0000485 // These can be followed by postfix-expr pieces.
486 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerac18be92006-11-20 06:49:47 +0000487 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000488 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000489 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000490 ConsumeToken();
491 // These can be followed by postfix-expr pieces.
492 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000493 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
494 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
495 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000496 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000497 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000498 // These can be followed by postfix-expr pieces.
499 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000500 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000501 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000502 Res = ParseStringLiteralExpression();
503 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000504 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
505 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000506 case tok::kw___builtin_va_arg:
507 case tok::kw___builtin_offsetof:
508 case tok::kw___builtin_choose_expr:
Nate Begeman1e36a852008-01-17 17:46:27 +0000509 case tok::kw___builtin_overload:
Chris Lattnerf8339772006-08-10 22:01:51 +0000510 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000511 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000512 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000513 case tok::minusminus: { // unary-expression: '--' unary-expression
514 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000515 Res = ParseCastExpression(true);
516 if (!Res.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000517 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000518 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000519 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000520 case tok::amp: // unary-expression: '&' cast-expression
521 case tok::star: // unary-expression: '*' cast-expression
522 case tok::plus: // unary-expression: '+' cast-expression
523 case tok::minus: // unary-expression: '-' cast-expression
524 case tok::tilde: // unary-expression: '~' cast-expression
525 case tok::exclaim: // unary-expression: '!' cast-expression
526 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000527 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000528 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000529 Res = ParseCastExpression(false);
530 if (!Res.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000531 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000532 return Res;
Chris Lattnerc43926f2008-02-02 20:20:10 +0000533 }
534
535 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
536 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000537 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000538 SourceLocation SavedLoc = ConsumeToken();
539 Res = ParseCastExpression(false);
540 if (!Res.isInvalid)
541 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattnerc43926f2008-02-02 20:20:10 +0000542 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000543 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000544 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
545 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000546 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000547 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
548 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000549 // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000550 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000551 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000552 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000553 if (Tok.isNot(tok::identifier)) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000554 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000555 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000556 }
Chris Lattnereefa10e2007-05-28 06:56:27 +0000557
558 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000559 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000560 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000561 ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000562 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000563 }
Chris Lattner29375652006-12-04 18:06:35 +0000564 case tok::kw_const_cast:
565 case tok::kw_dynamic_cast:
566 case tok::kw_reinterpret_cast:
567 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000568 Res = ParseCXXCasts();
569 // These can be followed by postfix-expr pieces.
570 return ParsePostfixExpressionSuffix(Res);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000571 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000572 Res = ParseCXXThis();
573 // This can be followed by postfix-expr pieces.
574 return ParsePostfixExpressionSuffix(Res);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000575
576 case tok::kw_char:
577 case tok::kw_wchar_t:
578 case tok::kw_bool:
579 case tok::kw_short:
580 case tok::kw_int:
581 case tok::kw_long:
582 case tok::kw_signed:
583 case tok::kw_unsigned:
584 case tok::kw_float:
585 case tok::kw_double:
586 case tok::kw_void:
587 case tok::kw_typeof: {
588 if (!getLang().CPlusPlus)
589 goto UnhandledToken;
590 HandleType:
591 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
592 //
593 DeclSpec DS;
594 ParseCXXSimpleTypeSpecifier(DS);
595 if (Tok.isNot(tok::l_paren))
596 return Diag(Tok.getLocation(), diag::err_expected_lparen_after_type,
597 DS.getSourceRange());
598
599 Res = ParseCXXTypeConstructExpression(DS);
600 // This can be followed by postfix-expr pieces.
601 return ParsePostfixExpressionSuffix(Res);
602 }
603
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000604 case tok::kw_operator: {
605 SourceLocation OperatorLoc = Tok.getLocation();
606 if (IdentifierInfo *II = MaybeParseOperatorFunctionId()) {
607 Res = Actions.ActOnIdentifierExpr(CurScope, OperatorLoc, *II,
608 Tok.is(tok::l_paren));
609 // These can be followed by postfix-expr pieces.
610 return ParsePostfixExpressionSuffix(Res);
611 }
612 break;
613 }
614
Chris Lattner644e1b72007-10-03 22:03:06 +0000615 case tok::at: {
616 SourceLocation AtLoc = ConsumeToken();
Steve Naroff126b4d82007-10-15 20:55:58 +0000617 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000618 }
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000619 case tok::l_square:
Steve Naroff126b4d82007-10-15 20:55:58 +0000620 // These can be followed by postfix-expr pieces.
Chris Lattner2fdcddd2008-05-09 05:28:21 +0000621 if (getLang().ObjC1)
622 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
623 // FALL THROUGH.
Steve Naroff0ac012832008-08-28 19:20:44 +0000624 case tok::caret:
625 if (getLang().Blocks)
626 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
627 Diag(Tok, diag::err_expected_expression);
628 return ExprResult(true);
Chris Lattner52a99e52006-08-10 20:56:00 +0000629 default:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000630 UnhandledToken:
Chris Lattner52a99e52006-08-10 20:56:00 +0000631 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000632 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000633 }
634
Chris Lattner20c6a452006-08-12 17:40:43 +0000635 // unreachable.
636 abort();
637}
638
639/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
640/// is parsed, this method parses any suffixes that apply.
641///
642/// postfix-expression: [C99 6.5.2]
643/// primary-expression
644/// postfix-expression '[' expression ']'
645/// postfix-expression '(' argument-expression-list[opt] ')'
646/// postfix-expression '.' identifier
647/// postfix-expression '->' identifier
648/// postfix-expression '++'
649/// postfix-expression '--'
650/// '(' type-name ')' '{' initializer-list '}'
651/// '(' type-name ')' '{' initializer-list ',' '}'
652///
653/// argument-expression-list: [C99 6.5.2]
654/// argument-expression
655/// argument-expression-list ',' assignment-expression
656///
657Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000658
Chris Lattnerf8339772006-08-10 22:01:51 +0000659 // Now that the primary-expression piece of the postfix-expression has been
660 // parsed, see if there are any postfix-expression pieces here.
661 SourceLocation Loc;
662 while (1) {
663 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000664 default: // Not a postfix-expression suffix.
665 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000666 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000667 Loc = ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000668 ExprResult Idx = ParseExpression();
669
670 SourceLocation RLoc = Tok.getLocation();
671
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000672 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square))
Steve Naroff83895f72007-09-16 03:34:24 +0000673 LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
Steve Narofff1e53692007-03-23 22:27:02 +0000674 else
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000675 LHS = ExprResult(true);
676
Chris Lattner89c50c62006-08-11 06:41:18 +0000677 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000678 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000679 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000680 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000681
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000682 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000683 ExprListTy ArgExprs;
684 CommaLocsTy CommaLocs;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000685
Chris Lattner04132372006-10-16 06:12:55 +0000686 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000687
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000688 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000689 if (ParseExpressionList(ArgExprs, CommaLocs)) {
690 SkipUntil(tok::r_paren);
691 return ExprResult(true);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000692 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000693 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000694
Chris Lattner89c50c62006-08-11 06:41:18 +0000695 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000696 if (!LHS.isInvalid && Tok.is(tok::r_paren)) {
Chris Lattnere165d942006-08-24 04:40:38 +0000697 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
698 "Unexpected number of commas!");
Steve Naroff83895f72007-09-16 03:34:24 +0000699 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000700 &CommaLocs[0], Tok.getLocation());
701 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000702
Chris Lattner5abb82c2007-07-21 05:18:12 +0000703 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000704 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000705 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000706 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000707 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000708 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000709 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000710
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000711 if (Tok.isNot(tok::identifier)) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000712 Diag(Tok, diag::err_expected_ident);
713 return ExprResult(true);
714 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000715
716 if (!LHS.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000717 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000718 Tok.getLocation(),
719 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000720 ConsumeToken();
721 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000722 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000723 case tok::plusplus: // postfix-expression: postfix-expression '++'
724 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000725 if (!LHS.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000726 LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
Chris Lattnerae319692006-10-25 03:49:28 +0000727 LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000728 ConsumeToken();
729 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000730 }
731 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000732}
733
Chris Lattner20c6a452006-08-12 17:40:43 +0000734
Chris Lattner81b576e2006-08-11 02:13:20 +0000735/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
736/// unary-expression: [C99 6.5.3]
737/// 'sizeof' unary-expression
738/// 'sizeof' '(' type-name ')'
739/// [GNU] '__alignof' unary-expression
740/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000741/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000742Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +0000743 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
744 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +0000745 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +0000746 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000747 ConsumeToken();
748
749 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000750 ExprResult Operand;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000751 if (Tok.isNot(tok::l_paren)) {
Chris Lattner26115ac2006-08-24 06:10:04 +0000752 Operand = ParseCastExpression(true);
753 } else {
754 // If it starts with a '(', we know that it is either a parenthesized
755 // type-name, or it is a unary-expression that starts with a compound
756 // literal, or starts with a primary-expression that is a parenthesized
757 // expression.
758 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000759 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000760 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000761 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000762
763 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
764 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner47791a42007-11-13 20:50:37 +0000765 if (ExprType == CastExpr)
Steve Naroff83895f72007-09-16 03:34:24 +0000766 return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(),
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000767 OpTok.is(tok::kw_sizeof),
Chris Lattner26da7302006-08-24 06:49:19 +0000768 LParenLoc, CastTy, RParenLoc);
Chris Lattner47791a42007-11-13 20:50:37 +0000769
770 // If this is a parenthesized expression, it is the start of a
771 // unary-expression, but doesn't include any postfix pieces. Parse these
772 // now if present.
773 Operand = ParsePostfixExpressionSuffix(Operand);
Chris Lattner26115ac2006-08-24 06:10:04 +0000774 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000775
Chris Lattner26115ac2006-08-24 06:10:04 +0000776 // If we get here, the operand to the sizeof/alignof was an expresion.
777 if (!Operand.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000778 Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(),
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000779 Operand.Val);
Chris Lattner26115ac2006-08-24 06:10:04 +0000780 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000781}
782
Chris Lattner11124352006-08-12 19:16:08 +0000783/// ParseBuiltinPrimaryExpression
784///
785/// primary-expression: [C99 6.5.1]
786/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
787/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
788/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
789/// assign-expr ')'
790/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman1e36a852008-01-17 17:46:27 +0000791/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner11124352006-08-12 19:16:08 +0000792///
793/// [GNU] offsetof-member-designator:
794/// [GNU] identifier
795/// [GNU] offsetof-member-designator '.' identifier
796/// [GNU] offsetof-member-designator '[' expression ']'
797///
798Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
799 ExprResult Res(false);
Chris Lattner11124352006-08-12 19:16:08 +0000800 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
801
802 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000803 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000804
805 // All of these start with an open paren.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000806 if (Tok.isNot(tok::l_paren)) {
Chris Lattner11124352006-08-12 19:16:08 +0000807 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
808 return ExprResult(true);
809 }
810
Chris Lattner04132372006-10-16 06:12:55 +0000811 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000812 // TODO: Build AST.
813
Chris Lattner11124352006-08-12 19:16:08 +0000814 switch (T) {
815 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000816 case tok::kw___builtin_va_arg: {
817 ExprResult Expr = ParseAssignmentExpression();
818 if (Expr.isInvalid) {
Chris Lattner11124352006-08-12 19:16:08 +0000819 SkipUntil(tok::r_paren);
Eli Friedman002ad122008-08-20 22:07:34 +0000820 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000821 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000822
Chris Lattner6d7e6342006-08-15 03:41:14 +0000823 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000824 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000825
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000826 TypeTy *Ty = ParseTypeName();
Chris Lattner5ad4f462007-08-30 15:52:49 +0000827
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000828 if (Tok.isNot(tok::r_paren)) {
829 Diag(Tok, diag::err_expected_rparen);
830 return ExprResult(true);
831 }
832 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +0000833 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000834 }
Chris Lattner687d6092007-08-30 15:51:11 +0000835 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +0000836 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner687d6092007-08-30 15:51:11 +0000837 TypeTy *Ty = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000838
Chris Lattner6d7e6342006-08-15 03:41:14 +0000839 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000840 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000841
842 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000843 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000844 Diag(Tok, diag::err_expected_ident);
845 SkipUntil(tok::r_paren);
846 return true;
847 }
848
849 // Keep track of the various subcomponents we see.
850 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
851
852 Comps.push_back(Action::OffsetOfComponent());
853 Comps.back().isBrackets = false;
854 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
855 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000856
Chris Lattner11124352006-08-12 19:16:08 +0000857 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000858 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +0000859 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +0000860 Comps.push_back(Action::OffsetOfComponent());
861 Comps.back().isBrackets = false;
862 Comps.back().LocStart = ConsumeToken();
Chris Lattner11124352006-08-12 19:16:08 +0000863
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000864 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000865 Diag(Tok, diag::err_expected_ident);
866 SkipUntil(tok::r_paren);
867 return true;
868 }
869 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
870 Comps.back().LocEnd = ConsumeToken();
871
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000872 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +0000873 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +0000874 Comps.push_back(Action::OffsetOfComponent());
875 Comps.back().isBrackets = true;
876 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000877 Res = ParseExpression();
878 if (Res.isInvalid) {
879 SkipUntil(tok::r_paren);
880 return Res;
881 }
Chris Lattner687d6092007-08-30 15:51:11 +0000882 Comps.back().U.E = Res.Val;
Chris Lattner11124352006-08-12 19:16:08 +0000883
Chris Lattner687d6092007-08-30 15:51:11 +0000884 Comps.back().LocEnd =
885 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000886 } else if (Tok.is(tok::r_paren)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000887 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner5ad4f462007-08-30 15:52:49 +0000888 Comps.size(), ConsumeParen());
889 break;
Chris Lattner11124352006-08-12 19:16:08 +0000890 } else {
Chris Lattner687d6092007-08-30 15:51:11 +0000891 // Error occurred.
892 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000893 }
894 }
895 break;
Chris Lattner687d6092007-08-30 15:51:11 +0000896 }
Steve Naroff9efdabc2007-08-03 21:21:27 +0000897 case tok::kw___builtin_choose_expr: {
898 ExprResult Cond = ParseAssignmentExpression();
899 if (Cond.isInvalid) {
900 SkipUntil(tok::r_paren);
901 return Cond;
902 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000903 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000904 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000905
Steve Naroff9efdabc2007-08-03 21:21:27 +0000906 ExprResult Expr1 = ParseAssignmentExpression();
907 if (Expr1.isInvalid) {
908 SkipUntil(tok::r_paren);
909 return Expr1;
910 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000911 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000912 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000913
Steve Naroff9efdabc2007-08-03 21:21:27 +0000914 ExprResult Expr2 = ParseAssignmentExpression();
915 if (Expr2.isInvalid) {
916 SkipUntil(tok::r_paren);
917 return Expr2;
918 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000919 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000920 Diag(Tok, diag::err_expected_rparen);
921 return ExprResult(true);
922 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000923 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
Chris Lattner5ad4f462007-08-30 15:52:49 +0000924 ConsumeParen());
925 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +0000926 }
Nate Begeman1e36a852008-01-17 17:46:27 +0000927 case tok::kw___builtin_overload: {
928 llvm::SmallVector<ExprTy*, 8> ArgExprs;
929 llvm::SmallVector<SourceLocation, 8> CommaLocs;
930
931 // For each iteration through the loop look for assign-expr followed by a
932 // comma. If there is no comma, break and attempt to match r-paren.
933 if (Tok.isNot(tok::r_paren)) {
934 while (1) {
935 ExprResult ArgExpr = ParseAssignmentExpression();
936 if (ArgExpr.isInvalid) {
937 SkipUntil(tok::r_paren);
938 return ExprResult(true);
939 } else
940 ArgExprs.push_back(ArgExpr.Val);
941
942 if (Tok.isNot(tok::comma))
943 break;
944 // Move to the next argument, remember where the comma was.
945 CommaLocs.push_back(ConsumeToken());
946 }
947 }
948
949 // Attempt to consume the r-paren
950 if (Tok.isNot(tok::r_paren)) {
951 Diag(Tok, diag::err_expected_rparen);
952 SkipUntil(tok::r_paren);
953 return ExprResult(true);
954 }
Nate Begeman1e36a852008-01-17 17:46:27 +0000955 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(),
956 &CommaLocs[0], StartLoc, ConsumeParen());
957 break;
958 }
Chris Lattner11124352006-08-12 19:16:08 +0000959 case tok::kw___builtin_types_compatible_p:
Steve Naroff788d8642007-08-01 23:45:51 +0000960 TypeTy *Ty1 = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000961
Chris Lattner6d7e6342006-08-15 03:41:14 +0000962 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000963 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000964
Steve Naroff788d8642007-08-01 23:45:51 +0000965 TypeTy *Ty2 = ParseTypeName();
966
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000967 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +0000968 Diag(Tok, diag::err_expected_rparen);
969 return ExprResult(true);
970 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000971 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +0000972 break;
Chris Lattner11124352006-08-12 19:16:08 +0000973 }
974
Chris Lattner11124352006-08-12 19:16:08 +0000975 // These can be followed by postfix-expr pieces because they are
976 // primary-expressions.
977 return ParsePostfixExpressionSuffix(Res);
978}
979
Chris Lattner4add4e62006-08-11 01:33:00 +0000980/// ParseParenExpression - This parses the unit that starts with a '(' token,
981/// based on what is allowed by ExprType. The actual thing parsed is returned
982/// in ExprType.
983///
984/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000985/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000986/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
987/// postfix-expression: [C99 6.5.2]
988/// '(' type-name ')' '{' initializer-list '}'
989/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000990/// cast-expression: [C99 6.5.4]
991/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000992///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000993Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
994 TypeTy *&CastTy,
995 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000996 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +0000997 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner366727f2007-07-24 16:58:17 +0000998 ExprResult Result(true);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000999 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +00001000
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001001 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001002 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattnercac27a52007-08-31 21:49:55 +00001003 Parser::StmtResult Stmt = ParseCompoundStatement(true);
Chris Lattner4add4e62006-08-11 01:33:00 +00001004 ExprType = CompoundStmt;
Chris Lattner366727f2007-07-24 16:58:17 +00001005
1006 // If the substmt parsed correctly, build the AST node.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001007 if (!Stmt.isInvalid && Tok.is(tok::r_paren))
Steve Naroff66356bd2007-09-16 14:56:35 +00001008 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
Chris Lattner366727f2007-07-24 16:58:17 +00001009
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001010 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001011 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001012 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001013
1014 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001015 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001016 RParenLoc = ConsumeParen();
1017 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001018 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001019
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001020 if (Tok.is(tok::l_brace)) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001021 if (!getLang().C99) // Compound literals don't exist in C90.
1022 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001023 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +00001024 ExprType = CompoundLiteral;
Steve Narofffbd09832007-07-19 01:06:55 +00001025 if (!Result.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +00001026 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Chris Lattner4add4e62006-08-11 01:33:00 +00001027 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +00001028 // Note that this doesn't parse the subsequence cast-expression, it just
1029 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +00001030 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001031 CastTy = Ty;
1032 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +00001033 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001034 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001035 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001036 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001037 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +00001038 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001039 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001040 ExprType = SimpleExpr;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001041 if (!Result.isInvalid && Tok.is(tok::r_paren))
Steve Naroff83895f72007-09-16 03:34:24 +00001042 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +00001043 }
Chris Lattnerc951dae2006-08-10 04:23:57 +00001044
Chris Lattner4564bc12006-08-10 23:14:52 +00001045 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +00001046 if (Result.isInvalid)
1047 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001048 else {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001049 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001050 RParenLoc = ConsumeParen();
1051 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001052 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001053 }
Chris Lattner1b926492006-08-23 06:42:10 +00001054
Chris Lattner89c50c62006-08-11 06:41:18 +00001055 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +00001056}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001057
Chris Lattnerd3e98952006-10-06 05:22:26 +00001058/// ParseStringLiteralExpression - This handles the various token types that
1059/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1060/// translation phase #6].
1061///
1062/// primary-expression: [C99 6.5.1]
1063/// string-literal
1064Parser::ExprResult Parser::ParseStringLiteralExpression() {
1065 assert(isTokenStringLiteral() && "Not a string literal!");
1066
1067 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1068 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001069 llvm::SmallVector<Token, 4> StringToks;
Chris Lattnerd3e98952006-10-06 05:22:26 +00001070
Chris Lattnerd3e98952006-10-06 05:22:26 +00001071 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001072 StringToks.push_back(Tok);
1073 ConsumeStringToken();
1074 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001075
1076 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroff83895f72007-09-16 03:34:24 +00001077 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001078}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001079
1080/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1081///
1082/// argument-expression-list:
1083/// assignment-expression
1084/// argument-expression-list , assignment-expression
1085///
1086/// [C++] expression-list:
1087/// [C++] assignment-expression
1088/// [C++] expression-list , assignment-expression
1089///
1090bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1091 while (1) {
1092 ExprResult Expr = ParseAssignmentExpression();
1093 if (Expr.isInvalid)
1094 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001095
1096 Exprs.push_back(Expr.Val);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001097
1098 if (Tok.isNot(tok::comma))
1099 return false;
1100 // Move to the next argument, remember where the comma was.
1101 CommaLocs.push_back(ConsumeToken());
1102 }
1103}
Steve Naroff0ac012832008-08-28 19:20:44 +00001104
1105/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001106/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001107///
1108/// block-literal:
1109/// [clang] '^' block-args[opt] compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001110/// [clang] block-args:
1111/// [clang] '(' parameter-list ')'
1112///
1113Parser::ExprResult Parser::ParseBlockLiteralExpression() {
1114 assert(Tok.is(tok::caret) && "block literal starts with ^");
1115 SourceLocation CaretLoc = ConsumeToken();
1116
1117 // Enter a scope to hold everything within the block. This includes the
1118 // argument decls, decls within the compound expression, etc. This also
1119 // allows determining whether a variable reference inside the block is
1120 // within or outside of the block.
1121 EnterScope(Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1122 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001123
1124 // Inform sema that we are starting a block.
1125 Actions.ActOnBlockStart(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001126
1127 // Parse the return type if present.
1128 DeclSpec DS;
1129 Declarator ParamInfo(DS, Declarator::PrototypeContext);
1130
1131 // If this block has arguments, parse them. There is no ambiguity here with
1132 // the expression case, because the expression case requires a parameter list.
1133 if (Tok.is(tok::l_paren)) {
1134 ParseParenDeclarator(ParamInfo);
1135 // Parse the pieces after the identifier as if we had "int(...)".
1136 ParamInfo.SetIdentifier(0, CaretLoc);
1137 if (ParamInfo.getInvalidType()) {
1138 // If there was an error parsing the arguments, they may have tried to use
1139 // ^(x+y) which requires an argument list. Just skip the whole block
1140 // literal.
1141 ExitScope();
1142 return true;
1143 }
1144 } else {
1145 // Otherwise, pretend we saw (void).
1146 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001147 0, 0, 0, CaretLoc));
Steve Naroff0ac012832008-08-28 19:20:44 +00001148 }
1149
1150 // Inform sema that we are starting a block.
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001151 Actions.ActOnBlockArguments(ParamInfo);
Steve Naroff0ac012832008-08-28 19:20:44 +00001152
Steve Naroff7a147c62008-09-16 23:11:46 +00001153 ExprResult Result = true;
Steve Naroff0ac012832008-08-28 19:20:44 +00001154 if (Tok.is(tok::l_brace)) {
1155 StmtResult Stmt = ParseCompoundStatementBody();
1156 if (!Stmt.isInvalid) {
1157 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.Val, CurScope);
1158 } else {
1159 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001160 }
1161 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001162 ExitScope();
1163 return Result;
1164}
1165