blob: 552e05acdcab65046813f998165589f5404a212e [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 Lattner62591722006-08-12 18:40:58 +0000236/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
237/// used in contexts where we have already consumed a '*' (which we saved in
238/// 'Tok'), then discovered that the '*' was really the leading token of an
239/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
240/// now in 'Tok') and the current token is "(".
241Parser::ExprResult Parser::
242ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
243 // We know that 'Tok' must correspond to this production:
244 // unary-expression: unary-operator cast-expression
245 // where 'unary-operator' is '*'.
246
247 // Parse the cast-expression that follows the '*'. This will parse the
248 // "*(int*)P" part of "*(int*)P+B".
249 ExprResult Res = ParseCastExpression(false);
250 if (Res.isInvalid) return Res;
251
252 // TODO: Combine Tok + Res to get the new AST.
253
254 // We have to parse an entire cast-expression before starting the
255 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
256 // we know that the only production above us is the cast-expression
257 // production, and because the only alternative productions start with a '('
258 // token (we know we had a '*'), there is no work to do to get a whole
259 // cast-expression.
260
261 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
262 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
263 // trailing operators (e.g. "+" in this example) and connected chunks of the
264 // assignment-expression.
265 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
266}
267
268
Chris Lattnercde626a2006-08-12 08:13:25 +0000269/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
270/// LHS and has a precedence of at least MinPrec.
271Parser::ExprResult
272Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
273 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
274
275 while (1) {
276 // If this token has a lower precedence than we are allowed to parse (e.g.
277 // because we are called recursively, or because the token is not a binop),
278 // then we are done!
279 if (NextTokPrec < MinPrec)
280 return LHS;
281
282 // Consume the operator, saving the operator token for error reporting.
283 LexerToken OpToken = Tok;
284 ConsumeToken();
285
Chris Lattner96c3deb2006-08-12 17:13:08 +0000286 // Special case handling for the ternary operator.
287 ExprResult TernaryMiddle;
288 if (NextTokPrec == prec::Conditional) {
289 if (Tok.getKind() != tok::colon) {
290 // Handle this production specially:
291 // logical-OR-expression '?' expression ':' conditional-expression
292 // In particular, the RHS of the '?' is 'expression', not
293 // 'logical-OR-expression' as we might expect.
294 TernaryMiddle = ParseExpression();
295 if (TernaryMiddle.isInvalid) return TernaryMiddle;
296 } else {
297 // Special case handling of "X ? Y : Z" where Y is empty:
298 // logical-OR-expression '?' ':' conditional-expression [GNU]
299 TernaryMiddle = ExprResult(false);
300 Diag(Tok, diag::ext_gnu_conditional_expr);
301 }
302
303 if (Tok.getKind() != tok::colon) {
304 Diag(Tok, diag::err_expected_colon);
305 Diag(OpToken, diag::err_matching, "?");
306 return ExprResult(true);
307 }
308
309 // Eat the colon.
310 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000311 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000312
313 // Parse another leaf here for the RHS of the operator.
314 ExprResult RHS = ParseCastExpression(false);
315 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000316
317 // Remember the precedence of this operator and get the precedence of the
318 // operator immediately to the right of the RHS.
319 unsigned ThisPrec = NextTokPrec;
320 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000321
322 // Assignment and conditional expressions are right-associative.
323 bool isRightAssoc = NextTokPrec == prec::Conditional ||
324 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000325
326 // Get the precedence of the operator to the right of the RHS. If it binds
327 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000328 if (ThisPrec < NextTokPrec ||
329 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000330 // If this is left-associative, only parse things on the RHS that bind
331 // more tightly than the current operator. If it is left-associative, it
332 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
333 // A=(B=(C=D)), where each paren is a level of recursion here.
334 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000335 if (RHS.isInvalid) return RHS;
336
337 NextTokPrec = getBinOpPrecedence(Tok.getKind());
338 }
339 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
340
Chris Lattner96c3deb2006-08-12 17:13:08 +0000341 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnercde626a2006-08-12 08:13:25 +0000342 }
343}
344
Chris Lattnereaf06592006-08-11 02:02:23 +0000345/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
346/// true, parse a unary-expression.
347///
Chris Lattner4564bc12006-08-10 23:14:52 +0000348/// cast-expression: [C99 6.5.4]
349/// unary-expression
350/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000351///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000352/// unary-expression: [C99 6.5.3]
353/// postfix-expression
354/// '++' unary-expression
355/// '--' unary-expression
356/// unary-operator cast-expression
357/// 'sizeof' unary-expression
358/// 'sizeof' '(' type-name ')'
359/// [GNU] '__alignof' unary-expression
360/// [GNU] '__alignof' '(' type-name ')'
361/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000362///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000363/// unary-operator: one of
364/// '&' '*' '+' '-' '~' '!'
365/// [GNU] '__extension__' '__real' '__imag'
366///
Chris Lattner52a99e52006-08-10 20:56:00 +0000367/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000368/// identifier
369/// constant
370/// string-literal
371/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000372/// '__func__' [C99 6.4.2.2]
373/// [GNU] '__FUNCTION__'
374/// [GNU] '__PRETTY_FUNCTION__'
375/// [GNU] '(' compound-statement ')'
376/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
377/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
378/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
379/// assign-expr ')'
380/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
381/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
382/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
383/// [OBC] '@protocol' '(' identifier ')' [TODO]
384/// [OBC] '@encode' '(' type-name ')' [TODO]
385/// [OBC] objc-string-literal [TODO]
386///
387/// constant: [C99 6.4.4]
388/// integer-constant
389/// floating-constant
390/// enumeration-constant -> identifier
391/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000392///
Chris Lattner89c50c62006-08-11 06:41:18 +0000393Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
394 ExprResult Res;
395
Chris Lattner81b576e2006-08-11 02:13:20 +0000396 // This handles all of cast-expression, unary-expression, postfix-expression,
397 // and primary-expression. We handle them together like this for efficiency
398 // and to simplify handling of an expression starting with a '(' token: which
399 // may be one of a parenthesized expression, cast-expression, compound literal
400 // expression, or statement expression.
401 //
402 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000403 // call ParsePostfixExpressionSuffix to handle the postfix expression
404 // suffixes. Cases that cannot be followed by postfix exprs should
405 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000406 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000407 case tok::l_paren:
408 // If this expression is limited to being a unary-expression, the parent can
409 // not start a cast expression.
410 ParenParseOption ParenExprType =
411 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000412 Res = ParseParenExpression(ParenExprType);
413 if (Res.isInvalid) return Res;
414
Chris Lattner81b576e2006-08-11 02:13:20 +0000415 switch (ParenExprType) {
416 case SimpleExpr: break; // Nothing else to do.
417 case CompoundStmt: break; // Nothing else to do.
418 case CompoundLiteral:
419 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
420 // postfix-expression exist, parse them now.
421 break;
422 case CastExpr:
423 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
424 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000425 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000426 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000427
428 // These can be followed by postfix-expr pieces.
429 return ParsePostfixExpressionSuffix(Res);
Chris Lattner89c50c62006-08-11 06:41:18 +0000430
Chris Lattner52a99e52006-08-10 20:56:00 +0000431 // primary-expression
432 case tok::identifier: // primary-expression: identifier
433 // constant: enumeration-constant
434 case tok::numeric_constant: // constant: integer-constant
435 // constant: floating-constant
436 case tok::char_constant: // constant: character-constant
437 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
438 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
439 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner20c6a452006-08-12 17:40:43 +0000440 Res = ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000441 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000442 // These can be followed by postfix-expr pieces.
443 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000444 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000445 Res = ParseStringLiteralExpression();
446 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000447 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
448 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000449 case tok::kw___builtin_va_arg:
450 case tok::kw___builtin_offsetof:
451 case tok::kw___builtin_choose_expr:
452 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000453 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000454 case tok::plusplus: // unary-expression: '++' unary-expression
455 case tok::minusminus: // unary-expression: '--' unary-expression
456 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000457 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000458 case tok::amp: // unary-expression: '&' cast-expression
459 case tok::star: // unary-expression: '*' cast-expression
460 case tok::plus: // unary-expression: '+' cast-expression
461 case tok::minus: // unary-expression: '-' cast-expression
462 case tok::tilde: // unary-expression: '~' cast-expression
463 case tok::exclaim: // unary-expression: '!' cast-expression
464 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
465 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
466 //case tok::kw__extension__: [TODO]
467 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000468 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000469
470 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
471 // unary-expression: 'sizeof' '(' type-name ')'
472 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
473 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000474 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000475 case tok::ampamp: // unary-expression: '&&' identifier
476 Diag(Tok, diag::ext_gnu_address_of_label);
477 ConsumeToken();
478 if (Tok.getKind() == tok::identifier) {
479 ConsumeToken();
480 } else {
481 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000482 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000483 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000484 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000485 default:
486 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000487 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000488 }
489
Chris Lattner20c6a452006-08-12 17:40:43 +0000490 // unreachable.
491 abort();
492}
493
494/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
495/// is parsed, this method parses any suffixes that apply.
496///
497/// postfix-expression: [C99 6.5.2]
498/// primary-expression
499/// postfix-expression '[' expression ']'
500/// postfix-expression '(' argument-expression-list[opt] ')'
501/// postfix-expression '.' identifier
502/// postfix-expression '->' identifier
503/// postfix-expression '++'
504/// postfix-expression '--'
505/// '(' type-name ')' '{' initializer-list '}'
506/// '(' type-name ')' '{' initializer-list ',' '}'
507///
508/// argument-expression-list: [C99 6.5.2]
509/// argument-expression
510/// argument-expression-list ',' assignment-expression
511///
512Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
513 assert(!LHS.isInvalid && "LHS is invalid already!");
514
Chris Lattnerf8339772006-08-10 22:01:51 +0000515 // Now that the primary-expression piece of the postfix-expression has been
516 // parsed, see if there are any postfix-expression pieces here.
517 SourceLocation Loc;
518 while (1) {
519 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000520 default: // Not a postfix-expression suffix.
521 return LHS;
Chris Lattner89c50c62006-08-11 06:41:18 +0000522 case tok::l_square: // postfix-expression: p-e '[' expression ']'
523 Loc = Tok.getLocation();
524 ConsumeBracket();
525 ParseExpression();
526 // Match the ']'.
527 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
528 break;
529
530 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
531 Loc = Tok.getLocation();
532 ConsumeParen();
533
Chris Lattner0c6c0342006-08-12 18:12:45 +0000534 if (Tok.getKind() != tok::r_paren) {
535 while (1) {
536 ParseAssignmentExpression();
537 if (Tok.getKind() != tok::comma)
538 break;
539 ConsumeToken(); // Next argument.
540 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000541 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000542
Chris Lattner89c50c62006-08-11 06:41:18 +0000543 // Match the ')'.
544 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
545 break;
546
547 case tok::arrow: // postfix-expression: p-e '->' identifier
548 case tok::period: // postfix-expression: p-e '.' identifier
549 ConsumeToken();
550 if (Tok.getKind() != tok::identifier) {
551 Diag(Tok, diag::err_expected_ident);
552 return ExprResult(true);
553 }
554 ConsumeToken();
555 break;
556
557 case tok::plusplus: // postfix-expression: postfix-expression '++'
558 case tok::minusminus: // postfix-expression: postfix-expression '--'
559 ConsumeToken();
560 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000561 }
562 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000563}
564
Chris Lattner20c6a452006-08-12 17:40:43 +0000565
Chris Lattner81b576e2006-08-11 02:13:20 +0000566/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
567/// unary-expression: [C99 6.5.3]
568/// 'sizeof' unary-expression
569/// 'sizeof' '(' type-name ')'
570/// [GNU] '__alignof' unary-expression
571/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000572Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000573 assert((Tok.getKind() == tok::kw_sizeof ||
574 Tok.getKind() == tok::kw___alignof) &&
575 "Not a sizeof/alignof expression!");
576 ConsumeToken();
577
578 // If the operand doesn't start with an '(', it must be an expression.
579 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000580 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000581 }
582
583 // If it starts with a '(', we know that it is either a parenthesized
584 // type-name, or it is a unary-expression that starts with a compound literal,
585 // or starts with a primary-expression that is a parenthesized expression.
586 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000587 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000588}
589
Chris Lattner11124352006-08-12 19:16:08 +0000590/// ParseBuiltinPrimaryExpression
591///
592/// primary-expression: [C99 6.5.1]
593/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
594/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
595/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
596/// assign-expr ')'
597/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
598///
599/// [GNU] offsetof-member-designator:
600/// [GNU] identifier
601/// [GNU] offsetof-member-designator '.' identifier
602/// [GNU] offsetof-member-designator '[' expression ']'
603///
604Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
605 ExprResult Res(false);
606 SourceLocation StartLoc = Tok.getLocation();
607 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
608
609 tok::TokenKind T = Tok.getKind();
610 ConsumeToken(); // Eat the builtin identifier.
611
612 // All of these start with an open paren.
613 if (Tok.getKind() != tok::l_paren) {
614 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
615 return ExprResult(true);
616 }
617
618 SourceLocation LParenLoc = Tok.getLocation();
619 ConsumeParen();
620
621 switch (T) {
622 default: assert(0 && "Not a builtin primary expression!");
623 case tok::kw___builtin_va_arg:
624 Res = ParseAssignmentExpression();
625 if (Res.isInvalid) {
626 SkipUntil(tok::r_paren);
627 return Res;
628 }
629 if (Tok.getKind() != tok::comma) {
630 Diag(Tok, diag::err_expected_comma);
631 SkipUntil(tok::r_paren);
632 return ExprResult(true);
633 }
634 ConsumeToken();
635
636 ParseTypeName();
637 break;
638
639 case tok::kw___builtin_offsetof:
640 ParseTypeName();
641
642 if (Tok.getKind() != tok::comma) {
643 Diag(Tok, diag::err_expected_comma);
644 SkipUntil(tok::r_paren);
645 return ExprResult(true);
646 }
647 ConsumeToken();
648
649 // We must have at least one identifier here.
650 if (Tok.getKind() != tok::identifier) {
651 Diag(Tok, diag::err_expected_ident);
652 SkipUntil(tok::r_paren);
653 return ExprResult(true);
654 }
655 ConsumeToken();
656
657 while (1) {
658 if (Tok.getKind() == tok::period) {
659 // offsetof-member-designator: offsetof-member-designator '.' identifier
660 ConsumeToken();
661
662 if (Tok.getKind() != tok::identifier) {
663 Diag(Tok, diag::err_expected_ident);
664 SkipUntil(tok::r_paren);
665 return ExprResult(true);
666 }
667 ConsumeToken();
668 } else if (Tok.getKind() == tok::l_square) {
669 // offsetof-member-designator: offsetof-member-design '[' expression ']'
670 SourceLocation LSquareLoc = Tok.getLocation();
671 ConsumeBracket();
672 Res = ParseExpression();
673 if (Res.isInvalid) {
674 SkipUntil(tok::r_paren);
675 return Res;
676 }
677
678 MatchRHSPunctuation(tok::r_square, LSquareLoc, "[",
679 diag::err_expected_rsquare);
680 } else {
681 break;
682 }
683 }
684 break;
685 case tok::kw___builtin_choose_expr:
686 Res = ParseAssignmentExpression();
687
688 if (Tok.getKind() != tok::comma) {
689 Diag(Tok, diag::err_expected_comma);
690 SkipUntil(tok::r_paren);
691 return ExprResult(true);
692 }
693 ConsumeToken();
694
695 Res = ParseAssignmentExpression();
696
697 if (Tok.getKind() != tok::comma) {
698 Diag(Tok, diag::err_expected_comma);
699 SkipUntil(tok::r_paren);
700 return ExprResult(true);
701 }
702 ConsumeToken();
703
704 Res = ParseAssignmentExpression();
705 break;
706 case tok::kw___builtin_types_compatible_p:
707 ParseTypeName();
708
709 if (Tok.getKind() != tok::comma) {
710 Diag(Tok, diag::err_expected_comma);
711 SkipUntil(tok::r_paren);
712 return ExprResult(true);
713 }
714 ConsumeToken();
715
716 ParseTypeName();
717 break;
718 }
719
720 MatchRHSPunctuation(tok::r_paren, LParenLoc, "(",
721 diag::err_expected_rparen);
722
723 // These can be followed by postfix-expr pieces because they are
724 // primary-expressions.
725 return ParsePostfixExpressionSuffix(Res);
726}
727
Chris Lattner52a99e52006-08-10 20:56:00 +0000728/// ParseStringLiteralExpression - This handles the various token types that
729/// form string literals, and also handles string concatenation [C99 5.1.1.2,
730/// translation phase #6].
731///
732/// primary-expression: [C99 6.5.1]
733/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000734Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000735 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000736 ConsumeStringToken();
737
738 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
739 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000740 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000741 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000742 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000743}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000744
Chris Lattnerc951dae2006-08-10 04:23:57 +0000745
Chris Lattner4add4e62006-08-11 01:33:00 +0000746/// ParseParenExpression - This parses the unit that starts with a '(' token,
747/// based on what is allowed by ExprType. The actual thing parsed is returned
748/// in ExprType.
749///
750/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000751/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000752/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
753/// postfix-expression: [C99 6.5.2]
754/// '(' type-name ')' '{' initializer-list '}'
755/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000756/// cast-expression: [C99 6.5.4]
757/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000758///
Chris Lattner89c50c62006-08-11 06:41:18 +0000759Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000760 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
761 SourceLocation OpenLoc = Tok.getLocation();
762 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000763 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000764
Chris Lattner4add4e62006-08-11 01:33:00 +0000765 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000766 !getLang().NoExtensions) {
767 Diag(Tok, diag::ext_gnu_statement_expr);
768 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000769 ExprType = CompoundStmt;
770 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000771 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000772 ParseTypeName();
773
774 // Match the ')'.
775 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
776
Chris Lattner4add4e62006-08-11 01:33:00 +0000777 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000778 if (!getLang().C99) // Compound literals don't exist in C90.
779 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000780 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000781 ExprType = CompoundLiteral;
782 } else if (ExprType == CastExpr) {
783 // Note that this doesn't parse the subsequence cast-expression.
784 ExprType = CastExpr;
785 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000786 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000787 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000788 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000789 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000790 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000791 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000792 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000793 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000794
Chris Lattner4564bc12006-08-10 23:14:52 +0000795 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000796 if (Result.isInvalid)
797 SkipUntil(tok::r_paren);
798 else
799 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
800 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000801}