blob: 902a570895059d156e076dda394665fc3d79924f [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 Lattner6d28d9b2006-08-24 03:51:22 +0000480 // TODO: Build AST.
Chris Lattner20c6a452006-08-12 17:40:43 +0000481 Res = ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000482 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000483 // These can be followed by postfix-expr pieces.
484 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000485 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000486 Res = ParseStringLiteralExpression();
487 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000488 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
489 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000490 case tok::kw___builtin_va_arg:
491 case tok::kw___builtin_offsetof:
492 case tok::kw___builtin_choose_expr:
493 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000494 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000495 case tok::plusplus: // unary-expression: '++' unary-expression
496 case tok::minusminus: // unary-expression: '--' unary-expression
Chris Lattner1b926492006-08-23 06:42:10 +0000497 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000498 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000499 Res = ParseCastExpression(true);
500 if (!Res.isInvalid)
501 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
502 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000503 case tok::amp: // unary-expression: '&' cast-expression
504 case tok::star: // unary-expression: '*' cast-expression
505 case tok::plus: // unary-expression: '+' cast-expression
506 case tok::minus: // unary-expression: '-' cast-expression
507 case tok::tilde: // unary-expression: '~' cast-expression
508 case tok::exclaim: // unary-expression: '!' cast-expression
509 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000510 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner81b576e2006-08-11 02:13:20 +0000511 //case tok::kw__extension__: [TODO]
Chris Lattner1b926492006-08-23 06:42:10 +0000512 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000513 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000514 Res = ParseCastExpression(false);
515 if (!Res.isInvalid)
516 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
517 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000518
519 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
520 // unary-expression: 'sizeof' '(' type-name ')'
521 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
522 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000523 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000524 case tok::ampamp: // unary-expression: '&&' identifier
525 Diag(Tok, diag::ext_gnu_address_of_label);
526 ConsumeToken();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000527 // TODO: Build AST.
Chris Lattner81b576e2006-08-11 02:13:20 +0000528 if (Tok.getKind() == tok::identifier) {
529 ConsumeToken();
530 } else {
531 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000532 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000533 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000534 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000535 default:
536 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000537 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000538 }
539
Chris Lattner20c6a452006-08-12 17:40:43 +0000540 // unreachable.
541 abort();
542}
543
544/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
545/// is parsed, this method parses any suffixes that apply.
546///
547/// postfix-expression: [C99 6.5.2]
548/// primary-expression
549/// postfix-expression '[' expression ']'
550/// postfix-expression '(' argument-expression-list[opt] ')'
551/// postfix-expression '.' identifier
552/// postfix-expression '->' identifier
553/// postfix-expression '++'
554/// postfix-expression '--'
555/// '(' type-name ')' '{' initializer-list '}'
556/// '(' type-name ')' '{' initializer-list ',' '}'
557///
558/// argument-expression-list: [C99 6.5.2]
559/// argument-expression
560/// argument-expression-list ',' assignment-expression
561///
562Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
563 assert(!LHS.isInvalid && "LHS is invalid already!");
564
Chris Lattnerf8339772006-08-10 22:01:51 +0000565 // Now that the primary-expression piece of the postfix-expression has been
566 // parsed, see if there are any postfix-expression pieces here.
567 SourceLocation Loc;
568 while (1) {
569 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000570 default: // Not a postfix-expression suffix.
571 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000572 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner89c50c62006-08-11 06:41:18 +0000573 Loc = Tok.getLocation();
574 ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000575 ExprResult Idx = ParseExpression();
576
577 SourceLocation RLoc = Tok.getLocation();
578
579 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
580 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
581 else
582 LHS = ExprResult(true);
583
Chris Lattner89c50c62006-08-11 06:41:18 +0000584 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000585 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000586 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000587 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000588
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000589 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
590 SmallVector<ExprTy*, 8> ArgExprs;
591 SmallVector<SourceLocation, 8> CommaLocs;
592 bool ArgExprsOk = true;
593
Chris Lattner89c50c62006-08-11 06:41:18 +0000594 Loc = Tok.getLocation();
595 ConsumeParen();
596
Chris Lattner0c6c0342006-08-12 18:12:45 +0000597 if (Tok.getKind() != tok::r_paren) {
598 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000599 ExprResult ArgExpr = ParseAssignmentExpression();
600 if (ArgExpr.isInvalid)
601 ArgExprsOk = false;
602 else
603 ArgExprs.push_back(ArgExpr.Val);
604
Chris Lattner0c6c0342006-08-12 18:12:45 +0000605 if (Tok.getKind() != tok::comma)
606 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000607 CommaLocs.push_back(Tok.getLocation());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000608 ConsumeToken(); // Next argument.
609 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000610 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000611
Chris Lattner89c50c62006-08-11 06:41:18 +0000612 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000613 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
614 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
615 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000616 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000617 &CommaLocs[0], Tok.getLocation());
618 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000619
Chris Lattner04f80192006-08-15 04:55:54 +0000620 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000621 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000622 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000623 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000624 case tok::period: { // postfix-expression: p-e '.' identifier
625 SourceLocation OpLoc = Tok.getLocation();
626 tok::TokenKind OpKind = Tok.getKind();
627 ConsumeToken(); // Eat the "." or "->" token.
628
Chris Lattner89c50c62006-08-11 06:41:18 +0000629 if (Tok.getKind() != tok::identifier) {
630 Diag(Tok, diag::err_expected_ident);
631 return ExprResult(true);
632 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000633
634 if (!LHS.isInvalid)
635 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
636 Tok.getLocation(),
637 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000638 ConsumeToken();
639 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000640 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000641 case tok::plusplus: // postfix-expression: postfix-expression '++'
642 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000643 if (!LHS.isInvalid)
644 LHS = Actions.ParsePostfixUnaryOp(Tok, LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000645 ConsumeToken();
646 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000647 }
648 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000649}
650
Chris Lattner20c6a452006-08-12 17:40:43 +0000651
Chris Lattner81b576e2006-08-11 02:13:20 +0000652/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
653/// unary-expression: [C99 6.5.3]
654/// 'sizeof' unary-expression
655/// 'sizeof' '(' type-name ')'
656/// [GNU] '__alignof' unary-expression
657/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000658Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000659 assert((Tok.getKind() == tok::kw_sizeof ||
660 Tok.getKind() == tok::kw___alignof) &&
661 "Not a sizeof/alignof expression!");
662 ConsumeToken();
663
664 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000665 // TODO: Build AST.
Chris Lattner0be454e2006-08-12 19:30:51 +0000666 if (Tok.getKind() != tok::l_paren)
Chris Lattner89c50c62006-08-11 06:41:18 +0000667 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000668
669 // If it starts with a '(', we know that it is either a parenthesized
670 // type-name, or it is a unary-expression that starts with a compound literal,
671 // or starts with a primary-expression that is a parenthesized expression.
672 ParenParseOption ExprType = CastExpr;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000673 // TODO: Build AST.
Chris Lattner89c50c62006-08-11 06:41:18 +0000674 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000675}
676
Chris Lattner11124352006-08-12 19:16:08 +0000677/// ParseBuiltinPrimaryExpression
678///
679/// primary-expression: [C99 6.5.1]
680/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
681/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
682/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
683/// assign-expr ')'
684/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
685///
686/// [GNU] offsetof-member-designator:
687/// [GNU] identifier
688/// [GNU] offsetof-member-designator '.' identifier
689/// [GNU] offsetof-member-designator '[' expression ']'
690///
691Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
692 ExprResult Res(false);
693 SourceLocation StartLoc = Tok.getLocation();
694 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
695
696 tok::TokenKind T = Tok.getKind();
697 ConsumeToken(); // Eat the builtin identifier.
698
699 // All of these start with an open paren.
700 if (Tok.getKind() != tok::l_paren) {
701 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
702 return ExprResult(true);
703 }
704
705 SourceLocation LParenLoc = Tok.getLocation();
706 ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000707 // TODO: Build AST.
708
Chris Lattner11124352006-08-12 19:16:08 +0000709 switch (T) {
710 default: assert(0 && "Not a builtin primary expression!");
711 case tok::kw___builtin_va_arg:
712 Res = ParseAssignmentExpression();
713 if (Res.isInvalid) {
714 SkipUntil(tok::r_paren);
715 return Res;
716 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000717
Chris Lattner6d7e6342006-08-15 03:41:14 +0000718 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000719 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000720
Chris Lattner11124352006-08-12 19:16:08 +0000721 ParseTypeName();
722 break;
723
724 case tok::kw___builtin_offsetof:
725 ParseTypeName();
726
Chris Lattner6d7e6342006-08-15 03:41:14 +0000727 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000728 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000729
730 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000731 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000732 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000733 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000734
Chris Lattner11124352006-08-12 19:16:08 +0000735 while (1) {
736 if (Tok.getKind() == tok::period) {
737 // offsetof-member-designator: offsetof-member-designator '.' identifier
738 ConsumeToken();
739
Chris Lattner6d7e6342006-08-15 03:41:14 +0000740 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000741 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000742 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000743 } else if (Tok.getKind() == tok::l_square) {
744 // offsetof-member-designator: offsetof-member-design '[' expression ']'
745 SourceLocation LSquareLoc = Tok.getLocation();
746 ConsumeBracket();
747 Res = ParseExpression();
748 if (Res.isInvalid) {
749 SkipUntil(tok::r_paren);
750 return Res;
751 }
752
Chris Lattner04f80192006-08-15 04:55:54 +0000753 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000754 } else {
755 break;
756 }
757 }
758 break;
759 case tok::kw___builtin_choose_expr:
760 Res = ParseAssignmentExpression();
761
Chris Lattner6d7e6342006-08-15 03:41:14 +0000762 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000763 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000764
765 Res = ParseAssignmentExpression();
766
Chris Lattner6d7e6342006-08-15 03:41:14 +0000767 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000768 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000769
770 Res = ParseAssignmentExpression();
771 break;
772 case tok::kw___builtin_types_compatible_p:
773 ParseTypeName();
774
Chris Lattner6d7e6342006-08-15 03:41:14 +0000775 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000776 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000777
778 ParseTypeName();
779 break;
780 }
781
Chris Lattner04f80192006-08-15 04:55:54 +0000782 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000783
784 // These can be followed by postfix-expr pieces because they are
785 // primary-expressions.
786 return ParsePostfixExpressionSuffix(Res);
787}
788
Chris Lattner52a99e52006-08-10 20:56:00 +0000789/// ParseStringLiteralExpression - This handles the various token types that
790/// form string literals, and also handles string concatenation [C99 5.1.1.2,
791/// translation phase #6].
792///
793/// primary-expression: [C99 6.5.1]
794/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000795Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000796 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000797 ConsumeStringToken();
798
799 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
800 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000801 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000802 ConsumeStringToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000803 // TODO: Build AST for string literals.
Chris Lattner89c50c62006-08-11 06:41:18 +0000804 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000805}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000806
Chris Lattnerc951dae2006-08-10 04:23:57 +0000807
Chris Lattner4add4e62006-08-11 01:33:00 +0000808/// ParseParenExpression - This parses the unit that starts with a '(' token,
809/// based on what is allowed by ExprType. The actual thing parsed is returned
810/// in ExprType.
811///
812/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000813/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000814/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
815/// postfix-expression: [C99 6.5.2]
816/// '(' type-name ')' '{' initializer-list '}'
817/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000818/// cast-expression: [C99 6.5.4]
819/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000820///
Chris Lattner89c50c62006-08-11 06:41:18 +0000821Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000822 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
823 SourceLocation OpenLoc = Tok.getLocation();
824 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000825 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000826
Chris Lattner4add4e62006-08-11 01:33:00 +0000827 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000828 !getLang().NoExtensions) {
829 Diag(Tok, diag::ext_gnu_statement_expr);
830 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000831 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000832 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000833 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000834 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000835 ParseTypeName();
836
837 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +0000838 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000839
Chris Lattner4add4e62006-08-11 01:33:00 +0000840 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000841 if (!getLang().C99) // Compound literals don't exist in C90.
842 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000843 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000844 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000845 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000846 } else if (ExprType == CastExpr) {
847 // Note that this doesn't parse the subsequence cast-expression.
848 ExprType = CastExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000849 // TODO: Build AST for cast in caller.
Chris Lattner4add4e62006-08-11 01:33:00 +0000850 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000851 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000852 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000853 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000854 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000855 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000856 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000857 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000858 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
859 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000860 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000861
Chris Lattner4564bc12006-08-10 23:14:52 +0000862 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000863 if (Result.isInvalid)
864 SkipUntil(tok::r_paren);
865 else
Chris Lattner04f80192006-08-15 04:55:54 +0000866 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattner1b926492006-08-23 06:42:10 +0000867
Chris Lattner89c50c62006-08-11 06:41:18 +0000868 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000869}