blob: c26415bd37c757429bd3ae560f8550ba4512cff0 [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 Lattner3b561a32006-08-13 00:12:11 +0000207Parser::ExprResult Parser::ParseConstantExpression() {
208 ExprResult LHS = ParseCastExpression(false);
209 if (LHS.isInvalid) return LHS;
210
211 // TODO: Validate that this is a constant expr!
212 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
213}
214
Chris Lattner0c6c0342006-08-12 18:12:45 +0000215/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
216/// in contexts where we have already consumed an identifier (which we saved in
217/// 'Tok'), then discovered that the identifier was really the leading token of
218/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
219/// now in 'Tok') and the current token is "[".
220Parser::ExprResult Parser::
221ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
222 // We know that 'Tok' must correspond to this production:
223 // primary-expression: identifier
224
225 // TODO: Pass 'Tok' to the action.
226 ExprResult Res = ExprResult(false);
227
228 // Because we have to parse an entire cast-expression before starting the
229 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
230 // need to handle the 'postfix-expression' rules. We do this by invoking
231 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
232 Res = ParsePostfixExpressionSuffix(Res);
233 if (Res.isInvalid) return Res;
234
235 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
236 // done, we know we don't have to do anything for cast-expression, because the
237 // only non-postfix-expression production starts with a '(' token, and we know
238 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
239 // to consume any trailing operators (e.g. "+" in this example) and connected
240 // chunks of the expression.
241 return ParseRHSOfBinaryExpression(Res, prec::Comma);
242}
243
Chris Lattner62591722006-08-12 18:40:58 +0000244/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
245/// used in contexts where we have already consumed a '*' (which we saved in
246/// 'Tok'), then discovered that the '*' was really the leading token of an
247/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
248/// now in 'Tok') and the current token is "(".
249Parser::ExprResult Parser::
250ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
251 // We know that 'Tok' must correspond to this production:
252 // unary-expression: unary-operator cast-expression
253 // where 'unary-operator' is '*'.
254
255 // Parse the cast-expression that follows the '*'. This will parse the
256 // "*(int*)P" part of "*(int*)P+B".
257 ExprResult Res = ParseCastExpression(false);
258 if (Res.isInvalid) return Res;
259
260 // TODO: Combine Tok + Res to get the new AST.
261
262 // We have to parse an entire cast-expression before starting the
263 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
264 // we know that the only production above us is the cast-expression
265 // production, and because the only alternative productions start with a '('
266 // token (we know we had a '*'), there is no work to do to get a whole
267 // cast-expression.
268
269 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
270 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
271 // trailing operators (e.g. "+" in this example) and connected chunks of the
272 // assignment-expression.
273 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
274}
275
276
Chris Lattnercde626a2006-08-12 08:13:25 +0000277/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
278/// LHS and has a precedence of at least MinPrec.
279Parser::ExprResult
280Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
281 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
282
283 while (1) {
284 // If this token has a lower precedence than we are allowed to parse (e.g.
285 // because we are called recursively, or because the token is not a binop),
286 // then we are done!
287 if (NextTokPrec < MinPrec)
288 return LHS;
289
290 // Consume the operator, saving the operator token for error reporting.
291 LexerToken OpToken = Tok;
292 ConsumeToken();
293
Chris Lattner96c3deb2006-08-12 17:13:08 +0000294 // Special case handling for the ternary operator.
295 ExprResult TernaryMiddle;
296 if (NextTokPrec == prec::Conditional) {
297 if (Tok.getKind() != tok::colon) {
298 // Handle this production specially:
299 // logical-OR-expression '?' expression ':' conditional-expression
300 // In particular, the RHS of the '?' is 'expression', not
301 // 'logical-OR-expression' as we might expect.
302 TernaryMiddle = ParseExpression();
303 if (TernaryMiddle.isInvalid) return TernaryMiddle;
304 } else {
305 // Special case handling of "X ? Y : Z" where Y is empty:
306 // logical-OR-expression '?' ':' conditional-expression [GNU]
307 TernaryMiddle = ExprResult(false);
308 Diag(Tok, diag::ext_gnu_conditional_expr);
309 }
310
311 if (Tok.getKind() != tok::colon) {
312 Diag(Tok, diag::err_expected_colon);
313 Diag(OpToken, diag::err_matching, "?");
314 return ExprResult(true);
315 }
316
317 // Eat the colon.
318 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000319 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000320
321 // Parse another leaf here for the RHS of the operator.
322 ExprResult RHS = ParseCastExpression(false);
323 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000324
325 // Remember the precedence of this operator and get the precedence of the
326 // operator immediately to the right of the RHS.
327 unsigned ThisPrec = NextTokPrec;
328 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000329
330 // Assignment and conditional expressions are right-associative.
331 bool isRightAssoc = NextTokPrec == prec::Conditional ||
332 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000333
334 // Get the precedence of the operator to the right of the RHS. If it binds
335 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000336 if (ThisPrec < NextTokPrec ||
337 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000338 // If this is left-associative, only parse things on the RHS that bind
339 // more tightly than the current operator. If it is left-associative, it
340 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
341 // A=(B=(C=D)), where each paren is a level of recursion here.
342 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000343 if (RHS.isInvalid) return RHS;
344
345 NextTokPrec = getBinOpPrecedence(Tok.getKind());
346 }
347 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
348
Chris Lattner96c3deb2006-08-12 17:13:08 +0000349 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000350 }
351}
352
Chris Lattnereaf06592006-08-11 02:02:23 +0000353/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
354/// true, parse a unary-expression.
355///
Chris Lattner4564bc12006-08-10 23:14:52 +0000356/// cast-expression: [C99 6.5.4]
357/// unary-expression
358/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000359///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000360/// unary-expression: [C99 6.5.3]
361/// postfix-expression
362/// '++' unary-expression
363/// '--' unary-expression
364/// unary-operator cast-expression
365/// 'sizeof' unary-expression
366/// 'sizeof' '(' type-name ')'
367/// [GNU] '__alignof' unary-expression
368/// [GNU] '__alignof' '(' type-name ')'
369/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000370///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000371/// unary-operator: one of
372/// '&' '*' '+' '-' '~' '!'
373/// [GNU] '__extension__' '__real' '__imag'
374///
Chris Lattner52a99e52006-08-10 20:56:00 +0000375/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000376/// identifier
377/// constant
378/// string-literal
379/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000380/// '__func__' [C99 6.4.2.2]
381/// [GNU] '__FUNCTION__'
382/// [GNU] '__PRETTY_FUNCTION__'
383/// [GNU] '(' compound-statement ')'
384/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
385/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
386/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
387/// assign-expr ')'
388/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
389/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
390/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
391/// [OBC] '@protocol' '(' identifier ')' [TODO]
392/// [OBC] '@encode' '(' type-name ')' [TODO]
393/// [OBC] objc-string-literal [TODO]
394///
395/// constant: [C99 6.4.4]
396/// integer-constant
397/// floating-constant
398/// enumeration-constant -> identifier
399/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000400///
Chris Lattner89c50c62006-08-11 06:41:18 +0000401Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
402 ExprResult Res;
403
Chris Lattner81b576e2006-08-11 02:13:20 +0000404 // This handles all of cast-expression, unary-expression, postfix-expression,
405 // and primary-expression. We handle them together like this for efficiency
406 // and to simplify handling of an expression starting with a '(' token: which
407 // may be one of a parenthesized expression, cast-expression, compound literal
408 // expression, or statement expression.
409 //
410 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000411 // call ParsePostfixExpressionSuffix to handle the postfix expression
412 // suffixes. Cases that cannot be followed by postfix exprs should
413 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000414 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000415 case tok::l_paren:
416 // If this expression is limited to being a unary-expression, the parent can
417 // not start a cast expression.
418 ParenParseOption ParenExprType =
419 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000420 Res = ParseParenExpression(ParenExprType);
421 if (Res.isInvalid) return Res;
422
Chris Lattner81b576e2006-08-11 02:13:20 +0000423 switch (ParenExprType) {
424 case SimpleExpr: break; // Nothing else to do.
425 case CompoundStmt: break; // Nothing else to do.
426 case CompoundLiteral:
427 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
428 // postfix-expression exist, parse them now.
429 break;
430 case CastExpr:
431 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
432 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000433 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000434 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000435
436 // These can be followed by postfix-expr pieces.
437 return ParsePostfixExpressionSuffix(Res);
Chris Lattner89c50c62006-08-11 06:41:18 +0000438
Chris Lattner52a99e52006-08-10 20:56:00 +0000439 // primary-expression
440 case tok::identifier: // primary-expression: identifier
441 // constant: enumeration-constant
442 case tok::numeric_constant: // constant: integer-constant
443 // constant: floating-constant
444 case tok::char_constant: // constant: character-constant
445 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
446 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
447 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner20c6a452006-08-12 17:40:43 +0000448 Res = ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000449 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000450 // These can be followed by postfix-expr pieces.
451 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000452 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000453 Res = ParseStringLiteralExpression();
454 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000455 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
456 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000457 case tok::kw___builtin_va_arg:
458 case tok::kw___builtin_offsetof:
459 case tok::kw___builtin_choose_expr:
460 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000461 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000462 case tok::plusplus: // unary-expression: '++' unary-expression
463 case tok::minusminus: // unary-expression: '--' unary-expression
464 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000465 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000466 case tok::amp: // unary-expression: '&' cast-expression
467 case tok::star: // unary-expression: '*' cast-expression
468 case tok::plus: // unary-expression: '+' cast-expression
469 case tok::minus: // unary-expression: '-' cast-expression
470 case tok::tilde: // unary-expression: '~' cast-expression
471 case tok::exclaim: // unary-expression: '!' cast-expression
472 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
473 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
474 //case tok::kw__extension__: [TODO]
475 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000476 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000477
478 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
479 // unary-expression: 'sizeof' '(' type-name ')'
480 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
481 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000482 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000483 case tok::ampamp: // unary-expression: '&&' identifier
484 Diag(Tok, diag::ext_gnu_address_of_label);
485 ConsumeToken();
486 if (Tok.getKind() == tok::identifier) {
487 ConsumeToken();
488 } else {
489 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000490 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000491 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000492 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000493 default:
494 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000495 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000496 }
497
Chris Lattner20c6a452006-08-12 17:40:43 +0000498 // unreachable.
499 abort();
500}
501
502/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
503/// is parsed, this method parses any suffixes that apply.
504///
505/// postfix-expression: [C99 6.5.2]
506/// primary-expression
507/// postfix-expression '[' expression ']'
508/// postfix-expression '(' argument-expression-list[opt] ')'
509/// postfix-expression '.' identifier
510/// postfix-expression '->' identifier
511/// postfix-expression '++'
512/// postfix-expression '--'
513/// '(' type-name ')' '{' initializer-list '}'
514/// '(' type-name ')' '{' initializer-list ',' '}'
515///
516/// argument-expression-list: [C99 6.5.2]
517/// argument-expression
518/// argument-expression-list ',' assignment-expression
519///
520Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
521 assert(!LHS.isInvalid && "LHS is invalid already!");
522
Chris Lattnerf8339772006-08-10 22:01:51 +0000523 // Now that the primary-expression piece of the postfix-expression has been
524 // parsed, see if there are any postfix-expression pieces here.
525 SourceLocation Loc;
526 while (1) {
527 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000528 default: // Not a postfix-expression suffix.
529 return LHS;
Chris Lattner89c50c62006-08-11 06:41:18 +0000530 case tok::l_square: // postfix-expression: p-e '[' expression ']'
531 Loc = Tok.getLocation();
532 ConsumeBracket();
533 ParseExpression();
534 // Match the ']'.
535 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
536 break;
537
538 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
539 Loc = Tok.getLocation();
540 ConsumeParen();
541
Chris Lattner0c6c0342006-08-12 18:12:45 +0000542 if (Tok.getKind() != tok::r_paren) {
543 while (1) {
544 ParseAssignmentExpression();
545 if (Tok.getKind() != tok::comma)
546 break;
547 ConsumeToken(); // Next argument.
548 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000549 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000550
Chris Lattner89c50c62006-08-11 06:41:18 +0000551 // Match the ')'.
552 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
553 break;
554
555 case tok::arrow: // postfix-expression: p-e '->' identifier
556 case tok::period: // postfix-expression: p-e '.' identifier
557 ConsumeToken();
558 if (Tok.getKind() != tok::identifier) {
559 Diag(Tok, diag::err_expected_ident);
560 return ExprResult(true);
561 }
562 ConsumeToken();
563 break;
564
565 case tok::plusplus: // postfix-expression: postfix-expression '++'
566 case tok::minusminus: // postfix-expression: postfix-expression '--'
567 ConsumeToken();
568 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000569 }
570 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000571}
572
Chris Lattner20c6a452006-08-12 17:40:43 +0000573
Chris Lattner81b576e2006-08-11 02:13:20 +0000574/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
575/// unary-expression: [C99 6.5.3]
576/// 'sizeof' unary-expression
577/// 'sizeof' '(' type-name ')'
578/// [GNU] '__alignof' unary-expression
579/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000580Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000581 assert((Tok.getKind() == tok::kw_sizeof ||
582 Tok.getKind() == tok::kw___alignof) &&
583 "Not a sizeof/alignof expression!");
584 ConsumeToken();
585
586 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner0be454e2006-08-12 19:30:51 +0000587 if (Tok.getKind() != tok::l_paren)
Chris Lattner89c50c62006-08-11 06:41:18 +0000588 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000589
590 // If it starts with a '(', we know that it is either a parenthesized
591 // type-name, or it is a unary-expression that starts with a compound literal,
592 // or starts with a primary-expression that is a parenthesized expression.
593 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000594 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000595}
596
Chris Lattner11124352006-08-12 19:16:08 +0000597/// ParseBuiltinPrimaryExpression
598///
599/// primary-expression: [C99 6.5.1]
600/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
601/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
602/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
603/// assign-expr ')'
604/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
605///
606/// [GNU] offsetof-member-designator:
607/// [GNU] identifier
608/// [GNU] offsetof-member-designator '.' identifier
609/// [GNU] offsetof-member-designator '[' expression ']'
610///
611Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
612 ExprResult Res(false);
613 SourceLocation StartLoc = Tok.getLocation();
614 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
615
616 tok::TokenKind T = Tok.getKind();
617 ConsumeToken(); // Eat the builtin identifier.
618
619 // All of these start with an open paren.
620 if (Tok.getKind() != tok::l_paren) {
621 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
622 return ExprResult(true);
623 }
624
625 SourceLocation LParenLoc = Tok.getLocation();
626 ConsumeParen();
627
628 switch (T) {
629 default: assert(0 && "Not a builtin primary expression!");
630 case tok::kw___builtin_va_arg:
631 Res = ParseAssignmentExpression();
632 if (Res.isInvalid) {
633 SkipUntil(tok::r_paren);
634 return Res;
635 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000636
637 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000638 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000639
Chris Lattner11124352006-08-12 19:16:08 +0000640 ParseTypeName();
641 break;
642
643 case tok::kw___builtin_offsetof:
644 ParseTypeName();
645
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000646 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000647 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000648
649 // We must have at least one identifier here.
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000650 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident,
651 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000652 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000653
Chris Lattner11124352006-08-12 19:16:08 +0000654 while (1) {
655 if (Tok.getKind() == tok::period) {
656 // offsetof-member-designator: offsetof-member-designator '.' identifier
657 ConsumeToken();
658
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000659 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident,
660 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000661 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000662 } else if (Tok.getKind() == tok::l_square) {
663 // offsetof-member-designator: offsetof-member-design '[' expression ']'
664 SourceLocation LSquareLoc = Tok.getLocation();
665 ConsumeBracket();
666 Res = ParseExpression();
667 if (Res.isInvalid) {
668 SkipUntil(tok::r_paren);
669 return Res;
670 }
671
672 MatchRHSPunctuation(tok::r_square, LSquareLoc, "[",
673 diag::err_expected_rsquare);
674 } else {
675 break;
676 }
677 }
678 break;
679 case tok::kw___builtin_choose_expr:
680 Res = ParseAssignmentExpression();
681
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000682 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000683 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000684
685 Res = ParseAssignmentExpression();
686
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000687 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000688 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000689
690 Res = ParseAssignmentExpression();
691 break;
692 case tok::kw___builtin_types_compatible_p:
693 ParseTypeName();
694
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000695 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000696 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000697
698 ParseTypeName();
699 break;
700 }
701
702 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(",
703 diag::err_expected_rparen);
704
705 // These can be followed by postfix-expr pieces because they are
706 // primary-expressions.
707 return ParsePostfixExpressionSuffix(Res);
708}
709
Chris Lattner52a99e52006-08-10 20:56:00 +0000710/// ParseStringLiteralExpression - This handles the various token types that
711/// form string literals, and also handles string concatenation [C99 5.1.1.2,
712/// translation phase #6].
713///
714/// primary-expression: [C99 6.5.1]
715/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000716Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000717 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000718 ConsumeStringToken();
719
720 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
721 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000722 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000723 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000724 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000725}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000726
Chris Lattnerc951dae2006-08-10 04:23:57 +0000727
Chris Lattner4add4e62006-08-11 01:33:00 +0000728/// ParseParenExpression - This parses the unit that starts with a '(' token,
729/// based on what is allowed by ExprType. The actual thing parsed is returned
730/// in ExprType.
731///
732/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000733/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000734/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
735/// postfix-expression: [C99 6.5.2]
736/// '(' type-name ')' '{' initializer-list '}'
737/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000738/// cast-expression: [C99 6.5.4]
739/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000740///
Chris Lattner89c50c62006-08-11 06:41:18 +0000741Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000742 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
743 SourceLocation OpenLoc = Tok.getLocation();
744 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000745 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000746
Chris Lattner4add4e62006-08-11 01:33:00 +0000747 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000748 !getLang().NoExtensions) {
749 Diag(Tok, diag::ext_gnu_statement_expr);
750 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000751 ExprType = CompoundStmt;
752 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000753 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000754 ParseTypeName();
755
756 // Match the ')'.
757 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
758
Chris Lattner4add4e62006-08-11 01:33:00 +0000759 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000760 if (!getLang().C99) // Compound literals don't exist in C90.
761 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000762 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000763 ExprType = CompoundLiteral;
764 } else if (ExprType == CastExpr) {
765 // Note that this doesn't parse the subsequence cast-expression.
766 ExprType = CastExpr;
767 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000768 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000769 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000770 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000771 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000772 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000773 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000774 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000775 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000776
Chris Lattner4564bc12006-08-10 23:14:52 +0000777 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000778 if (Result.isInvalid)
779 SkipUntil(tok::r_paren);
780 else
781 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
782 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000783}