blob: aae3e44721ef40b77e92101af78b1dded5a59ce2 [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 Lattner81b576e2006-08-11 02:13:20 +0000433 case tok::l_paren:
434 // 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 Lattner89c50c62006-08-11 06:41:18 +0000438 Res = ParseParenExpression(ParenExprType);
439 if (Res.isInvalid) return Res;
440
Chris Lattner81b576e2006-08-11 02:13:20 +0000441 switch (ParenExprType) {
442 case SimpleExpr: break; // Nothing else to do.
443 case CompoundStmt: break; // Nothing else to do.
444 case CompoundLiteral:
445 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
446 // postfix-expression exist, parse them now.
447 break;
448 case CastExpr:
449 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
450 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000451 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000452 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000453
454 // These can be followed by postfix-expr pieces.
455 return ParsePostfixExpressionSuffix(Res);
Chris Lattner89c50c62006-08-11 06:41:18 +0000456
Chris Lattner52a99e52006-08-10 20:56:00 +0000457 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000458 case tok::numeric_constant:
459 // constant: integer-constant
460 // constant: floating-constant
461
462 // TODO: Validate whether this is an integer or floating-constant or
463 // neither.
464 if (1) {
465 Res = Actions.ParseIntegerConstant(Tok);
466 } else {
467 Res = Actions.ParseFloatingConstant(Tok);
468 }
469 ConsumeToken();
470
471 // These can be followed by postfix-expr pieces.
472 return ParsePostfixExpressionSuffix(Res);
473
Chris Lattner52a99e52006-08-10 20:56:00 +0000474 case tok::identifier: // primary-expression: identifier
475 // constant: enumeration-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000476 case tok::char_constant: // constant: character-constant
477 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
478 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
479 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner879b9ad2006-08-24 04:53:44 +0000480 Res = Actions.ParseSimplePrimaryExpr(Tok);
Chris Lattner52a99e52006-08-10 20:56:00 +0000481 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000482 // These can be followed by postfix-expr pieces.
483 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000484 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000485 Res = ParseStringLiteralExpression();
486 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000487 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
488 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000489 case tok::kw___builtin_va_arg:
490 case tok::kw___builtin_offsetof:
491 case tok::kw___builtin_choose_expr:
492 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000493 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000494 case tok::plusplus: // unary-expression: '++' unary-expression
495 case tok::minusminus: // unary-expression: '--' unary-expression
Chris Lattner1b926492006-08-23 06:42:10 +0000496 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000497 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000498 Res = ParseCastExpression(true);
499 if (!Res.isInvalid)
500 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
501 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000502 case tok::amp: // unary-expression: '&' cast-expression
503 case tok::star: // unary-expression: '*' cast-expression
504 case tok::plus: // unary-expression: '+' cast-expression
505 case tok::minus: // unary-expression: '-' cast-expression
506 case tok::tilde: // unary-expression: '~' cast-expression
507 case tok::exclaim: // unary-expression: '!' cast-expression
508 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000509 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner81b576e2006-08-11 02:13:20 +0000510 //case tok::kw__extension__: [TODO]
Chris Lattner1b926492006-08-23 06:42:10 +0000511 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000512 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000513 Res = ParseCastExpression(false);
514 if (!Res.isInvalid)
515 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
516 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000517
518 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
519 // unary-expression: 'sizeof' '(' type-name ')'
520 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
521 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000522 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000523 case tok::ampamp: // unary-expression: '&&' identifier
524 Diag(Tok, diag::ext_gnu_address_of_label);
525 ConsumeToken();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000526 // TODO: Build AST.
Chris Lattner81b576e2006-08-11 02:13:20 +0000527 if (Tok.getKind() == tok::identifier) {
528 ConsumeToken();
529 } else {
530 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000531 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000532 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000533 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000534 default:
535 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000536 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000537 }
538
Chris Lattner20c6a452006-08-12 17:40:43 +0000539 // unreachable.
540 abort();
541}
542
543/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
544/// is parsed, this method parses any suffixes that apply.
545///
546/// postfix-expression: [C99 6.5.2]
547/// primary-expression
548/// postfix-expression '[' expression ']'
549/// postfix-expression '(' argument-expression-list[opt] ')'
550/// postfix-expression '.' identifier
551/// postfix-expression '->' identifier
552/// postfix-expression '++'
553/// postfix-expression '--'
554/// '(' type-name ')' '{' initializer-list '}'
555/// '(' type-name ')' '{' initializer-list ',' '}'
556///
557/// argument-expression-list: [C99 6.5.2]
558/// argument-expression
559/// argument-expression-list ',' assignment-expression
560///
561Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
562 assert(!LHS.isInvalid && "LHS is invalid already!");
563
Chris Lattnerf8339772006-08-10 22:01:51 +0000564 // Now that the primary-expression piece of the postfix-expression has been
565 // parsed, see if there are any postfix-expression pieces here.
566 SourceLocation Loc;
567 while (1) {
568 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000569 default: // Not a postfix-expression suffix.
570 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000571 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner89c50c62006-08-11 06:41:18 +0000572 Loc = Tok.getLocation();
573 ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000574 ExprResult Idx = ParseExpression();
575
576 SourceLocation RLoc = Tok.getLocation();
577
578 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
579 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
580 else
581 LHS = ExprResult(true);
582
Chris Lattner89c50c62006-08-11 06:41:18 +0000583 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000584 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000585 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000586 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000587
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000588 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
589 SmallVector<ExprTy*, 8> ArgExprs;
590 SmallVector<SourceLocation, 8> CommaLocs;
591 bool ArgExprsOk = true;
592
Chris Lattner89c50c62006-08-11 06:41:18 +0000593 Loc = Tok.getLocation();
594 ConsumeParen();
595
Chris Lattner0c6c0342006-08-12 18:12:45 +0000596 if (Tok.getKind() != tok::r_paren) {
597 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000598 ExprResult ArgExpr = ParseAssignmentExpression();
599 if (ArgExpr.isInvalid)
600 ArgExprsOk = false;
601 else
602 ArgExprs.push_back(ArgExpr.Val);
603
Chris Lattner0c6c0342006-08-12 18:12:45 +0000604 if (Tok.getKind() != tok::comma)
605 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000606 CommaLocs.push_back(Tok.getLocation());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000607 ConsumeToken(); // Next argument.
608 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000609 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000610
Chris Lattner89c50c62006-08-11 06:41:18 +0000611 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000612 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
613 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
614 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000615 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000616 &CommaLocs[0], Tok.getLocation());
617 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000618
Chris Lattner04f80192006-08-15 04:55:54 +0000619 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000620 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000621 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000622 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000623 case tok::period: { // postfix-expression: p-e '.' identifier
624 SourceLocation OpLoc = Tok.getLocation();
625 tok::TokenKind OpKind = Tok.getKind();
626 ConsumeToken(); // Eat the "." or "->" token.
627
Chris Lattner89c50c62006-08-11 06:41:18 +0000628 if (Tok.getKind() != tok::identifier) {
629 Diag(Tok, diag::err_expected_ident);
630 return ExprResult(true);
631 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000632
633 if (!LHS.isInvalid)
634 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
635 Tok.getLocation(),
636 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000637 ConsumeToken();
638 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000639 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000640 case tok::plusplus: // postfix-expression: postfix-expression '++'
641 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000642 if (!LHS.isInvalid)
643 LHS = Actions.ParsePostfixUnaryOp(Tok, LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000644 ConsumeToken();
645 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000646 }
647 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000648}
649
Chris Lattner20c6a452006-08-12 17:40:43 +0000650
Chris Lattner81b576e2006-08-11 02:13:20 +0000651/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
652/// unary-expression: [C99 6.5.3]
653/// 'sizeof' unary-expression
654/// 'sizeof' '(' type-name ')'
655/// [GNU] '__alignof' unary-expression
656/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000657Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000658 assert((Tok.getKind() == tok::kw_sizeof ||
659 Tok.getKind() == tok::kw___alignof) &&
660 "Not a sizeof/alignof expression!");
661 ConsumeToken();
662
663 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000664 // TODO: Build AST.
Chris Lattner0be454e2006-08-12 19:30:51 +0000665 if (Tok.getKind() != tok::l_paren)
Chris Lattner89c50c62006-08-11 06:41:18 +0000666 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000667
668 // If it starts with a '(', we know that it is either a parenthesized
669 // type-name, or it is a unary-expression that starts with a compound literal,
670 // or starts with a primary-expression that is a parenthesized expression.
671 ParenParseOption ExprType = CastExpr;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000672 // TODO: Build AST.
Chris Lattner89c50c62006-08-11 06:41:18 +0000673 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000674}
675
Chris Lattner11124352006-08-12 19:16:08 +0000676/// ParseBuiltinPrimaryExpression
677///
678/// primary-expression: [C99 6.5.1]
679/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
680/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
681/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
682/// assign-expr ')'
683/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
684///
685/// [GNU] offsetof-member-designator:
686/// [GNU] identifier
687/// [GNU] offsetof-member-designator '.' identifier
688/// [GNU] offsetof-member-designator '[' expression ']'
689///
690Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
691 ExprResult Res(false);
692 SourceLocation StartLoc = Tok.getLocation();
693 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
694
695 tok::TokenKind T = Tok.getKind();
696 ConsumeToken(); // Eat the builtin identifier.
697
698 // All of these start with an open paren.
699 if (Tok.getKind() != tok::l_paren) {
700 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
701 return ExprResult(true);
702 }
703
704 SourceLocation LParenLoc = Tok.getLocation();
705 ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000706 // TODO: Build AST.
707
Chris Lattner11124352006-08-12 19:16:08 +0000708 switch (T) {
709 default: assert(0 && "Not a builtin primary expression!");
710 case tok::kw___builtin_va_arg:
711 Res = ParseAssignmentExpression();
712 if (Res.isInvalid) {
713 SkipUntil(tok::r_paren);
714 return Res;
715 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000716
Chris Lattner6d7e6342006-08-15 03:41:14 +0000717 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000718 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000719
Chris Lattner11124352006-08-12 19:16:08 +0000720 ParseTypeName();
721 break;
722
723 case tok::kw___builtin_offsetof:
724 ParseTypeName();
725
Chris Lattner6d7e6342006-08-15 03:41:14 +0000726 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000727 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000728
729 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000730 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000731 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000732 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000733
Chris Lattner11124352006-08-12 19:16:08 +0000734 while (1) {
735 if (Tok.getKind() == tok::period) {
736 // offsetof-member-designator: offsetof-member-designator '.' identifier
737 ConsumeToken();
738
Chris Lattner6d7e6342006-08-15 03:41:14 +0000739 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000740 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000741 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000742 } else if (Tok.getKind() == tok::l_square) {
743 // offsetof-member-designator: offsetof-member-design '[' expression ']'
744 SourceLocation LSquareLoc = Tok.getLocation();
745 ConsumeBracket();
746 Res = ParseExpression();
747 if (Res.isInvalid) {
748 SkipUntil(tok::r_paren);
749 return Res;
750 }
751
Chris Lattner04f80192006-08-15 04:55:54 +0000752 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000753 } else {
754 break;
755 }
756 }
757 break;
758 case tok::kw___builtin_choose_expr:
759 Res = ParseAssignmentExpression();
760
Chris Lattner6d7e6342006-08-15 03:41:14 +0000761 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000762 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000763
764 Res = ParseAssignmentExpression();
765
Chris Lattner6d7e6342006-08-15 03:41:14 +0000766 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000767 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000768
769 Res = ParseAssignmentExpression();
770 break;
771 case tok::kw___builtin_types_compatible_p:
772 ParseTypeName();
773
Chris Lattner6d7e6342006-08-15 03:41:14 +0000774 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000775 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000776
777 ParseTypeName();
778 break;
779 }
780
Chris Lattner04f80192006-08-15 04:55:54 +0000781 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000782
783 // These can be followed by postfix-expr pieces because they are
784 // primary-expressions.
785 return ParsePostfixExpressionSuffix(Res);
786}
787
Chris Lattner52a99e52006-08-10 20:56:00 +0000788/// ParseStringLiteralExpression - This handles the various token types that
789/// form string literals, and also handles string concatenation [C99 5.1.1.2,
790/// translation phase #6].
791///
792/// primary-expression: [C99 6.5.1]
793/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000794Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000795 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000796 ConsumeStringToken();
797
798 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
799 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000800 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000801 ConsumeStringToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000802 // TODO: Build AST for string literals.
Chris Lattner89c50c62006-08-11 06:41:18 +0000803 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000804}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000805
Chris Lattnerc951dae2006-08-10 04:23:57 +0000806
Chris Lattner4add4e62006-08-11 01:33:00 +0000807/// ParseParenExpression - This parses the unit that starts with a '(' token,
808/// based on what is allowed by ExprType. The actual thing parsed is returned
809/// in ExprType.
810///
811/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000812/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000813/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
814/// postfix-expression: [C99 6.5.2]
815/// '(' type-name ')' '{' initializer-list '}'
816/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000817/// cast-expression: [C99 6.5.4]
818/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000819///
Chris Lattner89c50c62006-08-11 06:41:18 +0000820Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000821 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
822 SourceLocation OpenLoc = Tok.getLocation();
823 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000824 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000825
Chris Lattner4add4e62006-08-11 01:33:00 +0000826 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000827 !getLang().NoExtensions) {
828 Diag(Tok, diag::ext_gnu_statement_expr);
829 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000830 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000831 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000832 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000833 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000834 ParseTypeName();
835
836 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000837 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000838
Chris Lattner4add4e62006-08-11 01:33:00 +0000839 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000840 if (!getLang().C99) // Compound literals don't exist in C90.
841 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000842 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000843 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000844 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000845 } else if (ExprType == CastExpr) {
846 // Note that this doesn't parse the subsequence cast-expression.
847 ExprType = CastExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000848 // TODO: Build AST for cast in caller.
Chris Lattner4add4e62006-08-11 01:33:00 +0000849 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000850 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000851 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000852 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000853 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000854 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000855 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000856 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000857 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
858 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000859 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000860
Chris Lattner4564bc12006-08-10 23:14:52 +0000861 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000862 if (Result.isInvalid)
863 SkipUntil(tok::r_paren);
864 else
Chris Lattner04f80192006-08-15 04:55:54 +0000865 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattner1b926492006-08-23 06:42:10 +0000866
Chris Lattner89c50c62006-08-11 06:41:18 +0000867 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000868}