blob: 6702d8d29c08664282f4fad7252e5b74efdd2acd [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 Lattnerc5e0d4a2006-08-10 19:06:03 +000048// Expr that doesn't include commas.
Chris Lattner89c50c62006-08-11 06:41:18 +000049Parser::ExprResult Parser::ParseAssignmentExpression() {
50 return ParseExpression();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000051}
52
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000053/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000054/// the C99 grammar. These have been named to relate with the C99 grammar
55/// productions. Low precedences numbers bind more weakly than high numbers.
56namespace prec {
57 enum Level {
58 Unknown = 0, // Not binary operator.
59 Comma = 1, // ,
60 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
61 Conditional = 3, // ?
62 LogicalOr = 4, // ||
63 LogicalAnd = 5, // &&
64 InclusiveOr = 6, // |
65 ExclusiveOr = 7, // ^
66 And = 8, // &
67 MinMax = 9, // <?, >? min, max (GCC extensions)
68 Equality = 10, // ==, !=
69 Relational = 11, // >=, <=, >, <
70 Shift = 12, // <<, >>
71 Additive = 13, // -, +
72 Multiplicative = 14 // *, /, %
73 };
74}
75
76
77/// getBinOpPrecedence - Return the precedence of the specified binary operator
78/// token. This returns:
79///
80static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
81 switch (Kind) {
82 default: return prec::Unknown;
83 case tok::comma: return prec::Comma;
84 case tok::equal:
85 case tok::starequal:
86 case tok::slashequal:
87 case tok::percentequal:
88 case tok::plusequal:
89 case tok::minusequal:
90 case tok::lesslessequal:
91 case tok::greatergreaterequal:
92 case tok::ampequal:
93 case tok::caretequal:
94 case tok::pipeequal: return prec::Assignment;
95 case tok::question: return prec::Conditional;
96 case tok::pipepipe: return prec::LogicalOr;
97 case tok::ampamp: return prec::LogicalAnd;
98 case tok::pipe: return prec::InclusiveOr;
99 case tok::caret: return prec::ExclusiveOr;
100 case tok::amp: return prec::And;
101 case tok::lessquestion:
102 case tok::greaterquestion: return prec::MinMax;
103 case tok::exclaimequal:
104 case tok::equalequal: return prec::Equality;
105 case tok::lessequal:
106 case tok::less:
107 case tok::greaterequal:
108 case tok::greater: return prec::Relational;
109 case tok::lessless:
110 case tok::greatergreater: return prec::Shift;
111 case tok::plus:
112 case tok::minus: return prec::Additive;
113 case tok::percent:
114 case tok::slash:
115 case tok::star: return prec::Multiplicative;
116 }
117}
118
119
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000120/// ParseBinaryExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +0000121/// operators.
122///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000123/// Note: we diverge from the C99 grammar when parsing the assignment-expression
124/// production. C99 specifies that the LHS of an assignment operator should be
125/// parsed as a unary-expression, but consistency dictates that it be a
126/// conditional-expession. In practice, the important thing here is that the
127/// LHS of an assignment has to be an l-value, which productions between
128/// unary-expression and conditional-expression don't produce. Because we want
129/// consistency, we parse the LHS as a conditional-expression, then check for
130/// l-value-ness in semantic analysis stages.
131///
Chris Lattnercde626a2006-08-12 08:13:25 +0000132/// multiplicative-expression: [C99 6.5.5]
133/// cast-expression
134/// multiplicative-expression '*' cast-expression
135/// multiplicative-expression '/' cast-expression
136/// multiplicative-expression '%' cast-expression
137///
138/// additive-expression: [C99 6.5.6]
139/// multiplicative-expression
140/// additive-expression '+' multiplicative-expression
141/// additive-expression '-' multiplicative-expression
142///
143/// shift-expression: [C99 6.5.7]
144/// additive-expression
145/// shift-expression '<<' additive-expression
146/// shift-expression '>>' additive-expression
147///
148/// relational-expression: [C99 6.5.8]
149/// shift-expression
150/// relational-expression '<' shift-expression
151/// relational-expression '>' shift-expression
152/// relational-expression '<=' shift-expression
153/// relational-expression '>=' shift-expression
154///
155/// equality-expression: [C99 6.5.9]
156/// relational-expression
157/// equality-expression '==' relational-expression
158/// equality-expression '!=' relational-expression
159///
160/// AND-expression: [C99 6.5.10]
161/// equality-expression
162/// AND-expression '&' equality-expression
163///
164/// exclusive-OR-expression: [C99 6.5.11]
165/// AND-expression
166/// exclusive-OR-expression '^' AND-expression
167///
168/// inclusive-OR-expression: [C99 6.5.12]
169/// exclusive-OR-expression
170/// inclusive-OR-expression '|' exclusive-OR-expression
171///
172/// logical-AND-expression: [C99 6.5.13]
173/// inclusive-OR-expression
174/// logical-AND-expression '&&' inclusive-OR-expression
175///
176/// logical-OR-expression: [C99 6.5.14]
177/// logical-AND-expression
178/// logical-OR-expression '||' logical-AND-expression
179///
180/// conditional-expression: [C99 6.5.15]
181/// logical-OR-expression
182/// logical-OR-expression '?' expression ':' conditional-expression
183/// [GNU] logical-OR-expression '?' ':' conditional-expression
184///
185/// assignment-expression: [C99 6.5.16]
186/// conditional-expression
187/// unary-expression assignment-operator assignment-expression
188///
189/// assignment-operator: one of
190/// = *= /= %= += -= <<= >>= &= ^= |=
191///
192/// expression: [C99 6.5.17]
193/// assignment-expression
194/// expression ',' assignment-expression
195///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000196Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000197 ExprResult LHS = ParseCastExpression(false);
198 if (LHS.isInvalid) return LHS;
199
200 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
201}
202
203/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
204/// LHS and has a precedence of at least MinPrec.
205Parser::ExprResult
206Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
207 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
208
209 while (1) {
210 // If this token has a lower precedence than we are allowed to parse (e.g.
211 // because we are called recursively, or because the token is not a binop),
212 // then we are done!
213 if (NextTokPrec < MinPrec)
214 return LHS;
215
216 // Consume the operator, saving the operator token for error reporting.
217 LexerToken OpToken = Tok;
218 ConsumeToken();
219
Chris Lattner96c3deb2006-08-12 17:13:08 +0000220 // Special case handling for the ternary operator.
221 ExprResult TernaryMiddle;
222 if (NextTokPrec == prec::Conditional) {
223 if (Tok.getKind() != tok::colon) {
224 // Handle this production specially:
225 // logical-OR-expression '?' expression ':' conditional-expression
226 // In particular, the RHS of the '?' is 'expression', not
227 // 'logical-OR-expression' as we might expect.
228 TernaryMiddle = ParseExpression();
229 if (TernaryMiddle.isInvalid) return TernaryMiddle;
230 } else {
231 // Special case handling of "X ? Y : Z" where Y is empty:
232 // logical-OR-expression '?' ':' conditional-expression [GNU]
233 TernaryMiddle = ExprResult(false);
234 Diag(Tok, diag::ext_gnu_conditional_expr);
235 }
236
237 if (Tok.getKind() != tok::colon) {
238 Diag(Tok, diag::err_expected_colon);
239 Diag(OpToken, diag::err_matching, "?");
240 return ExprResult(true);
241 }
242
243 // Eat the colon.
244 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000245 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000246
247 // Parse another leaf here for the RHS of the operator.
248 ExprResult RHS = ParseCastExpression(false);
249 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000250
251 // Remember the precedence of this operator and get the precedence of the
252 // operator immediately to the right of the RHS.
253 unsigned ThisPrec = NextTokPrec;
254 NextTokPrec = getBinOpPrecedence(Tok.getKind());
255
256 // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE.
257 // FIXME: do we want to handle assignment here??
Chris Lattnercde626a2006-08-12 08:13:25 +0000258 bool isRightAssoc = OpToken.getKind() == tok::question;
259
260 // Get the precedence of the operator to the right of the RHS. If it binds
261 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000262 if (ThisPrec < NextTokPrec ||
263 (ThisPrec == NextTokPrec && isRightAssoc)) {
264 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1);
265 if (RHS.isInvalid) return RHS;
266
267 NextTokPrec = getBinOpPrecedence(Tok.getKind());
268 }
269 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
270
Chris Lattner96c3deb2006-08-12 17:13:08 +0000271 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000272 }
273}
274
275
Chris Lattnereaf06592006-08-11 02:02:23 +0000276/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
277/// true, parse a unary-expression.
278///
Chris Lattner4564bc12006-08-10 23:14:52 +0000279/// cast-expression: [C99 6.5.4]
280/// unary-expression
281/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000282///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000283/// unary-expression: [C99 6.5.3]
284/// postfix-expression
285/// '++' unary-expression
286/// '--' unary-expression
287/// unary-operator cast-expression
288/// 'sizeof' unary-expression
289/// 'sizeof' '(' type-name ')'
290/// [GNU] '__alignof' unary-expression
291/// [GNU] '__alignof' '(' type-name ')'
292/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000293///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000294/// unary-operator: one of
295/// '&' '*' '+' '-' '~' '!'
296/// [GNU] '__extension__' '__real' '__imag'
297///
Chris Lattner52a99e52006-08-10 20:56:00 +0000298/// postfix-expression: [C99 6.5.2]
299/// primary-expression
300/// postfix-expression '[' expression ']'
301/// postfix-expression '(' argument-expression-list[opt] ')'
302/// postfix-expression '.' identifier
303/// postfix-expression '->' identifier
304/// postfix-expression '++'
305/// postfix-expression '--'
306/// '(' type-name ')' '{' initializer-list '}'
307/// '(' type-name ')' '{' initializer-list ',' '}'
308///
309/// argument-expression-list: [C99 6.5.2]
310/// argument-expression
311/// argument-expression-list ',' argument-expression
312///
313/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000314/// identifier
315/// constant
316/// string-literal
317/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000318/// '__func__' [C99 6.4.2.2]
319/// [GNU] '__FUNCTION__'
320/// [GNU] '__PRETTY_FUNCTION__'
321/// [GNU] '(' compound-statement ')'
322/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
323/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
324/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
325/// assign-expr ')'
326/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
327/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
328/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
329/// [OBC] '@protocol' '(' identifier ')' [TODO]
330/// [OBC] '@encode' '(' type-name ')' [TODO]
331/// [OBC] objc-string-literal [TODO]
332///
333/// constant: [C99 6.4.4]
334/// integer-constant
335/// floating-constant
336/// enumeration-constant -> identifier
337/// character-constant
338///
339/// [GNU] offsetof-member-designator:
340/// [GNU] identifier
341/// [GNU] offsetof-member-designator '.' identifier
342/// [GNU] offsetof-member-designator '[' expression ']'
343///
Chris Lattner81b576e2006-08-11 02:13:20 +0000344///
Chris Lattner89c50c62006-08-11 06:41:18 +0000345Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
346 ExprResult Res;
347
Chris Lattner81b576e2006-08-11 02:13:20 +0000348 // This handles all of cast-expression, unary-expression, postfix-expression,
349 // and primary-expression. We handle them together like this for efficiency
350 // and to simplify handling of an expression starting with a '(' token: which
351 // may be one of a parenthesized expression, cast-expression, compound literal
352 // expression, or statement expression.
353 //
354 // If the parsed tokens consist of a primary-expression, the cases below
355 // 'break' out of the switch. This allows the postfix expression pieces to
356 // be applied to them. Cases that cannot be followed by postfix exprs should
357 // return instead.
Chris Lattner52a99e52006-08-10 20:56:00 +0000358 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000359 case tok::l_paren:
360 // If this expression is limited to being a unary-expression, the parent can
361 // not start a cast expression.
362 ParenParseOption ParenExprType =
363 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000364 Res = ParseParenExpression(ParenExprType);
365 if (Res.isInvalid) return Res;
366
Chris Lattner81b576e2006-08-11 02:13:20 +0000367 switch (ParenExprType) {
368 case SimpleExpr: break; // Nothing else to do.
369 case CompoundStmt: break; // Nothing else to do.
370 case CompoundLiteral:
371 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
372 // postfix-expression exist, parse them now.
373 break;
374 case CastExpr:
375 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
376 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000377 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000378 }
379 break; // These can be followed by postfix-expr pieces.
Chris Lattner89c50c62006-08-11 06:41:18 +0000380
Chris Lattner52a99e52006-08-10 20:56:00 +0000381 // primary-expression
382 case tok::identifier: // primary-expression: identifier
383 // constant: enumeration-constant
384 case tok::numeric_constant: // constant: integer-constant
385 // constant: floating-constant
386 case tok::char_constant: // constant: character-constant
387 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
388 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
389 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
390 ConsumeToken();
391 break;
392 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000393 Res = ParseStringLiteralExpression();
394 if (Res.isInvalid) return Res;
Chris Lattner52a99e52006-08-10 20:56:00 +0000395 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000396 case tok::kw___builtin_va_arg:
397 case tok::kw___builtin_offsetof:
398 case tok::kw___builtin_choose_expr:
399 case tok::kw___builtin_types_compatible_p:
400 assert(0 && "FIXME: UNIMP!");
Chris Lattner81b576e2006-08-11 02:13:20 +0000401 case tok::plusplus: // unary-expression: '++' unary-expression
402 case tok::minusminus: // unary-expression: '--' unary-expression
403 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000404 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000405 case tok::amp: // unary-expression: '&' cast-expression
406 case tok::star: // unary-expression: '*' cast-expression
407 case tok::plus: // unary-expression: '+' cast-expression
408 case tok::minus: // unary-expression: '-' cast-expression
409 case tok::tilde: // unary-expression: '~' cast-expression
410 case tok::exclaim: // unary-expression: '!' cast-expression
411 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
412 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
413 //case tok::kw__extension__: [TODO]
414 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000415 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000416
417 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
418 // unary-expression: 'sizeof' '(' type-name ')'
419 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
420 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000421 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000422 case tok::ampamp: // unary-expression: '&&' identifier
423 Diag(Tok, diag::ext_gnu_address_of_label);
424 ConsumeToken();
425 if (Tok.getKind() == tok::identifier) {
426 ConsumeToken();
427 } else {
428 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000429 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000430 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000431 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000432 default:
433 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000434 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000435 }
436
437 // Now that the primary-expression piece of the postfix-expression has been
438 // parsed, see if there are any postfix-expression pieces here.
439 SourceLocation Loc;
440 while (1) {
441 switch (Tok.getKind()) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000442 default:
443 return ExprResult(false);
444 case tok::l_square: // postfix-expression: p-e '[' expression ']'
445 Loc = Tok.getLocation();
446 ConsumeBracket();
447 ParseExpression();
448 // Match the ']'.
449 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
450 break;
451
452 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
453 Loc = Tok.getLocation();
454 ConsumeParen();
455
456 while (1) {
457 // FIXME: This should be argument-expression!
458 ParseAssignmentExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000459
Chris Lattner89c50c62006-08-11 06:41:18 +0000460 if (Tok.getKind() != tok::comma)
461 break;
462 ConsumeToken(); // Next argument.
463 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000464
Chris Lattner89c50c62006-08-11 06:41:18 +0000465 // Match the ')'.
466 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
467 break;
468
469 case tok::arrow: // postfix-expression: p-e '->' identifier
470 case tok::period: // postfix-expression: p-e '.' identifier
471 ConsumeToken();
472 if (Tok.getKind() != tok::identifier) {
473 Diag(Tok, diag::err_expected_ident);
474 return ExprResult(true);
475 }
476 ConsumeToken();
477 break;
478
479 case tok::plusplus: // postfix-expression: postfix-expression '++'
480 case tok::minusminus: // postfix-expression: postfix-expression '--'
481 ConsumeToken();
482 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000483 }
484 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000485}
486
Chris Lattner81b576e2006-08-11 02:13:20 +0000487/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
488/// unary-expression: [C99 6.5.3]
489/// 'sizeof' unary-expression
490/// 'sizeof' '(' type-name ')'
491/// [GNU] '__alignof' unary-expression
492/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000493Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000494 assert((Tok.getKind() == tok::kw_sizeof ||
495 Tok.getKind() == tok::kw___alignof) &&
496 "Not a sizeof/alignof expression!");
497 ConsumeToken();
498
499 // If the operand doesn't start with an '(', it must be an expression.
500 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000501 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000502 }
503
504 // If it starts with a '(', we know that it is either a parenthesized
505 // type-name, or it is a unary-expression that starts with a compound literal,
506 // or starts with a primary-expression that is a parenthesized expression.
507 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000508 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000509}
510
Chris Lattner52a99e52006-08-10 20:56:00 +0000511/// ParseStringLiteralExpression - This handles the various token types that
512/// form string literals, and also handles string concatenation [C99 5.1.1.2,
513/// translation phase #6].
514///
515/// primary-expression: [C99 6.5.1]
516/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000517Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000518 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000519 ConsumeStringToken();
520
521 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
522 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000523 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000524 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000525 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000526}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000527
Chris Lattnerc951dae2006-08-10 04:23:57 +0000528
Chris Lattner4add4e62006-08-11 01:33:00 +0000529/// ParseParenExpression - This parses the unit that starts with a '(' token,
530/// based on what is allowed by ExprType. The actual thing parsed is returned
531/// in ExprType.
532///
533/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000534/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000535/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
536/// postfix-expression: [C99 6.5.2]
537/// '(' type-name ')' '{' initializer-list '}'
538/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000539/// cast-expression: [C99 6.5.4]
540/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000541///
Chris Lattner89c50c62006-08-11 06:41:18 +0000542Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000543 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
544 SourceLocation OpenLoc = Tok.getLocation();
545 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000546 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000547
Chris Lattner4add4e62006-08-11 01:33:00 +0000548 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000549 !getLang().NoExtensions) {
550 Diag(Tok, diag::ext_gnu_statement_expr);
551 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000552 ExprType = CompoundStmt;
553 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000554 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000555 ParseTypeName();
556
557 // Match the ')'.
558 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
559
Chris Lattner4add4e62006-08-11 01:33:00 +0000560 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000561 if (!getLang().C99) // Compound literals don't exist in C90.
562 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000563 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000564 ExprType = CompoundLiteral;
565 } else if (ExprType == CastExpr) {
566 // Note that this doesn't parse the subsequence cast-expression.
567 ExprType = CastExpr;
568 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000569 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000570 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000571 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000572 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000573 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000574 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000575 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000576 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000577
Chris Lattner4564bc12006-08-10 23:14:52 +0000578 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000579 if (Result.isInvalid)
580 SkipUntil(tok::r_paren);
581 else
582 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
583 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000584}