blob: 9a22991fbc3266b938fec8bfe16b63f370c5b5c6 [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
16// operator (e.g. '/') or a trinary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
23#include "clang/Basic/Diagnostic.h"
24using namespace llvm;
25using namespace clang;
26
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000027// C99 6.7.8
Chris Lattner89c50c62006-08-11 06:41:18 +000028Parser::ExprResult Parser::ParseInitializer() {
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000029 // FIXME: STUB.
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030 if (Tok.getKind() == tok::l_brace) {
31 ConsumeBrace();
Chris Lattnera092cd1f2006-08-11 01:38:28 +000032
33 if (Tok.getKind() == tok::numeric_constant)
34 ConsumeToken();
35
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036 // FIXME: initializer-list
37 // Match the '}'.
38 MatchRHSPunctuation(tok::r_brace, Tok.getLocation(), "{",
39 diag::err_expected_rbrace);
Chris Lattner89c50c62006-08-11 06:41:18 +000040 return ExprResult(false);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000041 }
42
Chris Lattner89c50c62006-08-11 06:41:18 +000043 return ParseAssignmentExpression();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000044}
45
46
47
Chris Lattner89c50c62006-08-11 06:41:18 +000048Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +000049 return ParseBinaryExpression();
Chris Lattnerc951dae2006-08-10 04:23:57 +000050}
51
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000052// Expr that doesn't include commas.
Chris Lattner89c50c62006-08-11 06:41:18 +000053Parser::ExprResult Parser::ParseAssignmentExpression() {
54 return ParseExpression();
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000055}
56
Chris Lattnercde626a2006-08-12 08:13:25 +000057/// PrecedenceLevels - These are precedences for the binary/trinary operators in
58/// the C99 grammar. These have been named to relate with the C99 grammar
59/// productions. Low precedences numbers bind more weakly than high numbers.
60namespace prec {
61 enum Level {
62 Unknown = 0, // Not binary operator.
63 Comma = 1, // ,
64 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
65 Conditional = 3, // ?
66 LogicalOr = 4, // ||
67 LogicalAnd = 5, // &&
68 InclusiveOr = 6, // |
69 ExclusiveOr = 7, // ^
70 And = 8, // &
71 MinMax = 9, // <?, >? min, max (GCC extensions)
72 Equality = 10, // ==, !=
73 Relational = 11, // >=, <=, >, <
74 Shift = 12, // <<, >>
75 Additive = 13, // -, +
76 Multiplicative = 14 // *, /, %
77 };
78}
79
80
81/// getBinOpPrecedence - Return the precedence of the specified binary operator
82/// token. This returns:
83///
84static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
85 switch (Kind) {
86 default: return prec::Unknown;
87 case tok::comma: return prec::Comma;
88 case tok::equal:
89 case tok::starequal:
90 case tok::slashequal:
91 case tok::percentequal:
92 case tok::plusequal:
93 case tok::minusequal:
94 case tok::lesslessequal:
95 case tok::greatergreaterequal:
96 case tok::ampequal:
97 case tok::caretequal:
98 case tok::pipeequal: return prec::Assignment;
99 case tok::question: return prec::Conditional;
100 case tok::pipepipe: return prec::LogicalOr;
101 case tok::ampamp: return prec::LogicalAnd;
102 case tok::pipe: return prec::InclusiveOr;
103 case tok::caret: return prec::ExclusiveOr;
104 case tok::amp: return prec::And;
105 case tok::lessquestion:
106 case tok::greaterquestion: return prec::MinMax;
107 case tok::exclaimequal:
108 case tok::equalequal: return prec::Equality;
109 case tok::lessequal:
110 case tok::less:
111 case tok::greaterequal:
112 case tok::greater: return prec::Relational;
113 case tok::lessless:
114 case tok::greatergreater: return prec::Shift;
115 case tok::plus:
116 case tok::minus: return prec::Additive;
117 case tok::percent:
118 case tok::slash:
119 case tok::star: return prec::Multiplicative;
120 }
121}
122
123
124/// ParseBinaryExpression - Simple precedence-based parser for binary/trinary
125/// operators.
126///
127/// 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///
191Parser::ExprResult Parser::ParseBinaryExpression() {
192 ExprResult LHS = ParseCastExpression(false);
193 if (LHS.isInvalid) return LHS;
194
195 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
196}
197
198/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
199/// LHS and has a precedence of at least MinPrec.
200Parser::ExprResult
201Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
202 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
203
204 while (1) {
205 // If this token has a lower precedence than we are allowed to parse (e.g.
206 // because we are called recursively, or because the token is not a binop),
207 // then we are done!
208 if (NextTokPrec < MinPrec)
209 return LHS;
210
211 // Consume the operator, saving the operator token for error reporting.
212 LexerToken OpToken = Tok;
213 ConsumeToken();
214
215 // Parse the RHS of the operator.
216 ExprResult RHS;
217
218 // Special case handling of "X ? Y : Z" were Y is empty. This is a GCC
219 // extension.
220 if (OpToken.getKind() != tok::question || Tok.getKind() != tok::colon) {
221 RHS = ParseCastExpression(false);
222 if (RHS.isInvalid) return RHS;
223 } else {
224 RHS = ExprResult(false);
225 Diag(Tok, diag::ext_gnu_conditional_expr);
226 }
227
228 // Remember the precedence of this operator and get the precedence of the
229 // operator immediately to the right of the RHS.
230 unsigned ThisPrec = NextTokPrec;
231 NextTokPrec = getBinOpPrecedence(Tok.getKind());
232
233 // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE.
234 // FIXME: do we want to handle assignment here??
235 // ASSIGNMENT: Parse LHS as conditional expr, then catch errors in semantic
236 // analysis.
237 bool isRightAssoc = OpToken.getKind() == tok::question;
238
239 // Get the precedence of the operator to the right of the RHS. If it binds
240 // more tightly with RHS than we do, evaluate it completely first.
241
242 // FIXME: Is this enough for '?:' operators? The second term is suppsed to
243 // be 'expression', not 'assignment'.
244 if (ThisPrec < NextTokPrec ||
245 (ThisPrec == NextTokPrec && isRightAssoc)) {
246 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1);
247 if (RHS.isInvalid) return RHS;
248
249 NextTokPrec = getBinOpPrecedence(Tok.getKind());
250 }
251 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
252
253 // Handle the special case of our one trinary operator here.
254 if (OpToken.getKind() == tok::question) {
255 if (Tok.getKind() != tok::colon) {
256 Diag(Tok, diag::err_expected_colon);
257 Diag(OpToken, diag::err_matching, "?");
258 return ExprResult(true);
259 }
260
261 // Eat the colon.
262 ConsumeToken();
263
264 // Parse the value of the colon.
265 ExprResult AfterColonVal = ParseCastExpression(false);
266 if (AfterColonVal.isInvalid) return AfterColonVal;
267
268 // Parse anything after the RRHS that has a higher precedence than ?.
269 AfterColonVal = ParseRHSOfBinaryExpression(AfterColonVal, ThisPrec+1);
270 if (AfterColonVal.isInvalid) return AfterColonVal;
271
272 // TODO: Combine LHS = LHS ? RHS : AfterColonVal.
273
274 // Figure out the precedence of the token after the : part.
275 NextTokPrec = getBinOpPrecedence(Tok.getKind());
276 } else {
277 // TODO: combine the LHS and RHS into the LHS (e.g. build AST).
278 }
279 }
280}
281
282
Chris Lattnereaf06592006-08-11 02:02:23 +0000283/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
284/// true, parse a unary-expression.
285///
Chris Lattner4564bc12006-08-10 23:14:52 +0000286/// cast-expression: [C99 6.5.4]
287/// unary-expression
288/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000289///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000290/// unary-expression: [C99 6.5.3]
291/// postfix-expression
292/// '++' unary-expression
293/// '--' unary-expression
294/// unary-operator cast-expression
295/// 'sizeof' unary-expression
296/// 'sizeof' '(' type-name ')'
297/// [GNU] '__alignof' unary-expression
298/// [GNU] '__alignof' '(' type-name ')'
299/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000300///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000301/// unary-operator: one of
302/// '&' '*' '+' '-' '~' '!'
303/// [GNU] '__extension__' '__real' '__imag'
304///
Chris Lattner52a99e52006-08-10 20:56:00 +0000305/// postfix-expression: [C99 6.5.2]
306/// primary-expression
307/// postfix-expression '[' expression ']'
308/// postfix-expression '(' argument-expression-list[opt] ')'
309/// postfix-expression '.' identifier
310/// postfix-expression '->' identifier
311/// postfix-expression '++'
312/// postfix-expression '--'
313/// '(' type-name ')' '{' initializer-list '}'
314/// '(' type-name ')' '{' initializer-list ',' '}'
315///
316/// argument-expression-list: [C99 6.5.2]
317/// argument-expression
318/// argument-expression-list ',' argument-expression
319///
320/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000321/// identifier
322/// constant
323/// string-literal
324/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000325/// '__func__' [C99 6.4.2.2]
326/// [GNU] '__FUNCTION__'
327/// [GNU] '__PRETTY_FUNCTION__'
328/// [GNU] '(' compound-statement ')'
329/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
330/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
331/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
332/// assign-expr ')'
333/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
334/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
335/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
336/// [OBC] '@protocol' '(' identifier ')' [TODO]
337/// [OBC] '@encode' '(' type-name ')' [TODO]
338/// [OBC] objc-string-literal [TODO]
339///
340/// constant: [C99 6.4.4]
341/// integer-constant
342/// floating-constant
343/// enumeration-constant -> identifier
344/// character-constant
345///
346/// [GNU] offsetof-member-designator:
347/// [GNU] identifier
348/// [GNU] offsetof-member-designator '.' identifier
349/// [GNU] offsetof-member-designator '[' expression ']'
350///
Chris Lattner81b576e2006-08-11 02:13:20 +0000351///
Chris Lattner89c50c62006-08-11 06:41:18 +0000352Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
353 ExprResult Res;
354
Chris Lattner81b576e2006-08-11 02:13:20 +0000355 // This handles all of cast-expression, unary-expression, postfix-expression,
356 // and primary-expression. We handle them together like this for efficiency
357 // and to simplify handling of an expression starting with a '(' token: which
358 // may be one of a parenthesized expression, cast-expression, compound literal
359 // expression, or statement expression.
360 //
361 // If the parsed tokens consist of a primary-expression, the cases below
362 // 'break' out of the switch. This allows the postfix expression pieces to
363 // be applied to them. Cases that cannot be followed by postfix exprs should
364 // return instead.
Chris Lattner52a99e52006-08-10 20:56:00 +0000365 switch (Tok.getKind()) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000366 case tok::l_paren:
367 // If this expression is limited to being a unary-expression, the parent can
368 // not start a cast expression.
369 ParenParseOption ParenExprType =
370 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000371 Res = ParseParenExpression(ParenExprType);
372 if (Res.isInvalid) return Res;
373
Chris Lattner81b576e2006-08-11 02:13:20 +0000374 switch (ParenExprType) {
375 case SimpleExpr: break; // Nothing else to do.
376 case CompoundStmt: break; // Nothing else to do.
377 case CompoundLiteral:
378 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
379 // postfix-expression exist, parse them now.
380 break;
381 case CastExpr:
382 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
383 // the cast-expression that follows it next.
Chris Lattner89c50c62006-08-11 06:41:18 +0000384 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000385 }
386 break; // These can be followed by postfix-expr pieces.
Chris Lattner89c50c62006-08-11 06:41:18 +0000387
Chris Lattner52a99e52006-08-10 20:56:00 +0000388 // primary-expression
389 case tok::identifier: // primary-expression: identifier
390 // constant: enumeration-constant
391 case tok::numeric_constant: // constant: integer-constant
392 // constant: floating-constant
393 case tok::char_constant: // constant: character-constant
394 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
395 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
396 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
397 ConsumeToken();
398 break;
399 case tok::string_literal: // primary-expression: string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000400 Res = ParseStringLiteralExpression();
401 if (Res.isInvalid) return Res;
Chris Lattner52a99e52006-08-10 20:56:00 +0000402 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000403 case tok::kw___builtin_va_arg:
404 case tok::kw___builtin_offsetof:
405 case tok::kw___builtin_choose_expr:
406 case tok::kw___builtin_types_compatible_p:
407 assert(0 && "FIXME: UNIMP!");
Chris Lattner81b576e2006-08-11 02:13:20 +0000408 case tok::plusplus: // unary-expression: '++' unary-expression
409 case tok::minusminus: // unary-expression: '--' unary-expression
410 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000411 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000412 case tok::amp: // unary-expression: '&' cast-expression
413 case tok::star: // unary-expression: '*' cast-expression
414 case tok::plus: // unary-expression: '+' cast-expression
415 case tok::minus: // unary-expression: '-' cast-expression
416 case tok::tilde: // unary-expression: '~' cast-expression
417 case tok::exclaim: // unary-expression: '!' cast-expression
418 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
419 case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
420 //case tok::kw__extension__: [TODO]
421 ConsumeToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000422 return ParseCastExpression(false);
Chris Lattner81b576e2006-08-11 02:13:20 +0000423
424 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
425 // unary-expression: 'sizeof' '(' type-name ')'
426 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
427 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000428 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000429 case tok::ampamp: // unary-expression: '&&' identifier
430 Diag(Tok, diag::ext_gnu_address_of_label);
431 ConsumeToken();
432 if (Tok.getKind() == tok::identifier) {
433 ConsumeToken();
434 } else {
435 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000436 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000437 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000438 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000439 default:
440 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000441 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000442 }
443
444 // Now that the primary-expression piece of the postfix-expression has been
445 // parsed, see if there are any postfix-expression pieces here.
446 SourceLocation Loc;
447 while (1) {
448 switch (Tok.getKind()) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000449 default:
450 return ExprResult(false);
451 case tok::l_square: // postfix-expression: p-e '[' expression ']'
452 Loc = Tok.getLocation();
453 ConsumeBracket();
454 ParseExpression();
455 // Match the ']'.
456 MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
457 break;
458
459 case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
460 Loc = Tok.getLocation();
461 ConsumeParen();
462
463 while (1) {
464 // FIXME: This should be argument-expression!
465 ParseAssignmentExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000466
Chris Lattner89c50c62006-08-11 06:41:18 +0000467 if (Tok.getKind() != tok::comma)
468 break;
469 ConsumeToken(); // Next argument.
470 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000471
Chris Lattner89c50c62006-08-11 06:41:18 +0000472 // Match the ')'.
473 MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
474 break;
475
476 case tok::arrow: // postfix-expression: p-e '->' identifier
477 case tok::period: // postfix-expression: p-e '.' identifier
478 ConsumeToken();
479 if (Tok.getKind() != tok::identifier) {
480 Diag(Tok, diag::err_expected_ident);
481 return ExprResult(true);
482 }
483 ConsumeToken();
484 break;
485
486 case tok::plusplus: // postfix-expression: postfix-expression '++'
487 case tok::minusminus: // postfix-expression: postfix-expression '--'
488 ConsumeToken();
489 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000490 }
491 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000492}
493
Chris Lattner81b576e2006-08-11 02:13:20 +0000494/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
495/// unary-expression: [C99 6.5.3]
496/// 'sizeof' unary-expression
497/// 'sizeof' '(' type-name ')'
498/// [GNU] '__alignof' unary-expression
499/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000500Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000501 assert((Tok.getKind() == tok::kw_sizeof ||
502 Tok.getKind() == tok::kw___alignof) &&
503 "Not a sizeof/alignof expression!");
504 ConsumeToken();
505
506 // If the operand doesn't start with an '(', it must be an expression.
507 if (Tok.getKind() != tok::l_paren) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000508 return ParseCastExpression(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000509 }
510
511 // If it starts with a '(', we know that it is either a parenthesized
512 // type-name, or it is a unary-expression that starts with a compound literal,
513 // or starts with a primary-expression that is a parenthesized expression.
514 ParenParseOption ExprType = CastExpr;
Chris Lattner89c50c62006-08-11 06:41:18 +0000515 return ParseParenExpression(ExprType);
Chris Lattner81b576e2006-08-11 02:13:20 +0000516}
517
Chris Lattner52a99e52006-08-10 20:56:00 +0000518/// ParseStringLiteralExpression - This handles the various token types that
519/// form string literals, and also handles string concatenation [C99 5.1.1.2,
520/// translation phase #6].
521///
522/// primary-expression: [C99 6.5.1]
523/// string-literal
Chris Lattner89c50c62006-08-11 06:41:18 +0000524Parser::ExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4564bc12006-08-10 23:14:52 +0000525 assert(isTokenStringLiteral() && "Not a string literal!");
Chris Lattner52a99e52006-08-10 20:56:00 +0000526 ConsumeStringToken();
527
528 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
529 // considered to be strings.
Chris Lattner4564bc12006-08-10 23:14:52 +0000530 while (isTokenStringLiteral())
Chris Lattner52a99e52006-08-10 20:56:00 +0000531 ConsumeStringToken();
Chris Lattner89c50c62006-08-11 06:41:18 +0000532 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000533}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000534
Chris Lattnerc951dae2006-08-10 04:23:57 +0000535
Chris Lattner4add4e62006-08-11 01:33:00 +0000536/// ParseParenExpression - This parses the unit that starts with a '(' token,
537/// based on what is allowed by ExprType. The actual thing parsed is returned
538/// in ExprType.
539///
540/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000541/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000542/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
543/// postfix-expression: [C99 6.5.2]
544/// '(' type-name ')' '{' initializer-list '}'
545/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000546/// cast-expression: [C99 6.5.4]
547/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000548///
Chris Lattner89c50c62006-08-11 06:41:18 +0000549Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000550 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
551 SourceLocation OpenLoc = Tok.getLocation();
552 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000553 ExprResult Result(false);
Chris Lattnerc951dae2006-08-10 04:23:57 +0000554
Chris Lattner4add4e62006-08-11 01:33:00 +0000555 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000556 !getLang().NoExtensions) {
557 Diag(Tok, diag::ext_gnu_statement_expr);
558 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000559 ExprType = CompoundStmt;
560 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000561 // Otherwise, this is a compound expression.
562 ParseTypeName();
563
564 // Match the ')'.
565 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
566
Chris Lattner4add4e62006-08-11 01:33:00 +0000567 if (Tok.getKind() == tok::l_brace) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000568 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000569 ExprType = CompoundLiteral;
570 } else if (ExprType == CastExpr) {
571 // Note that this doesn't parse the subsequence cast-expression.
572 ExprType = CastExpr;
573 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000574 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000575 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000576 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000577 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000578 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000579 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000580 ExprType = SimpleExpr;
Chris Lattnerf8339772006-08-10 22:01:51 +0000581 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000582
Chris Lattner4564bc12006-08-10 23:14:52 +0000583 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000584 if (Result.isInvalid)
585 SkipUntil(tok::r_paren);
586 else
587 MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
588 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000589}