blob: 7a6fbe1a474b7552b8efa1471faec895d973f900 [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());
Chris Lattner89d53752006-08-12 17:18:19 +0000255
256 // Assignment and conditional expressions are right-associative.
257 bool isRightAssoc = NextTokPrec == prec::Conditional ||
258 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000259
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)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000264 // If this is left-associative, only parse things on the RHS that bind
265 // more tightly than the current operator. If it is left-associative, it
266 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
267 // A=(B=(C=D)), where each paren is a level of recursion here.
268 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000269 if (RHS.isInvalid) return RHS;
270
271 NextTokPrec = getBinOpPrecedence(Tok.getKind());
272 }
273 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
274
Chris Lattner96c3deb2006-08-12 17:13:08 +0000275 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000276 }
277}
278
279
Chris Lattnereaf06592006-08-11 02:02:23 +0000280/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
281/// true, parse a unary-expression.
282///
Chris Lattner4564bc12006-08-10 23:14:52 +0000283/// cast-expression: [C99 6.5.4]
284/// unary-expression
285/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000286///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000287/// unary-expression: [C99 6.5.3]
288/// postfix-expression
289/// '++' unary-expression
290/// '--' unary-expression
291/// unary-operator cast-expression
292/// 'sizeof' unary-expression
293/// 'sizeof' '(' type-name ')'
294/// [GNU] '__alignof' unary-expression
295/// [GNU] '__alignof' '(' type-name ')'
296/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000297///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000298/// unary-operator: one of
299/// '&' '*' '+' '-' '~' '!'
300/// [GNU] '__extension__' '__real' '__imag'
301///
Chris Lattner52a99e52006-08-10 20:56:00 +0000302/// postfix-expression: [C99 6.5.2]
303/// primary-expression
304/// postfix-expression '[' expression ']'
305/// postfix-expression '(' argument-expression-list[opt] ')'
306/// postfix-expression '.' identifier
307/// postfix-expression '->' identifier
308/// postfix-expression '++'
309/// postfix-expression '--'
310/// '(' type-name ')' '{' initializer-list '}'
311/// '(' type-name ')' '{' initializer-list ',' '}'
312///
313/// argument-expression-list: [C99 6.5.2]
314/// argument-expression
315/// argument-expression-list ',' argument-expression
316///
317/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000318/// identifier
319/// constant
320/// string-literal
321/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000322/// '__func__' [C99 6.4.2.2]
323/// [GNU] '__FUNCTION__'
324/// [GNU] '__PRETTY_FUNCTION__'
325/// [GNU] '(' compound-statement ')'
326/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
327/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
328/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
329/// assign-expr ')'
330/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
331/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
332/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
333/// [OBC] '@protocol' '(' identifier ')' [TODO]
334/// [OBC] '@encode' '(' type-name ')' [TODO]
335/// [OBC] objc-string-literal [TODO]
336///
337/// constant: [C99 6.4.4]
338/// integer-constant
339/// floating-constant
340/// enumeration-constant -> identifier
341/// character-constant
342///
343/// [GNU] offsetof-member-designator:
344/// [GNU] identifier
345/// [GNU] offsetof-member-designator '.' identifier
346/// [GNU] offsetof-member-designator '[' expression ']'
347///
Chris Lattner81b576e2006-08-11 02:13:20 +0000348///
Chris Lattner89c50c62006-08-11 06:41:18 +0000349Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
350 ExprResult Res;
351
Chris Lattner81b576e2006-08-11 02:13:20 +0000352 // This handles all of cast-expression, unary-expression, postfix-expression,
353 // and primary-expression. We handle them together like this for efficiency
354 // and to simplify handling of an expression starting with a '(' token: which
355 // may be one of a parenthesized expression, cast-expression, compound literal
356 // expression, or statement expression.
357 //
358 // If the parsed tokens consist of a primary-expression, the cases below
359 // 'break' out of the switch. This allows the postfix expression pieces to
360 // be applied to them. Cases that cannot be followed by postfix exprs should
361 // return instead.
Chris Lattner52a99e52006-08-10 20:56:00 +0000362 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000363 case tok::l_paren:
364 // If this expression is limited to being a unary-expression, the parent can
365 // not start a cast expression.
366 ParenParseOption ParenExprType =
367 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000368 Res = ParseParenExpression(ParenExprType);
369 if (Res.isInvalid) return Res;
370
Chris Lattner81b576e2006-08-11 02:13:20 +0000371 switch (ParenExprType) {
372 case SimpleExpr: break; // Nothing else to do.
373 case CompoundStmt: break; // Nothing else to do.
374 case CompoundLiteral:
375 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
376 // postfix-expression exist, parse them now.
377 break;
378 case CastExpr:
379 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
380 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000381 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000382 }
383 break; // These can be followed by postfix-expr pieces.
Chris Lattner89c50c62006-08-11 06:41:18 +0000384
Chris Lattner52a99e52006-08-10 20:56:00 +0000385 // primary-expression
386 case tok::identifier: // primary-expression: identifier
387 // constant: enumeration-constant
388 case tok::numeric_constant: // constant: integer-constant
389 // constant: floating-constant
390 case tok::char_constant: // constant: character-constant
391 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
392 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
393 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
394 ConsumeToken();
395 break;
396 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000397 Res = ParseStringLiteralExpression();
398 if (Res.isInvalid) return Res;
Chris Lattner52a99e52006-08-10 20:56:00 +0000399 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000400 case tok::kw___builtin_va_arg:
401 case tok::kw___builtin_offsetof:
402 case tok::kw___builtin_choose_expr:
403 case tok::kw___builtin_types_compatible_p:
404 assert(0 && "FIXME: UNIMP!");
Chris Lattner81b576e2006-08-11 02:13:20 +0000405 case tok::plusplus: // unary-expression: '++' unary-expression
406 case tok::minusminus: // unary-expression: '--' unary-expression
407 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000408 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000409 case tok::amp: // unary-expression: '&' cast-expression
410 case tok::star: // unary-expression: '*' cast-expression
411 case tok::plus: // unary-expression: '+' cast-expression
412 case tok::minus: // unary-expression: '-' cast-expression
413 case tok::tilde: // unary-expression: '~' cast-expression
414 case tok::exclaim: // unary-expression: '!' cast-expression
415 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
416 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
417 //case tok::kw__extension__: [TODO]
418 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000419 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000420
421 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
422 // unary-expression: 'sizeof' '(' type-name ')'
423 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
424 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000425 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000426 case tok::ampamp: // unary-expression: '&&' identifier
427 Diag(Tok, diag::ext_gnu_address_of_label);
428 ConsumeToken();
429 if (Tok.getKind() == tok::identifier) {
430 ConsumeToken();
431 } else {
432 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000433 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000434 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000435 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000436 default:
437 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000438 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000439 }
440
441 // Now that the primary-expression piece of the postfix-expression has been
442 // parsed, see if there are any postfix-expression pieces here.
443 SourceLocation Loc;
444 while (1) {
445 switch (Tok.getKind()) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000446 default:
447 return ExprResult(false);
448 case tok::l_square: // postfix-expression: p-e '[' expression ']'
449 Loc = Tok.getLocation();
450 ConsumeBracket();
451 ParseExpression();
452 // Match the ']'.
453 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
454 break;
455
456 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
457 Loc = Tok.getLocation();
458 ConsumeParen();
459
460 while (1) {
461 // FIXME: This should be argument-expression!
462 ParseAssignmentExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000463
Chris Lattner89c50c62006-08-11 06:41:18 +0000464 if (Tok.getKind() != tok::comma)
465 break;
466 ConsumeToken(); // Next argument.
467 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000468
Chris Lattner89c50c62006-08-11 06:41:18 +0000469 // Match the ')'.
470 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
471 break;
472
473 case tok::arrow: // postfix-expression: p-e '->' identifier
474 case tok::period: // postfix-expression: p-e '.' identifier
475 ConsumeToken();
476 if (Tok.getKind() != tok::identifier) {
477 Diag(Tok, diag::err_expected_ident);
478 return ExprResult(true);
479 }
480 ConsumeToken();
481 break;
482
483 case tok::plusplus: // postfix-expression: postfix-expression '++'
484 case tok::minusminus: // postfix-expression: postfix-expression '--'
485 ConsumeToken();
486 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000487 }
488 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000489}
490
Chris Lattner81b576e2006-08-11 02:13:20 +0000491/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
492/// unary-expression: [C99 6.5.3]
493/// 'sizeof' unary-expression
494/// 'sizeof' '(' type-name ')'
495/// [GNU] '__alignof' unary-expression
496/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000497Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000498 assert((Tok.getKind() == tok::kw_sizeof ||
499 Tok.getKind() == tok::kw___alignof) &&
500 "Not a sizeof/alignof expression!");
501 ConsumeToken();
502
503 // If the operand doesn't start with an '(', it must be an expression.
504 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000505 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000506 }
507
508 // If it starts with a '(', we know that it is either a parenthesized
509 // type-name, or it is a unary-expression that starts with a compound literal,
510 // or starts with a primary-expression that is a parenthesized expression.
511 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000512 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000513}
514
Chris Lattner52a99e52006-08-10 20:56:00 +0000515/// ParseStringLiteralExpression - This handles the various token types that
516/// form string literals, and also handles string concatenation [C99 5.1.1.2,
517/// translation phase #6].
518///
519/// primary-expression: [C99 6.5.1]
520/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000521Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000522 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000523 ConsumeStringToken();
524
525 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
526 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000527 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000528 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000529 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000530}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000531
Chris Lattnerc951dae2006-08-10 04:23:57 +0000532
Chris Lattner4add4e62006-08-11 01:33:00 +0000533/// ParseParenExpression - This parses the unit that starts with a '(' token,
534/// based on what is allowed by ExprType. The actual thing parsed is returned
535/// in ExprType.
536///
537/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000538/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000539/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
540/// postfix-expression: [C99 6.5.2]
541/// '(' type-name ')' '{' initializer-list '}'
542/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000543/// cast-expression: [C99 6.5.4]
544/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000545///
Chris Lattner89c50c62006-08-11 06:41:18 +0000546Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000547 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
548 SourceLocation OpenLoc = Tok.getLocation();
549 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000550 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000551
Chris Lattner4add4e62006-08-11 01:33:00 +0000552 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000553 !getLang().NoExtensions) {
554 Diag(Tok, diag::ext_gnu_statement_expr);
555 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000556 ExprType = CompoundStmt;
557 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000558 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000559 ParseTypeName();
560
561 // Match the ')'.
562 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
563
Chris Lattner4add4e62006-08-11 01:33:00 +0000564 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000565 if (!getLang().C99) // Compound literals don't exist in C90.
566 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000567 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000568 ExprType = CompoundLiteral;
569 } else if (ExprType == CastExpr) {
570 // Note that this doesn't parse the subsequence cast-expression.
571 ExprType = CastExpr;
572 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000573 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000574 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000575 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000576 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000577 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000578 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000579 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000580 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000581
Chris Lattner4564bc12006-08-10 23:14:52 +0000582 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000583 if (Result.isInvalid)
584 SkipUntil(tok::r_paren);
585 else
586 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
587 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000588}