blob: f6443bcce264829660a23d5626b8880888200384 [file] [log] [blame]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001//===--- Expression.cpp - Expression Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
23#include "clang/Basic/Diagnostic.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000025using namespace llvm;
26using namespace clang;
27
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000028/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000029/// the C99 grammar. These have been named to relate with the C99 grammar
30/// productions. Low precedences numbers bind more weakly than high numbers.
31namespace prec {
32 enum Level {
33 Unknown = 0, // Not binary operator.
34 Comma = 1, // ,
35 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
36 Conditional = 3, // ?
37 LogicalOr = 4, // ||
38 LogicalAnd = 5, // &&
39 InclusiveOr = 6, // |
40 ExclusiveOr = 7, // ^
41 And = 8, // &
42 MinMax = 9, // <?, >? min, max (GCC extensions)
43 Equality = 10, // ==, !=
44 Relational = 11, // >=, <=, >, <
45 Shift = 12, // <<, >>
46 Additive = 13, // -, +
47 Multiplicative = 14 // *, /, %
48 };
49}
50
51
52/// getBinOpPrecedence - Return the precedence of the specified binary operator
53/// token. This returns:
54///
55static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
56 switch (Kind) {
57 default: return prec::Unknown;
58 case tok::comma: return prec::Comma;
59 case tok::equal:
60 case tok::starequal:
61 case tok::slashequal:
62 case tok::percentequal:
63 case tok::plusequal:
64 case tok::minusequal:
65 case tok::lesslessequal:
66 case tok::greatergreaterequal:
67 case tok::ampequal:
68 case tok::caretequal:
69 case tok::pipeequal: return prec::Assignment;
70 case tok::question: return prec::Conditional;
71 case tok::pipepipe: return prec::LogicalOr;
72 case tok::ampamp: return prec::LogicalAnd;
73 case tok::pipe: return prec::InclusiveOr;
74 case tok::caret: return prec::ExclusiveOr;
75 case tok::amp: return prec::And;
76 case tok::lessquestion:
77 case tok::greaterquestion: return prec::MinMax;
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
Chris Lattnerce7e21d2006-08-12 17:22:40 +000095/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000096/// operators.
97///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// 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///
Chris Lattnercde626a2006-08-12 08:13:25 +0000107/// 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
163///
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 Lattnercde626a2006-08-12 08:13:25 +0000172 ExprResult LHS = ParseCastExpression(false);
173 if (LHS.isInvalid) return LHS;
174
175 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
176}
177
Chris Lattner0c6c0342006-08-12 18:12:45 +0000178/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
179///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000180Parser::ExprResult Parser::ParseAssignmentExpression() {
181 ExprResult LHS = ParseCastExpression(false);
182 if (LHS.isInvalid) return LHS;
183
184 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
185}
186
Chris Lattner3b561a32006-08-13 00:12:11 +0000187Parser::ExprResult Parser::ParseConstantExpression() {
188 ExprResult LHS = ParseCastExpression(false);
189 if (LHS.isInvalid) return LHS;
190
191 // TODO: Validate that this is a constant expr!
192 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
193}
194
Chris Lattner0c6c0342006-08-12 18:12:45 +0000195/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
196/// in contexts where we have already consumed an identifier (which we saved in
197/// 'Tok'), then discovered that the identifier was really the leading token of
198/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
199/// now in 'Tok') and the current token is "[".
200Parser::ExprResult Parser::
201ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
202 // We know that 'Tok' must correspond to this production:
203 // primary-expression: identifier
204
205 // TODO: Pass 'Tok' to the action.
206 ExprResult Res = ExprResult(false);
207
208 // Because we have to parse an entire cast-expression before starting the
209 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
210 // need to handle the 'postfix-expression' rules. We do this by invoking
211 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
212 Res = ParsePostfixExpressionSuffix(Res);
213 if (Res.isInvalid) return Res;
214
215 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
216 // done, we know we don't have to do anything for cast-expression, because the
217 // only non-postfix-expression production starts with a '(' token, and we know
218 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
219 // to consume any trailing operators (e.g. "+" in this example) and connected
220 // chunks of the expression.
221 return ParseRHSOfBinaryExpression(Res, prec::Comma);
222}
223
Chris Lattner8693a512006-08-13 21:54:02 +0000224/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
225/// in contexts where we have already consumed an identifier (which we saved in
226/// 'Tok'), then discovered that the identifier was really the leading token of
227/// part of an assignment-expression. For example, in "A[1]+B", we consumed "A"
228/// (which is now in 'Tok') and the current token is "[".
229Parser::ExprResult Parser::
230ParseAssignmentExprWithLeadingIdentifier(const LexerToken &Tok) {
231 // We know that 'Tok' must correspond to this production:
232 // primary-expression: identifier
233
234 // TODO: Pass 'Tok' to the action.
235 ExprResult Res = ExprResult(false);
236
237 // Because we have to parse an entire cast-expression before starting the
238 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
239 // need to handle the 'postfix-expression' rules. We do this by invoking
240 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
241 Res = ParsePostfixExpressionSuffix(Res);
242 if (Res.isInvalid) return Res;
243
244 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
245 // done, we know we don't have to do anything for cast-expression, because the
246 // only non-postfix-expression production starts with a '(' token, and we know
247 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
248 // to consume any trailing operators (e.g. "+" in this example) and connected
249 // chunks of the expression.
250 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
251}
252
253
Chris Lattner62591722006-08-12 18:40:58 +0000254/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
255/// used in contexts where we have already consumed a '*' (which we saved in
256/// 'Tok'), then discovered that the '*' was really the leading token of an
257/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
258/// now in 'Tok') and the current token is "(".
259Parser::ExprResult Parser::
260ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
261 // We know that 'Tok' must correspond to this production:
262 // unary-expression: unary-operator cast-expression
263 // where 'unary-operator' is '*'.
264
265 // Parse the cast-expression that follows the '*'. This will parse the
266 // "*(int*)P" part of "*(int*)P+B".
267 ExprResult Res = ParseCastExpression(false);
268 if (Res.isInvalid) return Res;
269
270 // TODO: Combine Tok + Res to get the new AST.
271
272 // We have to parse an entire cast-expression before starting the
273 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
274 // we know that the only production above us is the cast-expression
275 // production, and because the only alternative productions start with a '('
276 // token (we know we had a '*'), there is no work to do to get a whole
277 // cast-expression.
278
279 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
280 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
281 // trailing operators (e.g. "+" in this example) and connected chunks of the
282 // assignment-expression.
283 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
284}
285
286
Chris Lattnercde626a2006-08-12 08:13:25 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
289Parser::ExprResult
290Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000292 SourceLocation ColonLoc;
293
Chris Lattnercde626a2006-08-12 08:13:25 +0000294 while (1) {
295 // If this token has a lower precedence than we are allowed to parse (e.g.
296 // because we are called recursively, or because the token is not a binop),
297 // then we are done!
298 if (NextTokPrec < MinPrec)
299 return LHS;
300
301 // Consume the operator, saving the operator token for error reporting.
302 LexerToken OpToken = Tok;
303 ConsumeToken();
304
Chris Lattner96c3deb2006-08-12 17:13:08 +0000305 // Special case handling for the ternary operator.
306 ExprResult TernaryMiddle;
307 if (NextTokPrec == prec::Conditional) {
308 if (Tok.getKind() != tok::colon) {
309 // Handle this production specially:
310 // logical-OR-expression '?' expression ':' conditional-expression
311 // In particular, the RHS of the '?' is 'expression', not
312 // 'logical-OR-expression' as we might expect.
313 TernaryMiddle = ParseExpression();
314 if (TernaryMiddle.isInvalid) return TernaryMiddle;
315 } else {
316 // Special case handling of "X ? Y : Z" where Y is empty:
317 // logical-OR-expression '?' ':' conditional-expression [GNU]
318 TernaryMiddle = ExprResult(false);
319 Diag(Tok, diag::ext_gnu_conditional_expr);
320 }
321
322 if (Tok.getKind() != tok::colon) {
323 Diag(Tok, diag::err_expected_colon);
324 Diag(OpToken, diag::err_matching, "?");
325 return ExprResult(true);
326 }
327
328 // Eat the colon.
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000329 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000330 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000331 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000332
333 // Parse another leaf here for the RHS of the operator.
334 ExprResult RHS = ParseCastExpression(false);
335 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000336
337 // Remember the precedence of this operator and get the precedence of the
338 // operator immediately to the right of the RHS.
339 unsigned ThisPrec = NextTokPrec;
340 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000341
342 // Assignment and conditional expressions are right-associative.
343 bool isRightAssoc = NextTokPrec == prec::Conditional ||
344 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000345
346 // Get the precedence of the operator to the right of the RHS. If it binds
347 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000348 if (ThisPrec < NextTokPrec ||
349 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000350 // If this is left-associative, only parse things on the RHS that bind
351 // more tightly than the current operator. If it is left-associative, it
352 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
353 // A=(B=(C=D)), where each paren is a level of recursion here.
354 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000355 if (RHS.isInvalid) return RHS;
356
357 NextTokPrec = getBinOpPrecedence(Tok.getKind());
358 }
359 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
360
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000361 // Combine the LHS and RHS into the LHS (e.g. build AST).
362 if (NextTokPrec != prec::Conditional)
363 LHS = Actions.ParseBinOp(OpToken, LHS.Val, RHS.Val);
364 else
365 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
366 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000367 }
368}
369
Chris Lattnereaf06592006-08-11 02:02:23 +0000370/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
371/// true, parse a unary-expression.
372///
Chris Lattner4564bc12006-08-10 23:14:52 +0000373/// cast-expression: [C99 6.5.4]
374/// unary-expression
375/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000376///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000377/// unary-expression: [C99 6.5.3]
378/// postfix-expression
379/// '++' unary-expression
380/// '--' unary-expression
381/// unary-operator cast-expression
382/// 'sizeof' unary-expression
383/// 'sizeof' '(' type-name ')'
384/// [GNU] '__alignof' unary-expression
385/// [GNU] '__alignof' '(' type-name ')'
386/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000387///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000388/// unary-operator: one of
389/// '&' '*' '+' '-' '~' '!'
390/// [GNU] '__extension__' '__real' '__imag'
391///
Chris Lattner52a99e52006-08-10 20:56:00 +0000392/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000393/// identifier
394/// constant
395/// string-literal
396/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000397/// '__func__' [C99 6.4.2.2]
398/// [GNU] '__FUNCTION__'
399/// [GNU] '__PRETTY_FUNCTION__'
400/// [GNU] '(' compound-statement ')'
401/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
402/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
403/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
404/// assign-expr ')'
405/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
406/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
407/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
408/// [OBC] '@protocol' '(' identifier ')' [TODO]
409/// [OBC] '@encode' '(' type-name ')' [TODO]
410/// [OBC] objc-string-literal [TODO]
411///
412/// constant: [C99 6.4.4]
413/// integer-constant
414/// floating-constant
415/// enumeration-constant -> identifier
416/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000417///
Chris Lattner89c50c62006-08-11 06:41:18 +0000418Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
419 ExprResult Res;
Chris Lattner1b926492006-08-23 06:42:10 +0000420 LexerToken SavedTok;
Chris Lattner89c50c62006-08-11 06:41:18 +0000421
Chris Lattner81b576e2006-08-11 02:13:20 +0000422 // This handles all of cast-expression, unary-expression, postfix-expression,
423 // and primary-expression. We handle them together like this for efficiency
424 // and to simplify handling of an expression starting with a '(' token: which
425 // may be one of a parenthesized expression, cast-expression, compound literal
426 // expression, or statement expression.
427 //
428 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000429 // call ParsePostfixExpressionSuffix to handle the postfix expression
430 // suffixes. Cases that cannot be followed by postfix exprs should
431 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000432 switch (Tok.getKind()) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000433 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000434 // If this expression is limited to being a unary-expression, the parent can
435 // not start a cast expression.
436 ParenParseOption ParenExprType =
437 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000438 TypeTy *CastTy;
439 SourceLocation LParenLoc = Tok.getLocation();
440 SourceLocation RParenLoc;
441 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000442 if (Res.isInvalid) return Res;
443
Chris Lattner81b576e2006-08-11 02:13:20 +0000444 switch (ParenExprType) {
445 case SimpleExpr: break; // Nothing else to do.
446 case CompoundStmt: break; // Nothing else to do.
447 case CompoundLiteral:
448 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
449 // postfix-expression exist, parse them now.
450 break;
451 case CastExpr:
452 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
453 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000454 // TODO: For cast expression with CastTy.
455 Res = ParseCastExpression(false);
456 if (!Res.isInvalid)
457 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
458 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000459 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000460
461 // These can be followed by postfix-expr pieces.
462 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000463 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000464
Chris Lattner52a99e52006-08-10 20:56:00 +0000465 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000466 case tok::numeric_constant:
467 // constant: integer-constant
468 // constant: floating-constant
469
470 // TODO: Validate whether this is an integer or floating-constant or
471 // neither.
472 if (1) {
473 Res = Actions.ParseIntegerConstant(Tok);
474 } else {
475 Res = Actions.ParseFloatingConstant(Tok);
476 }
477 ConsumeToken();
478
479 // These can be followed by postfix-expr pieces.
480 return ParsePostfixExpressionSuffix(Res);
481
Chris Lattner52a99e52006-08-10 20:56:00 +0000482 case tok::identifier: // primary-expression: identifier
483 // constant: enumeration-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000484 case tok::char_constant: // constant: character-constant
485 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
486 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
487 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner879b9ad2006-08-24 04:53:44 +0000488 Res = Actions.ParseSimplePrimaryExpr(Tok);
Chris Lattner52a99e52006-08-10 20:56:00 +0000489 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000490 // These can be followed by postfix-expr pieces.
491 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000492 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000493 Res = ParseStringLiteralExpression();
494 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000495 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
496 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000497 case tok::kw___builtin_va_arg:
498 case tok::kw___builtin_offsetof:
499 case tok::kw___builtin_choose_expr:
500 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000501 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000502 case tok::plusplus: // unary-expression: '++' unary-expression
503 case tok::minusminus: // unary-expression: '--' unary-expression
Chris Lattner1b926492006-08-23 06:42:10 +0000504 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000505 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000506 Res = ParseCastExpression(true);
507 if (!Res.isInvalid)
508 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
509 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000510 case tok::amp: // unary-expression: '&' cast-expression
511 case tok::star: // unary-expression: '*' cast-expression
512 case tok::plus: // unary-expression: '+' cast-expression
513 case tok::minus: // unary-expression: '-' cast-expression
514 case tok::tilde: // unary-expression: '~' cast-expression
515 case tok::exclaim: // unary-expression: '!' cast-expression
516 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000517 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner81b576e2006-08-11 02:13:20 +0000518 //case tok::kw__extension__: [TODO]
Chris Lattner1b926492006-08-23 06:42:10 +0000519 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000520 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000521 Res = ParseCastExpression(false);
522 if (!Res.isInvalid)
523 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
524 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000525
526 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
527 // unary-expression: 'sizeof' '(' type-name ')'
528 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
529 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000530 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000531 case tok::ampamp: // unary-expression: '&&' identifier
532 Diag(Tok, diag::ext_gnu_address_of_label);
533 ConsumeToken();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000534 // TODO: Build AST.
Chris Lattner81b576e2006-08-11 02:13:20 +0000535 if (Tok.getKind() == tok::identifier) {
536 ConsumeToken();
537 } else {
538 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000539 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000540 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000541 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000542 default:
543 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000544 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000545 }
546
Chris Lattner20c6a452006-08-12 17:40:43 +0000547 // unreachable.
548 abort();
549}
550
551/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
552/// is parsed, this method parses any suffixes that apply.
553///
554/// postfix-expression: [C99 6.5.2]
555/// primary-expression
556/// postfix-expression '[' expression ']'
557/// postfix-expression '(' argument-expression-list[opt] ')'
558/// postfix-expression '.' identifier
559/// postfix-expression '->' identifier
560/// postfix-expression '++'
561/// postfix-expression '--'
562/// '(' type-name ')' '{' initializer-list '}'
563/// '(' type-name ')' '{' initializer-list ',' '}'
564///
565/// argument-expression-list: [C99 6.5.2]
566/// argument-expression
567/// argument-expression-list ',' assignment-expression
568///
569Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000570
Chris Lattnerf8339772006-08-10 22:01:51 +0000571 // Now that the primary-expression piece of the postfix-expression has been
572 // parsed, see if there are any postfix-expression pieces here.
573 SourceLocation Loc;
574 while (1) {
575 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000576 default: // Not a postfix-expression suffix.
577 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000578 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner89c50c62006-08-11 06:41:18 +0000579 Loc = Tok.getLocation();
580 ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000581 ExprResult Idx = ParseExpression();
582
583 SourceLocation RLoc = Tok.getLocation();
584
585 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
586 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
587 else
588 LHS = ExprResult(true);
589
Chris Lattner89c50c62006-08-11 06:41:18 +0000590 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000591 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000592 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000593 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000594
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000595 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
596 SmallVector<ExprTy*, 8> ArgExprs;
597 SmallVector<SourceLocation, 8> CommaLocs;
598 bool ArgExprsOk = true;
599
Chris Lattner89c50c62006-08-11 06:41:18 +0000600 Loc = Tok.getLocation();
601 ConsumeParen();
602
Chris Lattner0c6c0342006-08-12 18:12:45 +0000603 if (Tok.getKind() != tok::r_paren) {
604 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000605 ExprResult ArgExpr = ParseAssignmentExpression();
606 if (ArgExpr.isInvalid)
607 ArgExprsOk = false;
608 else
609 ArgExprs.push_back(ArgExpr.Val);
610
Chris Lattner0c6c0342006-08-12 18:12:45 +0000611 if (Tok.getKind() != tok::comma)
612 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000613 CommaLocs.push_back(Tok.getLocation());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000614 ConsumeToken(); // Next argument.
615 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000616 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000617
Chris Lattner89c50c62006-08-11 06:41:18 +0000618 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000619 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
620 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
621 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000622 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000623 &CommaLocs[0], Tok.getLocation());
624 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000625
Chris Lattner04f80192006-08-15 04:55:54 +0000626 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000627 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000628 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000629 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000630 case tok::period: { // postfix-expression: p-e '.' identifier
631 SourceLocation OpLoc = Tok.getLocation();
632 tok::TokenKind OpKind = Tok.getKind();
633 ConsumeToken(); // Eat the "." or "->" token.
634
Chris Lattner89c50c62006-08-11 06:41:18 +0000635 if (Tok.getKind() != tok::identifier) {
636 Diag(Tok, diag::err_expected_ident);
637 return ExprResult(true);
638 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000639
640 if (!LHS.isInvalid)
641 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
642 Tok.getLocation(),
643 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000644 ConsumeToken();
645 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000646 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000647 case tok::plusplus: // postfix-expression: postfix-expression '++'
648 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000649 if (!LHS.isInvalid)
650 LHS = Actions.ParsePostfixUnaryOp(Tok, LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000651 ConsumeToken();
652 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000653 }
654 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000655}
656
Chris Lattner20c6a452006-08-12 17:40:43 +0000657
Chris Lattner81b576e2006-08-11 02:13:20 +0000658/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
659/// unary-expression: [C99 6.5.3]
660/// 'sizeof' unary-expression
661/// 'sizeof' '(' type-name ')'
662/// [GNU] '__alignof' unary-expression
663/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000664Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000665 assert((Tok.getKind() == tok::kw_sizeof ||
666 Tok.getKind() == tok::kw___alignof) &&
667 "Not a sizeof/alignof expression!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000668 LexerToken OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000669 ConsumeToken();
670
671 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000672 ExprResult Operand;
673 if (Tok.getKind() != tok::l_paren) {
674 Operand = ParseCastExpression(true);
675 } else {
676 // If it starts with a '(', we know that it is either a parenthesized
677 // type-name, or it is a unary-expression that starts with a compound
678 // literal, or starts with a primary-expression that is a parenthesized
679 // expression.
680 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000681 TypeTy *CastTy;
682 SourceLocation RParenLoc;
683 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000684
685 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
686 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
687 if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000688 // TODO: Build AST here for sizeof type.
689 CastTy;
Chris Lattner26115ac2006-08-24 06:10:04 +0000690 return ExprResult(false);
691 }
692 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000693
Chris Lattner26115ac2006-08-24 06:10:04 +0000694 // If we get here, the operand to the sizeof/alignof was an expresion.
695 if (!Operand.isInvalid)
696 Operand = Actions.ParseUnaryOp(OpTok, Operand.Val);
697 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000698}
699
Chris Lattner11124352006-08-12 19:16:08 +0000700/// ParseBuiltinPrimaryExpression
701///
702/// primary-expression: [C99 6.5.1]
703/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
704/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
705/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
706/// assign-expr ')'
707/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
708///
709/// [GNU] offsetof-member-designator:
710/// [GNU] identifier
711/// [GNU] offsetof-member-designator '.' identifier
712/// [GNU] offsetof-member-designator '[' expression ']'
713///
714Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
715 ExprResult Res(false);
716 SourceLocation StartLoc = Tok.getLocation();
717 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
718
719 tok::TokenKind T = Tok.getKind();
720 ConsumeToken(); // Eat the builtin identifier.
721
722 // All of these start with an open paren.
723 if (Tok.getKind() != tok::l_paren) {
724 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
725 return ExprResult(true);
726 }
727
728 SourceLocation LParenLoc = Tok.getLocation();
729 ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000730 // TODO: Build AST.
731
Chris Lattner11124352006-08-12 19:16:08 +0000732 switch (T) {
733 default: assert(0 && "Not a builtin primary expression!");
734 case tok::kw___builtin_va_arg:
735 Res = ParseAssignmentExpression();
736 if (Res.isInvalid) {
737 SkipUntil(tok::r_paren);
738 return Res;
739 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000740
Chris Lattner6d7e6342006-08-15 03:41:14 +0000741 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000742 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000743
Chris Lattner11124352006-08-12 19:16:08 +0000744 ParseTypeName();
745 break;
746
747 case tok::kw___builtin_offsetof:
748 ParseTypeName();
749
Chris Lattner6d7e6342006-08-15 03:41:14 +0000750 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000751 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000752
753 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000754 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000755 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000756 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000757
Chris Lattner11124352006-08-12 19:16:08 +0000758 while (1) {
759 if (Tok.getKind() == tok::period) {
760 // offsetof-member-designator: offsetof-member-designator '.' identifier
761 ConsumeToken();
762
Chris Lattner6d7e6342006-08-15 03:41:14 +0000763 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000764 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000765 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000766 } else if (Tok.getKind() == tok::l_square) {
767 // offsetof-member-designator: offsetof-member-design '[' expression ']'
768 SourceLocation LSquareLoc = Tok.getLocation();
769 ConsumeBracket();
770 Res = ParseExpression();
771 if (Res.isInvalid) {
772 SkipUntil(tok::r_paren);
773 return Res;
774 }
775
Chris Lattner04f80192006-08-15 04:55:54 +0000776 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000777 } else {
778 break;
779 }
780 }
781 break;
782 case tok::kw___builtin_choose_expr:
783 Res = ParseAssignmentExpression();
784
Chris Lattner6d7e6342006-08-15 03:41:14 +0000785 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000786 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000787
788 Res = ParseAssignmentExpression();
789
Chris Lattner6d7e6342006-08-15 03:41:14 +0000790 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000791 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000792
793 Res = ParseAssignmentExpression();
794 break;
795 case tok::kw___builtin_types_compatible_p:
796 ParseTypeName();
797
Chris Lattner6d7e6342006-08-15 03:41:14 +0000798 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000799 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000800
801 ParseTypeName();
802 break;
803 }
804
Chris Lattner04f80192006-08-15 04:55:54 +0000805 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000806
807 // These can be followed by postfix-expr pieces because they are
808 // primary-expressions.
809 return ParsePostfixExpressionSuffix(Res);
810}
811
Chris Lattner52a99e52006-08-10 20:56:00 +0000812/// ParseStringLiteralExpression - This handles the various token types that
813/// form string literals, and also handles string concatenation [C99 5.1.1.2,
814/// translation phase #6].
815///
816/// primary-expression: [C99 6.5.1]
817/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000818Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000819 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000820 ConsumeStringToken();
821
822 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
823 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000824 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000825 ConsumeStringToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000826 // TODO: Build AST for string literals.
Chris Lattner89c50c62006-08-11 06:41:18 +0000827 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000828}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000829
Chris Lattnerc951dae2006-08-10 04:23:57 +0000830
Chris Lattner4add4e62006-08-11 01:33:00 +0000831/// ParseParenExpression - This parses the unit that starts with a '(' token,
832/// based on what is allowed by ExprType. The actual thing parsed is returned
833/// in ExprType.
834///
835/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000836/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000837/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
838/// postfix-expression: [C99 6.5.2]
839/// '(' type-name ')' '{' initializer-list '}'
840/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000841/// cast-expression: [C99 6.5.4]
842/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000843///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000844Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
845 TypeTy *&CastTy,
846 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000847 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
848 SourceLocation OpenLoc = Tok.getLocation();
849 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000850 ExprResult Result(false);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000851 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000852
Chris Lattner4add4e62006-08-11 01:33:00 +0000853 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000854 !getLang().NoExtensions) {
855 Diag(Tok, diag::ext_gnu_statement_expr);
856 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000857 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000858 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000859 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000860 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000861 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000862
863 // Match the ')'.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000864 if (Tok.getKind() == tok::r_paren) {
865 RParenLoc = Tok.getLocation();
866 ConsumeParen();
867 } else {
868 MatchRHSPunctuation(tok::r_paren, OpenLoc);
869 }
870
Chris Lattner4add4e62006-08-11 01:33:00 +0000871 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000872 if (!getLang().C99) // Compound literals don't exist in C90.
873 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000874 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000875 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000876 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000877 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000878 // Note that this doesn't parse the subsequence cast-expression, it just
879 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000880 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000881 CastTy = Ty;
882 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000883 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000884 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000885 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000886 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000887 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000888 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000889 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000890 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000891 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
892 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000893 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000894
Chris Lattner4564bc12006-08-10 23:14:52 +0000895 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000896 if (Result.isInvalid)
897 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000898 else {
899 if (Tok.getKind() == tok::r_paren) {
900 RParenLoc = Tok.getLocation();
901 ConsumeParen();
902 } else {
903 MatchRHSPunctuation(tok::r_paren, OpenLoc);
904 }
905 }
Chris Lattner1b926492006-08-23 06:42:10 +0000906
Chris Lattner89c50c62006-08-11 06:41:18 +0000907 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000908}