blob: 74b0715de2acbe196071fea0ac8a731a56425b59 [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,
Steve Naroff9e4ac112008-11-19 15:54:23 +0000215 SourceLocation NameLoc,
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000216 IdentifierInfo *ReceiverName,
217 ExprTy *ReceiverExpr) {
Steve Naroff9e4ac112008-11-19 15:54:23 +0000218 ExprResult R = ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000219 ReceiverExpr);
220 if (R.isInvalid) return R;
221 R = ParsePostfixExpressionSuffix(R);
222 if (R.isInvalid) return R;
223 return ParseRHSOfBinaryExpression(R, 2);
224}
225
226
Chris Lattner3b561a32006-08-13 00:12:11 +0000227Parser::ExprResult Parser::ParseConstantExpression() {
228 ExprResult LHS = ParseCastExpression(false);
229 if (LHS.isInvalid) return LHS;
230
Chris Lattner3b561a32006-08-13 00:12:11 +0000231 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
232}
233
Chris Lattnercde626a2006-08-12 08:13:25 +0000234/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
235/// LHS and has a precedence of at least MinPrec.
236Parser::ExprResult
237Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
238 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000239 SourceLocation ColonLoc;
240
Chris Lattnercde626a2006-08-12 08:13:25 +0000241 while (1) {
242 // If this token has a lower precedence than we are allowed to parse (e.g.
243 // because we are called recursively, or because the token is not a binop),
244 // then we are done!
245 if (NextTokPrec < MinPrec)
246 return LHS;
247
248 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000249 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000250 ConsumeToken();
251
Chris Lattner96c3deb2006-08-12 17:13:08 +0000252 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000253 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000254 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000255 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000256 // Handle this production specially:
257 // logical-OR-expression '?' expression ':' conditional-expression
258 // In particular, the RHS of the '?' is 'expression', not
259 // 'logical-OR-expression' as we might expect.
260 TernaryMiddle = ParseExpression();
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000261 if (TernaryMiddle.isInvalid) {
262 Actions.DeleteExpr(LHS.Val);
263 return TernaryMiddle;
264 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000265 } else {
266 // Special case handling of "X ? Y : Z" where Y is empty:
267 // logical-OR-expression '?' ':' conditional-expression [GNU]
268 TernaryMiddle = ExprResult(false);
269 Diag(Tok, diag::ext_gnu_conditional_expr);
270 }
271
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000272 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000273 Diag(Tok, diag::err_expected_colon);
Chris Lattner6d29c102008-11-18 07:48:38 +0000274 Diag(OpToken, diag::err_matching) << "?";
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000275 Actions.DeleteExpr(LHS.Val);
276 Actions.DeleteExpr(TernaryMiddle.Val);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000277 return ExprResult(true);
278 }
279
280 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000281 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000282 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000283
284 // Parse another leaf here for the RHS of the operator.
285 ExprResult RHS = ParseCastExpression(false);
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000286 if (RHS.isInvalid) {
287 Actions.DeleteExpr(LHS.Val);
288 Actions.DeleteExpr(TernaryMiddle.Val);
289 return RHS;
290 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000291
292 // Remember the precedence of this operator and get the precedence of the
293 // operator immediately to the right of the RHS.
294 unsigned ThisPrec = NextTokPrec;
295 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000296
297 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000298 bool isRightAssoc = ThisPrec == prec::Conditional ||
299 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000300
301 // Get the precedence of the operator to the right of the RHS. If it binds
302 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000303 if (ThisPrec < NextTokPrec ||
304 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000305 // If this is left-associative, only parse things on the RHS that bind
306 // more tightly than the current operator. If it is left-associative, it
307 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
308 // A=(B=(C=D)), where each paren is a level of recursion here.
309 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnerc6c66c42007-08-31 04:58:34 +0000310 if (RHS.isInvalid) {
311 Actions.DeleteExpr(LHS.Val);
312 Actions.DeleteExpr(TernaryMiddle.Val);
313 return RHS;
314 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000315
316 NextTokPrec = getBinOpPrecedence(Tok.getKind());
317 }
318 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
319
Chris Lattner319079c2007-08-31 05:01:50 +0000320 if (!LHS.isInvalid) {
321 // Combine the LHS and RHS into the LHS (e.g. build AST).
322 if (TernaryMiddle.isInvalid)
Douglas Gregor7d5fc7e2008-11-06 23:29:22 +0000323 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
324 OpToken.getKind(), LHS.Val, RHS.Val);
Chris Lattner319079c2007-08-31 05:01:50 +0000325 else
Steve Naroff83895f72007-09-16 03:34:24 +0000326 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Chris Lattner319079c2007-08-31 05:01:50 +0000327 LHS.Val, TernaryMiddle.Val, RHS.Val);
328 } else {
329 // We had a semantic error on the LHS. Just free the RHS and continue.
330 Actions.DeleteExpr(TernaryMiddle.Val);
331 Actions.DeleteExpr(RHS.Val);
332 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000333 }
334}
335
Chris Lattnereaf06592006-08-11 02:02:23 +0000336/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
337/// true, parse a unary-expression.
338///
Chris Lattner4564bc12006-08-10 23:14:52 +0000339/// cast-expression: [C99 6.5.4]
340/// unary-expression
341/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000342///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000343/// unary-expression: [C99 6.5.3]
344/// postfix-expression
345/// '++' unary-expression
346/// '--' unary-expression
347/// unary-operator cast-expression
348/// 'sizeof' unary-expression
349/// 'sizeof' '(' type-name ')'
350/// [GNU] '__alignof' unary-expression
351/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000352/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000353/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000354///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000355/// unary-operator: one of
356/// '&' '*' '+' '-' '~' '!'
357/// [GNU] '__extension__' '__real' '__imag'
358///
Chris Lattner52a99e52006-08-10 20:56:00 +0000359/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000360/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000361/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000362/// constant
363/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000364/// [C++] boolean-literal [C++ 2.13.5]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000365/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000366/// '__func__' [C99 6.4.2.2]
367/// [GNU] '__FUNCTION__'
368/// [GNU] '__PRETTY_FUNCTION__'
369/// [GNU] '(' compound-statement ')'
370/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
371/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
372/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
373/// assign-expr ')'
374/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000375/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000376/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000377/// [OBJC] '@protocol' '(' identifier ')'
378/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000379/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000380/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
381/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000382/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
383/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
384/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
385/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000386/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
387/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000388/// [C++] 'this' [C++ 9.3.2]
Steve Naroff0ac012832008-08-28 19:20:44 +0000389/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000390///
391/// constant: [C99 6.4.4]
392/// integer-constant
393/// floating-constant
394/// enumeration-constant -> identifier
395/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000396///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000397/// id-expression: [C++ 5.1]
398/// unqualified-id
399/// qualified-id [TODO]
400///
401/// unqualified-id: [C++ 5.1]
402/// identifier
403/// operator-function-id
404/// conversion-function-id [TODO]
405/// '~' class-name [TODO]
406/// template-id [TODO]
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000407///
Chris Lattner89c50c62006-08-11 06:41:18 +0000408Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000409 if (getLang().CPlusPlus) {
410 // Annotate typenames and C++ scope specifiers.
411 // Used only in C++; in C let the typedef name be handled as an identifier.
412 TryAnnotateTypeOrScopeToken();
413 }
414
Chris Lattner89c50c62006-08-11 06:41:18 +0000415 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000416 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000417
Chris Lattner81b576e2006-08-11 02:13:20 +0000418 // This handles all of cast-expression, unary-expression, postfix-expression,
419 // and primary-expression. We handle them together like this for efficiency
420 // and to simplify handling of an expression starting with a '(' token: which
421 // may be one of a parenthesized expression, cast-expression, compound literal
422 // expression, or statement expression.
423 //
424 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000425 // call ParsePostfixExpressionSuffix to handle the postfix expression
426 // suffixes. Cases that cannot be followed by postfix exprs should
427 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000428 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000429 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000430 // If this expression is limited to being a unary-expression, the parent can
431 // not start a cast expression.
432 ParenParseOption ParenExprType =
433 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000434 TypeTy *CastTy;
435 SourceLocation LParenLoc = Tok.getLocation();
436 SourceLocation RParenLoc;
437 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000438 if (Res.isInvalid) return Res;
439
Chris Lattner81b576e2006-08-11 02:13:20 +0000440 switch (ParenExprType) {
441 case SimpleExpr: break; // Nothing else to do.
442 case CompoundStmt: break; // Nothing else to do.
443 case CompoundLiteral:
444 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
445 // postfix-expression exist, parse them now.
446 break;
447 case CastExpr:
448 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
449 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000450 // TODO: For cast expression with CastTy.
451 Res = ParseCastExpression(false);
452 if (!Res.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000453 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000454 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000455 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000456
457 // These can be followed by postfix-expr pieces.
458 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000459 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000460
Chris Lattner52a99e52006-08-10 20:56:00 +0000461 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000462 case tok::numeric_constant:
463 // constant: integer-constant
464 // constant: floating-constant
465
Steve Naroff83895f72007-09-16 03:34:24 +0000466 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000467 ConsumeToken();
468
469 // These can be followed by postfix-expr pieces.
470 return ParsePostfixExpressionSuffix(Res);
471
Bill Wendling4073ed52007-02-13 01:51:42 +0000472 case tok::kw_true:
473 case tok::kw_false:
474 return ParseCXXBoolLiteral();
475
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000476 case tok::identifier: { // primary-expression: identifier
477 // unqualified-id: identifier
478 // constant: enumeration-constant
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000479
Chris Lattnerac18be92006-11-20 06:49:47 +0000480 // Consume the identifier so that we can see if it is followed by a '('.
481 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
482 // need to know whether or not this identifier is a function designator or
483 // not.
484 IdentifierInfo &II = *Tok.getIdentifierInfo();
485 SourceLocation L = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000486 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner17ed4872006-11-20 04:58:19 +0000487 // These can be followed by postfix-expr pieces.
488 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerac18be92006-11-20 06:49:47 +0000489 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000490 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000491 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000492 ConsumeToken();
493 // These can be followed by postfix-expr pieces.
494 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000495 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
496 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
497 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000498 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000499 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000500 // These can be followed by postfix-expr pieces.
501 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000502 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000503 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000504 Res = ParseStringLiteralExpression();
505 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000506 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
507 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000508 case tok::kw___builtin_va_arg:
509 case tok::kw___builtin_offsetof:
510 case tok::kw___builtin_choose_expr:
Nate Begeman1e36a852008-01-17 17:46:27 +0000511 case tok::kw___builtin_overload:
Chris Lattnerf8339772006-08-10 22:01:51 +0000512 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000513 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000514 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000515 case tok::minusminus: { // unary-expression: '--' unary-expression
516 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000517 Res = ParseCastExpression(true);
518 if (!Res.isInvalid)
Douglas Gregord08452f2008-11-19 15:42:04 +0000519 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000520 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000521 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000522 case tok::amp: // unary-expression: '&' cast-expression
523 case tok::star: // unary-expression: '*' cast-expression
524 case tok::plus: // unary-expression: '+' cast-expression
525 case tok::minus: // unary-expression: '-' cast-expression
526 case tok::tilde: // unary-expression: '~' cast-expression
527 case tok::exclaim: // unary-expression: '!' cast-expression
528 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000529 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000530 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000531 Res = ParseCastExpression(false);
532 if (!Res.isInvalid)
Douglas Gregord08452f2008-11-19 15:42:04 +0000533 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000534 return Res;
Chris Lattnerc43926f2008-02-02 20:20:10 +0000535 }
536
537 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
538 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000539 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000540 SourceLocation SavedLoc = ConsumeToken();
541 Res = ParseCastExpression(false);
542 if (!Res.isInvalid)
Douglas Gregord08452f2008-11-19 15:42:04 +0000543 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.Val);
Chris Lattnerc43926f2008-02-02 20:20:10 +0000544 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000545 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000546 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
547 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000548 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000549 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
550 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000551 // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000552 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000553 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000554 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000555 if (Tok.isNot(tok::identifier)) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000556 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000557 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000558 }
Chris Lattnereefa10e2007-05-28 06:56:27 +0000559
560 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000561 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000562 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000563 ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000564 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000565 }
Chris Lattner29375652006-12-04 18:06:35 +0000566 case tok::kw_const_cast:
567 case tok::kw_dynamic_cast:
568 case tok::kw_reinterpret_cast:
569 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000570 Res = ParseCXXCasts();
571 // These can be followed by postfix-expr pieces.
572 return ParsePostfixExpressionSuffix(Res);
Sebastian Redlc4704762008-11-11 11:37:55 +0000573 case tok::kw_typeid:
574 Res = ParseCXXTypeid();
575 // This can be followed by postfix-expr pieces.
576 return ParsePostfixExpressionSuffix(Res);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000577 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000578 Res = ParseCXXThis();
579 // This can be followed by postfix-expr pieces.
580 return ParsePostfixExpressionSuffix(Res);
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000581
582 case tok::kw_char:
583 case tok::kw_wchar_t:
584 case tok::kw_bool:
585 case tok::kw_short:
586 case tok::kw_int:
587 case tok::kw_long:
588 case tok::kw_signed:
589 case tok::kw_unsigned:
590 case tok::kw_float:
591 case tok::kw_double:
592 case tok::kw_void:
593 case tok::kw_typeof: {
594 if (!getLang().CPlusPlus)
595 goto UnhandledToken;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000596 case tok::annot_qualtypename:
597 assert(getLang().CPlusPlus && "Expected C++");
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000598 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
599 //
600 DeclSpec DS;
601 ParseCXXSimpleTypeSpecifier(DS);
602 if (Tok.isNot(tok::l_paren))
Chris Lattner6d29c102008-11-18 07:48:38 +0000603 return Diag(Tok, diag::err_expected_lparen_after_type)
604 << DS.getSourceRange();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000605
606 Res = ParseCXXTypeConstructExpression(DS);
607 // This can be followed by postfix-expr pieces.
608 return ParsePostfixExpressionSuffix(Res);
609 }
610
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000611 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
612 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
613 // template-id
614 Res = ParseCXXIdExpression();
615 return ParsePostfixExpressionSuffix(Res);
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000616
Chris Lattner644e1b72007-10-03 22:03:06 +0000617 case tok::at: {
618 SourceLocation AtLoc = ConsumeToken();
Steve Naroff126b4d82007-10-15 20:55:58 +0000619 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000620 }
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000621 case tok::l_square:
Steve Naroff126b4d82007-10-15 20:55:58 +0000622 // These can be followed by postfix-expr pieces.
Chris Lattner2fdcddd2008-05-09 05:28:21 +0000623 if (getLang().ObjC1)
624 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
625 // FALL THROUGH.
Steve Naroff0ac012832008-08-28 19:20:44 +0000626 case tok::caret:
627 if (getLang().Blocks)
628 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
629 Diag(Tok, diag::err_expected_expression);
630 return ExprResult(true);
Chris Lattner52a99e52006-08-10 20:56:00 +0000631 default:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000632 UnhandledToken:
Chris Lattner52a99e52006-08-10 20:56:00 +0000633 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000634 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000635 }
636
Chris Lattner20c6a452006-08-12 17:40:43 +0000637 // unreachable.
638 abort();
639}
640
641/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
642/// is parsed, this method parses any suffixes that apply.
643///
644/// postfix-expression: [C99 6.5.2]
645/// primary-expression
646/// postfix-expression '[' expression ']'
647/// postfix-expression '(' argument-expression-list[opt] ')'
648/// postfix-expression '.' identifier
649/// postfix-expression '->' identifier
650/// postfix-expression '++'
651/// postfix-expression '--'
652/// '(' type-name ')' '{' initializer-list '}'
653/// '(' type-name ')' '{' initializer-list ',' '}'
654///
655/// argument-expression-list: [C99 6.5.2]
656/// argument-expression
657/// argument-expression-list ',' assignment-expression
658///
659Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000660
Chris Lattnerf8339772006-08-10 22:01:51 +0000661 // Now that the primary-expression piece of the postfix-expression has been
662 // parsed, see if there are any postfix-expression pieces here.
663 SourceLocation Loc;
664 while (1) {
665 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000666 default: // Not a postfix-expression suffix.
667 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000668 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000669 Loc = ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000670 ExprResult Idx = ParseExpression();
671
672 SourceLocation RLoc = Tok.getLocation();
673
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000674 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square))
Douglas Gregor40412ac2008-11-19 17:17:41 +0000675 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.Val, Loc,
676 Idx.Val, RLoc);
Steve Narofff1e53692007-03-23 22:27:02 +0000677 else
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000678 LHS = ExprResult(true);
679
Chris Lattner89c50c62006-08-11 06:41:18 +0000680 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000681 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000682 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000683 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000684
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000685 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000686 ExprListTy ArgExprs;
687 CommaLocsTy CommaLocs;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000688
Chris Lattner04132372006-10-16 06:12:55 +0000689 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000690
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000691 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000692 if (ParseExpressionList(ArgExprs, CommaLocs)) {
693 SkipUntil(tok::r_paren);
694 return ExprResult(true);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000695 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000696 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000697
Chris Lattner89c50c62006-08-11 06:41:18 +0000698 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000699 if (!LHS.isInvalid && Tok.is(tok::r_paren)) {
Chris Lattnere165d942006-08-24 04:40:38 +0000700 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
701 "Unexpected number of commas!");
Steve Naroff83895f72007-09-16 03:34:24 +0000702 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000703 &CommaLocs[0], Tok.getLocation());
704 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000705
Chris Lattner5abb82c2007-07-21 05:18:12 +0000706 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000707 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000708 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000709 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000710 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000711 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000712 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000713
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000714 if (Tok.isNot(tok::identifier)) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000715 Diag(Tok, diag::err_expected_ident);
716 return ExprResult(true);
717 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000718
719 if (!LHS.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +0000720 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000721 Tok.getLocation(),
722 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000723 ConsumeToken();
724 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000725 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000726 case tok::plusplus: // postfix-expression: postfix-expression '++'
727 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000728 if (!LHS.isInvalid)
Douglas Gregord08452f2008-11-19 15:42:04 +0000729 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
730 Tok.getKind(), LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000731 ConsumeToken();
732 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000733 }
734 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000735}
736
Chris Lattner20c6a452006-08-12 17:40:43 +0000737
Chris Lattner81b576e2006-08-11 02:13:20 +0000738/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
739/// unary-expression: [C99 6.5.3]
740/// 'sizeof' unary-expression
741/// 'sizeof' '(' type-name ')'
742/// [GNU] '__alignof' unary-expression
743/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000744/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000745Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +0000746 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
747 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +0000748 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +0000749 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000750 ConsumeToken();
751
752 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000753 ExprResult Operand;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000754 if (Tok.isNot(tok::l_paren)) {
Chris Lattner26115ac2006-08-24 06:10:04 +0000755 Operand = ParseCastExpression(true);
756 } else {
757 // If it starts with a '(', we know that it is either a parenthesized
758 // type-name, or it is a unary-expression that starts with a compound
759 // literal, or starts with a primary-expression that is a parenthesized
760 // expression.
761 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000762 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000763 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000764 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000765
766 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
767 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner47791a42007-11-13 20:50:37 +0000768 if (ExprType == CastExpr)
Sebastian Redl6f282892008-11-11 17:56:53 +0000769 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
770 OpTok.is(tok::kw_sizeof),
771 /*isType=*/true, CastTy,
772 SourceRange(LParenLoc, RParenLoc));
Chris Lattner47791a42007-11-13 20:50:37 +0000773
774 // If this is a parenthesized expression, it is the start of a
775 // unary-expression, but doesn't include any postfix pieces. Parse these
776 // now if present.
777 Operand = ParsePostfixExpressionSuffix(Operand);
Chris Lattner26115ac2006-08-24 06:10:04 +0000778 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000779
Chris Lattner26115ac2006-08-24 06:10:04 +0000780 // If we get here, the operand to the sizeof/alignof was an expresion.
781 if (!Operand.isInvalid)
Sebastian Redl6f282892008-11-11 17:56:53 +0000782 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
783 OpTok.is(tok::kw_sizeof),
784 /*isType=*/false, Operand.Val,
785 SourceRange());
Chris Lattner26115ac2006-08-24 06:10:04 +0000786 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000787}
788
Chris Lattner11124352006-08-12 19:16:08 +0000789/// ParseBuiltinPrimaryExpression
790///
791/// primary-expression: [C99 6.5.1]
792/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
793/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
794/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
795/// assign-expr ')'
796/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman1e36a852008-01-17 17:46:27 +0000797/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner11124352006-08-12 19:16:08 +0000798///
799/// [GNU] offsetof-member-designator:
800/// [GNU] identifier
801/// [GNU] offsetof-member-designator '.' identifier
802/// [GNU] offsetof-member-designator '[' expression ']'
803///
804Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
805 ExprResult Res(false);
Chris Lattner11124352006-08-12 19:16:08 +0000806 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
807
808 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000809 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000810
811 // All of these start with an open paren.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000812 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +0000813 Diag(Tok, diag::err_expected_lparen_after) << BuiltinII;
Chris Lattner11124352006-08-12 19:16:08 +0000814 return ExprResult(true);
815 }
816
Chris Lattner04132372006-10-16 06:12:55 +0000817 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000818 // TODO: Build AST.
819
Chris Lattner11124352006-08-12 19:16:08 +0000820 switch (T) {
821 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000822 case tok::kw___builtin_va_arg: {
823 ExprResult Expr = ParseAssignmentExpression();
824 if (Expr.isInvalid) {
Chris Lattner11124352006-08-12 19:16:08 +0000825 SkipUntil(tok::r_paren);
Eli Friedman002ad122008-08-20 22:07:34 +0000826 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000827 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000828
Chris Lattner6d7e6342006-08-15 03:41:14 +0000829 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000830 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000831
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000832 TypeTy *Ty = ParseTypeName();
Chris Lattner5ad4f462007-08-30 15:52:49 +0000833
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000834 if (Tok.isNot(tok::r_paren)) {
835 Diag(Tok, diag::err_expected_rparen);
836 return ExprResult(true);
837 }
838 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +0000839 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000840 }
Chris Lattner687d6092007-08-30 15:51:11 +0000841 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +0000842 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner687d6092007-08-30 15:51:11 +0000843 TypeTy *Ty = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000844
Chris Lattner6d7e6342006-08-15 03:41:14 +0000845 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000846 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000847
848 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000849 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000850 Diag(Tok, diag::err_expected_ident);
851 SkipUntil(tok::r_paren);
852 return true;
853 }
854
855 // Keep track of the various subcomponents we see.
856 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
857
858 Comps.push_back(Action::OffsetOfComponent());
859 Comps.back().isBrackets = false;
860 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
861 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000862
Chris Lattner11124352006-08-12 19:16:08 +0000863 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000864 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +0000865 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +0000866 Comps.push_back(Action::OffsetOfComponent());
867 Comps.back().isBrackets = false;
868 Comps.back().LocStart = ConsumeToken();
Chris Lattner11124352006-08-12 19:16:08 +0000869
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000870 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000871 Diag(Tok, diag::err_expected_ident);
872 SkipUntil(tok::r_paren);
873 return true;
874 }
875 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
876 Comps.back().LocEnd = ConsumeToken();
877
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000878 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +0000879 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +0000880 Comps.push_back(Action::OffsetOfComponent());
881 Comps.back().isBrackets = true;
882 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000883 Res = ParseExpression();
884 if (Res.isInvalid) {
885 SkipUntil(tok::r_paren);
886 return Res;
887 }
Chris Lattner687d6092007-08-30 15:51:11 +0000888 Comps.back().U.E = Res.Val;
Chris Lattner11124352006-08-12 19:16:08 +0000889
Chris Lattner687d6092007-08-30 15:51:11 +0000890 Comps.back().LocEnd =
891 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000892 } else if (Tok.is(tok::r_paren)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000893 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner5ad4f462007-08-30 15:52:49 +0000894 Comps.size(), ConsumeParen());
895 break;
Chris Lattner11124352006-08-12 19:16:08 +0000896 } else {
Chris Lattner687d6092007-08-30 15:51:11 +0000897 // Error occurred.
898 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000899 }
900 }
901 break;
Chris Lattner687d6092007-08-30 15:51:11 +0000902 }
Steve Naroff9efdabc2007-08-03 21:21:27 +0000903 case tok::kw___builtin_choose_expr: {
904 ExprResult Cond = ParseAssignmentExpression();
905 if (Cond.isInvalid) {
906 SkipUntil(tok::r_paren);
907 return Cond;
908 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000909 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000910 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000911
Steve Naroff9efdabc2007-08-03 21:21:27 +0000912 ExprResult Expr1 = ParseAssignmentExpression();
913 if (Expr1.isInvalid) {
914 SkipUntil(tok::r_paren);
915 return Expr1;
916 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000917 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000918 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000919
Steve Naroff9efdabc2007-08-03 21:21:27 +0000920 ExprResult Expr2 = ParseAssignmentExpression();
921 if (Expr2.isInvalid) {
922 SkipUntil(tok::r_paren);
923 return Expr2;
924 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000925 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000926 Diag(Tok, diag::err_expected_rparen);
927 return ExprResult(true);
928 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000929 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
Chris Lattner5ad4f462007-08-30 15:52:49 +0000930 ConsumeParen());
931 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +0000932 }
Nate Begeman1e36a852008-01-17 17:46:27 +0000933 case tok::kw___builtin_overload: {
934 llvm::SmallVector<ExprTy*, 8> ArgExprs;
935 llvm::SmallVector<SourceLocation, 8> CommaLocs;
936
937 // For each iteration through the loop look for assign-expr followed by a
938 // comma. If there is no comma, break and attempt to match r-paren.
939 if (Tok.isNot(tok::r_paren)) {
940 while (1) {
941 ExprResult ArgExpr = ParseAssignmentExpression();
942 if (ArgExpr.isInvalid) {
943 SkipUntil(tok::r_paren);
944 return ExprResult(true);
945 } else
946 ArgExprs.push_back(ArgExpr.Val);
947
948 if (Tok.isNot(tok::comma))
949 break;
950 // Move to the next argument, remember where the comma was.
951 CommaLocs.push_back(ConsumeToken());
952 }
953 }
954
955 // Attempt to consume the r-paren
956 if (Tok.isNot(tok::r_paren)) {
957 Diag(Tok, diag::err_expected_rparen);
958 SkipUntil(tok::r_paren);
959 return ExprResult(true);
960 }
Nate Begeman1e36a852008-01-17 17:46:27 +0000961 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(),
962 &CommaLocs[0], StartLoc, ConsumeParen());
963 break;
964 }
Chris Lattner11124352006-08-12 19:16:08 +0000965 case tok::kw___builtin_types_compatible_p:
Steve Naroff788d8642007-08-01 23:45:51 +0000966 TypeTy *Ty1 = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000967
Chris Lattner6d7e6342006-08-15 03:41:14 +0000968 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000969 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000970
Steve Naroff788d8642007-08-01 23:45:51 +0000971 TypeTy *Ty2 = ParseTypeName();
972
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000973 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +0000974 Diag(Tok, diag::err_expected_rparen);
975 return ExprResult(true);
976 }
Steve Naroff66356bd2007-09-16 14:56:35 +0000977 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +0000978 break;
Chris Lattner11124352006-08-12 19:16:08 +0000979 }
980
Chris Lattner11124352006-08-12 19:16:08 +0000981 // These can be followed by postfix-expr pieces because they are
982 // primary-expressions.
983 return ParsePostfixExpressionSuffix(Res);
984}
985
Chris Lattner4add4e62006-08-11 01:33:00 +0000986/// ParseParenExpression - This parses the unit that starts with a '(' token,
987/// based on what is allowed by ExprType. The actual thing parsed is returned
988/// in ExprType.
989///
990/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000991/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000992/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
993/// postfix-expression: [C99 6.5.2]
994/// '(' type-name ')' '{' initializer-list '}'
995/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000996/// cast-expression: [C99 6.5.4]
997/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000998///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000999Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
1000 TypeTy *&CastTy,
1001 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001002 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +00001003 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner366727f2007-07-24 16:58:17 +00001004 ExprResult Result(true);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001005 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +00001006
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001007 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001008 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattnercac27a52007-08-31 21:49:55 +00001009 Parser::StmtResult Stmt = ParseCompoundStatement(true);
Chris Lattner4add4e62006-08-11 01:33:00 +00001010 ExprType = CompoundStmt;
Chris Lattner366727f2007-07-24 16:58:17 +00001011
1012 // If the substmt parsed correctly, build the AST node.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001013 if (!Stmt.isInvalid && Tok.is(tok::r_paren))
Steve Naroff66356bd2007-09-16 14:56:35 +00001014 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
Chris Lattner366727f2007-07-24 16:58:17 +00001015
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001016 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001017 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001018 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001019
1020 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001021 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001022 RParenLoc = ConsumeParen();
1023 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001024 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001025
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001026 if (Tok.is(tok::l_brace)) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001027 if (!getLang().C99) // Compound literals don't exist in C90.
1028 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001029 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +00001030 ExprType = CompoundLiteral;
Steve Narofffbd09832007-07-19 01:06:55 +00001031 if (!Result.isInvalid)
Steve Naroff83895f72007-09-16 03:34:24 +00001032 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Chris Lattner4add4e62006-08-11 01:33:00 +00001033 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +00001034 // Note that this doesn't parse the subsequence cast-expression, it just
1035 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +00001036 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001037 CastTy = Ty;
1038 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +00001039 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001040 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001041 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001042 }
Chris Lattner89c50c62006-08-11 06:41:18 +00001043 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +00001044 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001045 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001046 ExprType = SimpleExpr;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001047 if (!Result.isInvalid && Tok.is(tok::r_paren))
Steve Naroff83895f72007-09-16 03:34:24 +00001048 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +00001049 }
Chris Lattnerc951dae2006-08-10 04:23:57 +00001050
Chris Lattner4564bc12006-08-10 23:14:52 +00001051 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +00001052 if (Result.isInvalid)
1053 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001054 else {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001055 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001056 RParenLoc = ConsumeParen();
1057 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001058 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001059 }
Chris Lattner1b926492006-08-23 06:42:10 +00001060
Chris Lattner89c50c62006-08-11 06:41:18 +00001061 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +00001062}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001063
Chris Lattnerd3e98952006-10-06 05:22:26 +00001064/// ParseStringLiteralExpression - This handles the various token types that
1065/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1066/// translation phase #6].
1067///
1068/// primary-expression: [C99 6.5.1]
1069/// string-literal
1070Parser::ExprResult Parser::ParseStringLiteralExpression() {
1071 assert(isTokenStringLiteral() && "Not a string literal!");
1072
1073 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1074 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001075 llvm::SmallVector<Token, 4> StringToks;
Chris Lattnerd3e98952006-10-06 05:22:26 +00001076
Chris Lattnerd3e98952006-10-06 05:22:26 +00001077 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001078 StringToks.push_back(Tok);
1079 ConsumeStringToken();
1080 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001081
1082 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroff83895f72007-09-16 03:34:24 +00001083 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001084}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001085
1086/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1087///
1088/// argument-expression-list:
1089/// assignment-expression
1090/// argument-expression-list , assignment-expression
1091///
1092/// [C++] expression-list:
1093/// [C++] assignment-expression
1094/// [C++] expression-list , assignment-expression
1095///
1096bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1097 while (1) {
1098 ExprResult Expr = ParseAssignmentExpression();
1099 if (Expr.isInvalid)
1100 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001101
1102 Exprs.push_back(Expr.Val);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001103
1104 if (Tok.isNot(tok::comma))
1105 return false;
1106 // Move to the next argument, remember where the comma was.
1107 CommaLocs.push_back(ConsumeToken());
1108 }
1109}
Steve Naroff0ac012832008-08-28 19:20:44 +00001110
1111/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001112/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001113///
1114/// block-literal:
1115/// [clang] '^' block-args[opt] compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001116/// [clang] block-args:
1117/// [clang] '(' parameter-list ')'
1118///
1119Parser::ExprResult Parser::ParseBlockLiteralExpression() {
1120 assert(Tok.is(tok::caret) && "block literal starts with ^");
1121 SourceLocation CaretLoc = ConsumeToken();
1122
1123 // Enter a scope to hold everything within the block. This includes the
1124 // argument decls, decls within the compound expression, etc. This also
1125 // allows determining whether a variable reference inside the block is
1126 // within or outside of the block.
1127 EnterScope(Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1128 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001129
1130 // Inform sema that we are starting a block.
1131 Actions.ActOnBlockStart(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001132
1133 // Parse the return type if present.
1134 DeclSpec DS;
1135 Declarator ParamInfo(DS, Declarator::PrototypeContext);
1136
1137 // If this block has arguments, parse them. There is no ambiguity here with
1138 // the expression case, because the expression case requires a parameter list.
1139 if (Tok.is(tok::l_paren)) {
1140 ParseParenDeclarator(ParamInfo);
1141 // Parse the pieces after the identifier as if we had "int(...)".
1142 ParamInfo.SetIdentifier(0, CaretLoc);
1143 if (ParamInfo.getInvalidType()) {
1144 // If there was an error parsing the arguments, they may have tried to use
1145 // ^(x+y) which requires an argument list. Just skip the whole block
1146 // literal.
1147 ExitScope();
1148 return true;
1149 }
1150 } else {
1151 // Otherwise, pretend we saw (void).
1152 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001153 0, 0, 0, CaretLoc));
Steve Naroff0ac012832008-08-28 19:20:44 +00001154 }
1155
1156 // Inform sema that we are starting a block.
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001157 Actions.ActOnBlockArguments(ParamInfo);
Steve Naroff0ac012832008-08-28 19:20:44 +00001158
Steve Naroff7a147c62008-09-16 23:11:46 +00001159 ExprResult Result = true;
Steve Naroff0ac012832008-08-28 19:20:44 +00001160 if (Tok.is(tok::l_brace)) {
1161 StmtResult Stmt = ParseCompoundStatementBody();
1162 if (!Stmt.isInvalid) {
1163 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.Val, CurScope);
1164 } else {
1165 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001166 }
1167 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001168 ExitScope();
1169 return Result;
1170}
1171