blob: 46e967ec88432c4d208233f8ee48ada7973f25b6 [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 Lattnerdaa5c002008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000026#include "AstGuard.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 {
36 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 };
51}
52
53
54/// getBinOpPrecedence - Return the precedence of the specified binary operator
55/// token. This returns:
56///
57static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
58 switch (Kind) {
59 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
78 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
82 case tok::greaterequal:
83 case tok::greater: return prec::Relational;
84 case tok::lessless:
85 case tok::greatergreater: return prec::Shift;
86 case tok::plus:
87 case tok::minus: return prec::Additive;
88 case tok::percent:
89 case tok::slash:
90 case tok::star: return prec::Multiplicative;
91 }
92}
93
94
95/// ParseExpression - Simple precedence-based parser for binary/ternary
96/// operators.
97///
98/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
107/// multiplicative-expression: [C99 6.5.5]
108/// cast-expression
109/// multiplicative-expression '*' cast-expression
110/// multiplicative-expression '/' cast-expression
111/// multiplicative-expression '%' cast-expression
112///
113/// additive-expression: [C99 6.5.6]
114/// multiplicative-expression
115/// additive-expression '+' multiplicative-expression
116/// additive-expression '-' multiplicative-expression
117///
118/// shift-expression: [C99 6.5.7]
119/// additive-expression
120/// shift-expression '<<' additive-expression
121/// shift-expression '>>' additive-expression
122///
123/// relational-expression: [C99 6.5.8]
124/// shift-expression
125/// relational-expression '<' shift-expression
126/// relational-expression '>' shift-expression
127/// relational-expression '<=' shift-expression
128/// relational-expression '>=' shift-expression
129///
130/// equality-expression: [C99 6.5.9]
131/// relational-expression
132/// equality-expression '==' relational-expression
133/// equality-expression '!=' relational-expression
134///
135/// AND-expression: [C99 6.5.10]
136/// equality-expression
137/// AND-expression '&' equality-expression
138///
139/// exclusive-OR-expression: [C99 6.5.11]
140/// AND-expression
141/// exclusive-OR-expression '^' AND-expression
142///
143/// inclusive-OR-expression: [C99 6.5.12]
144/// exclusive-OR-expression
145/// inclusive-OR-expression '|' exclusive-OR-expression
146///
147/// logical-AND-expression: [C99 6.5.13]
148/// inclusive-OR-expression
149/// logical-AND-expression '&&' inclusive-OR-expression
150///
151/// logical-OR-expression: [C99 6.5.14]
152/// logical-AND-expression
153/// logical-OR-expression '||' logical-AND-expression
154///
155/// conditional-expression: [C99 6.5.15]
156/// logical-OR-expression
157/// logical-OR-expression '?' expression ':' conditional-expression
158/// [GNU] logical-OR-expression '?' ':' conditional-expression
159///
160/// assignment-expression: [C99 6.5.16]
161/// conditional-expression
162/// unary-expression assignment-operator assignment-expression
Chris Lattnera7447ba2008-02-26 00:51:44 +0000163/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000164///
165/// assignment-operator: one of
166/// = *= /= %= += -= <<= >>= &= ^= |=
167///
168/// expression: [C99 6.5.17]
169/// assignment-expression
170/// expression ',' assignment-expression
171///
172Parser::ExprResult Parser::ParseExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000173 if (Tok.is(tok::kw_throw))
174 return ParseThrowExpression();
175
Sebastian Redl62261042008-12-09 20:22:58 +0000176 OwningExprResult LHS(Actions, ParseCastExpression(false));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000177 if (LHS.isInvalid()) return LHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000178
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000179 return ParseRHSOfBinaryExpression(LHS.result(), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000180}
181
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000182/// This routine is called when the '@' is seen and consumed.
183/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000184/// routine is necessary to disambiguate @try-statement from,
185/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000186///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +0000187Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl62261042008-12-09 20:22:58 +0000188 OwningExprResult LHS(Actions, ParseObjCAtExpression(AtLoc));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000189 if (LHS.isInvalid()) return LHS.result();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000190
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000191 return ParseRHSOfBinaryExpression(LHS.result(), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000192}
193
Chris Lattner4b009652007-07-25 00:24:17 +0000194/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
195///
196Parser::ExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000197 if (Tok.is(tok::kw_throw))
198 return ParseThrowExpression();
199
Sebastian Redl62261042008-12-09 20:22:58 +0000200 OwningExprResult LHS(Actions, ParseCastExpression(false));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000201 if (LHS.isInvalid()) return LHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000202
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000203 return ParseRHSOfBinaryExpression(LHS.result(), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000204}
205
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000206/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
207/// where part of an objc message send has already been parsed. In this case
208/// LBracLoc indicates the location of the '[' of the message send, and either
209/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
210/// message.
211///
212/// Since this handles full assignment-expression's, it handles postfix
213/// expressions and other binary operators for these expressions as well.
214Parser::ExprResult
215Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000216 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000217 IdentifierInfo *ReceiverName,
218 ExprTy *ReceiverExpr) {
Sebastian Redl62261042008-12-09 20:22:58 +0000219 OwningExprResult R(Actions, ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
220 ReceiverName,
221 ReceiverExpr));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000222 if (R.isInvalid()) return R.result();
223 R = ParsePostfixExpressionSuffix(R.result());
224 if (R.isInvalid()) return R.result();
225 return ParseRHSOfBinaryExpression(R.result(), 2);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000226}
227
228
Chris Lattner4b009652007-07-25 00:24:17 +0000229Parser::ExprResult Parser::ParseConstantExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000230 OwningExprResult LHS(Actions, ParseCastExpression(false));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000231 if (LHS.isInvalid()) return LHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000232
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000233 return ParseRHSOfBinaryExpression(LHS.result(), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000234}
235
Chris Lattner4b009652007-07-25 00:24:17 +0000236/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
237/// LHS and has a precedence of at least MinPrec.
238Parser::ExprResult
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000239Parser::ParseRHSOfBinaryExpression(ExprResult LHSArg, unsigned MinPrec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000240 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
241 SourceLocation ColonLoc;
242
Sebastian Redl62261042008-12-09 20:22:58 +0000243 OwningExprResult LHS(Actions, LHSArg);
Chris Lattner4b009652007-07-25 00:24:17 +0000244 while (1) {
245 // If this token has a lower precedence than we are allowed to parse (e.g.
246 // because we are called recursively, or because the token is not a binop),
247 // then we are done!
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000248 if (NextTokPrec < MinPrec)
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000249 return LHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000250
251 // Consume the operator, saving the operator token for error reporting.
252 Token OpToken = Tok;
253 ConsumeToken();
254
255 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000256 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000257 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000258 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000259 // Handle this production specially:
260 // logical-OR-expression '?' expression ':' conditional-expression
261 // In particular, the RHS of the '?' is 'expression', not
262 // 'logical-OR-expression' as we might expect.
263 TernaryMiddle = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000264 if (TernaryMiddle.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000265 return TernaryMiddle.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000266 } else {
267 // Special case handling of "X ? Y : Z" where Y is empty:
268 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000269 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000270 Diag(Tok, diag::ext_gnu_conditional_expr);
271 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000272
Chris Lattner4d7d2342007-10-09 17:41:39 +0000273 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000274 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000275 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner4b009652007-07-25 00:24:17 +0000276 return ExprResult(true);
277 }
278
279 // Eat the colon.
280 ColonLoc = ConsumeToken();
281 }
282
283 // Parse another leaf here for the RHS of the operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000284 OwningExprResult RHS(Actions, ParseCastExpression(false));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000285 if (RHS.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000286 return RHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000287
288 // Remember the precedence of this operator and get the precedence of the
289 // operator immediately to the right of the RHS.
290 unsigned ThisPrec = NextTokPrec;
291 NextTokPrec = getBinOpPrecedence(Tok.getKind());
292
293 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000294 bool isRightAssoc = ThisPrec == prec::Conditional ||
295 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +0000296
297 // Get the precedence of the operator to the right of the RHS. If it binds
298 // more tightly with RHS than we do, evaluate it completely first.
299 if (ThisPrec < NextTokPrec ||
300 (ThisPrec == NextTokPrec && isRightAssoc)) {
301 // If this is left-associative, only parse things on the RHS that bind
302 // more tightly than the current operator. If it is left-associative, it
303 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
304 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000305 // The function takes ownership of the RHS.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000306 RHS = ParseRHSOfBinaryExpression(RHS.result(), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000307 if (RHS.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000308 return RHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000309
310 NextTokPrec = getBinOpPrecedence(Tok.getKind());
311 }
312 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000313
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000314 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000315 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000317 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
318 OpToken.getKind(), LHS.release(),
319 RHS.release());
Chris Lattner4a149b62007-08-31 05:01:50 +0000320 else
Steve Naroff87d58b42007-09-16 03:34:24 +0000321 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000322 LHS.release(), TernaryMiddle.release(),
323 RHS.release());
Chris Lattner4a149b62007-08-31 05:01:50 +0000324 }
Chris Lattner4b009652007-07-25 00:24:17 +0000325 }
326}
327
328/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
329/// true, parse a unary-expression.
330///
331/// cast-expression: [C99 6.5.4]
332/// unary-expression
333/// '(' type-name ')' cast-expression
334///
335/// unary-expression: [C99 6.5.3]
336/// postfix-expression
337/// '++' unary-expression
338/// '--' unary-expression
339/// unary-operator cast-expression
340/// 'sizeof' unary-expression
341/// 'sizeof' '(' type-name ')'
342/// [GNU] '__alignof' unary-expression
343/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000344/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000345/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000346/// [C++] new-expression
347/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000348///
349/// unary-operator: one of
350/// '&' '*' '+' '-' '~' '!'
351/// [GNU] '__extension__' '__real' '__imag'
352///
353/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000354/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000355/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000356/// constant
357/// string-literal
358/// [C++] boolean-literal [C++ 2.13.5]
359/// '(' expression ')'
360/// '__func__' [C99 6.4.2.2]
361/// [GNU] '__FUNCTION__'
362/// [GNU] '__PRETTY_FUNCTION__'
363/// [GNU] '(' compound-statement ')'
364/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
365/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
366/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
367/// assign-expr ')'
368/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregorad4b3792008-11-29 04:51:27 +0000369/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000370/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000371/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000372/// [OBJC] '@protocol' '(' identifier ')'
373/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000374/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000375/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
376/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000377/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
378/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
379/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
380/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000381/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
382/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000383/// [C++] 'this' [C++ 9.3.2]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000384/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000385///
386/// constant: [C99 6.4.4]
387/// integer-constant
388/// floating-constant
389/// enumeration-constant -> identifier
390/// character-constant
391///
Douglas Gregore60e5d32008-11-06 22:13:31 +0000392/// id-expression: [C++ 5.1]
393/// unqualified-id
394/// qualified-id [TODO]
395///
396/// unqualified-id: [C++ 5.1]
397/// identifier
398/// operator-function-id
399/// conversion-function-id [TODO]
400/// '~' class-name [TODO]
401/// template-id [TODO]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000402///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000403/// new-expression: [C++ 5.3.4]
404/// '::'[opt] 'new' new-placement[opt] new-type-id
405/// new-initializer[opt]
406/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
407/// new-initializer[opt]
408///
409/// delete-expression: [C++ 5.3.5]
410/// '::'[opt] 'delete' cast-expression
411/// '::'[opt] 'delete' '[' ']' cast-expression
412///
Chris Lattner4b009652007-07-25 00:24:17 +0000413Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000414 if (getLang().CPlusPlus) {
415 // Annotate typenames and C++ scope specifiers.
Argiris Kirtzidisfc332322008-11-26 21:51:07 +0000416 // Used only in C++, where the typename can be considered as a functional
417 // style cast ("int(1)").
418 // In C we don't expect identifiers to be treated as typenames; if it's a
419 // typedef name, let it be handled as an identifier and
420 // Actions.ActOnIdentifierExpr will emit the proper diagnostic.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000421 TryAnnotateTypeOrScopeToken();
422 }
423
Sebastian Redl62261042008-12-09 20:22:58 +0000424 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000425 tok::TokenKind SavedKind = Tok.getKind();
426
427 // This handles all of cast-expression, unary-expression, postfix-expression,
428 // and primary-expression. We handle them together like this for efficiency
429 // and to simplify handling of an expression starting with a '(' token: which
430 // may be one of a parenthesized expression, cast-expression, compound literal
431 // expression, or statement expression.
432 //
433 // If the parsed tokens consist of a primary-expression, the cases below
434 // call ParsePostfixExpressionSuffix to handle the postfix expression
435 // suffixes. Cases that cannot be followed by postfix exprs should
436 // return without invoking ParsePostfixExpressionSuffix.
437 switch (SavedKind) {
438 case tok::l_paren: {
439 // If this expression is limited to being a unary-expression, the parent can
440 // not start a cast expression.
441 ParenParseOption ParenExprType =
442 isUnaryExpression ? CompoundLiteral : CastExpr;
443 TypeTy *CastTy;
444 SourceLocation LParenLoc = Tok.getLocation();
445 SourceLocation RParenLoc;
446 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000447 if (Res.isInvalid()) return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000448
449 switch (ParenExprType) {
450 case SimpleExpr: break; // Nothing else to do.
451 case CompoundStmt: break; // Nothing else to do.
452 case CompoundLiteral:
453 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
454 // postfix-expression exist, parse them now.
455 break;
456 case CastExpr:
457 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
458 // the cast-expression that follows it next.
459 // TODO: For cast expression with CastTy.
460 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000461 if (!Res.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000462 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,
463 Res.release());
464 return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000465 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000466
Chris Lattner4b009652007-07-25 00:24:17 +0000467 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000468 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000469 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000470
Chris Lattner4b009652007-07-25 00:24:17 +0000471 // primary-expression
472 case tok::numeric_constant:
473 // constant: integer-constant
474 // constant: floating-constant
475
Steve Naroff87d58b42007-09-16 03:34:24 +0000476 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000477 ConsumeToken();
478
479 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000480 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000481
482 case tok::kw_true:
483 case tok::kw_false:
484 return ParseCXXBoolLiteral();
485
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000486 case tok::identifier: { // primary-expression: identifier
487 // unqualified-id: identifier
488 // constant: enumeration-constant
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000489
Chris Lattner4b009652007-07-25 00:24:17 +0000490 // Consume the identifier so that we can see if it is followed by a '('.
491 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
492 // need to know whether or not this identifier is a function designator or
493 // not.
494 IdentifierInfo &II = *Tok.getIdentifierInfo();
495 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000496 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000497 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000498 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000499 }
500 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000501 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000502 ConsumeToken();
503 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000504 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000505 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
506 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
507 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner69909292008-08-10 01:53:14 +0000508 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000509 ConsumeToken();
510 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000511 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000512 case tok::string_literal: // primary-expression: string-literal
513 case tok::wide_string_literal:
514 Res = ParseStringLiteralExpression();
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000515 if (Res.isInvalid()) return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000516 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000517 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000518 case tok::kw___builtin_va_arg:
519 case tok::kw___builtin_offsetof:
520 case tok::kw___builtin_choose_expr:
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000521 case tok::kw___builtin_overload:
Chris Lattner4b009652007-07-25 00:24:17 +0000522 case tok::kw___builtin_types_compatible_p:
523 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000524 case tok::kw___null:
525 return Actions.ActOnGNUNullExpr(ConsumeToken());
526 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000527 case tok::plusplus: // unary-expression: '++' unary-expression
528 case tok::minusminus: { // unary-expression: '--' unary-expression
529 SourceLocation SavedLoc = ConsumeToken();
530 Res = ParseCastExpression(true);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000531 if (!Res.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000532 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
533 return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000534 }
535 case tok::amp: // unary-expression: '&' cast-expression
536 case tok::star: // unary-expression: '*' cast-expression
537 case tok::plus: // unary-expression: '+' cast-expression
538 case tok::minus: // unary-expression: '-' cast-expression
539 case tok::tilde: // unary-expression: '~' cast-expression
540 case tok::exclaim: // unary-expression: '!' cast-expression
541 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000542 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000543 SourceLocation SavedLoc = ConsumeToken();
544 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000545 if (!Res.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000546 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
547 return Res.result();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000548 }
549
Chris Lattner6cf92942008-02-02 20:20:10 +0000550 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
551 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000552 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000553 SourceLocation SavedLoc = ConsumeToken();
554 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000555 if (!Res.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000556 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
557 return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000558 }
559 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
560 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000561 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000562 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
563 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000564 // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000565 return ParseSizeofAlignofExpression();
566 case tok::ampamp: { // unary-expression: '&&' identifier
567 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000568 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000569 Diag(Tok, diag::err_expected_ident);
570 return ExprResult(true);
571 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000572
Chris Lattner4b009652007-07-25 00:24:17 +0000573 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000574 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000575 Tok.getIdentifierInfo());
576 ConsumeToken();
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000577 return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000578 }
579 case tok::kw_const_cast:
580 case tok::kw_dynamic_cast:
581 case tok::kw_reinterpret_cast:
582 case tok::kw_static_cast:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000583 Res = ParseCXXCasts();
584 // These can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000585 return ParsePostfixExpressionSuffix(Res.result());
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000586 case tok::kw_typeid:
587 Res = ParseCXXTypeid();
588 // This can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000589 return ParsePostfixExpressionSuffix(Res.result());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000590 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000591 Res = ParseCXXThis();
592 // This can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000593 return ParsePostfixExpressionSuffix(Res.result());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000594
595 case tok::kw_char:
596 case tok::kw_wchar_t:
597 case tok::kw_bool:
598 case tok::kw_short:
599 case tok::kw_int:
600 case tok::kw_long:
601 case tok::kw_signed:
602 case tok::kw_unsigned:
603 case tok::kw_float:
604 case tok::kw_double:
605 case tok::kw_void:
606 case tok::kw_typeof: {
607 if (!getLang().CPlusPlus)
608 goto UnhandledToken;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000609 case tok::annot_qualtypename:
610 assert(getLang().CPlusPlus && "Expected C++");
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000611 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
612 //
613 DeclSpec DS;
614 ParseCXXSimpleTypeSpecifier(DS);
615 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +0000616 return Diag(Tok, diag::err_expected_lparen_after_type)
617 << DS.getSourceRange();
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000618
619 Res = ParseCXXTypeConstructExpression(DS);
620 // This can be followed by postfix-expr pieces.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000621 return ParsePostfixExpressionSuffix(Res.result());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000622 }
623
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000624 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
625 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
626 // template-id
627 Res = ParseCXXIdExpression();
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000628 return ParsePostfixExpressionSuffix(Res.result());
Douglas Gregore60e5d32008-11-06 22:13:31 +0000629
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000630 case tok::coloncolon: // [C++] new-expression or [C++] delete-expression
Sebastian Redlb761e132008-12-02 17:10:24 +0000631 // If the next token is neither 'new' nor 'delete', the :: would have been
632 // parsed as a scope specifier already.
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000633 if (NextToken().is(tok::kw_new))
634 return ParseCXXNewExpression();
635 else
636 return ParseCXXDeleteExpression();
637
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000638 case tok::kw_new: // [C++] new-expression
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000639 return ParseCXXNewExpression();
640
641 case tok::kw_delete: // [C++] delete-expression
642 return ParseCXXDeleteExpression();
643
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000644 case tok::at: {
645 SourceLocation AtLoc = ConsumeToken();
Steve Narofffb9dd752007-10-15 20:55:58 +0000646 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000647 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000648 case tok::l_square:
Steve Narofffb9dd752007-10-15 20:55:58 +0000649 // These can be followed by postfix-expr pieces.
Chris Lattner02d3c732008-05-09 05:28:21 +0000650 if (getLang().ObjC1)
651 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
652 // FALL THROUGH.
Steve Narofffd5b19d2008-08-28 19:20:44 +0000653 case tok::caret:
654 if (getLang().Blocks)
655 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
656 Diag(Tok, diag::err_expected_expression);
657 return ExprResult(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000658 default:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000659 UnhandledToken:
Chris Lattner4b009652007-07-25 00:24:17 +0000660 Diag(Tok, diag::err_expected_expression);
661 return ExprResult(true);
662 }
663
664 // unreachable.
665 abort();
666}
667
668/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
669/// is parsed, this method parses any suffixes that apply.
670///
671/// postfix-expression: [C99 6.5.2]
672/// primary-expression
673/// postfix-expression '[' expression ']'
674/// postfix-expression '(' argument-expression-list[opt] ')'
675/// postfix-expression '.' identifier
676/// postfix-expression '->' identifier
677/// postfix-expression '++'
678/// postfix-expression '--'
679/// '(' type-name ')' '{' initializer-list '}'
680/// '(' type-name ')' '{' initializer-list ',' '}'
681///
682/// argument-expression-list: [C99 6.5.2]
683/// argument-expression
684/// argument-expression-list ',' assignment-expression
685///
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000686Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHSArg) {
Sebastian Redl62261042008-12-09 20:22:58 +0000687 OwningExprResult LHS(Actions, LHSArg);
Chris Lattner4b009652007-07-25 00:24:17 +0000688 // Now that the primary-expression piece of the postfix-expression has been
689 // parsed, see if there are any postfix-expression pieces here.
690 SourceLocation Loc;
691 while (1) {
692 switch (Tok.getKind()) {
693 default: // Not a postfix-expression suffix.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000694 return LHS.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000695 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
696 Loc = ConsumeBracket();
Sebastian Redl62261042008-12-09 20:22:58 +0000697 OwningExprResult Idx(Actions, ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000698
Chris Lattner4b009652007-07-25 00:24:17 +0000699 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000700
701 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000702 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.release(), Loc,
703 Idx.release(), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000704 } else
Chris Lattner4b009652007-07-25 00:24:17 +0000705 LHS = ExprResult(true);
706
707 // Match the ']'.
708 MatchRHSPunctuation(tok::r_square, Loc);
709 break;
710 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000711
Chris Lattner4b009652007-07-25 00:24:17 +0000712 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000713 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000714 CommaLocsTy CommaLocs;
Chris Lattner4b009652007-07-25 00:24:17 +0000715
716 Loc = ConsumeParen();
717
Chris Lattner4d7d2342007-10-09 17:41:39 +0000718 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000719 if (ParseExpressionList(ArgExprs, CommaLocs)) {
720 SkipUntil(tok::r_paren);
721 return ExprResult(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000722 }
723 }
724
725 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000726 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000727 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
728 "Unexpected number of commas!");
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000729 LHS = Actions.ActOnCallExpr(CurScope, LHS.release(), Loc,
Douglas Gregora133e262008-12-06 00:22:45 +0000730 ArgExprs.take(),
Sebastian Redl6008ac32008-11-25 22:21:31 +0000731 ArgExprs.size(), &CommaLocs[0],
732 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000733 }
734
735 MatchRHSPunctuation(tok::r_paren, Loc);
736 break;
737 }
738 case tok::arrow: // postfix-expression: p-e '->' identifier
739 case tok::period: { // postfix-expression: p-e '.' identifier
740 tok::TokenKind OpKind = Tok.getKind();
741 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
742
Chris Lattner4d7d2342007-10-09 17:41:39 +0000743 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000744 Diag(Tok, diag::err_expected_ident);
745 return ExprResult(true);
746 }
747
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000748 if (!LHS.isInvalid()) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000749 LHS = Actions.ActOnMemberReferenceExpr(LHS.release(), OpLoc, OpKind,
Chris Lattner4b009652007-07-25 00:24:17 +0000750 Tok.getLocation(),
751 *Tok.getIdentifierInfo());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000752 }
Chris Lattner4b009652007-07-25 00:24:17 +0000753 ConsumeToken();
754 break;
755 }
756 case tok::plusplus: // postfix-expression: postfix-expression '++'
757 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000758 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000759 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000760 Tok.getKind(), LHS.release());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000761 }
Chris Lattner4b009652007-07-25 00:24:17 +0000762 ConsumeToken();
763 break;
764 }
765 }
766}
767
768
769/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
770/// unary-expression: [C99 6.5.3]
771/// 'sizeof' unary-expression
772/// 'sizeof' '(' type-name ')'
773/// [GNU] '__alignof' unary-expression
774/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000775/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000776Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +0000777 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
778 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000779 "Not a sizeof/alignof expression!");
780 Token OpTok = Tok;
781 ConsumeToken();
782
783 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl62261042008-12-09 20:22:58 +0000784 OwningExprResult Operand(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000785 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000786 Operand = ParseCastExpression(true);
787 } else {
788 // If it starts with a '(', we know that it is either a parenthesized
789 // type-name, or it is a unary-expression that starts with a compound
790 // literal, or starts with a primary-expression that is a parenthesized
791 // expression.
792 ParenParseOption ExprType = CastExpr;
793 TypeTy *CastTy;
794 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
795 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
796
797 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
798 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000799 if (ExprType == CastExpr)
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000800 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
801 OpTok.is(tok::kw_sizeof),
802 /*isType=*/true, CastTy,
803 SourceRange(LParenLoc, RParenLoc));
Chris Lattner48553562007-11-13 20:50:37 +0000804
805 // If this is a parenthesized expression, it is the start of a
806 // unary-expression, but doesn't include any postfix pieces. Parse these
807 // now if present.
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000808 Operand = ParsePostfixExpressionSuffix(Operand.result());
Chris Lattner4b009652007-07-25 00:24:17 +0000809 }
810
811 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000812 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000813 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
814 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000815 /*isType=*/false,
816 Operand.release(), SourceRange());
817 return Operand.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000818}
819
820/// ParseBuiltinPrimaryExpression
821///
822/// primary-expression: [C99 6.5.1]
823/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
824/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
825/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
826/// assign-expr ')'
827/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000828/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000829///
830/// [GNU] offsetof-member-designator:
831/// [GNU] identifier
832/// [GNU] offsetof-member-designator '.' identifier
833/// [GNU] offsetof-member-designator '[' expression ']'
834///
835Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000836 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000837 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
838
839 tok::TokenKind T = Tok.getKind();
840 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
841
842 // All of these start with an open paren.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000843 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerb1753422008-11-23 21:45:46 +0000844 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Chris Lattner4b009652007-07-25 00:24:17 +0000845 return ExprResult(true);
846 }
847
848 SourceLocation LParenLoc = ConsumeParen();
849 // TODO: Build AST.
850
851 switch (T) {
852 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000853 case tok::kw___builtin_va_arg: {
Sebastian Redl62261042008-12-09 20:22:58 +0000854 OwningExprResult Expr(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000855 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000856 SkipUntil(tok::r_paren);
Eli Friedmana1b6d802008-08-20 22:07:34 +0000857 return ExprResult(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000858 }
859
860 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
861 return ExprResult(true);
862
Anders Carlsson36760332007-10-15 20:28:48 +0000863 TypeTy *Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000864
Anders Carlsson36760332007-10-15 20:28:48 +0000865 if (Tok.isNot(tok::r_paren)) {
866 Diag(Tok, diag::err_expected_rparen);
867 return ExprResult(true);
868 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000869 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +0000870 break;
Anders Carlsson36760332007-10-15 20:28:48 +0000871 }
Chris Lattner69638b12007-08-30 15:51:11 +0000872 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +0000873 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner69638b12007-08-30 15:51:11 +0000874 TypeTy *Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000875
876 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
877 return ExprResult(true);
878
879 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000880 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000881 Diag(Tok, diag::err_expected_ident);
882 SkipUntil(tok::r_paren);
883 return true;
884 }
885
886 // Keep track of the various subcomponents we see.
887 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
888
889 Comps.push_back(Action::OffsetOfComponent());
890 Comps.back().isBrackets = false;
891 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
892 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000893
Sebastian Redl6008ac32008-11-25 22:21:31 +0000894 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +0000895 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000896 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000897 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +0000898 Comps.push_back(Action::OffsetOfComponent());
899 Comps.back().isBrackets = false;
900 Comps.back().LocStart = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000901
Chris Lattner4d7d2342007-10-09 17:41:39 +0000902 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000903 Diag(Tok, diag::err_expected_ident);
904 SkipUntil(tok::r_paren);
905 return true;
906 }
907 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
908 Comps.back().LocEnd = ConsumeToken();
909
Chris Lattner4d7d2342007-10-09 17:41:39 +0000910 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000911 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +0000912 Comps.push_back(Action::OffsetOfComponent());
913 Comps.back().isBrackets = true;
914 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +0000915 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000916 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000917 SkipUntil(tok::r_paren);
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000918 return Res.result();
Chris Lattner4b009652007-07-25 00:24:17 +0000919 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000920 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +0000921
Chris Lattner69638b12007-08-30 15:51:11 +0000922 Comps.back().LocEnd =
923 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000924 } else if (Tok.is(tok::r_paren)) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000925 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattnercb8943a2007-08-30 15:52:49 +0000926 Comps.size(), ConsumeParen());
927 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000928 } else {
Chris Lattner69638b12007-08-30 15:51:11 +0000929 // Error occurred.
930 return ExprResult(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000931 }
932 }
933 break;
Chris Lattner69638b12007-08-30 15:51:11 +0000934 }
Steve Naroff93c53012007-08-03 21:21:27 +0000935 case tok::kw___builtin_choose_expr: {
Sebastian Redl62261042008-12-09 20:22:58 +0000936 OwningExprResult Cond(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000937 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +0000938 SkipUntil(tok::r_paren);
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000939 return Cond.result();
Steve Naroff93c53012007-08-03 21:21:27 +0000940 }
Chris Lattner4b009652007-07-25 00:24:17 +0000941 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
942 return ExprResult(true);
943
Sebastian Redl62261042008-12-09 20:22:58 +0000944 OwningExprResult Expr1(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000945 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +0000946 SkipUntil(tok::r_paren);
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000947 return Expr1.result();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000948 }
Chris Lattner4b009652007-07-25 00:24:17 +0000949 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
950 return ExprResult(true);
951
Sebastian Redl62261042008-12-09 20:22:58 +0000952 OwningExprResult Expr2(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000953 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +0000954 SkipUntil(tok::r_paren);
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000955 return Expr2.result();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000956 }
Chris Lattner4d7d2342007-10-09 17:41:39 +0000957 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +0000958 Diag(Tok, diag::err_expected_rparen);
959 return ExprResult(true);
960 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000961 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
962 Expr2.release(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +0000963 break;
Steve Naroff93c53012007-08-03 21:21:27 +0000964 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000965 case tok::kw___builtin_overload: {
Sebastian Redl6008ac32008-11-25 22:21:31 +0000966 ExprVector ArgExprs(Actions);
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000967 llvm::SmallVector<SourceLocation, 8> CommaLocs;
968
969 // For each iteration through the loop look for assign-expr followed by a
970 // comma. If there is no comma, break and attempt to match r-paren.
971 if (Tok.isNot(tok::r_paren)) {
972 while (1) {
Sebastian Redl62261042008-12-09 20:22:58 +0000973 OwningExprResult ArgExpr(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000974 if (ArgExpr.isInvalid()) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000975 SkipUntil(tok::r_paren);
976 return ExprResult(true);
977 } else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000978 ArgExprs.push_back(ArgExpr.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000979
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000980 if (Tok.isNot(tok::comma))
981 break;
982 // Move to the next argument, remember where the comma was.
983 CommaLocs.push_back(ConsumeToken());
984 }
985 }
986
987 // Attempt to consume the r-paren
988 if (Tok.isNot(tok::r_paren)) {
989 Diag(Tok, diag::err_expected_rparen);
990 SkipUntil(tok::r_paren);
991 return ExprResult(true);
992 }
Sebastian Redl6008ac32008-11-25 22:21:31 +0000993 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000994 &CommaLocs[0], StartLoc, ConsumeParen());
995 break;
996 }
Chris Lattner4b009652007-07-25 00:24:17 +0000997 case tok::kw___builtin_types_compatible_p:
Steve Naroff5b528922007-08-01 23:45:51 +0000998 TypeTy *Ty1 = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000999
1000 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
1001 return ExprResult(true);
1002
Steve Naroff5b528922007-08-01 23:45:51 +00001003 TypeTy *Ty2 = ParseTypeName();
1004
Chris Lattner4d7d2342007-10-09 17:41:39 +00001005 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001006 Diag(Tok, diag::err_expected_rparen);
1007 return ExprResult(true);
1008 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001009 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001010 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001011 }
1012
Chris Lattner4b009652007-07-25 00:24:17 +00001013 // These can be followed by postfix-expr pieces because they are
1014 // primary-expressions.
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001015 return ParsePostfixExpressionSuffix(Res.result());
Chris Lattner4b009652007-07-25 00:24:17 +00001016}
1017
1018/// ParseParenExpression - This parses the unit that starts with a '(' token,
1019/// based on what is allowed by ExprType. The actual thing parsed is returned
1020/// in ExprType.
1021///
1022/// primary-expression: [C99 6.5.1]
1023/// '(' expression ')'
1024/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1025/// postfix-expression: [C99 6.5.2]
1026/// '(' type-name ')' '{' initializer-list '}'
1027/// '(' type-name ')' '{' initializer-list ',' '}'
1028/// cast-expression: [C99 6.5.4]
1029/// '(' type-name ')' cast-expression
1030///
1031Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
1032 TypeTy *&CastTy,
1033 SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001034 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner4b009652007-07-25 00:24:17 +00001035 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001036 OwningExprResult Result(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001037 CastTy = 0;
1038
Chris Lattner4d7d2342007-10-09 17:41:39 +00001039 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001040 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001041 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001042 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001043
Chris Lattner4b009652007-07-25 00:24:17 +00001044 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001045 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1046 Result = Actions.ActOnStmtExpr(
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001047 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001048
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001049 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001050 // Otherwise, this is a compound literal expression or cast expression.
1051 TypeTy *Ty = ParseTypeName();
1052
1053 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001054 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001055 RParenLoc = ConsumeParen();
1056 else
1057 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1058
Chris Lattner4d7d2342007-10-09 17:41:39 +00001059 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001060 if (!getLang().C99) // Compound literals don't exist in C90.
1061 Diag(OpenLoc, diag::ext_c99_compound_literal);
1062 Result = ParseInitializer();
1063 ExprType = CompoundLiteral;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001064 if (!Result.isInvalid())
1065 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001066 Result.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001067 } else if (ExprType == CastExpr) {
1068 // Note that this doesn't parse the subsequence cast-expression, it just
1069 // returns the parsed type to the callee.
1070 ExprType = CastExpr;
1071 CastTy = Ty;
1072 return ExprResult(false);
1073 } else {
1074 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1075 return ExprResult(true);
1076 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001077 return Result.result();
Chris Lattner4b009652007-07-25 00:24:17 +00001078 } else {
1079 Result = ParseExpression();
1080 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001081 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1082 Result = Actions.ActOnParenExpr(
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001083 OpenLoc, Tok.getLocation(), Result.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001084 }
1085
1086 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001087 if (Result.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001088 SkipUntil(tok::r_paren);
1089 else {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001090 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001091 RParenLoc = ConsumeParen();
1092 else
1093 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1094 }
1095
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001096 return Result.result();
Chris Lattner4b009652007-07-25 00:24:17 +00001097}
1098
1099/// ParseStringLiteralExpression - This handles the various token types that
1100/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1101/// translation phase #6].
1102///
1103/// primary-expression: [C99 6.5.1]
1104/// string-literal
1105Parser::ExprResult Parser::ParseStringLiteralExpression() {
1106 assert(isTokenStringLiteral() && "Not a string literal!");
1107
1108 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1109 // considered to be strings for concatenation purposes.
1110 llvm::SmallVector<Token, 4> StringToks;
1111
1112 do {
1113 StringToks.push_back(Tok);
1114 ConsumeStringToken();
1115 } while (isTokenStringLiteral());
1116
1117 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroff87d58b42007-09-16 03:34:24 +00001118 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001119}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001120
1121/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1122///
1123/// argument-expression-list:
1124/// assignment-expression
1125/// argument-expression-list , assignment-expression
1126///
1127/// [C++] expression-list:
1128/// [C++] assignment-expression
1129/// [C++] expression-list , assignment-expression
1130///
1131bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1132 while (1) {
Sebastian Redl62261042008-12-09 20:22:58 +00001133 OwningExprResult Expr(Actions, ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001134 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001135 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001136
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001137 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001138
1139 if (Tok.isNot(tok::comma))
1140 return false;
1141 // Move to the next argument, remember where the comma was.
1142 CommaLocs.push_back(ConsumeToken());
1143 }
1144}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001145
1146/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001147/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001148///
1149/// block-literal:
1150/// [clang] '^' block-args[opt] compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001151/// [clang] block-args:
1152/// [clang] '(' parameter-list ')'
1153///
1154Parser::ExprResult Parser::ParseBlockLiteralExpression() {
1155 assert(Tok.is(tok::caret) && "block literal starts with ^");
1156 SourceLocation CaretLoc = ConsumeToken();
1157
1158 // Enter a scope to hold everything within the block. This includes the
1159 // argument decls, decls within the compound expression, etc. This also
1160 // allows determining whether a variable reference inside the block is
1161 // within or outside of the block.
Douglas Gregor95d40792008-12-10 06:34:36 +00001162 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1163 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001164
1165 // Inform sema that we are starting a block.
1166 Actions.ActOnBlockStart(CaretLoc, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001167
1168 // Parse the return type if present.
1169 DeclSpec DS;
1170 Declarator ParamInfo(DS, Declarator::PrototypeContext);
1171
1172 // If this block has arguments, parse them. There is no ambiguity here with
1173 // the expression case, because the expression case requires a parameter list.
1174 if (Tok.is(tok::l_paren)) {
1175 ParseParenDeclarator(ParamInfo);
1176 // Parse the pieces after the identifier as if we had "int(...)".
1177 ParamInfo.SetIdentifier(0, CaretLoc);
1178 if (ParamInfo.getInvalidType()) {
1179 // If there was an error parsing the arguments, they may have tried to use
1180 // ^(x+y) which requires an argument list. Just skip the whole block
1181 // literal.
Steve Narofffd5b19d2008-08-28 19:20:44 +00001182 return true;
1183 }
1184 } else {
1185 // Otherwise, pretend we saw (void).
1186 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001187 0, 0, 0, CaretLoc));
Steve Narofffd5b19d2008-08-28 19:20:44 +00001188 }
1189
1190 // Inform sema that we are starting a block.
Steve Naroff52059382008-10-10 01:28:17 +00001191 Actions.ActOnBlockArguments(ParamInfo);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001192
Sebastian Redl62261042008-12-09 20:22:58 +00001193 OwningExprResult Result(Actions, true);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001194 if (Tok.is(tok::l_brace)) {
Sebastian Redl10c32952008-12-11 19:30:53 +00001195 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001196 if (!Stmt.isInvalid()) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001197 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001198 } else {
1199 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001200 }
1201 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001202 return Result.result();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001203}
1204