blob: 1fddbd8a5e096b57cdf02ae5bacb45e80925c7ce [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 Lattner89c50c62006-08-11 06:41:18 +000048Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +000049 return ParseBinaryExpression();
Chris Lattnerc951dae2006-08-10 04:23:57 +000050}
51
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000052// Expr that doesn't include commas.
Chris Lattner89c50c62006-08-11 06:41:18 +000053Parser::ExprResult Parser::ParseAssignmentExpression() {
54 return ParseExpression();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000055}
56
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000057/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000058/// the C99 grammar. These have been named to relate with the C99 grammar
59/// productions. Low precedences numbers bind more weakly than high numbers.
60namespace prec {
61 enum Level {
62 Unknown = 0, // Not binary operator.
63 Comma = 1, // ,
64 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
65 Conditional = 3, // ?
66 LogicalOr = 4, // ||
67 LogicalAnd = 5, // &&
68 InclusiveOr = 6, // |
69 ExclusiveOr = 7, // ^
70 And = 8, // &
71 MinMax = 9, // <?, >? min, max (GCC extensions)
72 Equality = 10, // ==, !=
73 Relational = 11, // >=, <=, >, <
74 Shift = 12, // <<, >>
75 Additive = 13, // -, +
76 Multiplicative = 14 // *, /, %
77 };
78}
79
80
81/// getBinOpPrecedence - Return the precedence of the specified binary operator
82/// token. This returns:
83///
84static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
85 switch (Kind) {
86 default: return prec::Unknown;
87 case tok::comma: return prec::Comma;
88 case tok::equal:
89 case tok::starequal:
90 case tok::slashequal:
91 case tok::percentequal:
92 case tok::plusequal:
93 case tok::minusequal:
94 case tok::lesslessequal:
95 case tok::greatergreaterequal:
96 case tok::ampequal:
97 case tok::caretequal:
98 case tok::pipeequal: return prec::Assignment;
99 case tok::question: return prec::Conditional;
100 case tok::pipepipe: return prec::LogicalOr;
101 case tok::ampamp: return prec::LogicalAnd;
102 case tok::pipe: return prec::InclusiveOr;
103 case tok::caret: return prec::ExclusiveOr;
104 case tok::amp: return prec::And;
105 case tok::lessquestion:
106 case tok::greaterquestion: return prec::MinMax;
107 case tok::exclaimequal:
108 case tok::equalequal: return prec::Equality;
109 case tok::lessequal:
110 case tok::less:
111 case tok::greaterequal:
112 case tok::greater: return prec::Relational;
113 case tok::lessless:
114 case tok::greatergreater: return prec::Shift;
115 case tok::plus:
116 case tok::minus: return prec::Additive;
117 case tok::percent:
118 case tok::slash:
119 case tok::star: return prec::Multiplicative;
120 }
121}
122
123
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000124/// ParseBinaryExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +0000125/// operators.
126///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000127/// Note: we diverge from the C99 grammar when parsing the assignment-expression
128/// production. C99 specifies that the LHS of an assignment operator should be
129/// parsed as a unary-expression, but consistency dictates that it be a
130/// conditional-expession. In practice, the important thing here is that the
131/// LHS of an assignment has to be an l-value, which productions between
132/// unary-expression and conditional-expression don't produce. Because we want
133/// consistency, we parse the LHS as a conditional-expression, then check for
134/// l-value-ness in semantic analysis stages.
135///
Chris Lattnercde626a2006-08-12 08:13:25 +0000136/// multiplicative-expression: [C99 6.5.5]
137/// cast-expression
138/// multiplicative-expression '*' cast-expression
139/// multiplicative-expression '/' cast-expression
140/// multiplicative-expression '%' cast-expression
141///
142/// additive-expression: [C99 6.5.6]
143/// multiplicative-expression
144/// additive-expression '+' multiplicative-expression
145/// additive-expression '-' multiplicative-expression
146///
147/// shift-expression: [C99 6.5.7]
148/// additive-expression
149/// shift-expression '<<' additive-expression
150/// shift-expression '>>' additive-expression
151///
152/// relational-expression: [C99 6.5.8]
153/// shift-expression
154/// relational-expression '<' shift-expression
155/// relational-expression '>' shift-expression
156/// relational-expression '<=' shift-expression
157/// relational-expression '>=' shift-expression
158///
159/// equality-expression: [C99 6.5.9]
160/// relational-expression
161/// equality-expression '==' relational-expression
162/// equality-expression '!=' relational-expression
163///
164/// AND-expression: [C99 6.5.10]
165/// equality-expression
166/// AND-expression '&' equality-expression
167///
168/// exclusive-OR-expression: [C99 6.5.11]
169/// AND-expression
170/// exclusive-OR-expression '^' AND-expression
171///
172/// inclusive-OR-expression: [C99 6.5.12]
173/// exclusive-OR-expression
174/// inclusive-OR-expression '|' exclusive-OR-expression
175///
176/// logical-AND-expression: [C99 6.5.13]
177/// inclusive-OR-expression
178/// logical-AND-expression '&&' inclusive-OR-expression
179///
180/// logical-OR-expression: [C99 6.5.14]
181/// logical-AND-expression
182/// logical-OR-expression '||' logical-AND-expression
183///
184/// conditional-expression: [C99 6.5.15]
185/// logical-OR-expression
186/// logical-OR-expression '?' expression ':' conditional-expression
187/// [GNU] logical-OR-expression '?' ':' conditional-expression
188///
189/// assignment-expression: [C99 6.5.16]
190/// conditional-expression
191/// unary-expression assignment-operator assignment-expression
192///
193/// assignment-operator: one of
194/// = *= /= %= += -= <<= >>= &= ^= |=
195///
196/// expression: [C99 6.5.17]
197/// assignment-expression
198/// expression ',' assignment-expression
199///
200Parser::ExprResult Parser::ParseBinaryExpression() {
201 ExprResult LHS = ParseCastExpression(false);
202 if (LHS.isInvalid) return LHS;
203
204 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
205}
206
207/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
208/// LHS and has a precedence of at least MinPrec.
209Parser::ExprResult
210Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
211 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
212
213 while (1) {
214 // If this token has a lower precedence than we are allowed to parse (e.g.
215 // because we are called recursively, or because the token is not a binop),
216 // then we are done!
217 if (NextTokPrec < MinPrec)
218 return LHS;
219
220 // Consume the operator, saving the operator token for error reporting.
221 LexerToken OpToken = Tok;
222 ConsumeToken();
223
224 // Parse the RHS of the operator.
225 ExprResult RHS;
226
227 // Special case handling of "X ? Y : Z" were Y is empty. This is a GCC
228 // extension.
229 if (OpToken.getKind() != tok::question || Tok.getKind() != tok::colon) {
230 RHS = ParseCastExpression(false);
231 if (RHS.isInvalid) return RHS;
232 } else {
233 RHS = ExprResult(false);
234 Diag(Tok, diag::ext_gnu_conditional_expr);
235 }
236
237 // Remember the precedence of this operator and get the precedence of the
238 // operator immediately to the right of the RHS.
239 unsigned ThisPrec = NextTokPrec;
240 NextTokPrec = getBinOpPrecedence(Tok.getKind());
241
242 // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE.
243 // FIXME: do we want to handle assignment here??
Chris Lattnercde626a2006-08-12 08:13:25 +0000244 bool isRightAssoc = OpToken.getKind() == tok::question;
245
246 // Get the precedence of the operator to the right of the RHS. If it binds
247 // more tightly with RHS than we do, evaluate it completely first.
248
249 // FIXME: Is this enough for '?:' operators? The second term is suppsed to
250 // be 'expression', not 'assignment'.
251 if (ThisPrec < NextTokPrec ||
252 (ThisPrec == NextTokPrec && isRightAssoc)) {
253 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1);
254 if (RHS.isInvalid) return RHS;
255
256 NextTokPrec = getBinOpPrecedence(Tok.getKind());
257 }
258 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
259
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000260 // Handle the special case of our one ternary operator here.
Chris Lattnercde626a2006-08-12 08:13:25 +0000261 if (OpToken.getKind() == tok::question) {
262 if (Tok.getKind() != tok::colon) {
263 Diag(Tok, diag::err_expected_colon);
264 Diag(OpToken, diag::err_matching, "?");
265 return ExprResult(true);
266 }
267
268 // Eat the colon.
269 ConsumeToken();
270
271 // Parse the value of the colon.
272 ExprResult AfterColonVal = ParseCastExpression(false);
273 if (AfterColonVal.isInvalid) return AfterColonVal;
274
275 // Parse anything after the RRHS that has a higher precedence than ?.
276 AfterColonVal = ParseRHSOfBinaryExpression(AfterColonVal, ThisPrec+1);
277 if (AfterColonVal.isInvalid) return AfterColonVal;
278
279 // TODO: Combine LHS = LHS ? RHS : AfterColonVal.
280
281 // Figure out the precedence of the token after the : part.
282 NextTokPrec = getBinOpPrecedence(Tok.getKind());
283 } else {
284 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
285 }
286 }
287}
288
289
Chris Lattnereaf06592006-08-11 02:02:23 +0000290/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
291/// true, parse a unary-expression.
292///
Chris Lattner4564bc12006-08-10 23:14:52 +0000293/// cast-expression: [C99 6.5.4]
294/// unary-expression
295/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000296///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000297/// unary-expression: [C99 6.5.3]
298/// postfix-expression
299/// '++' unary-expression
300/// '--' unary-expression
301/// unary-operator cast-expression
302/// 'sizeof' unary-expression
303/// 'sizeof' '(' type-name ')'
304/// [GNU] '__alignof' unary-expression
305/// [GNU] '__alignof' '(' type-name ')'
306/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000307///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000308/// unary-operator: one of
309/// '&' '*' '+' '-' '~' '!'
310/// [GNU] '__extension__' '__real' '__imag'
311///
Chris Lattner52a99e52006-08-10 20:56:00 +0000312/// postfix-expression: [C99 6.5.2]
313/// primary-expression
314/// postfix-expression '[' expression ']'
315/// postfix-expression '(' argument-expression-list[opt] ')'
316/// postfix-expression '.' identifier
317/// postfix-expression '->' identifier
318/// postfix-expression '++'
319/// postfix-expression '--'
320/// '(' type-name ')' '{' initializer-list '}'
321/// '(' type-name ')' '{' initializer-list ',' '}'
322///
323/// argument-expression-list: [C99 6.5.2]
324/// argument-expression
325/// argument-expression-list ',' argument-expression
326///
327/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000328/// identifier
329/// constant
330/// string-literal
331/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000332/// '__func__' [C99 6.4.2.2]
333/// [GNU] '__FUNCTION__'
334/// [GNU] '__PRETTY_FUNCTION__'
335/// [GNU] '(' compound-statement ')'
336/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
337/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
338/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
339/// assign-expr ')'
340/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
341/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
342/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
343/// [OBC] '@protocol' '(' identifier ')' [TODO]
344/// [OBC] '@encode' '(' type-name ')' [TODO]
345/// [OBC] objc-string-literal [TODO]
346///
347/// constant: [C99 6.4.4]
348/// integer-constant
349/// floating-constant
350/// enumeration-constant -> identifier
351/// character-constant
352///
353/// [GNU] offsetof-member-designator:
354/// [GNU] identifier
355/// [GNU] offsetof-member-designator '.' identifier
356/// [GNU] offsetof-member-designator '[' expression ']'
357///
Chris Lattner81b576e2006-08-11 02:13:20 +0000358///
Chris Lattner89c50c62006-08-11 06:41:18 +0000359Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
360 ExprResult Res;
361
Chris Lattner81b576e2006-08-11 02:13:20 +0000362 // This handles all of cast-expression, unary-expression, postfix-expression,
363 // and primary-expression. We handle them together like this for efficiency
364 // and to simplify handling of an expression starting with a '(' token: which
365 // may be one of a parenthesized expression, cast-expression, compound literal
366 // expression, or statement expression.
367 //
368 // If the parsed tokens consist of a primary-expression, the cases below
369 // 'break' out of the switch. This allows the postfix expression pieces to
370 // be applied to them. Cases that cannot be followed by postfix exprs should
371 // return instead.
Chris Lattner52a99e52006-08-10 20:56:00 +0000372 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000373 case tok::l_paren:
374 // If this expression is limited to being a unary-expression, the parent can
375 // not start a cast expression.
376 ParenParseOption ParenExprType =
377 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000378 Res = ParseParenExpression(ParenExprType);
379 if (Res.isInvalid) return Res;
380
Chris Lattner81b576e2006-08-11 02:13:20 +0000381 switch (ParenExprType) {
382 case SimpleExpr: break; // Nothing else to do.
383 case CompoundStmt: break; // Nothing else to do.
384 case CompoundLiteral:
385 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
386 // postfix-expression exist, parse them now.
387 break;
388 case CastExpr:
389 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
390 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000391 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000392 }
393 break; // These can be followed by postfix-expr pieces.
Chris Lattner89c50c62006-08-11 06:41:18 +0000394
Chris Lattner52a99e52006-08-10 20:56:00 +0000395 // primary-expression
396 case tok::identifier: // primary-expression: identifier
397 // constant: enumeration-constant
398 case tok::numeric_constant: // constant: integer-constant
399 // constant: floating-constant
400 case tok::char_constant: // constant: character-constant
401 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
402 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
403 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
404 ConsumeToken();
405 break;
406 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000407 Res = ParseStringLiteralExpression();
408 if (Res.isInvalid) return Res;
Chris Lattner52a99e52006-08-10 20:56:00 +0000409 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000410 case tok::kw___builtin_va_arg:
411 case tok::kw___builtin_offsetof:
412 case tok::kw___builtin_choose_expr:
413 case tok::kw___builtin_types_compatible_p:
414 assert(0 && "FIXME: UNIMP!");
Chris Lattner81b576e2006-08-11 02:13:20 +0000415 case tok::plusplus: // unary-expression: '++' unary-expression
416 case tok::minusminus: // unary-expression: '--' unary-expression
417 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000418 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000419 case tok::amp: // unary-expression: '&' cast-expression
420 case tok::star: // unary-expression: '*' cast-expression
421 case tok::plus: // unary-expression: '+' cast-expression
422 case tok::minus: // unary-expression: '-' cast-expression
423 case tok::tilde: // unary-expression: '~' cast-expression
424 case tok::exclaim: // unary-expression: '!' cast-expression
425 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
426 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
427 //case tok::kw__extension__: [TODO]
428 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000429 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000430
431 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
432 // unary-expression: 'sizeof' '(' type-name ')'
433 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
434 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000435 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000436 case tok::ampamp: // unary-expression: '&&' identifier
437 Diag(Tok, diag::ext_gnu_address_of_label);
438 ConsumeToken();
439 if (Tok.getKind() == tok::identifier) {
440 ConsumeToken();
441 } else {
442 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000443 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000444 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000445 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000446 default:
447 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000448 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000449 }
450
451 // Now that the primary-expression piece of the postfix-expression has been
452 // parsed, see if there are any postfix-expression pieces here.
453 SourceLocation Loc;
454 while (1) {
455 switch (Tok.getKind()) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000456 default:
457 return ExprResult(false);
458 case tok::l_square: // postfix-expression: p-e '[' expression ']'
459 Loc = Tok.getLocation();
460 ConsumeBracket();
461 ParseExpression();
462 // Match the ']'.
463 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
464 break;
465
466 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
467 Loc = Tok.getLocation();
468 ConsumeParen();
469
470 while (1) {
471 // FIXME: This should be argument-expression!
472 ParseAssignmentExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000473
Chris Lattner89c50c62006-08-11 06:41:18 +0000474 if (Tok.getKind() != tok::comma)
475 break;
476 ConsumeToken(); // Next argument.
477 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000478
Chris Lattner89c50c62006-08-11 06:41:18 +0000479 // Match the ')'.
480 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
481 break;
482
483 case tok::arrow: // postfix-expression: p-e '->' identifier
484 case tok::period: // postfix-expression: p-e '.' identifier
485 ConsumeToken();
486 if (Tok.getKind() != tok::identifier) {
487 Diag(Tok, diag::err_expected_ident);
488 return ExprResult(true);
489 }
490 ConsumeToken();
491 break;
492
493 case tok::plusplus: // postfix-expression: postfix-expression '++'
494 case tok::minusminus: // postfix-expression: postfix-expression '--'
495 ConsumeToken();
496 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000497 }
498 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000499}
500
Chris Lattner81b576e2006-08-11 02:13:20 +0000501/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
502/// unary-expression: [C99 6.5.3]
503/// 'sizeof' unary-expression
504/// 'sizeof' '(' type-name ')'
505/// [GNU] '__alignof' unary-expression
506/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000507Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000508 assert((Tok.getKind() == tok::kw_sizeof ||
509 Tok.getKind() == tok::kw___alignof) &&
510 "Not a sizeof/alignof expression!");
511 ConsumeToken();
512
513 // If the operand doesn't start with an '(', it must be an expression.
514 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000515 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000516 }
517
518 // If it starts with a '(', we know that it is either a parenthesized
519 // type-name, or it is a unary-expression that starts with a compound literal,
520 // or starts with a primary-expression that is a parenthesized expression.
521 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000522 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000523}
524
Chris Lattner52a99e52006-08-10 20:56:00 +0000525/// ParseStringLiteralExpression - This handles the various token types that
526/// form string literals, and also handles string concatenation [C99 5.1.1.2,
527/// translation phase #6].
528///
529/// primary-expression: [C99 6.5.1]
530/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000531Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000532 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000533 ConsumeStringToken();
534
535 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
536 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000537 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000538 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000539 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000540}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000541
Chris Lattnerc951dae2006-08-10 04:23:57 +0000542
Chris Lattner4add4e62006-08-11 01:33:00 +0000543/// ParseParenExpression - This parses the unit that starts with a '(' token,
544/// based on what is allowed by ExprType. The actual thing parsed is returned
545/// in ExprType.
546///
547/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000548/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000549/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
550/// postfix-expression: [C99 6.5.2]
551/// '(' type-name ')' '{' initializer-list '}'
552/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000553/// cast-expression: [C99 6.5.4]
554/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000555///
Chris Lattner89c50c62006-08-11 06:41:18 +0000556Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000557 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
558 SourceLocation OpenLoc = Tok.getLocation();
559 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000560 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000561
Chris Lattner4add4e62006-08-11 01:33:00 +0000562 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000563 !getLang().NoExtensions) {
564 Diag(Tok, diag::ext_gnu_statement_expr);
565 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000566 ExprType = CompoundStmt;
567 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000568 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000569 ParseTypeName();
570
571 // Match the ')'.
572 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
573
Chris Lattner4add4e62006-08-11 01:33:00 +0000574 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000575 if (!getLang().C99) // Compound literals don't exist in C90.
576 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000577 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000578 ExprType = CompoundLiteral;
579 } else if (ExprType == CastExpr) {
580 // Note that this doesn't parse the subsequence cast-expression.
581 ExprType = CastExpr;
582 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000583 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000584 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000585 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000586 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000587 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000588 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000589 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000590 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000591
Chris Lattner4564bc12006-08-10 23:14:52 +0000592 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000593 if (Result.isInvalid)
594 SkipUntil(tok::r_paren);
595 else
596 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
597 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000598}