blob: 645ab3307b04e234093ae5e639fbdeba737e0034 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
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"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000025#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000026using namespace llvm;
27using namespace clang;
28
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000029/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000030/// the C99 grammar. These have been named to relate with the C99 grammar
31/// productions. Low precedences numbers bind more weakly than high numbers.
32namespace prec {
33 enum Level {
34 Unknown = 0, // Not binary operator.
35 Comma = 1, // ,
36 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
37 Conditional = 3, // ?
38 LogicalOr = 4, // ||
39 LogicalAnd = 5, // &&
40 InclusiveOr = 6, // |
41 ExclusiveOr = 7, // ^
42 And = 8, // &
Chris Lattner9916c5c2006-10-27 05:24:37 +000043 Equality = 9, // ==, !=
44 Relational = 10, // >=, <=, >, <
45 Shift = 11, // <<, >>
46 Additive = 12, // -, +
47 Multiplicative = 13 // *, /, %
Chris Lattnercde626a2006-08-12 08:13:25 +000048 };
49}
50
51
52/// getBinOpPrecedence - Return the precedence of the specified binary operator
53/// token. This returns:
54///
55static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
56 switch (Kind) {
57 default: return prec::Unknown;
58 case tok::comma: return prec::Comma;
59 case tok::equal:
60 case tok::starequal:
61 case tok::slashequal:
62 case tok::percentequal:
63 case tok::plusequal:
64 case tok::minusequal:
65 case tok::lesslessequal:
66 case tok::greatergreaterequal:
67 case tok::ampequal:
68 case tok::caretequal:
69 case tok::pipeequal: return prec::Assignment;
70 case tok::question: return prec::Conditional;
71 case tok::pipepipe: return prec::LogicalOr;
72 case tok::ampamp: return prec::LogicalAnd;
73 case tok::pipe: return prec::InclusiveOr;
74 case tok::caret: return prec::ExclusiveOr;
75 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000076 case tok::exclaimequal:
77 case tok::equalequal: return prec::Equality;
78 case tok::lessequal:
79 case tok::less:
80 case tok::greaterequal:
81 case tok::greater: return prec::Relational;
82 case tok::lessless:
83 case tok::greatergreater: return prec::Shift;
84 case tok::plus:
85 case tok::minus: return prec::Additive;
86 case tok::percent:
87 case tok::slash:
88 case tok::star: return prec::Multiplicative;
89 }
90}
91
92
Chris Lattnerce7e21d2006-08-12 17:22:40 +000093/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000094/// operators.
95///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000096/// Note: we diverge from the C99 grammar when parsing the assignment-expression
97/// production. C99 specifies that the LHS of an assignment operator should be
98/// parsed as a unary-expression, but consistency dictates that it be a
99/// conditional-expession. In practice, the important thing here is that the
100/// LHS of an assignment has to be an l-value, which productions between
101/// unary-expression and conditional-expression don't produce. Because we want
102/// consistency, we parse the LHS as a conditional-expression, then check for
103/// l-value-ness in semantic analysis stages.
104///
Chris Lattnercde626a2006-08-12 08:13:25 +0000105/// multiplicative-expression: [C99 6.5.5]
106/// cast-expression
107/// multiplicative-expression '*' cast-expression
108/// multiplicative-expression '/' cast-expression
109/// multiplicative-expression '%' cast-expression
110///
111/// additive-expression: [C99 6.5.6]
112/// multiplicative-expression
113/// additive-expression '+' multiplicative-expression
114/// additive-expression '-' multiplicative-expression
115///
116/// shift-expression: [C99 6.5.7]
117/// additive-expression
118/// shift-expression '<<' additive-expression
119/// shift-expression '>>' additive-expression
120///
121/// relational-expression: [C99 6.5.8]
122/// shift-expression
123/// relational-expression '<' shift-expression
124/// relational-expression '>' shift-expression
125/// relational-expression '<=' shift-expression
126/// relational-expression '>=' shift-expression
127///
128/// equality-expression: [C99 6.5.9]
129/// relational-expression
130/// equality-expression '==' relational-expression
131/// equality-expression '!=' relational-expression
132///
133/// AND-expression: [C99 6.5.10]
134/// equality-expression
135/// AND-expression '&' equality-expression
136///
137/// exclusive-OR-expression: [C99 6.5.11]
138/// AND-expression
139/// exclusive-OR-expression '^' AND-expression
140///
141/// inclusive-OR-expression: [C99 6.5.12]
142/// exclusive-OR-expression
143/// inclusive-OR-expression '|' exclusive-OR-expression
144///
145/// logical-AND-expression: [C99 6.5.13]
146/// inclusive-OR-expression
147/// logical-AND-expression '&&' inclusive-OR-expression
148///
149/// logical-OR-expression: [C99 6.5.14]
150/// logical-AND-expression
151/// logical-OR-expression '||' logical-AND-expression
152///
153/// conditional-expression: [C99 6.5.15]
154/// logical-OR-expression
155/// logical-OR-expression '?' expression ':' conditional-expression
156/// [GNU] logical-OR-expression '?' ':' conditional-expression
157///
158/// assignment-expression: [C99 6.5.16]
159/// conditional-expression
160/// unary-expression assignment-operator assignment-expression
161///
162/// assignment-operator: one of
163/// = *= /= %= += -= <<= >>= &= ^= |=
164///
165/// expression: [C99 6.5.17]
166/// assignment-expression
167/// expression ',' assignment-expression
168///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000169Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000170 ExprResult LHS = ParseCastExpression(false);
171 if (LHS.isInvalid) return LHS;
172
173 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
174}
175
Chris Lattner0c6c0342006-08-12 18:12:45 +0000176/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
177///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000178Parser::ExprResult Parser::ParseAssignmentExpression() {
179 ExprResult LHS = ParseCastExpression(false);
180 if (LHS.isInvalid) return LHS;
181
182 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
183}
184
Chris Lattner3b561a32006-08-13 00:12:11 +0000185Parser::ExprResult Parser::ParseConstantExpression() {
186 ExprResult LHS = ParseCastExpression(false);
187 if (LHS.isInvalid) return LHS;
188
189 // TODO: Validate that this is a constant expr!
190 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
191}
192
Chris Lattner0c6c0342006-08-12 18:12:45 +0000193/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
194/// in contexts where we have already consumed an identifier (which we saved in
195/// 'Tok'), then discovered that the identifier was really the leading token of
196/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
197/// now in 'Tok') and the current token is "[".
198Parser::ExprResult Parser::
199ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
200 // We know that 'Tok' must correspond to this production:
201 // primary-expression: identifier
202
Chris Lattnereb2feef2006-11-04 19:14:32 +0000203 // Let the actions module handle the identifier.
Chris Lattner17ed4872006-11-20 04:58:19 +0000204 ExprResult Res = Actions.ParseIdentifierExpr(Tok.getLocation(),
205 *Tok.getIdentifierInfo());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000206
207 // Because we have to parse an entire cast-expression before starting the
208 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
209 // need to handle the 'postfix-expression' rules. We do this by invoking
210 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
211 Res = ParsePostfixExpressionSuffix(Res);
212 if (Res.isInvalid) return Res;
213
214 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
215 // done, we know we don't have to do anything for cast-expression, because the
216 // only non-postfix-expression production starts with a '(' token, and we know
217 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
218 // to consume any trailing operators (e.g. "+" in this example) and connected
219 // chunks of the expression.
220 return ParseRHSOfBinaryExpression(Res, prec::Comma);
221}
222
Chris Lattner8693a512006-08-13 21:54:02 +0000223/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
224/// in contexts where we have already consumed an identifier (which we saved in
225/// 'Tok'), then discovered that the identifier was really the leading token of
226/// part of an assignment-expression. For example, in "A[1]+B", we consumed "A"
227/// (which is now in 'Tok') and the current token is "[".
228Parser::ExprResult Parser::
229ParseAssignmentExprWithLeadingIdentifier(const LexerToken &Tok) {
230 // We know that 'Tok' must correspond to this production:
231 // primary-expression: identifier
232
Chris Lattnereb2feef2006-11-04 19:14:32 +0000233 // Let the actions module handle the identifier.
Chris Lattner17ed4872006-11-20 04:58:19 +0000234 ExprResult Res = Actions.ParseIdentifierExpr(Tok.getLocation(),
235 *Tok.getIdentifierInfo());
Chris Lattner8693a512006-08-13 21:54:02 +0000236
237 // Because we have to parse an entire cast-expression before starting the
238 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
239 // need to handle the 'postfix-expression' rules. We do this by invoking
240 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
241 Res = ParsePostfixExpressionSuffix(Res);
242 if (Res.isInvalid) return Res;
243
244 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
245 // done, we know we don't have to do anything for cast-expression, because the
246 // only non-postfix-expression production starts with a '(' token, and we know
247 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
248 // to consume any trailing operators (e.g. "+" in this example) and connected
249 // chunks of the expression.
250 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
251}
252
253
Chris Lattner62591722006-08-12 18:40:58 +0000254/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
255/// used in contexts where we have already consumed a '*' (which we saved in
256/// 'Tok'), then discovered that the '*' was really the leading token of an
257/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
258/// now in 'Tok') and the current token is "(".
259Parser::ExprResult Parser::
260ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
261 // We know that 'Tok' must correspond to this production:
262 // unary-expression: unary-operator cast-expression
263 // where 'unary-operator' is '*'.
264
265 // Parse the cast-expression that follows the '*'. This will parse the
266 // "*(int*)P" part of "*(int*)P+B".
267 ExprResult Res = ParseCastExpression(false);
268 if (Res.isInvalid) return Res;
269
270 // TODO: Combine Tok + Res to get the new AST.
271
272 // We have to parse an entire cast-expression before starting the
273 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
274 // we know that the only production above us is the cast-expression
275 // production, and because the only alternative productions start with a '('
276 // token (we know we had a '*'), there is no work to do to get a whole
277 // cast-expression.
278
279 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
280 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
281 // trailing operators (e.g. "+" in this example) and connected chunks of the
282 // assignment-expression.
283 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
284}
285
286
Chris Lattnercde626a2006-08-12 08:13:25 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
289Parser::ExprResult
290Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000292 SourceLocation ColonLoc;
293
Chris Lattnercde626a2006-08-12 08:13:25 +0000294 while (1) {
295 // If this token has a lower precedence than we are allowed to parse (e.g.
296 // because we are called recursively, or because the token is not a binop),
297 // then we are done!
298 if (NextTokPrec < MinPrec)
299 return LHS;
300
301 // Consume the operator, saving the operator token for error reporting.
302 LexerToken OpToken = Tok;
303 ConsumeToken();
304
Chris Lattner96c3deb2006-08-12 17:13:08 +0000305 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000306 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000307 if (NextTokPrec == prec::Conditional) {
308 if (Tok.getKind() != tok::colon) {
309 // Handle this production specially:
310 // logical-OR-expression '?' expression ':' conditional-expression
311 // In particular, the RHS of the '?' is 'expression', not
312 // 'logical-OR-expression' as we might expect.
313 TernaryMiddle = ParseExpression();
314 if (TernaryMiddle.isInvalid) return TernaryMiddle;
315 } else {
316 // Special case handling of "X ? Y : Z" where Y is empty:
317 // logical-OR-expression '?' ':' conditional-expression [GNU]
318 TernaryMiddle = ExprResult(false);
319 Diag(Tok, diag::ext_gnu_conditional_expr);
320 }
321
322 if (Tok.getKind() != tok::colon) {
323 Diag(Tok, diag::err_expected_colon);
324 Diag(OpToken, diag::err_matching, "?");
325 return ExprResult(true);
326 }
327
328 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000329 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000330 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000331
332 // Parse another leaf here for the RHS of the operator.
333 ExprResult RHS = ParseCastExpression(false);
334 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000335
336 // Remember the precedence of this operator and get the precedence of the
337 // operator immediately to the right of the RHS.
338 unsigned ThisPrec = NextTokPrec;
339 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000340
341 // Assignment and conditional expressions are right-associative.
342 bool isRightAssoc = NextTokPrec == prec::Conditional ||
343 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000344
345 // Get the precedence of the operator to the right of the RHS. If it binds
346 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000347 if (ThisPrec < NextTokPrec ||
348 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000349 // If this is left-associative, only parse things on the RHS that bind
350 // more tightly than the current operator. If it is left-associative, it
351 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
352 // A=(B=(C=D)), where each paren is a level of recursion here.
353 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000354 if (RHS.isInvalid) return RHS;
355
356 NextTokPrec = getBinOpPrecedence(Tok.getKind());
357 }
358 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
359
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000360 // Combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnerb5600a62006-10-06 05:40:05 +0000361 if (TernaryMiddle.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000362 LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(),
363 LHS.Val, RHS.Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000364 else
365 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
366 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000367 }
368}
369
Chris Lattnereaf06592006-08-11 02:02:23 +0000370/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
371/// true, parse a unary-expression.
372///
Chris Lattner4564bc12006-08-10 23:14:52 +0000373/// cast-expression: [C99 6.5.4]
374/// unary-expression
375/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000376///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000377/// unary-expression: [C99 6.5.3]
378/// postfix-expression
379/// '++' unary-expression
380/// '--' unary-expression
381/// unary-operator cast-expression
382/// 'sizeof' unary-expression
383/// 'sizeof' '(' type-name ')'
384/// [GNU] '__alignof' unary-expression
385/// [GNU] '__alignof' '(' type-name ')'
386/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000387///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000388/// unary-operator: one of
389/// '&' '*' '+' '-' '~' '!'
390/// [GNU] '__extension__' '__real' '__imag'
391///
Chris Lattner52a99e52006-08-10 20:56:00 +0000392/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000393/// identifier
394/// constant
395/// string-literal
396/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000397/// '__func__' [C99 6.4.2.2]
398/// [GNU] '__FUNCTION__'
399/// [GNU] '__PRETTY_FUNCTION__'
400/// [GNU] '(' compound-statement ')'
401/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
402/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
403/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
404/// assign-expr ')'
405/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
406/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
407/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
408/// [OBC] '@protocol' '(' identifier ')' [TODO]
409/// [OBC] '@encode' '(' type-name ')' [TODO]
410/// [OBC] objc-string-literal [TODO]
411///
412/// constant: [C99 6.4.4]
413/// integer-constant
414/// floating-constant
415/// enumeration-constant -> identifier
416/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000417///
Chris Lattner89c50c62006-08-11 06:41:18 +0000418Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
419 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000420 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000421
Chris Lattner81b576e2006-08-11 02:13:20 +0000422 // This handles all of cast-expression, unary-expression, postfix-expression,
423 // and primary-expression. We handle them together like this for efficiency
424 // and to simplify handling of an expression starting with a '(' token: which
425 // may be one of a parenthesized expression, cast-expression, compound literal
426 // expression, or statement expression.
427 //
428 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000429 // call ParsePostfixExpressionSuffix to handle the postfix expression
430 // suffixes. Cases that cannot be followed by postfix exprs should
431 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000432 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000433 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000434 // If this expression is limited to being a unary-expression, the parent can
435 // not start a cast expression.
436 ParenParseOption ParenExprType =
437 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000438 TypeTy *CastTy;
439 SourceLocation LParenLoc = Tok.getLocation();
440 SourceLocation RParenLoc;
441 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000442 if (Res.isInvalid) return Res;
443
Chris Lattner81b576e2006-08-11 02:13:20 +0000444 switch (ParenExprType) {
445 case SimpleExpr: break; // Nothing else to do.
446 case CompoundStmt: break; // Nothing else to do.
447 case CompoundLiteral:
448 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
449 // postfix-expression exist, parse them now.
450 break;
451 case CastExpr:
452 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
453 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000454 // TODO: For cast expression with CastTy.
455 Res = ParseCastExpression(false);
456 if (!Res.isInvalid)
457 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
458 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000459 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000460
461 // These can be followed by postfix-expr pieces.
462 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000463 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000464
Chris Lattner52a99e52006-08-10 20:56:00 +0000465 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000466 case tok::numeric_constant:
467 // constant: integer-constant
468 // constant: floating-constant
469
470 // TODO: Validate whether this is an integer or floating-constant or
471 // neither.
472 if (1) {
Chris Lattnerae319692006-10-25 03:49:28 +0000473 Res = Actions.ParseIntegerConstant(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000474 } else {
Chris Lattnerae319692006-10-25 03:49:28 +0000475 Res = Actions.ParseFloatingConstant(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000476 }
477 ConsumeToken();
478
479 // These can be followed by postfix-expr pieces.
480 return ParsePostfixExpressionSuffix(Res);
481
Chris Lattner52a99e52006-08-10 20:56:00 +0000482 case tok::identifier: // primary-expression: identifier
483 // constant: enumeration-constant
Chris Lattner17ed4872006-11-20 04:58:19 +0000484 Res = Actions.ParseIdentifierExpr(Tok.getLocation(),
485 *Tok.getIdentifierInfo());
486 ConsumeToken();
487 // These can be followed by postfix-expr pieces.
488 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000489 case tok::char_constant: // constant: character-constant
490 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
491 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
492 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerae319692006-10-25 03:49:28 +0000493 Res = Actions.ParseSimplePrimaryExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000494 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000495 // These can be followed by postfix-expr pieces.
496 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000497 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000498 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000499 Res = ParseStringLiteralExpression();
500 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000501 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
502 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000503 case tok::kw___builtin_va_arg:
504 case tok::kw___builtin_offsetof:
505 case tok::kw___builtin_choose_expr:
506 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000507 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000508 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000509 case tok::minusminus: { // unary-expression: '--' unary-expression
510 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000511 Res = ParseCastExpression(true);
512 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000513 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000514 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000515 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000516 case tok::amp: // unary-expression: '&' cast-expression
517 case tok::star: // unary-expression: '*' cast-expression
518 case tok::plus: // unary-expression: '+' cast-expression
519 case tok::minus: // unary-expression: '-' cast-expression
520 case tok::tilde: // unary-expression: '~' cast-expression
521 case tok::exclaim: // unary-expression: '!' cast-expression
522 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000523 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000524 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
Chris Lattner4daa0772006-10-20 05:03:44 +0000525 // FIXME: Extension not handled correctly here!
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000526 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000527 Res = ParseCastExpression(false);
528 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000529 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000530 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000531 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000532 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
533 // unary-expression: 'sizeof' '(' type-name ')'
534 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
535 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000536 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000537 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000538 Diag(Tok, diag::ext_gnu_address_of_label);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000539 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000540
541 if (Tok.getKind() != tok::identifier) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000542 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000543 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000544 }
Chris Lattner14a1b642006-10-15 22:33:58 +0000545 // FIXME: Create a label ref for Tok.Ident.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000546 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, 0);
Chris Lattner14a1b642006-10-15 22:33:58 +0000547 ConsumeToken();
548
549 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000550 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000551 default:
552 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000553 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000554 }
555
Chris Lattner20c6a452006-08-12 17:40:43 +0000556 // unreachable.
557 abort();
558}
559
560/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
561/// is parsed, this method parses any suffixes that apply.
562///
563/// postfix-expression: [C99 6.5.2]
564/// primary-expression
565/// postfix-expression '[' expression ']'
566/// postfix-expression '(' argument-expression-list[opt] ')'
567/// postfix-expression '.' identifier
568/// postfix-expression '->' identifier
569/// postfix-expression '++'
570/// postfix-expression '--'
571/// '(' type-name ')' '{' initializer-list '}'
572/// '(' type-name ')' '{' initializer-list ',' '}'
573///
574/// argument-expression-list: [C99 6.5.2]
575/// argument-expression
576/// argument-expression-list ',' assignment-expression
577///
578Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000579
Chris Lattnerf8339772006-08-10 22:01:51 +0000580 // Now that the primary-expression piece of the postfix-expression has been
581 // parsed, see if there are any postfix-expression pieces here.
582 SourceLocation Loc;
583 while (1) {
584 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000585 default: // Not a postfix-expression suffix.
586 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000587 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000588 Loc = ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000589 ExprResult Idx = ParseExpression();
590
591 SourceLocation RLoc = Tok.getLocation();
592
593 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
594 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
595 else
596 LHS = ExprResult(true);
597
Chris Lattner89c50c62006-08-11 06:41:18 +0000598 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000599 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000600 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000601 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000602
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000603 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
604 SmallVector<ExprTy*, 8> ArgExprs;
605 SmallVector<SourceLocation, 8> CommaLocs;
606 bool ArgExprsOk = true;
607
Chris Lattner04132372006-10-16 06:12:55 +0000608 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000609
Chris Lattner0c6c0342006-08-12 18:12:45 +0000610 if (Tok.getKind() != tok::r_paren) {
611 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000612 ExprResult ArgExpr = ParseAssignmentExpression();
613 if (ArgExpr.isInvalid)
614 ArgExprsOk = false;
615 else
616 ArgExprs.push_back(ArgExpr.Val);
617
Chris Lattner0c6c0342006-08-12 18:12:45 +0000618 if (Tok.getKind() != tok::comma)
619 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000620 // Move to the next argument, remember where the comma was.
621 CommaLocs.push_back(ConsumeToken());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000622 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000623 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000624
Chris Lattner89c50c62006-08-11 06:41:18 +0000625 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000626 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
627 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
628 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000629 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000630 &CommaLocs[0], Tok.getLocation());
631 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000632
Chris Lattner04f80192006-08-15 04:55:54 +0000633 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000634 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000635 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000636 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000637 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000638 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000639 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000640
Chris Lattner89c50c62006-08-11 06:41:18 +0000641 if (Tok.getKind() != tok::identifier) {
642 Diag(Tok, diag::err_expected_ident);
643 return ExprResult(true);
644 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000645
646 if (!LHS.isInvalid)
647 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
648 Tok.getLocation(),
649 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000650 ConsumeToken();
651 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000652 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000653 case tok::plusplus: // postfix-expression: postfix-expression '++'
654 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000655 if (!LHS.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000656 LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
657 LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000658 ConsumeToken();
659 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000660 }
661 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000662}
663
Chris Lattner20c6a452006-08-12 17:40:43 +0000664
Chris Lattner81b576e2006-08-11 02:13:20 +0000665/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
666/// unary-expression: [C99 6.5.3]
667/// 'sizeof' unary-expression
668/// 'sizeof' '(' type-name ')'
669/// [GNU] '__alignof' unary-expression
670/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000671Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000672 assert((Tok.getKind() == tok::kw_sizeof ||
673 Tok.getKind() == tok::kw___alignof) &&
674 "Not a sizeof/alignof expression!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000675 LexerToken OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000676 ConsumeToken();
677
678 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000679 ExprResult Operand;
680 if (Tok.getKind() != tok::l_paren) {
681 Operand = ParseCastExpression(true);
682 } else {
683 // If it starts with a '(', we know that it is either a parenthesized
684 // type-name, or it is a unary-expression that starts with a compound
685 // literal, or starts with a primary-expression that is a parenthesized
686 // expression.
687 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000688 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000689 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000690 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000691
692 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
693 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
694 if (ExprType == CastExpr) {
Chris Lattner26da7302006-08-24 06:49:19 +0000695 return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
696 OpTok.getKind() == tok::kw_sizeof,
697 LParenLoc, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000698 }
699 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000700
Chris Lattner26115ac2006-08-24 06:10:04 +0000701 // If we get here, the operand to the sizeof/alignof was an expresion.
702 if (!Operand.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000703 Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(),
704 Operand.Val);
Chris Lattner26115ac2006-08-24 06:10:04 +0000705 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000706}
707
Chris Lattner11124352006-08-12 19:16:08 +0000708/// ParseBuiltinPrimaryExpression
709///
710/// primary-expression: [C99 6.5.1]
711/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
712/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
713/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
714/// assign-expr ')'
715/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
716///
717/// [GNU] offsetof-member-designator:
718/// [GNU] identifier
719/// [GNU] offsetof-member-designator '.' identifier
720/// [GNU] offsetof-member-designator '[' expression ']'
721///
722Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
723 ExprResult Res(false);
Chris Lattner11124352006-08-12 19:16:08 +0000724 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
725
726 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000727 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000728
729 // All of these start with an open paren.
730 if (Tok.getKind() != tok::l_paren) {
731 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
732 return ExprResult(true);
733 }
734
Chris Lattner04132372006-10-16 06:12:55 +0000735 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000736 // TODO: Build AST.
737
Chris Lattner11124352006-08-12 19:16:08 +0000738 switch (T) {
739 default: assert(0 && "Not a builtin primary expression!");
740 case tok::kw___builtin_va_arg:
741 Res = ParseAssignmentExpression();
742 if (Res.isInvalid) {
743 SkipUntil(tok::r_paren);
744 return Res;
745 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000746
Chris Lattner6d7e6342006-08-15 03:41:14 +0000747 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000748 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000749
Chris Lattner11124352006-08-12 19:16:08 +0000750 ParseTypeName();
751 break;
752
753 case tok::kw___builtin_offsetof:
754 ParseTypeName();
755
Chris Lattner6d7e6342006-08-15 03:41:14 +0000756 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000757 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000758
759 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000760 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000761 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000762 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000763
Chris Lattner11124352006-08-12 19:16:08 +0000764 while (1) {
765 if (Tok.getKind() == tok::period) {
766 // offsetof-member-designator: offsetof-member-designator '.' identifier
767 ConsumeToken();
768
Chris Lattner6d7e6342006-08-15 03:41:14 +0000769 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000770 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000771 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000772 } else if (Tok.getKind() == tok::l_square) {
773 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000774 SourceLocation LSquareLoc = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000775 Res = ParseExpression();
776 if (Res.isInvalid) {
777 SkipUntil(tok::r_paren);
778 return Res;
779 }
780
Chris Lattner04f80192006-08-15 04:55:54 +0000781 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000782 } else {
783 break;
784 }
785 }
786 break;
787 case tok::kw___builtin_choose_expr:
788 Res = ParseAssignmentExpression();
789
Chris Lattner6d7e6342006-08-15 03:41:14 +0000790 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000791 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000792
793 Res = ParseAssignmentExpression();
794
Chris Lattner6d7e6342006-08-15 03:41:14 +0000795 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000796 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000797
798 Res = ParseAssignmentExpression();
799 break;
800 case tok::kw___builtin_types_compatible_p:
801 ParseTypeName();
802
Chris Lattner6d7e6342006-08-15 03:41:14 +0000803 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000804 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000805
806 ParseTypeName();
807 break;
808 }
809
Chris Lattner04f80192006-08-15 04:55:54 +0000810 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000811
812 // These can be followed by postfix-expr pieces because they are
813 // primary-expressions.
814 return ParsePostfixExpressionSuffix(Res);
815}
816
Chris Lattnerc951dae2006-08-10 04:23:57 +0000817
Chris Lattner4add4e62006-08-11 01:33:00 +0000818/// ParseParenExpression - This parses the unit that starts with a '(' token,
819/// based on what is allowed by ExprType. The actual thing parsed is returned
820/// in ExprType.
821///
822/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000823/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000824/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
825/// postfix-expression: [C99 6.5.2]
826/// '(' type-name ')' '{' initializer-list '}'
827/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000828/// cast-expression: [C99 6.5.4]
829/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000830///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000831Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
832 TypeTy *&CastTy,
833 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000834 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +0000835 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000836 ExprResult Result(false);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000837 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000838
Chris Lattner4add4e62006-08-11 01:33:00 +0000839 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000840 !getLang().NoExtensions) {
841 Diag(Tok, diag::ext_gnu_statement_expr);
842 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000843 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000844 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000845 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000846 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000847 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000848
849 // Match the ')'.
Chris Lattner04132372006-10-16 06:12:55 +0000850 if (Tok.getKind() == tok::r_paren)
851 RParenLoc = ConsumeParen();
852 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000853 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000854
Chris Lattner4add4e62006-08-11 01:33:00 +0000855 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000856 if (!getLang().C99) // Compound literals don't exist in C90.
857 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000858 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000859 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000860 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000861 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000862 // Note that this doesn't parse the subsequence cast-expression, it just
863 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000864 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000865 CastTy = Ty;
866 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000867 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000868 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000869 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000870 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000871 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000872 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000873 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000874 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000875 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
876 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000877 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000878
Chris Lattner4564bc12006-08-10 23:14:52 +0000879 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000880 if (Result.isInvalid)
881 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000882 else {
Chris Lattner04132372006-10-16 06:12:55 +0000883 if (Tok.getKind() == tok::r_paren)
884 RParenLoc = ConsumeParen();
885 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000886 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000887 }
Chris Lattner1b926492006-08-23 06:42:10 +0000888
Chris Lattner89c50c62006-08-11 06:41:18 +0000889 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000890}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000891
Chris Lattnerd3e98952006-10-06 05:22:26 +0000892/// ParseStringLiteralExpression - This handles the various token types that
893/// form string literals, and also handles string concatenation [C99 5.1.1.2,
894/// translation phase #6].
895///
896/// primary-expression: [C99 6.5.1]
897/// string-literal
898Parser::ExprResult Parser::ParseStringLiteralExpression() {
899 assert(isTokenStringLiteral() && "Not a string literal!");
900
901 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
902 // considered to be strings for concatenation purposes.
903 SmallVector<LexerToken, 4> StringToks;
904
Chris Lattnerd3e98952006-10-06 05:22:26 +0000905 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000906 StringToks.push_back(Tok);
907 ConsumeStringToken();
908 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +0000909
910 // Pass the set of string tokens, ready for concatenation, to the actions.
Chris Lattner697e5d62006-11-09 06:32:27 +0000911 return Actions.ParseStringExpr(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +0000912}
913