blob: b38397533a9a2f8811aab0fce0e713abe7db7426 [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"
24using namespace llvm;
25using namespace clang;
26
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000027// C99 6.7.8
Chris Lattner89c50c62006-08-11 06:41:18 +000028Parser::ExprResult Parser::ParseInitializer() {
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000029 // FIXME: STUB.
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030 if (Tok.getKind() == tok::l_brace) {
31 ConsumeBrace();
Chris Lattnera092cd1f2006-08-11 01:38:28 +000032
33 if (Tok.getKind() == tok::numeric_constant)
34 ConsumeToken();
35
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036 // FIXME: initializer-list
37 // Match the '}'.
38 MatchRHSPunctuation(tok::r_brace, Tok.getLocation(), "{",
39 diag::err_expected_rbrace);
Chris Lattner89c50c62006-08-11 06:41:18 +000040 return ExprResult(false);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000041 }
42
Chris Lattner89c50c62006-08-11 06:41:18 +000043 return ParseAssignmentExpression();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000044}
45
46
47
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000048/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000049/// the C99 grammar. These have been named to relate with the C99 grammar
50/// productions. Low precedences numbers bind more weakly than high numbers.
51namespace prec {
52 enum Level {
53 Unknown = 0, // Not binary operator.
54 Comma = 1, // ,
55 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
56 Conditional = 3, // ?
57 LogicalOr = 4, // ||
58 LogicalAnd = 5, // &&
59 InclusiveOr = 6, // |
60 ExclusiveOr = 7, // ^
61 And = 8, // &
62 MinMax = 9, // <?, >? min, max (GCC extensions)
63 Equality = 10, // ==, !=
64 Relational = 11, // >=, <=, >, <
65 Shift = 12, // <<, >>
66 Additive = 13, // -, +
67 Multiplicative = 14 // *, /, %
68 };
69}
70
71
72/// getBinOpPrecedence - Return the precedence of the specified binary operator
73/// token. This returns:
74///
75static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
76 switch (Kind) {
77 default: return prec::Unknown;
78 case tok::comma: return prec::Comma;
79 case tok::equal:
80 case tok::starequal:
81 case tok::slashequal:
82 case tok::percentequal:
83 case tok::plusequal:
84 case tok::minusequal:
85 case tok::lesslessequal:
86 case tok::greatergreaterequal:
87 case tok::ampequal:
88 case tok::caretequal:
89 case tok::pipeequal: return prec::Assignment;
90 case tok::question: return prec::Conditional;
91 case tok::pipepipe: return prec::LogicalOr;
92 case tok::ampamp: return prec::LogicalAnd;
93 case tok::pipe: return prec::InclusiveOr;
94 case tok::caret: return prec::ExclusiveOr;
95 case tok::amp: return prec::And;
96 case tok::lessquestion:
97 case tok::greaterquestion: return prec::MinMax;
98 case tok::exclaimequal:
99 case tok::equalequal: return prec::Equality;
100 case tok::lessequal:
101 case tok::less:
102 case tok::greaterequal:
103 case tok::greater: return prec::Relational;
104 case tok::lessless:
105 case tok::greatergreater: return prec::Shift;
106 case tok::plus:
107 case tok::minus: return prec::Additive;
108 case tok::percent:
109 case tok::slash:
110 case tok::star: return prec::Multiplicative;
111 }
112}
113
114
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000115/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +0000116/// operators.
117///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000118/// Note: we diverge from the C99 grammar when parsing the assignment-expression
119/// production. C99 specifies that the LHS of an assignment operator should be
120/// parsed as a unary-expression, but consistency dictates that it be a
121/// conditional-expession. In practice, the important thing here is that the
122/// LHS of an assignment has to be an l-value, which productions between
123/// unary-expression and conditional-expression don't produce. Because we want
124/// consistency, we parse the LHS as a conditional-expression, then check for
125/// l-value-ness in semantic analysis stages.
126///
Chris Lattnercde626a2006-08-12 08:13:25 +0000127/// multiplicative-expression: [C99 6.5.5]
128/// cast-expression
129/// multiplicative-expression '*' cast-expression
130/// multiplicative-expression '/' cast-expression
131/// multiplicative-expression '%' cast-expression
132///
133/// additive-expression: [C99 6.5.6]
134/// multiplicative-expression
135/// additive-expression '+' multiplicative-expression
136/// additive-expression '-' multiplicative-expression
137///
138/// shift-expression: [C99 6.5.7]
139/// additive-expression
140/// shift-expression '<<' additive-expression
141/// shift-expression '>>' additive-expression
142///
143/// relational-expression: [C99 6.5.8]
144/// shift-expression
145/// relational-expression '<' shift-expression
146/// relational-expression '>' shift-expression
147/// relational-expression '<=' shift-expression
148/// relational-expression '>=' shift-expression
149///
150/// equality-expression: [C99 6.5.9]
151/// relational-expression
152/// equality-expression '==' relational-expression
153/// equality-expression '!=' relational-expression
154///
155/// AND-expression: [C99 6.5.10]
156/// equality-expression
157/// AND-expression '&' equality-expression
158///
159/// exclusive-OR-expression: [C99 6.5.11]
160/// AND-expression
161/// exclusive-OR-expression '^' AND-expression
162///
163/// inclusive-OR-expression: [C99 6.5.12]
164/// exclusive-OR-expression
165/// inclusive-OR-expression '|' exclusive-OR-expression
166///
167/// logical-AND-expression: [C99 6.5.13]
168/// inclusive-OR-expression
169/// logical-AND-expression '&&' inclusive-OR-expression
170///
171/// logical-OR-expression: [C99 6.5.14]
172/// logical-AND-expression
173/// logical-OR-expression '||' logical-AND-expression
174///
175/// conditional-expression: [C99 6.5.15]
176/// logical-OR-expression
177/// logical-OR-expression '?' expression ':' conditional-expression
178/// [GNU] logical-OR-expression '?' ':' conditional-expression
179///
180/// assignment-expression: [C99 6.5.16]
181/// conditional-expression
182/// unary-expression assignment-operator assignment-expression
183///
184/// assignment-operator: one of
185/// = *= /= %= += -= <<= >>= &= ^= |=
186///
187/// expression: [C99 6.5.17]
188/// assignment-expression
189/// expression ',' assignment-expression
190///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000191Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000192 ExprResult LHS = ParseCastExpression(false);
193 if (LHS.isInvalid) return LHS;
194
195 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
196}
197
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000198// Expr that doesn't include commas.
199Parser::ExprResult Parser::ParseAssignmentExpression() {
200 ExprResult LHS = ParseCastExpression(false);
201 if (LHS.isInvalid) return LHS;
202
203 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
204}
205
Chris Lattnercde626a2006-08-12 08:13:25 +0000206/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
207/// LHS and has a precedence of at least MinPrec.
208Parser::ExprResult
209Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
210 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
211
212 while (1) {
213 // If this token has a lower precedence than we are allowed to parse (e.g.
214 // because we are called recursively, or because the token is not a binop),
215 // then we are done!
216 if (NextTokPrec < MinPrec)
217 return LHS;
218
219 // Consume the operator, saving the operator token for error reporting.
220 LexerToken OpToken = Tok;
221 ConsumeToken();
222
Chris Lattner96c3deb2006-08-12 17:13:08 +0000223 // Special case handling for the ternary operator.
224 ExprResult TernaryMiddle;
225 if (NextTokPrec == prec::Conditional) {
226 if (Tok.getKind() != tok::colon) {
227 // Handle this production specially:
228 // logical-OR-expression '?' expression ':' conditional-expression
229 // In particular, the RHS of the '?' is 'expression', not
230 // 'logical-OR-expression' as we might expect.
231 TernaryMiddle = ParseExpression();
232 if (TernaryMiddle.isInvalid) return TernaryMiddle;
233 } else {
234 // Special case handling of "X ? Y : Z" where Y is empty:
235 // logical-OR-expression '?' ':' conditional-expression [GNU]
236 TernaryMiddle = ExprResult(false);
237 Diag(Tok, diag::ext_gnu_conditional_expr);
238 }
239
240 if (Tok.getKind() != tok::colon) {
241 Diag(Tok, diag::err_expected_colon);
242 Diag(OpToken, diag::err_matching, "?");
243 return ExprResult(true);
244 }
245
246 // Eat the colon.
247 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000248 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000249
250 // Parse another leaf here for the RHS of the operator.
251 ExprResult RHS = ParseCastExpression(false);
252 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000253
254 // Remember the precedence of this operator and get the precedence of the
255 // operator immediately to the right of the RHS.
256 unsigned ThisPrec = NextTokPrec;
257 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000258
259 // Assignment and conditional expressions are right-associative.
260 bool isRightAssoc = NextTokPrec == prec::Conditional ||
261 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000262
263 // Get the precedence of the operator to the right of the RHS. If it binds
264 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000265 if (ThisPrec < NextTokPrec ||
266 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000267 // If this is left-associative, only parse things on the RHS that bind
268 // more tightly than the current operator. If it is left-associative, it
269 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
270 // A=(B=(C=D)), where each paren is a level of recursion here.
271 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000272 if (RHS.isInvalid) return RHS;
273
274 NextTokPrec = getBinOpPrecedence(Tok.getKind());
275 }
276 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
277
Chris Lattner96c3deb2006-08-12 17:13:08 +0000278 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000279 }
280}
281
282
Chris Lattnereaf06592006-08-11 02:02:23 +0000283/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
284/// true, parse a unary-expression.
285///
Chris Lattner4564bc12006-08-10 23:14:52 +0000286/// cast-expression: [C99 6.5.4]
287/// unary-expression
288/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000289///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000290/// unary-expression: [C99 6.5.3]
291/// postfix-expression
292/// '++' unary-expression
293/// '--' unary-expression
294/// unary-operator cast-expression
295/// 'sizeof' unary-expression
296/// 'sizeof' '(' type-name ')'
297/// [GNU] '__alignof' unary-expression
298/// [GNU] '__alignof' '(' type-name ')'
299/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000300///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000301/// unary-operator: one of
302/// '&' '*' '+' '-' '~' '!'
303/// [GNU] '__extension__' '__real' '__imag'
304///
Chris Lattner52a99e52006-08-10 20:56:00 +0000305/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000306/// identifier
307/// constant
308/// string-literal
309/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000310/// '__func__' [C99 6.4.2.2]
311/// [GNU] '__FUNCTION__'
312/// [GNU] '__PRETTY_FUNCTION__'
313/// [GNU] '(' compound-statement ')'
314/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
315/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
316/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
317/// assign-expr ')'
318/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
319/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
320/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
321/// [OBC] '@protocol' '(' identifier ')' [TODO]
322/// [OBC] '@encode' '(' type-name ')' [TODO]
323/// [OBC] objc-string-literal [TODO]
324///
325/// constant: [C99 6.4.4]
326/// integer-constant
327/// floating-constant
328/// enumeration-constant -> identifier
329/// character-constant
330///
331/// [GNU] offsetof-member-designator:
332/// [GNU] identifier
333/// [GNU] offsetof-member-designator '.' identifier
334/// [GNU] offsetof-member-designator '[' expression ']'
335///
Chris Lattner89c50c62006-08-11 06:41:18 +0000336Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
337 ExprResult Res;
338
Chris Lattner81b576e2006-08-11 02:13:20 +0000339 // This handles all of cast-expression, unary-expression, postfix-expression,
340 // and primary-expression. We handle them together like this for efficiency
341 // and to simplify handling of an expression starting with a '(' token: which
342 // may be one of a parenthesized expression, cast-expression, compound literal
343 // expression, or statement expression.
344 //
345 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000346 // call ParsePostfixExpressionSuffix to handle the postfix expression
347 // suffixes. Cases that cannot be followed by postfix exprs should
348 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000349 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000350 case tok::l_paren:
351 // If this expression is limited to being a unary-expression, the parent can
352 // not start a cast expression.
353 ParenParseOption ParenExprType =
354 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000355 Res = ParseParenExpression(ParenExprType);
356 if (Res.isInvalid) return Res;
357
Chris Lattner81b576e2006-08-11 02:13:20 +0000358 switch (ParenExprType) {
359 case SimpleExpr: break; // Nothing else to do.
360 case CompoundStmt: break; // Nothing else to do.
361 case CompoundLiteral:
362 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
363 // postfix-expression exist, parse them now.
364 break;
365 case CastExpr:
366 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
367 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000368 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000369 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000370
371 // These can be followed by postfix-expr pieces.
372 return ParsePostfixExpressionSuffix(Res);
Chris Lattner89c50c62006-08-11 06:41:18 +0000373
Chris Lattner52a99e52006-08-10 20:56:00 +0000374 // primary-expression
375 case tok::identifier: // primary-expression: identifier
376 // constant: enumeration-constant
377 case tok::numeric_constant: // constant: integer-constant
378 // constant: floating-constant
379 case tok::char_constant: // constant: character-constant
380 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
381 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
382 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner20c6a452006-08-12 17:40:43 +0000383 Res = ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000384 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000385 // These can be followed by postfix-expr pieces.
386 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000387 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000388 Res = ParseStringLiteralExpression();
389 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000390 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
391 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000392 case tok::kw___builtin_va_arg:
393 case tok::kw___builtin_offsetof:
394 case tok::kw___builtin_choose_expr:
395 case tok::kw___builtin_types_compatible_p:
396 assert(0 && "FIXME: UNIMP!");
Chris Lattner20c6a452006-08-12 17:40:43 +0000397 // This can be followed by postfix-expr pieces.
398 return ParsePostfixExpressionSuffix(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000399 case tok::plusplus: // unary-expression: '++' unary-expression
400 case tok::minusminus: // unary-expression: '--' unary-expression
401 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000402 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000403 case tok::amp: // unary-expression: '&' cast-expression
404 case tok::star: // unary-expression: '*' cast-expression
405 case tok::plus: // unary-expression: '+' cast-expression
406 case tok::minus: // unary-expression: '-' cast-expression
407 case tok::tilde: // unary-expression: '~' cast-expression
408 case tok::exclaim: // unary-expression: '!' cast-expression
409 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
410 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
411 //case tok::kw__extension__: [TODO]
412 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000413 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000414
415 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
416 // unary-expression: 'sizeof' '(' type-name ')'
417 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
418 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000419 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000420 case tok::ampamp: // unary-expression: '&&' identifier
421 Diag(Tok, diag::ext_gnu_address_of_label);
422 ConsumeToken();
423 if (Tok.getKind() == tok::identifier) {
424 ConsumeToken();
425 } else {
426 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000427 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000428 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000429 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000430 default:
431 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000432 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000433 }
434
Chris Lattner20c6a452006-08-12 17:40:43 +0000435 // unreachable.
436 abort();
437}
438
439/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
440/// is parsed, this method parses any suffixes that apply.
441///
442/// postfix-expression: [C99 6.5.2]
443/// primary-expression
444/// postfix-expression '[' expression ']'
445/// postfix-expression '(' argument-expression-list[opt] ')'
446/// postfix-expression '.' identifier
447/// postfix-expression '->' identifier
448/// postfix-expression '++'
449/// postfix-expression '--'
450/// '(' type-name ')' '{' initializer-list '}'
451/// '(' type-name ')' '{' initializer-list ',' '}'
452///
453/// argument-expression-list: [C99 6.5.2]
454/// argument-expression
455/// argument-expression-list ',' assignment-expression
456///
457Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
458 assert(!LHS.isInvalid && "LHS is invalid already!");
459
Chris Lattnerf8339772006-08-10 22:01:51 +0000460 // Now that the primary-expression piece of the postfix-expression has been
461 // parsed, see if there are any postfix-expression pieces here.
462 SourceLocation Loc;
463 while (1) {
464 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000465 default: // Not a postfix-expression suffix.
466 return LHS;
Chris Lattner89c50c62006-08-11 06:41:18 +0000467 case tok::l_square: // postfix-expression: p-e '[' expression ']'
468 Loc = Tok.getLocation();
469 ConsumeBracket();
470 ParseExpression();
471 // Match the ']'.
472 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
473 break;
474
475 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
476 Loc = Tok.getLocation();
477 ConsumeParen();
478
479 while (1) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000480 ParseAssignmentExpression();
Chris Lattner89c50c62006-08-11 06:41:18 +0000481 if (Tok.getKind() != tok::comma)
482 break;
483 ConsumeToken(); // Next argument.
484 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000485
Chris Lattner89c50c62006-08-11 06:41:18 +0000486 // Match the ')'.
487 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
488 break;
489
490 case tok::arrow: // postfix-expression: p-e '->' identifier
491 case tok::period: // postfix-expression: p-e '.' identifier
492 ConsumeToken();
493 if (Tok.getKind() != tok::identifier) {
494 Diag(Tok, diag::err_expected_ident);
495 return ExprResult(true);
496 }
497 ConsumeToken();
498 break;
499
500 case tok::plusplus: // postfix-expression: postfix-expression '++'
501 case tok::minusminus: // postfix-expression: postfix-expression '--'
502 ConsumeToken();
503 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000504 }
505 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000506}
507
Chris Lattner20c6a452006-08-12 17:40:43 +0000508
Chris Lattner81b576e2006-08-11 02:13:20 +0000509/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
510/// unary-expression: [C99 6.5.3]
511/// 'sizeof' unary-expression
512/// 'sizeof' '(' type-name ')'
513/// [GNU] '__alignof' unary-expression
514/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000515Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000516 assert((Tok.getKind() == tok::kw_sizeof ||
517 Tok.getKind() == tok::kw___alignof) &&
518 "Not a sizeof/alignof expression!");
519 ConsumeToken();
520
521 // If the operand doesn't start with an '(', it must be an expression.
522 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000523 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000524 }
525
526 // If it starts with a '(', we know that it is either a parenthesized
527 // type-name, or it is a unary-expression that starts with a compound literal,
528 // or starts with a primary-expression that is a parenthesized expression.
529 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000530 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000531}
532
Chris Lattner52a99e52006-08-10 20:56:00 +0000533/// ParseStringLiteralExpression - This handles the various token types that
534/// form string literals, and also handles string concatenation [C99 5.1.1.2,
535/// translation phase #6].
536///
537/// primary-expression: [C99 6.5.1]
538/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000539Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000540 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000541 ConsumeStringToken();
542
543 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
544 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000545 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000546 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000547 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000548}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000549
Chris Lattnerc951dae2006-08-10 04:23:57 +0000550
Chris Lattner4add4e62006-08-11 01:33:00 +0000551/// ParseParenExpression - This parses the unit that starts with a '(' token,
552/// based on what is allowed by ExprType. The actual thing parsed is returned
553/// in ExprType.
554///
555/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000556/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000557/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
558/// postfix-expression: [C99 6.5.2]
559/// '(' type-name ')' '{' initializer-list '}'
560/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000561/// cast-expression: [C99 6.5.4]
562/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000563///
Chris Lattner89c50c62006-08-11 06:41:18 +0000564Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000565 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
566 SourceLocation OpenLoc = Tok.getLocation();
567 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000568 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000569
Chris Lattner4add4e62006-08-11 01:33:00 +0000570 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000571 !getLang().NoExtensions) {
572 Diag(Tok, diag::ext_gnu_statement_expr);
573 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000574 ExprType = CompoundStmt;
575 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000576 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000577 ParseTypeName();
578
579 // Match the ')'.
580 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
581
Chris Lattner4add4e62006-08-11 01:33:00 +0000582 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000583 if (!getLang().C99) // Compound literals don't exist in C90.
584 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000585 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000586 ExprType = CompoundLiteral;
587 } else if (ExprType == CastExpr) {
588 // Note that this doesn't parse the subsequence cast-expression.
589 ExprType = CastExpr;
590 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000591 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000592 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000593 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000594 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000595 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000596 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000597 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000598 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000599
Chris Lattner4564bc12006-08-10 23:14:52 +0000600 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000601 if (Result.isInvalid)
602 SkipUntil(tok::r_paren);
603 else
604 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
605 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000606}