blob: b2d51441db11aba495df936c4300416e3c704162 [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
Chris Lattnera966bf62006-11-21 01:40:01 +0000195/// 'IdTok'), then discovered that the identifier was really the leading token
196/// of part of an expression. For example, in "A[1]+B", we consumed "A" (which
197/// is now in 'IdTok') and the current token is "[".
Chris Lattner0c6c0342006-08-12 18:12:45 +0000198Parser::ExprResult Parser::
Chris Lattnera966bf62006-11-21 01:40:01 +0000199ParseExpressionWithLeadingIdentifier(const LexerToken &IdTok) {
200 // We know that 'IdTok' must correspond to this production:
Chris Lattner0c6c0342006-08-12 18:12:45 +0000201 // primary-expression: identifier
202
Chris Lattnereb2feef2006-11-04 19:14:32 +0000203 // Let the actions module handle the identifier.
Chris Lattnera966bf62006-11-21 01:40:01 +0000204 ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
205 *IdTok.getIdentifierInfo(),
Chris Lattnerac18be92006-11-20 06:49:47 +0000206 Tok.getKind() == tok::l_paren);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000207
208 // Because we have to parse an entire cast-expression before starting the
209 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
210 // need to handle the 'postfix-expression' rules. We do this by invoking
211 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
212 Res = ParsePostfixExpressionSuffix(Res);
213 if (Res.isInvalid) return Res;
214
215 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
216 // done, we know we don't have to do anything for cast-expression, because the
217 // only non-postfix-expression production starts with a '(' token, and we know
218 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
219 // to consume any trailing operators (e.g. "+" in this example) and connected
220 // chunks of the expression.
221 return ParseRHSOfBinaryExpression(Res, prec::Comma);
222}
223
Chris Lattner8693a512006-08-13 21:54:02 +0000224/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
225/// in contexts where we have already consumed an identifier (which we saved in
Chris Lattnera966bf62006-11-21 01:40:01 +0000226/// 'IdTok'), then discovered that the identifier was really the leading token
227/// of part of an assignment-expression. For example, in "A[1]+B", we consumed
228/// "A" (which is now in 'IdTok') and the current token is "[".
Chris Lattner8693a512006-08-13 21:54:02 +0000229Parser::ExprResult Parser::
Chris Lattnera966bf62006-11-21 01:40:01 +0000230ParseAssignmentExprWithLeadingIdentifier(const LexerToken &IdTok) {
231 // We know that 'IdTok' must correspond to this production:
Chris Lattner8693a512006-08-13 21:54:02 +0000232 // primary-expression: identifier
233
Chris Lattnereb2feef2006-11-04 19:14:32 +0000234 // Let the actions module handle the identifier.
Chris Lattnera966bf62006-11-21 01:40:01 +0000235 ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
236 *IdTok.getIdentifierInfo(),
Chris Lattnerac18be92006-11-20 06:49:47 +0000237 Tok.getKind() == tok::l_paren);
Chris Lattner8693a512006-08-13 21:54:02 +0000238
239 // Because we have to parse an entire cast-expression before starting the
240 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
241 // need to handle the 'postfix-expression' rules. We do this by invoking
242 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
243 Res = ParsePostfixExpressionSuffix(Res);
244 if (Res.isInvalid) return Res;
245
246 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
247 // done, we know we don't have to do anything for cast-expression, because the
248 // only non-postfix-expression production starts with a '(' token, and we know
249 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
250 // to consume any trailing operators (e.g. "+" in this example) and connected
251 // chunks of the expression.
252 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
253}
254
255
Chris Lattner62591722006-08-12 18:40:58 +0000256/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
257/// used in contexts where we have already consumed a '*' (which we saved in
Chris Lattnera966bf62006-11-21 01:40:01 +0000258/// 'StarTok'), then discovered that the '*' was really the leading token of an
Chris Lattner62591722006-08-12 18:40:58 +0000259/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
Chris Lattnera966bf62006-11-21 01:40:01 +0000260/// now in 'StarTok') and the current token is "(".
Chris Lattner62591722006-08-12 18:40:58 +0000261Parser::ExprResult Parser::
Chris Lattnera966bf62006-11-21 01:40:01 +0000262ParseAssignmentExpressionWithLeadingStar(const LexerToken &StarTok) {
263 // We know that 'StarTok' must correspond to this production:
Chris Lattner62591722006-08-12 18:40:58 +0000264 // unary-expression: unary-operator cast-expression
265 // where 'unary-operator' is '*'.
266
267 // Parse the cast-expression that follows the '*'. This will parse the
268 // "*(int*)P" part of "*(int*)P+B".
269 ExprResult Res = ParseCastExpression(false);
270 if (Res.isInvalid) return Res;
271
Chris Lattnerd8702cd2006-11-21 03:12:15 +0000272 // Combine StarTok + Res to get the new AST for the combined expression..
273 Res = Actions.ParseUnaryOp(StarTok.getLocation(), tok::star, Res.Val);
274 if (Res.isInvalid) return Res;
275
Chris Lattner62591722006-08-12 18:40:58 +0000276
277 // We have to parse an entire cast-expression before starting the
278 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
279 // we know that the only production above us is the cast-expression
280 // production, and because the only alternative productions start with a '('
281 // token (we know we had a '*'), there is no work to do to get a whole
282 // cast-expression.
283
284 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
285 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
286 // trailing operators (e.g. "+" in this example) and connected chunks of the
287 // assignment-expression.
288 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
289}
290
291
Chris Lattnercde626a2006-08-12 08:13:25 +0000292/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
293/// LHS and has a precedence of at least MinPrec.
294Parser::ExprResult
295Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
296 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000297 SourceLocation ColonLoc;
298
Chris Lattnercde626a2006-08-12 08:13:25 +0000299 while (1) {
300 // If this token has a lower precedence than we are allowed to parse (e.g.
301 // because we are called recursively, or because the token is not a binop),
302 // then we are done!
303 if (NextTokPrec < MinPrec)
304 return LHS;
305
306 // Consume the operator, saving the operator token for error reporting.
307 LexerToken OpToken = Tok;
308 ConsumeToken();
309
Chris Lattner96c3deb2006-08-12 17:13:08 +0000310 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000311 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000312 if (NextTokPrec == prec::Conditional) {
313 if (Tok.getKind() != tok::colon) {
314 // Handle this production specially:
315 // logical-OR-expression '?' expression ':' conditional-expression
316 // In particular, the RHS of the '?' is 'expression', not
317 // 'logical-OR-expression' as we might expect.
318 TernaryMiddle = ParseExpression();
319 if (TernaryMiddle.isInvalid) return TernaryMiddle;
320 } else {
321 // Special case handling of "X ? Y : Z" where Y is empty:
322 // logical-OR-expression '?' ':' conditional-expression [GNU]
323 TernaryMiddle = ExprResult(false);
324 Diag(Tok, diag::ext_gnu_conditional_expr);
325 }
326
327 if (Tok.getKind() != tok::colon) {
328 Diag(Tok, diag::err_expected_colon);
329 Diag(OpToken, diag::err_matching, "?");
330 return ExprResult(true);
331 }
332
333 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000334 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000335 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000336
337 // Parse another leaf here for the RHS of the operator.
338 ExprResult RHS = ParseCastExpression(false);
339 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000340
341 // Remember the precedence of this operator and get the precedence of the
342 // operator immediately to the right of the RHS.
343 unsigned ThisPrec = NextTokPrec;
344 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000345
346 // Assignment and conditional expressions are right-associative.
347 bool isRightAssoc = NextTokPrec == prec::Conditional ||
348 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000349
350 // Get the precedence of the operator to the right of the RHS. If it binds
351 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000352 if (ThisPrec < NextTokPrec ||
353 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000354 // If this is left-associative, only parse things on the RHS that bind
355 // more tightly than the current operator. If it is left-associative, it
356 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
357 // A=(B=(C=D)), where each paren is a level of recursion here.
358 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000359 if (RHS.isInvalid) return RHS;
360
361 NextTokPrec = getBinOpPrecedence(Tok.getKind());
362 }
363 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
364
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000365 // Combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnerb5600a62006-10-06 05:40:05 +0000366 if (TernaryMiddle.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000367 LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(),
368 LHS.Val, RHS.Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000369 else
370 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
371 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000372 }
373}
374
Chris Lattnereaf06592006-08-11 02:02:23 +0000375/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
376/// true, parse a unary-expression.
377///
Chris Lattner4564bc12006-08-10 23:14:52 +0000378/// cast-expression: [C99 6.5.4]
379/// unary-expression
380/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000381///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000382/// unary-expression: [C99 6.5.3]
383/// postfix-expression
384/// '++' unary-expression
385/// '--' unary-expression
386/// unary-operator cast-expression
387/// 'sizeof' unary-expression
388/// 'sizeof' '(' type-name ')'
389/// [GNU] '__alignof' unary-expression
390/// [GNU] '__alignof' '(' type-name ')'
391/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000392///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000393/// unary-operator: one of
394/// '&' '*' '+' '-' '~' '!'
395/// [GNU] '__extension__' '__real' '__imag'
396///
Chris Lattner52a99e52006-08-10 20:56:00 +0000397/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000398/// identifier
399/// constant
400/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000401/// [C++] boolean-literal [C++ 2.13.5]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000402/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000403/// '__func__' [C99 6.4.2.2]
404/// [GNU] '__FUNCTION__'
405/// [GNU] '__PRETTY_FUNCTION__'
406/// [GNU] '(' compound-statement ')'
407/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
408/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
409/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
410/// assign-expr ')'
411/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
412/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
413/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
414/// [OBC] '@protocol' '(' identifier ')' [TODO]
415/// [OBC] '@encode' '(' type-name ')' [TODO]
416/// [OBC] objc-string-literal [TODO]
417///
418/// constant: [C99 6.4.4]
419/// integer-constant
420/// floating-constant
421/// enumeration-constant -> identifier
422/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000423///
Chris Lattner89c50c62006-08-11 06:41:18 +0000424Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
425 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000426 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000427
Chris Lattner81b576e2006-08-11 02:13:20 +0000428 // This handles all of cast-expression, unary-expression, postfix-expression,
429 // and primary-expression. We handle them together like this for efficiency
430 // and to simplify handling of an expression starting with a '(' token: which
431 // may be one of a parenthesized expression, cast-expression, compound literal
432 // expression, or statement expression.
433 //
434 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000435 // call ParsePostfixExpressionSuffix to handle the postfix expression
436 // suffixes. Cases that cannot be followed by postfix exprs should
437 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000438 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000439 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000440 // If this expression is limited to being a unary-expression, the parent can
441 // not start a cast expression.
442 ParenParseOption ParenExprType =
443 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000444 TypeTy *CastTy;
445 SourceLocation LParenLoc = Tok.getLocation();
446 SourceLocation RParenLoc;
447 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000448 if (Res.isInvalid) return Res;
449
Chris Lattner81b576e2006-08-11 02:13:20 +0000450 switch (ParenExprType) {
451 case SimpleExpr: break; // Nothing else to do.
452 case CompoundStmt: break; // Nothing else to do.
453 case CompoundLiteral:
454 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
455 // postfix-expression exist, parse them now.
456 break;
457 case CastExpr:
458 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
459 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000460 // TODO: For cast expression with CastTy.
461 Res = ParseCastExpression(false);
462 if (!Res.isInvalid)
463 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
464 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000465 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000466
467 // These can be followed by postfix-expr pieces.
468 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000469 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000470
Chris Lattner52a99e52006-08-10 20:56:00 +0000471 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000472 case tok::numeric_constant:
473 // constant: integer-constant
474 // constant: floating-constant
475
476 // TODO: Validate whether this is an integer or floating-constant or
477 // neither.
478 if (1) {
Steve Naroffdf7855b2007-02-21 23:46:25 +0000479 Res = Actions.ParseIntegerLiteral(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000480 } else {
Steve Naroffab624882007-02-21 22:05:47 +0000481 Res = Actions.ParseFloatingLiteral(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000482 }
483 ConsumeToken();
484
485 // These can be followed by postfix-expr pieces.
486 return ParsePostfixExpressionSuffix(Res);
487
Bill Wendling4073ed52007-02-13 01:51:42 +0000488 case tok::kw_true:
489 case tok::kw_false:
490 return ParseCXXBoolLiteral();
491
Chris Lattnerac18be92006-11-20 06:49:47 +0000492 case tok::identifier: { // primary-expression: identifier
Chris Lattner52a99e52006-08-10 20:56:00 +0000493 // constant: enumeration-constant
Chris Lattnerac18be92006-11-20 06:49:47 +0000494 // Consume the identifier so that we can see if it is followed by a '('.
495 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
496 // need to know whether or not this identifier is a function designator or
497 // not.
498 IdentifierInfo &II = *Tok.getIdentifierInfo();
499 SourceLocation L = ConsumeToken();
500 Res = Actions.ParseIdentifierExpr(CurScope, L, II,
501 Tok.getKind() == tok::l_paren);
Chris Lattner17ed4872006-11-20 04:58:19 +0000502 // These can be followed by postfix-expr pieces.
503 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerac18be92006-11-20 06:49:47 +0000504 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000505 case tok::char_constant: // constant: character-constant
506 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
507 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
508 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerae319692006-10-25 03:49:28 +0000509 Res = Actions.ParseSimplePrimaryExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000510 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000511 // These can be followed by postfix-expr pieces.
512 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000513 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000514 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000515 Res = ParseStringLiteralExpression();
516 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000517 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
518 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000519 case tok::kw___builtin_va_arg:
520 case tok::kw___builtin_offsetof:
521 case tok::kw___builtin_choose_expr:
522 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000523 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000524 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000525 case tok::minusminus: { // unary-expression: '--' unary-expression
526 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000527 Res = ParseCastExpression(true);
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::amp: // unary-expression: '&' cast-expression
533 case tok::star: // unary-expression: '*' cast-expression
534 case tok::plus: // unary-expression: '+' cast-expression
535 case tok::minus: // unary-expression: '-' cast-expression
536 case tok::tilde: // unary-expression: '~' cast-expression
537 case tok::exclaim: // unary-expression: '!' cast-expression
538 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000539 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000540 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
Chris Lattner4daa0772006-10-20 05:03:44 +0000541 // FIXME: Extension not handled correctly here!
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000542 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000543 Res = ParseCastExpression(false);
544 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000545 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000546 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000547 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000548 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
549 // unary-expression: 'sizeof' '(' type-name ')'
550 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
551 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000552 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000553 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000554 Diag(Tok, diag::ext_gnu_address_of_label);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000555 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000556
557 if (Tok.getKind() != tok::identifier) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000558 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000559 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000560 }
Chris Lattner14a1b642006-10-15 22:33:58 +0000561 // FIXME: Create a label ref for Tok.Ident.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000562 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, 0);
Chris Lattner14a1b642006-10-15 22:33:58 +0000563 ConsumeToken();
564
565 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000566 }
Chris Lattner29375652006-12-04 18:06:35 +0000567 case tok::kw_const_cast:
568 case tok::kw_dynamic_cast:
569 case tok::kw_reinterpret_cast:
570 case tok::kw_static_cast:
571 Res = ParseCXXCasts();
572 return Res;
Chris Lattner52a99e52006-08-10 20:56:00 +0000573 default:
574 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000575 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000576 }
577
Chris Lattner20c6a452006-08-12 17:40:43 +0000578 // unreachable.
579 abort();
580}
581
582/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
583/// is parsed, this method parses any suffixes that apply.
584///
585/// postfix-expression: [C99 6.5.2]
586/// primary-expression
587/// postfix-expression '[' expression ']'
588/// postfix-expression '(' argument-expression-list[opt] ')'
589/// postfix-expression '.' identifier
590/// postfix-expression '->' identifier
591/// postfix-expression '++'
592/// postfix-expression '--'
593/// '(' type-name ')' '{' initializer-list '}'
594/// '(' type-name ')' '{' initializer-list ',' '}'
595///
596/// argument-expression-list: [C99 6.5.2]
597/// argument-expression
598/// argument-expression-list ',' assignment-expression
599///
600Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000601
Chris Lattnerf8339772006-08-10 22:01:51 +0000602 // Now that the primary-expression piece of the postfix-expression has been
603 // parsed, see if there are any postfix-expression pieces here.
604 SourceLocation Loc;
605 while (1) {
606 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000607 default: // Not a postfix-expression suffix.
608 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000609 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000610 Loc = ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000611 ExprResult Idx = ParseExpression();
612
613 SourceLocation RLoc = Tok.getLocation();
614
615 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
616 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
617 else
618 LHS = ExprResult(true);
619
Chris Lattner89c50c62006-08-11 06:41:18 +0000620 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000621 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000622 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000623 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000624
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000625 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
626 SmallVector<ExprTy*, 8> ArgExprs;
627 SmallVector<SourceLocation, 8> CommaLocs;
628 bool ArgExprsOk = true;
629
Chris Lattner04132372006-10-16 06:12:55 +0000630 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000631
Chris Lattner0c6c0342006-08-12 18:12:45 +0000632 if (Tok.getKind() != tok::r_paren) {
633 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000634 ExprResult ArgExpr = ParseAssignmentExpression();
635 if (ArgExpr.isInvalid)
636 ArgExprsOk = false;
637 else
638 ArgExprs.push_back(ArgExpr.Val);
639
Chris Lattner0c6c0342006-08-12 18:12:45 +0000640 if (Tok.getKind() != tok::comma)
641 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000642 // Move to the next argument, remember where the comma was.
643 CommaLocs.push_back(ConsumeToken());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000644 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000645 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000646
Chris Lattner89c50c62006-08-11 06:41:18 +0000647 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000648 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
649 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
650 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000651 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000652 &CommaLocs[0], Tok.getLocation());
653 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000654
Chris Lattner04f80192006-08-15 04:55:54 +0000655 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000656 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000657 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000658 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000659 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000660 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000661 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000662
Chris Lattner89c50c62006-08-11 06:41:18 +0000663 if (Tok.getKind() != tok::identifier) {
664 Diag(Tok, diag::err_expected_ident);
665 return ExprResult(true);
666 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000667
668 if (!LHS.isInvalid)
669 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
670 Tok.getLocation(),
671 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000672 ConsumeToken();
673 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000674 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000675 case tok::plusplus: // postfix-expression: postfix-expression '++'
676 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000677 if (!LHS.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000678 LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
679 LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000680 ConsumeToken();
681 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000682 }
683 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000684}
685
Chris Lattner20c6a452006-08-12 17:40:43 +0000686
Chris Lattner81b576e2006-08-11 02:13:20 +0000687/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
688/// unary-expression: [C99 6.5.3]
689/// 'sizeof' unary-expression
690/// 'sizeof' '(' type-name ')'
691/// [GNU] '__alignof' unary-expression
692/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000693Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000694 assert((Tok.getKind() == tok::kw_sizeof ||
695 Tok.getKind() == tok::kw___alignof) &&
696 "Not a sizeof/alignof expression!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000697 LexerToken OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000698 ConsumeToken();
699
700 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000701 ExprResult Operand;
702 if (Tok.getKind() != tok::l_paren) {
703 Operand = ParseCastExpression(true);
704 } else {
705 // If it starts with a '(', we know that it is either a parenthesized
706 // type-name, or it is a unary-expression that starts with a compound
707 // literal, or starts with a primary-expression that is a parenthesized
708 // expression.
709 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000710 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000711 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000712 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000713
714 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
715 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
716 if (ExprType == CastExpr) {
Chris Lattner26da7302006-08-24 06:49:19 +0000717 return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
718 OpTok.getKind() == tok::kw_sizeof,
719 LParenLoc, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000720 }
721 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000722
Chris Lattner26115ac2006-08-24 06:10:04 +0000723 // If we get here, the operand to the sizeof/alignof was an expresion.
724 if (!Operand.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000725 Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(),
726 Operand.Val);
Chris Lattner26115ac2006-08-24 06:10:04 +0000727 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000728}
729
Chris Lattner11124352006-08-12 19:16:08 +0000730/// ParseBuiltinPrimaryExpression
731///
732/// primary-expression: [C99 6.5.1]
733/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
734/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
735/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
736/// assign-expr ')'
737/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
738///
739/// [GNU] offsetof-member-designator:
740/// [GNU] identifier
741/// [GNU] offsetof-member-designator '.' identifier
742/// [GNU] offsetof-member-designator '[' expression ']'
743///
744Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
745 ExprResult Res(false);
Chris Lattner11124352006-08-12 19:16:08 +0000746 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
747
748 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000749 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000750
751 // All of these start with an open paren.
752 if (Tok.getKind() != tok::l_paren) {
753 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
754 return ExprResult(true);
755 }
756
Chris Lattner04132372006-10-16 06:12:55 +0000757 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000758 // TODO: Build AST.
759
Chris Lattner11124352006-08-12 19:16:08 +0000760 switch (T) {
761 default: assert(0 && "Not a builtin primary expression!");
762 case tok::kw___builtin_va_arg:
763 Res = ParseAssignmentExpression();
764 if (Res.isInvalid) {
765 SkipUntil(tok::r_paren);
766 return Res;
767 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000768
Chris Lattner6d7e6342006-08-15 03:41:14 +0000769 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000770 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000771
Chris Lattner11124352006-08-12 19:16:08 +0000772 ParseTypeName();
773 break;
774
775 case tok::kw___builtin_offsetof:
776 ParseTypeName();
777
Chris Lattner6d7e6342006-08-15 03:41:14 +0000778 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000779 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000780
781 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000782 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000783 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000784 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000785
Chris Lattner11124352006-08-12 19:16:08 +0000786 while (1) {
787 if (Tok.getKind() == tok::period) {
788 // offsetof-member-designator: offsetof-member-designator '.' identifier
789 ConsumeToken();
790
Chris Lattner6d7e6342006-08-15 03:41:14 +0000791 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000792 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000793 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000794 } else if (Tok.getKind() == tok::l_square) {
795 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000796 SourceLocation LSquareLoc = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000797 Res = ParseExpression();
798 if (Res.isInvalid) {
799 SkipUntil(tok::r_paren);
800 return Res;
801 }
802
Chris Lattner04f80192006-08-15 04:55:54 +0000803 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000804 } else {
805 break;
806 }
807 }
808 break;
809 case tok::kw___builtin_choose_expr:
810 Res = ParseAssignmentExpression();
811
Chris Lattner6d7e6342006-08-15 03:41:14 +0000812 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000813 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000814
815 Res = ParseAssignmentExpression();
816
Chris Lattner6d7e6342006-08-15 03:41:14 +0000817 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000818 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000819
820 Res = ParseAssignmentExpression();
821 break;
822 case tok::kw___builtin_types_compatible_p:
823 ParseTypeName();
824
Chris Lattner6d7e6342006-08-15 03:41:14 +0000825 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000826 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000827
828 ParseTypeName();
829 break;
830 }
831
Chris Lattner04f80192006-08-15 04:55:54 +0000832 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000833
834 // These can be followed by postfix-expr pieces because they are
835 // primary-expressions.
836 return ParsePostfixExpressionSuffix(Res);
837}
838
Chris Lattner4add4e62006-08-11 01:33:00 +0000839/// ParseParenExpression - This parses the unit that starts with a '(' token,
840/// based on what is allowed by ExprType. The actual thing parsed is returned
841/// in ExprType.
842///
843/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000844/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000845/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
846/// postfix-expression: [C99 6.5.2]
847/// '(' type-name ')' '{' initializer-list '}'
848/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000849/// cast-expression: [C99 6.5.4]
850/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000851///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000852Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
853 TypeTy *&CastTy,
854 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000855 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +0000856 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000857 ExprResult Result(false);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000858 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000859
Chris Lattner4add4e62006-08-11 01:33:00 +0000860 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000861 !getLang().NoExtensions) {
862 Diag(Tok, diag::ext_gnu_statement_expr);
863 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000864 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000865 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000866 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000867 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000868 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000869
870 // Match the ')'.
Chris Lattner04132372006-10-16 06:12:55 +0000871 if (Tok.getKind() == tok::r_paren)
872 RParenLoc = ConsumeParen();
873 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000874 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000875
Chris Lattner4add4e62006-08-11 01:33:00 +0000876 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000877 if (!getLang().C99) // Compound literals don't exist in C90.
878 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000879 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000880 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000881 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000882 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000883 // Note that this doesn't parse the subsequence cast-expression, it just
884 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000885 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000886 CastTy = Ty;
887 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000888 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000889 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000890 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000891 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000892 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000893 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000894 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000895 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000896 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
897 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000898 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000899
Chris Lattner4564bc12006-08-10 23:14:52 +0000900 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000901 if (Result.isInvalid)
902 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000903 else {
Chris Lattner04132372006-10-16 06:12:55 +0000904 if (Tok.getKind() == tok::r_paren)
905 RParenLoc = ConsumeParen();
906 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000907 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000908 }
Chris Lattner1b926492006-08-23 06:42:10 +0000909
Chris Lattner89c50c62006-08-11 06:41:18 +0000910 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000911}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000912
Chris Lattnerd3e98952006-10-06 05:22:26 +0000913/// ParseStringLiteralExpression - This handles the various token types that
914/// form string literals, and also handles string concatenation [C99 5.1.1.2,
915/// translation phase #6].
916///
917/// primary-expression: [C99 6.5.1]
918/// string-literal
919Parser::ExprResult Parser::ParseStringLiteralExpression() {
920 assert(isTokenStringLiteral() && "Not a string literal!");
921
922 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
923 // considered to be strings for concatenation purposes.
924 SmallVector<LexerToken, 4> StringToks;
925
Chris Lattnerd3e98952006-10-06 05:22:26 +0000926 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000927 StringToks.push_back(Tok);
928 ConsumeStringToken();
929 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +0000930
931 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroffdf7855b2007-02-21 23:46:25 +0000932 return Actions.ParseStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +0000933}