blob: 75414f7b076abd24dbf89e49ab07eda41703680a [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 clang;
27
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000028/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000029/// the C99 grammar. These have been named to relate with the C99 grammar
30/// productions. Low precedences numbers bind more weakly than high numbers.
31namespace prec {
32 enum Level {
33 Unknown = 0, // Not binary operator.
34 Comma = 1, // ,
35 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
36 Conditional = 3, // ?
37 LogicalOr = 4, // ||
38 LogicalAnd = 5, // &&
39 InclusiveOr = 6, // |
40 ExclusiveOr = 7, // ^
41 And = 8, // &
Chris Lattner9916c5c2006-10-27 05:24:37 +000042 Equality = 9, // ==, !=
43 Relational = 10, // >=, <=, >, <
44 Shift = 11, // <<, >>
45 Additive = 12, // -, +
46 Multiplicative = 13 // *, /, %
Chris Lattnercde626a2006-08-12 08:13:25 +000047 };
48}
49
50
51/// getBinOpPrecedence - Return the precedence of the specified binary operator
52/// token. This returns:
53///
54static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
55 switch (Kind) {
56 default: return prec::Unknown;
57 case tok::comma: return prec::Comma;
58 case tok::equal:
59 case tok::starequal:
60 case tok::slashequal:
61 case tok::percentequal:
62 case tok::plusequal:
63 case tok::minusequal:
64 case tok::lesslessequal:
65 case tok::greatergreaterequal:
66 case tok::ampequal:
67 case tok::caretequal:
68 case tok::pipeequal: return prec::Assignment;
69 case tok::question: return prec::Conditional;
70 case tok::pipepipe: return prec::LogicalOr;
71 case tok::ampamp: return prec::LogicalAnd;
72 case tok::pipe: return prec::InclusiveOr;
73 case tok::caret: return prec::ExclusiveOr;
74 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000075 case tok::exclaimequal:
76 case tok::equalequal: return prec::Equality;
77 case tok::lessequal:
78 case tok::less:
79 case tok::greaterequal:
80 case tok::greater: return prec::Relational;
81 case tok::lessless:
82 case tok::greatergreater: return prec::Shift;
83 case tok::plus:
84 case tok::minus: return prec::Additive;
85 case tok::percent:
86 case tok::slash:
87 case tok::star: return prec::Multiplicative;
88 }
89}
90
91
Chris Lattnerce7e21d2006-08-12 17:22:40 +000092/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000093/// operators.
94///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000095/// Note: we diverge from the C99 grammar when parsing the assignment-expression
96/// production. C99 specifies that the LHS of an assignment operator should be
97/// parsed as a unary-expression, but consistency dictates that it be a
98/// conditional-expession. In practice, the important thing here is that the
99/// LHS of an assignment has to be an l-value, which productions between
100/// unary-expression and conditional-expression don't produce. Because we want
101/// consistency, we parse the LHS as a conditional-expression, then check for
102/// l-value-ness in semantic analysis stages.
103///
Chris Lattnercde626a2006-08-12 08:13:25 +0000104/// multiplicative-expression: [C99 6.5.5]
105/// cast-expression
106/// multiplicative-expression '*' cast-expression
107/// multiplicative-expression '/' cast-expression
108/// multiplicative-expression '%' cast-expression
109///
110/// additive-expression: [C99 6.5.6]
111/// multiplicative-expression
112/// additive-expression '+' multiplicative-expression
113/// additive-expression '-' multiplicative-expression
114///
115/// shift-expression: [C99 6.5.7]
116/// additive-expression
117/// shift-expression '<<' additive-expression
118/// shift-expression '>>' additive-expression
119///
120/// relational-expression: [C99 6.5.8]
121/// shift-expression
122/// relational-expression '<' shift-expression
123/// relational-expression '>' shift-expression
124/// relational-expression '<=' shift-expression
125/// relational-expression '>=' shift-expression
126///
127/// equality-expression: [C99 6.5.9]
128/// relational-expression
129/// equality-expression '==' relational-expression
130/// equality-expression '!=' relational-expression
131///
132/// AND-expression: [C99 6.5.10]
133/// equality-expression
134/// AND-expression '&' equality-expression
135///
136/// exclusive-OR-expression: [C99 6.5.11]
137/// AND-expression
138/// exclusive-OR-expression '^' AND-expression
139///
140/// inclusive-OR-expression: [C99 6.5.12]
141/// exclusive-OR-expression
142/// inclusive-OR-expression '|' exclusive-OR-expression
143///
144/// logical-AND-expression: [C99 6.5.13]
145/// inclusive-OR-expression
146/// logical-AND-expression '&&' inclusive-OR-expression
147///
148/// logical-OR-expression: [C99 6.5.14]
149/// logical-AND-expression
150/// logical-OR-expression '||' logical-AND-expression
151///
152/// conditional-expression: [C99 6.5.15]
153/// logical-OR-expression
154/// logical-OR-expression '?' expression ':' conditional-expression
155/// [GNU] logical-OR-expression '?' ':' conditional-expression
156///
157/// assignment-expression: [C99 6.5.16]
158/// conditional-expression
159/// unary-expression assignment-operator assignment-expression
160///
161/// assignment-operator: one of
162/// = *= /= %= += -= <<= >>= &= ^= |=
163///
164/// expression: [C99 6.5.17]
165/// assignment-expression
166/// expression ',' assignment-expression
167///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000168Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000169 ExprResult LHS = ParseCastExpression(false);
170 if (LHS.isInvalid) return LHS;
171
172 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
173}
174
Chris Lattner0c6c0342006-08-12 18:12:45 +0000175/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
176///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000177Parser::ExprResult Parser::ParseAssignmentExpression() {
178 ExprResult LHS = ParseCastExpression(false);
179 if (LHS.isInvalid) return LHS;
180
181 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
182}
183
Chris Lattner3b561a32006-08-13 00:12:11 +0000184Parser::ExprResult Parser::ParseConstantExpression() {
185 ExprResult LHS = ParseCastExpression(false);
186 if (LHS.isInvalid) return LHS;
187
188 // TODO: Validate that this is a constant expr!
189 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
190}
191
Chris Lattner0c6c0342006-08-12 18:12:45 +0000192/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
193/// in contexts where we have already consumed an identifier (which we saved in
Chris Lattnera966bf62006-11-21 01:40:01 +0000194/// 'IdTok'), then discovered that the identifier was really the leading token
195/// of part of an expression. For example, in "A[1]+B", we consumed "A" (which
196/// is now in 'IdTok') and the current token is "[".
Chris Lattner0c6c0342006-08-12 18:12:45 +0000197Parser::ExprResult Parser::
Chris Lattner146762e2007-07-20 16:59:19 +0000198ParseExpressionWithLeadingIdentifier(const Token &IdTok) {
Chris Lattnera966bf62006-11-21 01:40:01 +0000199 // We know that 'IdTok' must correspond to this production:
Chris Lattner0c6c0342006-08-12 18:12:45 +0000200 // primary-expression: identifier
201
Chris Lattnereb2feef2006-11-04 19:14:32 +0000202 // Let the actions module handle the identifier.
Chris Lattnera966bf62006-11-21 01:40:01 +0000203 ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
204 *IdTok.getIdentifierInfo(),
Chris Lattnerac18be92006-11-20 06:49:47 +0000205 Tok.getKind() == tok::l_paren);
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
Chris Lattnera966bf62006-11-21 01:40:01 +0000225/// 'IdTok'), then discovered that the identifier was really the leading token
226/// of part of an assignment-expression. For example, in "A[1]+B", we consumed
227/// "A" (which is now in 'IdTok') and the current token is "[".
Chris Lattner8693a512006-08-13 21:54:02 +0000228Parser::ExprResult Parser::
Chris Lattner146762e2007-07-20 16:59:19 +0000229ParseAssignmentExprWithLeadingIdentifier(const Token &IdTok) {
Chris Lattnera966bf62006-11-21 01:40:01 +0000230 // We know that 'IdTok' must correspond to this production:
Chris Lattner8693a512006-08-13 21:54:02 +0000231 // primary-expression: identifier
232
Chris Lattnereb2feef2006-11-04 19:14:32 +0000233 // Let the actions module handle the identifier.
Chris Lattnera966bf62006-11-21 01:40:01 +0000234 ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
235 *IdTok.getIdentifierInfo(),
Chris Lattnerac18be92006-11-20 06:49:47 +0000236 Tok.getKind() == tok::l_paren);
Chris Lattner8693a512006-08-13 21:54:02 +0000237
238 // Because we have to parse an entire cast-expression before starting the
239 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
240 // need to handle the 'postfix-expression' rules. We do this by invoking
241 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
242 Res = ParsePostfixExpressionSuffix(Res);
243 if (Res.isInvalid) return Res;
244
245 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
246 // done, we know we don't have to do anything for cast-expression, because the
247 // only non-postfix-expression production starts with a '(' token, and we know
248 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
249 // to consume any trailing operators (e.g. "+" in this example) and connected
250 // chunks of the expression.
251 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
252}
253
254
Chris Lattner62591722006-08-12 18:40:58 +0000255/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
256/// used in contexts where we have already consumed a '*' (which we saved in
Chris Lattnera966bf62006-11-21 01:40:01 +0000257/// 'StarTok'), then discovered that the '*' was really the leading token of an
Chris Lattner62591722006-08-12 18:40:58 +0000258/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
Chris Lattnera966bf62006-11-21 01:40:01 +0000259/// now in 'StarTok') and the current token is "(".
Chris Lattner62591722006-08-12 18:40:58 +0000260Parser::ExprResult Parser::
Chris Lattner146762e2007-07-20 16:59:19 +0000261ParseAssignmentExpressionWithLeadingStar(const Token &StarTok) {
Chris Lattnera966bf62006-11-21 01:40:01 +0000262 // We know that 'StarTok' must correspond to this production:
Chris Lattner62591722006-08-12 18:40:58 +0000263 // unary-expression: unary-operator cast-expression
264 // where 'unary-operator' is '*'.
265
266 // Parse the cast-expression that follows the '*'. This will parse the
267 // "*(int*)P" part of "*(int*)P+B".
268 ExprResult Res = ParseCastExpression(false);
269 if (Res.isInvalid) return Res;
270
Chris Lattnerd8702cd2006-11-21 03:12:15 +0000271 // Combine StarTok + Res to get the new AST for the combined expression..
272 Res = Actions.ParseUnaryOp(StarTok.getLocation(), tok::star, Res.Val);
273 if (Res.isInvalid) return Res;
274
Chris Lattner62591722006-08-12 18:40:58 +0000275
276 // We have to parse an entire cast-expression before starting the
277 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
278 // we know that the only production above us is the cast-expression
279 // production, and because the only alternative productions start with a '('
280 // token (we know we had a '*'), there is no work to do to get a whole
281 // cast-expression.
282
283 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
284 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
285 // trailing operators (e.g. "+" in this example) and connected chunks of the
286 // assignment-expression.
287 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
288}
289
290
Chris Lattnercde626a2006-08-12 08:13:25 +0000291/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
292/// LHS and has a precedence of at least MinPrec.
293Parser::ExprResult
294Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
295 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000296 SourceLocation ColonLoc;
297
Chris Lattnercde626a2006-08-12 08:13:25 +0000298 while (1) {
299 // If this token has a lower precedence than we are allowed to parse (e.g.
300 // because we are called recursively, or because the token is not a binop),
301 // then we are done!
302 if (NextTokPrec < MinPrec)
303 return LHS;
304
305 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000306 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000307 ConsumeToken();
308
Chris Lattner96c3deb2006-08-12 17:13:08 +0000309 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000310 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000311 if (NextTokPrec == prec::Conditional) {
312 if (Tok.getKind() != tok::colon) {
313 // Handle this production specially:
314 // logical-OR-expression '?' expression ':' conditional-expression
315 // In particular, the RHS of the '?' is 'expression', not
316 // 'logical-OR-expression' as we might expect.
317 TernaryMiddle = ParseExpression();
318 if (TernaryMiddle.isInvalid) return TernaryMiddle;
319 } else {
320 // Special case handling of "X ? Y : Z" where Y is empty:
321 // logical-OR-expression '?' ':' conditional-expression [GNU]
322 TernaryMiddle = ExprResult(false);
323 Diag(Tok, diag::ext_gnu_conditional_expr);
324 }
325
326 if (Tok.getKind() != tok::colon) {
327 Diag(Tok, diag::err_expected_colon);
328 Diag(OpToken, diag::err_matching, "?");
329 return ExprResult(true);
330 }
331
332 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000333 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000334 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000335
336 // Parse another leaf here for the RHS of the operator.
337 ExprResult RHS = ParseCastExpression(false);
338 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000339
340 // Remember the precedence of this operator and get the precedence of the
341 // operator immediately to the right of the RHS.
342 unsigned ThisPrec = NextTokPrec;
343 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000344
345 // Assignment and conditional expressions are right-associative.
346 bool isRightAssoc = NextTokPrec == prec::Conditional ||
347 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000348
349 // Get the precedence of the operator to the right of the RHS. If it binds
350 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000351 if (ThisPrec < NextTokPrec ||
352 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000353 // If this is left-associative, only parse things on the RHS that bind
354 // more tightly than the current operator. If it is left-associative, it
355 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
356 // A=(B=(C=D)), where each paren is a level of recursion here.
357 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000358 if (RHS.isInvalid) return RHS;
359
360 NextTokPrec = getBinOpPrecedence(Tok.getKind());
361 }
362 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
363
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000364 // Combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnerb5600a62006-10-06 05:40:05 +0000365 if (TernaryMiddle.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000366 LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(),
367 LHS.Val, RHS.Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000368 else
369 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
370 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000371 }
372}
373
Chris Lattnereaf06592006-08-11 02:02:23 +0000374/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
375/// true, parse a unary-expression.
376///
Chris Lattner4564bc12006-08-10 23:14:52 +0000377/// cast-expression: [C99 6.5.4]
378/// unary-expression
379/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000380///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000381/// unary-expression: [C99 6.5.3]
382/// postfix-expression
383/// '++' unary-expression
384/// '--' unary-expression
385/// unary-operator cast-expression
386/// 'sizeof' unary-expression
387/// 'sizeof' '(' type-name ')'
388/// [GNU] '__alignof' unary-expression
389/// [GNU] '__alignof' '(' type-name ')'
390/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000391///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000392/// unary-operator: one of
393/// '&' '*' '+' '-' '~' '!'
394/// [GNU] '__extension__' '__real' '__imag'
395///
Chris Lattner52a99e52006-08-10 20:56:00 +0000396/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000397/// identifier
398/// constant
399/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000400/// [C++] boolean-literal [C++ 2.13.5]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000401/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000402/// '__func__' [C99 6.4.2.2]
403/// [GNU] '__FUNCTION__'
404/// [GNU] '__PRETTY_FUNCTION__'
405/// [GNU] '(' compound-statement ')'
406/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
407/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
408/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
409/// assign-expr ')'
410/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
411/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
412/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
413/// [OBC] '@protocol' '(' identifier ')' [TODO]
414/// [OBC] '@encode' '(' type-name ')' [TODO]
415/// [OBC] objc-string-literal [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000416/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
417/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
418/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
419/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Chris Lattner52a99e52006-08-10 20:56:00 +0000420///
421/// constant: [C99 6.4.4]
422/// integer-constant
423/// floating-constant
424/// enumeration-constant -> identifier
425/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000426///
Chris Lattner89c50c62006-08-11 06:41:18 +0000427Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
428 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000429 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000430
Chris Lattner81b576e2006-08-11 02:13:20 +0000431 // This handles all of cast-expression, unary-expression, postfix-expression,
432 // and primary-expression. We handle them together like this for efficiency
433 // and to simplify handling of an expression starting with a '(' token: which
434 // may be one of a parenthesized expression, cast-expression, compound literal
435 // expression, or statement expression.
436 //
437 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000438 // call ParsePostfixExpressionSuffix to handle the postfix expression
439 // suffixes. Cases that cannot be followed by postfix exprs should
440 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000441 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000442 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000443 // If this expression is limited to being a unary-expression, the parent can
444 // not start a cast expression.
445 ParenParseOption ParenExprType =
446 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000447 TypeTy *CastTy;
448 SourceLocation LParenLoc = Tok.getLocation();
449 SourceLocation RParenLoc;
450 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000451 if (Res.isInvalid) return Res;
452
Chris Lattner81b576e2006-08-11 02:13:20 +0000453 switch (ParenExprType) {
454 case SimpleExpr: break; // Nothing else to do.
455 case CompoundStmt: break; // Nothing else to do.
456 case CompoundLiteral:
457 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
458 // postfix-expression exist, parse them now.
459 break;
460 case CastExpr:
461 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
462 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000463 // TODO: For cast expression with CastTy.
464 Res = ParseCastExpression(false);
465 if (!Res.isInvalid)
466 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
467 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000468 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000469
470 // These can be followed by postfix-expr pieces.
471 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000472 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000473
Chris Lattner52a99e52006-08-10 20:56:00 +0000474 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000475 case tok::numeric_constant:
476 // constant: integer-constant
477 // constant: floating-constant
478
Steve Naroffb7d49242007-03-14 19:55:17 +0000479 Res = Actions.ParseNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000480 ConsumeToken();
481
482 // These can be followed by postfix-expr pieces.
483 return ParsePostfixExpressionSuffix(Res);
484
Bill Wendling4073ed52007-02-13 01:51:42 +0000485 case tok::kw_true:
486 case tok::kw_false:
487 return ParseCXXBoolLiteral();
488
Chris Lattnerac18be92006-11-20 06:49:47 +0000489 case tok::identifier: { // primary-expression: identifier
Chris Lattner52a99e52006-08-10 20:56:00 +0000490 // constant: enumeration-constant
Chris Lattnerac18be92006-11-20 06:49:47 +0000491 // Consume the identifier so that we can see if it is followed by a '('.
492 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
493 // need to know whether or not this identifier is a function designator or
494 // not.
495 IdentifierInfo &II = *Tok.getIdentifierInfo();
496 SourceLocation L = ConsumeToken();
497 Res = Actions.ParseIdentifierExpr(CurScope, L, II,
498 Tok.getKind() == tok::l_paren);
Chris Lattner17ed4872006-11-20 04:58:19 +0000499 // These can be followed by postfix-expr pieces.
500 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerac18be92006-11-20 06:49:47 +0000501 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000502 case tok::char_constant: // constant: character-constant
Steve Naroffae4143e2007-04-26 20:39:23 +0000503 Res = Actions.ParseCharacterConstant(Tok);
504 ConsumeToken();
505 // These can be followed by postfix-expr pieces.
506 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000507 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
508 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
509 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Anders Carlsson625bfc82007-07-21 05:21:51 +0000510 Res = Actions.ParsePreDefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000511 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000512 // These can be followed by postfix-expr pieces.
513 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000514 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000515 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000516 Res = ParseStringLiteralExpression();
517 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000518 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
519 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000520 case tok::kw___builtin_va_arg:
521 case tok::kw___builtin_offsetof:
522 case tok::kw___builtin_choose_expr:
523 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000524 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000525 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000526 case tok::minusminus: { // unary-expression: '--' unary-expression
527 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000528 Res = ParseCastExpression(true);
529 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000530 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000531 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000532 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000533 case tok::amp: // unary-expression: '&' cast-expression
534 case tok::star: // unary-expression: '*' cast-expression
535 case tok::plus: // unary-expression: '+' cast-expression
536 case tok::minus: // unary-expression: '-' cast-expression
537 case tok::tilde: // unary-expression: '~' cast-expression
538 case tok::exclaim: // unary-expression: '!' cast-expression
539 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000540 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000541 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
Chris Lattner366727f2007-07-24 16:58:17 +0000542 // FIXME: Extension should silence extwarns in subexpressions.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000543 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000544 Res = ParseCastExpression(false);
545 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000546 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000547 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000548 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000549 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
550 // unary-expression: 'sizeof' '(' type-name ')'
551 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
552 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000553 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000554 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000555 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000556 if (Tok.getKind() != tok::identifier) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000557 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000558 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000559 }
Chris Lattnereefa10e2007-05-28 06:56:27 +0000560
561 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
562 Res = Actions.ParseAddrLabel(AmpAmpLoc, Tok.getLocation(),
563 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000564 ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000565 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:
Bill Wendling5c9dde02007-06-28 00:45:30 +0000571 return ParseCXXCasts();
Chris Lattner52a99e52006-08-10 20:56:00 +0000572 default:
573 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000574 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000575 }
576
Chris Lattner20c6a452006-08-12 17:40:43 +0000577 // unreachable.
578 abort();
579}
580
581/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
582/// is parsed, this method parses any suffixes that apply.
583///
584/// postfix-expression: [C99 6.5.2]
585/// primary-expression
586/// postfix-expression '[' expression ']'
587/// postfix-expression '(' argument-expression-list[opt] ')'
588/// postfix-expression '.' identifier
589/// postfix-expression '->' identifier
590/// postfix-expression '++'
591/// postfix-expression '--'
592/// '(' type-name ')' '{' initializer-list '}'
593/// '(' type-name ')' '{' initializer-list ',' '}'
594///
595/// argument-expression-list: [C99 6.5.2]
596/// argument-expression
597/// argument-expression-list ',' assignment-expression
598///
599Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000600
Chris Lattnerf8339772006-08-10 22:01:51 +0000601 // Now that the primary-expression piece of the postfix-expression has been
602 // parsed, see if there are any postfix-expression pieces here.
603 SourceLocation Loc;
604 while (1) {
605 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000606 default: // Not a postfix-expression suffix.
607 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000608 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000609 Loc = ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000610 ExprResult Idx = ParseExpression();
611
612 SourceLocation RLoc = Tok.getLocation();
613
614 if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
615 LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
Steve Narofff1e53692007-03-23 22:27:02 +0000616 else
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000617 LHS = ExprResult(true);
618
Chris Lattner89c50c62006-08-11 06:41:18 +0000619 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000620 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000621 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000622 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000623
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000624 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Chris Lattner23b7eb62007-06-15 23:05:46 +0000625 llvm::SmallVector<ExprTy*, 8> ArgExprs;
626 llvm::SmallVector<SourceLocation, 8> CommaLocs;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000627
Chris Lattner04132372006-10-16 06:12:55 +0000628 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000629
Chris Lattner0c6c0342006-08-12 18:12:45 +0000630 if (Tok.getKind() != tok::r_paren) {
631 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000632 ExprResult ArgExpr = ParseAssignmentExpression();
Chris Lattnerde5a4722007-05-21 05:27:47 +0000633 if (ArgExpr.isInvalid) {
Chris Lattnerde5a4722007-05-21 05:27:47 +0000634 SkipUntil(tok::r_paren);
Chris Lattner5abb82c2007-07-21 05:18:12 +0000635 return ExprResult(true);
Chris Lattnerde5a4722007-05-21 05:27:47 +0000636 } else
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000637 ArgExprs.push_back(ArgExpr.Val);
638
Chris Lattner0c6c0342006-08-12 18:12:45 +0000639 if (Tok.getKind() != tok::comma)
640 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000641 // Move to the next argument, remember where the comma was.
642 CommaLocs.push_back(ConsumeToken());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000643 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000644 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000645
Chris Lattner89c50c62006-08-11 06:41:18 +0000646 // Match the ')'.
Chris Lattner5abb82c2007-07-21 05:18:12 +0000647 if (!LHS.isInvalid && Tok.getKind() == tok::r_paren) {
Chris Lattnere165d942006-08-24 04:40:38 +0000648 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
649 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000650 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000651 &CommaLocs[0], Tok.getLocation());
652 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000653
Chris Lattner5abb82c2007-07-21 05:18:12 +0000654 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000655 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000656 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000657 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000658 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000659 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000660 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000661
Chris Lattner89c50c62006-08-11 06:41:18 +0000662 if (Tok.getKind() != tok::identifier) {
663 Diag(Tok, diag::err_expected_ident);
664 return ExprResult(true);
665 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000666
667 if (!LHS.isInvalid)
668 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
669 Tok.getLocation(),
670 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000671 ConsumeToken();
672 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000673 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000674 case tok::plusplus: // postfix-expression: postfix-expression '++'
675 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000676 if (!LHS.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000677 LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
678 LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000679 ConsumeToken();
680 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000681 }
682 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000683}
684
Chris Lattner20c6a452006-08-12 17:40:43 +0000685
Chris Lattner81b576e2006-08-11 02:13:20 +0000686/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
687/// unary-expression: [C99 6.5.3]
688/// 'sizeof' unary-expression
689/// 'sizeof' '(' type-name ')'
690/// [GNU] '__alignof' unary-expression
691/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000692Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000693 assert((Tok.getKind() == tok::kw_sizeof ||
694 Tok.getKind() == tok::kw___alignof) &&
695 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +0000696 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000697 ConsumeToken();
698
699 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000700 ExprResult Operand;
701 if (Tok.getKind() != tok::l_paren) {
702 Operand = ParseCastExpression(true);
703 } else {
704 // If it starts with a '(', we know that it is either a parenthesized
705 // type-name, or it is a unary-expression that starts with a compound
706 // literal, or starts with a primary-expression that is a parenthesized
707 // expression.
708 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000709 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000710 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000711 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000712
713 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
714 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
715 if (ExprType == CastExpr) {
Chris Lattner26da7302006-08-24 06:49:19 +0000716 return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
717 OpTok.getKind() == tok::kw_sizeof,
718 LParenLoc, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000719 }
720 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000721
Chris Lattner26115ac2006-08-24 06:10:04 +0000722 // If we get here, the operand to the sizeof/alignof was an expresion.
723 if (!Operand.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000724 Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(),
725 Operand.Val);
Chris Lattner26115ac2006-08-24 06:10:04 +0000726 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000727}
728
Chris Lattner11124352006-08-12 19:16:08 +0000729/// ParseBuiltinPrimaryExpression
730///
731/// primary-expression: [C99 6.5.1]
732/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
733/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
734/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
735/// assign-expr ')'
736/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
737///
738/// [GNU] offsetof-member-designator:
739/// [GNU] identifier
740/// [GNU] offsetof-member-designator '.' identifier
741/// [GNU] offsetof-member-designator '[' expression ']'
742///
743Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
744 ExprResult Res(false);
Chris Lattner11124352006-08-12 19:16:08 +0000745 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
746
747 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000748 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000749
750 // All of these start with an open paren.
751 if (Tok.getKind() != tok::l_paren) {
752 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
753 return ExprResult(true);
754 }
755
Chris Lattner04132372006-10-16 06:12:55 +0000756 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000757 // TODO: Build AST.
758
Chris Lattner11124352006-08-12 19:16:08 +0000759 switch (T) {
760 default: assert(0 && "Not a builtin primary expression!");
761 case tok::kw___builtin_va_arg:
762 Res = ParseAssignmentExpression();
763 if (Res.isInvalid) {
764 SkipUntil(tok::r_paren);
765 return Res;
766 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000767
Chris Lattner6d7e6342006-08-15 03:41:14 +0000768 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000769 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000770
Chris Lattner11124352006-08-12 19:16:08 +0000771 ParseTypeName();
772 break;
773
774 case tok::kw___builtin_offsetof:
775 ParseTypeName();
776
Chris Lattner6d7e6342006-08-15 03:41:14 +0000777 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000778 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000779
780 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000781 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000782 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000783 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000784
Chris Lattner11124352006-08-12 19:16:08 +0000785 while (1) {
786 if (Tok.getKind() == tok::period) {
787 // offsetof-member-designator: offsetof-member-designator '.' identifier
788 ConsumeToken();
789
Chris Lattner6d7e6342006-08-15 03:41:14 +0000790 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000791 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000792 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000793 } else if (Tok.getKind() == tok::l_square) {
794 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000795 SourceLocation LSquareLoc = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000796 Res = ParseExpression();
797 if (Res.isInvalid) {
798 SkipUntil(tok::r_paren);
799 return Res;
800 }
801
Chris Lattner04f80192006-08-15 04:55:54 +0000802 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000803 } else {
804 break;
805 }
806 }
807 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +0000808 case tok::kw___builtin_choose_expr: {
809 ExprResult Cond = ParseAssignmentExpression();
810 if (Cond.isInvalid) {
811 SkipUntil(tok::r_paren);
812 return Cond;
813 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000814 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000815 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000816
Steve Naroff9efdabc2007-08-03 21:21:27 +0000817 ExprResult Expr1 = ParseAssignmentExpression();
818 if (Expr1.isInvalid) {
819 SkipUntil(tok::r_paren);
820 return Expr1;
821 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000822 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000823 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000824
Steve Naroff9efdabc2007-08-03 21:21:27 +0000825 ExprResult Expr2 = ParseAssignmentExpression();
826 if (Expr2.isInvalid) {
827 SkipUntil(tok::r_paren);
828 return Expr2;
829 }
830 if (Tok.getKind() != tok::r_paren) {
831 Diag(Tok, diag::err_expected_rparen);
832 return ExprResult(true);
833 }
834 return Actions.ParseChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
835 ConsumeParen());
836 }
Chris Lattner11124352006-08-12 19:16:08 +0000837 case tok::kw___builtin_types_compatible_p:
Steve Naroff788d8642007-08-01 23:45:51 +0000838 TypeTy *Ty1 = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000839
Chris Lattner6d7e6342006-08-15 03:41:14 +0000840 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000841 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000842
Steve Naroff788d8642007-08-01 23:45:51 +0000843 TypeTy *Ty2 = ParseTypeName();
844
845 if (Tok.getKind() != tok::r_paren) {
846 Diag(Tok, diag::err_expected_rparen);
847 return ExprResult(true);
848 }
849 return Actions.ParseTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +0000850 }
851
Chris Lattner04f80192006-08-15 04:55:54 +0000852 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000853
854 // These can be followed by postfix-expr pieces because they are
855 // primary-expressions.
856 return ParsePostfixExpressionSuffix(Res);
857}
858
Chris Lattner4add4e62006-08-11 01:33:00 +0000859/// ParseParenExpression - This parses the unit that starts with a '(' token,
860/// based on what is allowed by ExprType. The actual thing parsed is returned
861/// in ExprType.
862///
863/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000864/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000865/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
866/// postfix-expression: [C99 6.5.2]
867/// '(' type-name ')' '{' initializer-list '}'
868/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000869/// cast-expression: [C99 6.5.4]
870/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000871///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000872Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
873 TypeTy *&CastTy,
874 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000875 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +0000876 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner366727f2007-07-24 16:58:17 +0000877 ExprResult Result(true);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000878 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000879
Chris Lattner4add4e62006-08-11 01:33:00 +0000880 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000881 !getLang().NoExtensions) {
882 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattner366727f2007-07-24 16:58:17 +0000883 Parser::StmtResult Stmt = ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000884 ExprType = CompoundStmt;
Chris Lattner366727f2007-07-24 16:58:17 +0000885
886 // If the substmt parsed correctly, build the AST node.
887 if (!Stmt.isInvalid && Tok.getKind() == tok::r_paren)
888 Result = Actions.ParseStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
889
Chris Lattner4add4e62006-08-11 01:33:00 +0000890 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000891 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000892 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000893
894 // Match the ')'.
Chris Lattner04132372006-10-16 06:12:55 +0000895 if (Tok.getKind() == tok::r_paren)
896 RParenLoc = ConsumeParen();
897 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000898 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000899
Chris Lattner4add4e62006-08-11 01:33:00 +0000900 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000901 if (!getLang().C99) // Compound literals don't exist in C90.
902 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000903 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000904 ExprType = CompoundLiteral;
Steve Narofffbd09832007-07-19 01:06:55 +0000905 if (!Result.isInvalid)
906 return Actions.ParseCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Chris Lattner4add4e62006-08-11 01:33:00 +0000907 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000908 // Note that this doesn't parse the subsequence cast-expression, it just
909 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000910 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000911 CastTy = Ty;
912 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000913 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000914 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000915 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000916 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000917 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000918 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000919 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000920 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000921 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
922 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000923 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000924
Chris Lattner4564bc12006-08-10 23:14:52 +0000925 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000926 if (Result.isInvalid)
927 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000928 else {
Chris Lattner04132372006-10-16 06:12:55 +0000929 if (Tok.getKind() == tok::r_paren)
930 RParenLoc = ConsumeParen();
931 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000932 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000933 }
Chris Lattner1b926492006-08-23 06:42:10 +0000934
Chris Lattner89c50c62006-08-11 06:41:18 +0000935 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000936}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000937
Chris Lattnerd3e98952006-10-06 05:22:26 +0000938/// ParseStringLiteralExpression - This handles the various token types that
939/// form string literals, and also handles string concatenation [C99 5.1.1.2,
940/// translation phase #6].
941///
942/// primary-expression: [C99 6.5.1]
943/// string-literal
944Parser::ExprResult Parser::ParseStringLiteralExpression() {
945 assert(isTokenStringLiteral() && "Not a string literal!");
946
947 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
948 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +0000949 llvm::SmallVector<Token, 4> StringToks;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000950
Chris Lattnerd3e98952006-10-06 05:22:26 +0000951 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +0000952 StringToks.push_back(Tok);
953 ConsumeStringToken();
954 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +0000955
956 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroffdf7855b2007-02-21 23:46:25 +0000957 return Actions.ParseStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +0000958}