blob: d1a4af7bc4ec112e03e8ad32be2b46b195097965 [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 Lattner0c6c0342006-08-12 18:12:45 +0000198/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
199///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000200Parser::ExprResult Parser::ParseAssignmentExpression() {
201 ExprResult LHS = ParseCastExpression(false);
202 if (LHS.isInvalid) return LHS;
203
204 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
205}
206
Chris Lattner0c6c0342006-08-12 18:12:45 +0000207/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
208/// in contexts where we have already consumed an identifier (which we saved in
209/// 'Tok'), then discovered that the identifier was really the leading token of
210/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
211/// now in 'Tok') and the current token is "[".
212Parser::ExprResult Parser::
213ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
214 // We know that 'Tok' must correspond to this production:
215 // primary-expression: identifier
216
217 // TODO: Pass 'Tok' to the action.
218 ExprResult Res = ExprResult(false);
219
220 // Because we have to parse an entire cast-expression before starting the
221 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
222 // need to handle the 'postfix-expression' rules. We do this by invoking
223 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
224 Res = ParsePostfixExpressionSuffix(Res);
225 if (Res.isInvalid) return Res;
226
227 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
228 // done, we know we don't have to do anything for cast-expression, because the
229 // only non-postfix-expression production starts with a '(' token, and we know
230 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
231 // to consume any trailing operators (e.g. "+" in this example) and connected
232 // chunks of the expression.
233 return ParseRHSOfBinaryExpression(Res, prec::Comma);
234}
235
Chris Lattnercde626a2006-08-12 08:13:25 +0000236/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
237/// LHS and has a precedence of at least MinPrec.
238Parser::ExprResult
239Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
240 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
241
242 while (1) {
243 // If this token has a lower precedence than we are allowed to parse (e.g.
244 // because we are called recursively, or because the token is not a binop),
245 // then we are done!
246 if (NextTokPrec < MinPrec)
247 return LHS;
248
249 // Consume the operator, saving the operator token for error reporting.
250 LexerToken OpToken = Tok;
251 ConsumeToken();
252
Chris Lattner96c3deb2006-08-12 17:13:08 +0000253 // Special case handling for the ternary operator.
254 ExprResult TernaryMiddle;
255 if (NextTokPrec == prec::Conditional) {
256 if (Tok.getKind() != tok::colon) {
257 // Handle this production specially:
258 // logical-OR-expression '?' expression ':' conditional-expression
259 // In particular, the RHS of the '?' is 'expression', not
260 // 'logical-OR-expression' as we might expect.
261 TernaryMiddle = ParseExpression();
262 if (TernaryMiddle.isInvalid) return TernaryMiddle;
263 } else {
264 // Special case handling of "X ? Y : Z" where Y is empty:
265 // logical-OR-expression '?' ':' conditional-expression [GNU]
266 TernaryMiddle = ExprResult(false);
267 Diag(Tok, diag::ext_gnu_conditional_expr);
268 }
269
270 if (Tok.getKind() != tok::colon) {
271 Diag(Tok, diag::err_expected_colon);
272 Diag(OpToken, diag::err_matching, "?");
273 return ExprResult(true);
274 }
275
276 // Eat the colon.
277 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000278 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000279
280 // Parse another leaf here for the RHS of the operator.
281 ExprResult RHS = ParseCastExpression(false);
282 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000283
284 // Remember the precedence of this operator and get the precedence of the
285 // operator immediately to the right of the RHS.
286 unsigned ThisPrec = NextTokPrec;
287 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000288
289 // Assignment and conditional expressions are right-associative.
290 bool isRightAssoc = NextTokPrec == prec::Conditional ||
291 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000292
293 // Get the precedence of the operator to the right of the RHS. If it binds
294 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000295 if (ThisPrec < NextTokPrec ||
296 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000297 // If this is left-associative, only parse things on the RHS that bind
298 // more tightly than the current operator. If it is left-associative, it
299 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
300 // A=(B=(C=D)), where each paren is a level of recursion here.
301 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000302 if (RHS.isInvalid) return RHS;
303
304 NextTokPrec = getBinOpPrecedence(Tok.getKind());
305 }
306 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
307
Chris Lattner96c3deb2006-08-12 17:13:08 +0000308 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000309 }
310}
311
312
Chris Lattnereaf06592006-08-11 02:02:23 +0000313/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
314/// true, parse a unary-expression.
315///
Chris Lattner4564bc12006-08-10 23:14:52 +0000316/// cast-expression: [C99 6.5.4]
317/// unary-expression
318/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000319///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000320/// unary-expression: [C99 6.5.3]
321/// postfix-expression
322/// '++' unary-expression
323/// '--' unary-expression
324/// unary-operator cast-expression
325/// 'sizeof' unary-expression
326/// 'sizeof' '(' type-name ')'
327/// [GNU] '__alignof' unary-expression
328/// [GNU] '__alignof' '(' type-name ')'
329/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000330///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000331/// unary-operator: one of
332/// '&' '*' '+' '-' '~' '!'
333/// [GNU] '__extension__' '__real' '__imag'
334///
Chris Lattner52a99e52006-08-10 20:56:00 +0000335/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000336/// identifier
337/// constant
338/// string-literal
339/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000340/// '__func__' [C99 6.4.2.2]
341/// [GNU] '__FUNCTION__'
342/// [GNU] '__PRETTY_FUNCTION__'
343/// [GNU] '(' compound-statement ')'
344/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
345/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
346/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
347/// assign-expr ')'
348/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
349/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
350/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
351/// [OBC] '@protocol' '(' identifier ')' [TODO]
352/// [OBC] '@encode' '(' type-name ')' [TODO]
353/// [OBC] objc-string-literal [TODO]
354///
355/// constant: [C99 6.4.4]
356/// integer-constant
357/// floating-constant
358/// enumeration-constant -> identifier
359/// character-constant
360///
361/// [GNU] offsetof-member-designator:
362/// [GNU] identifier
363/// [GNU] offsetof-member-designator '.' identifier
364/// [GNU] offsetof-member-designator '[' expression ']'
365///
Chris Lattner89c50c62006-08-11 06:41:18 +0000366Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
367 ExprResult Res;
368
Chris Lattner81b576e2006-08-11 02:13:20 +0000369 // This handles all of cast-expression, unary-expression, postfix-expression,
370 // and primary-expression. We handle them together like this for efficiency
371 // and to simplify handling of an expression starting with a '(' token: which
372 // may be one of a parenthesized expression, cast-expression, compound literal
373 // expression, or statement expression.
374 //
375 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000376 // call ParsePostfixExpressionSuffix to handle the postfix expression
377 // suffixes. Cases that cannot be followed by postfix exprs should
378 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000379 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000380 case tok::l_paren:
381 // If this expression is limited to being a unary-expression, the parent can
382 // not start a cast expression.
383 ParenParseOption ParenExprType =
384 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000385 Res = ParseParenExpression(ParenExprType);
386 if (Res.isInvalid) return Res;
387
Chris Lattner81b576e2006-08-11 02:13:20 +0000388 switch (ParenExprType) {
389 case SimpleExpr: break; // Nothing else to do.
390 case CompoundStmt: break; // Nothing else to do.
391 case CompoundLiteral:
392 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
393 // postfix-expression exist, parse them now.
394 break;
395 case CastExpr:
396 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
397 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000398 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000399 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000400
401 // These can be followed by postfix-expr pieces.
402 return ParsePostfixExpressionSuffix(Res);
Chris Lattner89c50c62006-08-11 06:41:18 +0000403
Chris Lattner52a99e52006-08-10 20:56:00 +0000404 // primary-expression
405 case tok::identifier: // primary-expression: identifier
406 // constant: enumeration-constant
407 case tok::numeric_constant: // constant: integer-constant
408 // constant: floating-constant
409 case tok::char_constant: // constant: character-constant
410 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
411 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
412 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner20c6a452006-08-12 17:40:43 +0000413 Res = ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000414 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000415 // These can be followed by postfix-expr pieces.
416 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000417 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000418 Res = ParseStringLiteralExpression();
419 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000420 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
421 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000422 case tok::kw___builtin_va_arg:
423 case tok::kw___builtin_offsetof:
424 case tok::kw___builtin_choose_expr:
425 case tok::kw___builtin_types_compatible_p:
426 assert(0 && "FIXME: UNIMP!");
Chris Lattner20c6a452006-08-12 17:40:43 +0000427 // This can be followed by postfix-expr pieces.
428 return ParsePostfixExpressionSuffix(Res);
Chris Lattner81b576e2006-08-11 02:13:20 +0000429 case tok::plusplus: // unary-expression: '++' unary-expression
430 case tok::minusminus: // unary-expression: '--' unary-expression
431 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000432 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000433 case tok::amp: // unary-expression: '&' cast-expression
434 case tok::star: // unary-expression: '*' cast-expression
435 case tok::plus: // unary-expression: '+' cast-expression
436 case tok::minus: // unary-expression: '-' cast-expression
437 case tok::tilde: // unary-expression: '~' cast-expression
438 case tok::exclaim: // unary-expression: '!' cast-expression
439 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
440 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
441 //case tok::kw__extension__: [TODO]
442 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000443 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000444
445 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
446 // unary-expression: 'sizeof' '(' type-name ')'
447 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
448 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000449 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000450 case tok::ampamp: // unary-expression: '&&' identifier
451 Diag(Tok, diag::ext_gnu_address_of_label);
452 ConsumeToken();
453 if (Tok.getKind() == tok::identifier) {
454 ConsumeToken();
455 } else {
456 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000457 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000458 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000459 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000460 default:
461 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000462 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000463 }
464
Chris Lattner20c6a452006-08-12 17:40:43 +0000465 // unreachable.
466 abort();
467}
468
469/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
470/// is parsed, this method parses any suffixes that apply.
471///
472/// postfix-expression: [C99 6.5.2]
473/// primary-expression
474/// postfix-expression '[' expression ']'
475/// postfix-expression '(' argument-expression-list[opt] ')'
476/// postfix-expression '.' identifier
477/// postfix-expression '->' identifier
478/// postfix-expression '++'
479/// postfix-expression '--'
480/// '(' type-name ')' '{' initializer-list '}'
481/// '(' type-name ')' '{' initializer-list ',' '}'
482///
483/// argument-expression-list: [C99 6.5.2]
484/// argument-expression
485/// argument-expression-list ',' assignment-expression
486///
487Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
488 assert(!LHS.isInvalid && "LHS is invalid already!");
489
Chris Lattnerf8339772006-08-10 22:01:51 +0000490 // Now that the primary-expression piece of the postfix-expression has been
491 // parsed, see if there are any postfix-expression pieces here.
492 SourceLocation Loc;
493 while (1) {
494 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000495 default: // Not a postfix-expression suffix.
496 return LHS;
Chris Lattner89c50c62006-08-11 06:41:18 +0000497 case tok::l_square: // postfix-expression: p-e '[' expression ']'
498 Loc = Tok.getLocation();
499 ConsumeBracket();
500 ParseExpression();
501 // Match the ']'.
502 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
503 break;
504
505 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
506 Loc = Tok.getLocation();
507 ConsumeParen();
508
Chris Lattner0c6c0342006-08-12 18:12:45 +0000509 if (Tok.getKind() != tok::r_paren) {
510 while (1) {
511 ParseAssignmentExpression();
512 if (Tok.getKind() != tok::comma)
513 break;
514 ConsumeToken(); // Next argument.
515 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000516 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000517
Chris Lattner89c50c62006-08-11 06:41:18 +0000518 // Match the ')'.
519 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
520 break;
521
522 case tok::arrow: // postfix-expression: p-e '->' identifier
523 case tok::period: // postfix-expression: p-e '.' identifier
524 ConsumeToken();
525 if (Tok.getKind() != tok::identifier) {
526 Diag(Tok, diag::err_expected_ident);
527 return ExprResult(true);
528 }
529 ConsumeToken();
530 break;
531
532 case tok::plusplus: // postfix-expression: postfix-expression '++'
533 case tok::minusminus: // postfix-expression: postfix-expression '--'
534 ConsumeToken();
535 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000536 }
537 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000538}
539
Chris Lattner20c6a452006-08-12 17:40:43 +0000540
Chris Lattner81b576e2006-08-11 02:13:20 +0000541/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
542/// unary-expression: [C99 6.5.3]
543/// 'sizeof' unary-expression
544/// 'sizeof' '(' type-name ')'
545/// [GNU] '__alignof' unary-expression
546/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000547Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000548 assert((Tok.getKind() == tok::kw_sizeof ||
549 Tok.getKind() == tok::kw___alignof) &&
550 "Not a sizeof/alignof expression!");
551 ConsumeToken();
552
553 // If the operand doesn't start with an '(', it must be an expression.
554 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000555 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000556 }
557
558 // If it starts with a '(', we know that it is either a parenthesized
559 // type-name, or it is a unary-expression that starts with a compound literal,
560 // or starts with a primary-expression that is a parenthesized expression.
561 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000562 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000563}
564
Chris Lattner52a99e52006-08-10 20:56:00 +0000565/// ParseStringLiteralExpression - This handles the various token types that
566/// form string literals, and also handles string concatenation [C99 5.1.1.2,
567/// translation phase #6].
568///
569/// primary-expression: [C99 6.5.1]
570/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000571Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000572 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000573 ConsumeStringToken();
574
575 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
576 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000577 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000578 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000579 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000580}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000581
Chris Lattnerc951dae2006-08-10 04:23:57 +0000582
Chris Lattner4add4e62006-08-11 01:33:00 +0000583/// ParseParenExpression - This parses the unit that starts with a '(' token,
584/// based on what is allowed by ExprType. The actual thing parsed is returned
585/// in ExprType.
586///
587/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000588/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000589/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
590/// postfix-expression: [C99 6.5.2]
591/// '(' type-name ')' '{' initializer-list '}'
592/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000593/// cast-expression: [C99 6.5.4]
594/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000595///
Chris Lattner89c50c62006-08-11 06:41:18 +0000596Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000597 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
598 SourceLocation OpenLoc = Tok.getLocation();
599 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000600 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000601
Chris Lattner4add4e62006-08-11 01:33:00 +0000602 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000603 !getLang().NoExtensions) {
604 Diag(Tok, diag::ext_gnu_statement_expr);
605 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000606 ExprType = CompoundStmt;
607 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000608 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000609 ParseTypeName();
610
611 // Match the ')'.
612 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
613
Chris Lattner4add4e62006-08-11 01:33:00 +0000614 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000615 if (!getLang().C99) // Compound literals don't exist in C90.
616 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000617 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000618 ExprType = CompoundLiteral;
619 } else if (ExprType == CastExpr) {
620 // Note that this doesn't parse the subsequence cast-expression.
621 ExprType = CastExpr;
622 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000623 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000624 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000625 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000626 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000627 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000628 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000629 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000630 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000631
Chris Lattner4564bc12006-08-10 23:14:52 +0000632 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000633 if (Result.isInvalid)
634 SkipUntil(tok::r_paren);
635 else
636 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
637 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000638}