blob: 331f3181cd479ba4d9bb3f383fad3b3be42842b0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000027#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/SmallString.h"
30using namespace clang;
31
32/// PrecedenceLevels - These are precedences for the binary/ternary operators in
33/// the C99 grammar. These have been named to relate with the C99 grammar
34/// productions. Low precedences numbers bind more weakly than high numbers.
35namespace prec {
36 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000037 Unknown = 0, // Not binary operator.
38 Comma = 1, // ,
39 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
40 Conditional = 3, // ?
41 LogicalOr = 4, // ||
42 LogicalAnd = 5, // &&
43 InclusiveOr = 6, // |
44 ExclusiveOr = 7, // ^
45 And = 8, // &
46 Equality = 9, // ==, !=
47 Relational = 10, // >=, <=, >, <
48 Shift = 11, // <<, >>
49 Additive = 12, // -, +
50 Multiplicative = 13, // *, /, %
51 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000052 };
53}
54
55
56/// getBinOpPrecedence - Return the precedence of the specified binary operator
57/// token. This returns:
58///
Douglas Gregor55f6b142009-02-09 18:46:07 +000059static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000060 bool GreaterThanIsOperator,
61 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000063 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000064 // C++ [temp.names]p3:
65 // [...] When parsing a template-argument-list, the first
66 // non-nested > is taken as the ending delimiter rather than a
67 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000068 if (GreaterThanIsOperator)
69 return prec::Relational;
70 return prec::Unknown;
71
Douglas Gregor3965b7b2009-02-25 23:02:36 +000072 case tok::greatergreater:
73 // C++0x [temp.names]p3:
74 //
75 // [...] Similarly, the first non-nested >> is treated as two
76 // consecutive but distinct > tokens, the first of which is
77 // taken as the end of the template-argument-list and completes
78 // the template-id. [...]
79 if (GreaterThanIsOperator || !CPlusPlus0x)
80 return prec::Shift;
81 return prec::Unknown;
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 default: return prec::Unknown;
84 case tok::comma: return prec::Comma;
85 case tok::equal:
86 case tok::starequal:
87 case tok::slashequal:
88 case tok::percentequal:
89 case tok::plusequal:
90 case tok::minusequal:
91 case tok::lesslessequal:
92 case tok::greatergreaterequal:
93 case tok::ampequal:
94 case tok::caretequal:
95 case tok::pipeequal: return prec::Assignment;
96 case tok::question: return prec::Conditional;
97 case tok::pipepipe: return prec::LogicalOr;
98 case tok::ampamp: return prec::LogicalAnd;
99 case tok::pipe: return prec::InclusiveOr;
100 case tok::caret: return prec::ExclusiveOr;
101 case tok::amp: return prec::And;
102 case tok::exclaimequal:
103 case tok::equalequal: return prec::Equality;
104 case tok::lessequal:
105 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000106 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000107 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 case tok::plus:
109 case tok::minus: return prec::Additive;
110 case tok::percent:
111 case tok::slash:
112 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000113 case tok::periodstar:
114 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116}
117
118
119/// ParseExpression - Simple precedence-based parser for binary/ternary
120/// operators.
121///
122/// Note: we diverge from the C99 grammar when parsing the assignment-expression
123/// production. C99 specifies that the LHS of an assignment operator should be
124/// parsed as a unary-expression, but consistency dictates that it be a
125/// conditional-expession. In practice, the important thing here is that the
126/// LHS of an assignment has to be an l-value, which productions between
127/// unary-expression and conditional-expression don't produce. Because we want
128/// consistency, we parse the LHS as a conditional-expression, then check for
129/// l-value-ness in semantic analysis stages.
130///
Sebastian Redl22460502009-02-07 00:15:38 +0000131/// pm-expression: [C++ 5.5]
132/// cast-expression
133/// pm-expression '.*' cast-expression
134/// pm-expression '->*' cast-expression
135///
Reid Spencer5f016e22007-07-11 17:01:13 +0000136/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000137/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// cast-expression
139/// multiplicative-expression '*' cast-expression
140/// multiplicative-expression '/' cast-expression
141/// multiplicative-expression '%' cast-expression
142///
143/// additive-expression: [C99 6.5.6]
144/// multiplicative-expression
145/// additive-expression '+' multiplicative-expression
146/// additive-expression '-' multiplicative-expression
147///
148/// shift-expression: [C99 6.5.7]
149/// additive-expression
150/// shift-expression '<<' additive-expression
151/// shift-expression '>>' additive-expression
152///
153/// relational-expression: [C99 6.5.8]
154/// shift-expression
155/// relational-expression '<' shift-expression
156/// relational-expression '>' shift-expression
157/// relational-expression '<=' shift-expression
158/// relational-expression '>=' shift-expression
159///
160/// equality-expression: [C99 6.5.9]
161/// relational-expression
162/// equality-expression '==' relational-expression
163/// equality-expression '!=' relational-expression
164///
165/// AND-expression: [C99 6.5.10]
166/// equality-expression
167/// AND-expression '&' equality-expression
168///
169/// exclusive-OR-expression: [C99 6.5.11]
170/// AND-expression
171/// exclusive-OR-expression '^' AND-expression
172///
173/// inclusive-OR-expression: [C99 6.5.12]
174/// exclusive-OR-expression
175/// inclusive-OR-expression '|' exclusive-OR-expression
176///
177/// logical-AND-expression: [C99 6.5.13]
178/// inclusive-OR-expression
179/// logical-AND-expression '&&' inclusive-OR-expression
180///
181/// logical-OR-expression: [C99 6.5.14]
182/// logical-AND-expression
183/// logical-OR-expression '||' logical-AND-expression
184///
185/// conditional-expression: [C99 6.5.15]
186/// logical-OR-expression
187/// logical-OR-expression '?' expression ':' conditional-expression
188/// [GNU] logical-OR-expression '?' ':' conditional-expression
189///
190/// assignment-expression: [C99 6.5.16]
191/// conditional-expression
192/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000193/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000194///
195/// assignment-operator: one of
196/// = *= /= %= += -= <<= >>= &= ^= |=
197///
198/// expression: [C99 6.5.17]
199/// assignment-expression
200/// expression ',' assignment-expression
201///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000202Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000203 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000204 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000205
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000206 OwningExprResult LHS(ParseCastExpression(false));
207 if (LHS.isInvalid()) return move(LHS);
208
Sebastian Redld8c4e152008-12-11 22:33:27 +0000209 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000212/// This routine is called when the '@' is seen and consumed.
213/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000214/// routine is necessary to disambiguate @try-statement from,
215/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000216///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000217Parser::OwningExprResult
218Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000219 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000220 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000221
Sebastian Redld8c4e152008-12-11 22:33:27 +0000222 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000223}
224
Eli Friedmanadf077f2009-01-27 08:43:38 +0000225/// This routine is called when a leading '__extension__' is seen and
226/// consumed. This is necessary because the token gets consumed in the
227/// process of disambiguating between an expression and a declaration.
228Parser::OwningExprResult
229Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
230 // FIXME: The handling for throw is almost certainly wrong.
231 if (Tok.is(tok::kw_throw))
232 return ParseThrowExpression();
233
234 OwningExprResult LHS(ParseCastExpression(false));
235 if (LHS.isInvalid()) return move(LHS);
236
237 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000238 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000239 if (LHS.isInvalid()) return move(LHS);
240
241 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
242}
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
245///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000246Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000247 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000248 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000249
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000250 OwningExprResult LHS(ParseCastExpression(false));
251 if (LHS.isInvalid()) return move(LHS);
252
Sebastian Redld8c4e152008-12-11 22:33:27 +0000253 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254}
255
Chris Lattnerb93fb492008-06-02 21:31:07 +0000256/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
257/// where part of an objc message send has already been parsed. In this case
258/// LBracLoc indicates the location of the '[' of the message send, and either
259/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
260/// message.
261///
262/// Since this handles full assignment-expression's, it handles postfix
263/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000264Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000265Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000266 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000267 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000268 ExprArg ReceiverExpr) {
269 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
270 ReceiverName,
271 move(ReceiverExpr)));
272 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000273 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000274 if (R.isInvalid()) return move(R);
275 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000276}
277
278
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000279Parser::OwningExprResult Parser::ParseConstantExpression() {
280 OwningExprResult LHS(ParseCastExpression(false));
281 if (LHS.isInvalid()) return move(LHS);
282
Sebastian Redld8c4e152008-12-11 22:33:27 +0000283 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284}
285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
287/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000288Parser::OwningExprResult
289Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000290 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
291 GreaterThanIsOperator,
292 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 SourceLocation ColonLoc;
294
295 while (1) {
296 // If this token has a lower precedence than we are allowed to parse (e.g.
297 // because we are called recursively, or because the token is not a binop),
298 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000299 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000300 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301
302 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000303 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000307 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000309 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Handle this production specially:
311 // logical-OR-expression '?' expression ':' conditional-expression
312 // In particular, the RHS of the '?' is 'expression', not
313 // 'logical-OR-expression' as we might expect.
314 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000315 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000316 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 } else {
318 // Special case handling of "X ? Y : Z" where Y is empty:
319 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000320 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 Diag(Tok, diag::ext_gnu_conditional_expr);
322 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000323
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000324 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000326 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000327 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000329
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // Eat the colon.
331 ColonLoc = ConsumeToken();
332 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000335 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000336 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000337 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338
339 // Remember the precedence of this operator and get the precedence of the
340 // operator immediately to the right of the RHS.
341 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000342 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
343 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
345 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000346 bool isRightAssoc = ThisPrec == prec::Conditional ||
347 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +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.
351 if (ThisPrec < NextTokPrec ||
352 (ThisPrec == NextTokPrec && isRightAssoc)) {
353 // 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.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000357 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000358 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000359 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000360 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000361
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000362 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
363 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 }
365 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000366
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000367 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000368 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000369 if (TernaryMiddle.isInvalid()) {
370 // If we're using '>>' as an operator within a template
371 // argument list (in C++98), suggest the addition of
372 // parentheses so that the code remains well-formed in C++0x.
373 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
374 SuggestParentheses(OpToken.getLocation(),
375 diag::warn_cxx0x_right_shift_in_template_arg,
376 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
377 Actions.getExprRange(RHS.get()).getEnd()));
378
Sebastian Redleffa8d12008-12-10 00:02:53 +0000379 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000380 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000381 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000382 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000383 move(LHS), move(TernaryMiddle),
384 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000385 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 }
387}
388
389/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000390/// true, parse a unary-expression. isAddressOfOperand exists because an
391/// id-expression that is the operand of address-of gets special treatment
392/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000393///
394/// cast-expression: [C99 6.5.4]
395/// unary-expression
396/// '(' type-name ')' cast-expression
397///
398/// unary-expression: [C99 6.5.3]
399/// postfix-expression
400/// '++' unary-expression
401/// '--' unary-expression
402/// unary-operator cast-expression
403/// 'sizeof' unary-expression
404/// 'sizeof' '(' type-name ')'
405/// [GNU] '__alignof' unary-expression
406/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000407/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000408/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000409/// [C++] new-expression
410/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000411///
412/// unary-operator: one of
413/// '&' '*' '+' '-' '~' '!'
414/// [GNU] '__extension__' '__real' '__imag'
415///
416/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000417/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000418/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// constant
420/// string-literal
421/// [C++] boolean-literal [C++ 2.13.5]
422/// '(' expression ')'
423/// '__func__' [C99 6.4.2.2]
424/// [GNU] '__FUNCTION__'
425/// [GNU] '__PRETTY_FUNCTION__'
426/// [GNU] '(' compound-statement ')'
427/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
428/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
429/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
430/// assign-expr ')'
431/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000432/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000433/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000434/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000435/// [OBJC] '@protocol' '(' identifier ')'
436/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000437/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000438/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
439/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000440/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
441/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
442/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
443/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000444/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
445/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000446/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000447/// [G++] unary-type-trait '(' type-id ')'
448/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000449/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000450///
451/// constant: [C99 6.4.4]
452/// integer-constant
453/// floating-constant
454/// enumeration-constant -> identifier
455/// character-constant
456///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000457/// id-expression: [C++ 5.1]
458/// unqualified-id
459/// qualified-id [TODO]
460///
461/// unqualified-id: [C++ 5.1]
462/// identifier
463/// operator-function-id
464/// conversion-function-id [TODO]
465/// '~' class-name [TODO]
466/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000467///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000468/// new-expression: [C++ 5.3.4]
469/// '::'[opt] 'new' new-placement[opt] new-type-id
470/// new-initializer[opt]
471/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
472/// new-initializer[opt]
473///
474/// delete-expression: [C++ 5.3.5]
475/// '::'[opt] 'delete' cast-expression
476/// '::'[opt] 'delete' '[' ']' cast-expression
477///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000478/// [GNU] unary-type-trait:
479/// '__has_nothrow_assign' [TODO]
480/// '__has_nothrow_copy' [TODO]
481/// '__has_nothrow_constructor' [TODO]
482/// '__has_trivial_assign' [TODO]
483/// '__has_trivial_copy' [TODO]
484/// '__has_trivial_constructor' [TODO]
485/// '__has_trivial_destructor' [TODO]
486/// '__has_virtual_destructor' [TODO]
487/// '__is_abstract' [TODO]
488/// '__is_class'
489/// '__is_empty' [TODO]
490/// '__is_enum'
491/// '__is_pod'
492/// '__is_polymorphic'
493/// '__is_union'
494///
495/// [GNU] binary-type-trait:
496/// '__is_base_of' [TODO]
497///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000498Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
499 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000500 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 tok::TokenKind SavedKind = Tok.getKind();
502
503 // This handles all of cast-expression, unary-expression, postfix-expression,
504 // and primary-expression. We handle them together like this for efficiency
505 // and to simplify handling of an expression starting with a '(' token: which
506 // may be one of a parenthesized expression, cast-expression, compound literal
507 // expression, or statement expression.
508 //
509 // If the parsed tokens consist of a primary-expression, the cases below
510 // call ParsePostfixExpressionSuffix to handle the postfix expression
511 // suffixes. Cases that cannot be followed by postfix exprs should
512 // return without invoking ParsePostfixExpressionSuffix.
513 switch (SavedKind) {
514 case tok::l_paren: {
515 // If this expression is limited to being a unary-expression, the parent can
516 // not start a cast expression.
517 ParenParseOption ParenExprType =
518 isUnaryExpression ? CompoundLiteral : CastExpr;
519 TypeTy *CastTy;
520 SourceLocation LParenLoc = Tok.getLocation();
521 SourceLocation RParenLoc;
522 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000523 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000524
525 switch (ParenExprType) {
526 case SimpleExpr: break; // Nothing else to do.
527 case CompoundStmt: break; // Nothing else to do.
528 case CompoundLiteral:
529 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
530 // postfix-expression exist, parse them now.
531 break;
532 case CastExpr:
533 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
534 // the cast-expression that follows it next.
535 // TODO: For cast expression with CastTy.
536 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000537 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000538 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000539 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000541
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000543 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 // primary-expression
547 case tok::numeric_constant:
548 // constant: integer-constant
549 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000550
Steve Narofff69936d2007-09-16 03:34:24 +0000551 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000555 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
557 case tok::kw_true:
558 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000559 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000560
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000561 case tok::identifier: { // primary-expression: identifier
562 // unqualified-id: identifier
563 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000564 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000565 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000566 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000567 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
568 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000569 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000570 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // Consume the identifier so that we can see if it is followed by a '('.
573 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
574 // need to know whether or not this identifier is a function designator or
575 // not.
576 IdentifierInfo &II = *Tok.getIdentifierInfo();
577 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000578 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000580 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 }
582 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000583 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 ConsumeToken();
585 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000586 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
588 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
589 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000590 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 ConsumeToken();
592 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000593 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 case tok::string_literal: // primary-expression: string-literal
595 case tok::wide_string_literal:
596 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000597 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000599 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 case tok::kw___builtin_va_arg:
601 case tok::kw___builtin_offsetof:
602 case tok::kw___builtin_choose_expr:
603 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000604 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000605 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000606 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000607 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 case tok::plusplus: // unary-expression: '++' unary-expression
609 case tok::minusminus: { // unary-expression: '--' unary-expression
610 SourceLocation SavedLoc = ConsumeToken();
611 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000612 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000613 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000614 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000616 case tok::amp: { // unary-expression: '&' cast-expression
617 // Special treatment because of member pointers
618 SourceLocation SavedLoc = ConsumeToken();
619 Res = ParseCastExpression(false, true);
620 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000621 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000622 return move(Res);
623 }
624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 case tok::star: // unary-expression: '*' cast-expression
626 case tok::plus: // unary-expression: '+' cast-expression
627 case tok::minus: // unary-expression: '-' cast-expression
628 case tok::tilde: // unary-expression: '~' cast-expression
629 case tok::exclaim: // unary-expression: '!' cast-expression
630 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000631 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 SourceLocation SavedLoc = ConsumeToken();
633 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000634 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000635 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000636 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000637 }
638
Chris Lattner35080842008-02-02 20:20:10 +0000639 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
640 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000641 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000642 SourceLocation SavedLoc = ConsumeToken();
643 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000644 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000645 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000646 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 }
648 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
649 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000650 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
652 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000653 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000654 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 case tok::ampamp: { // unary-expression: '&&' identifier
656 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000657 if (Tok.isNot(tok::identifier))
658 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000661 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 Tok.getIdentifierInfo());
663 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000664 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 }
666 case tok::kw_const_cast:
667 case tok::kw_dynamic_cast:
668 case tok::kw_reinterpret_cast:
669 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000670 Res = ParseCXXCasts();
671 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000672 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000673 case tok::kw_typeid:
674 Res = ParseCXXTypeid();
675 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000676 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000677 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000678 Res = ParseCXXThis();
679 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000680 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000681
682 case tok::kw_char:
683 case tok::kw_wchar_t:
684 case tok::kw_bool:
685 case tok::kw_short:
686 case tok::kw_int:
687 case tok::kw_long:
688 case tok::kw_signed:
689 case tok::kw_unsigned:
690 case tok::kw_float:
691 case tok::kw_double:
692 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000693 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000694 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000695 if (!getLang().CPlusPlus) {
696 Diag(Tok, diag::err_expected_expression);
697 return ExprError();
698 }
699
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000700 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
701 //
702 DeclSpec DS;
703 ParseCXXSimpleTypeSpecifier(DS);
704 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000705 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
706 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000707
708 Res = ParseCXXTypeConstructExpression(DS);
709 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000710 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000711 }
712
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000713 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
714 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
715 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000716 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000717 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000718
Chris Lattner74ba4102009-01-04 22:52:14 +0000719 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000720 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
721 // annotates the token, tail recurse.
722 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000723 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
724
Chris Lattner74ba4102009-01-04 22:52:14 +0000725 // ::new -> [C++] new-expression
726 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000727 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000728 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000729 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000730 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000731 return ParseCXXDeleteExpression(true, CCLoc);
732
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000733 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000734 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000735 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000736 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000737
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000738 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000739 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000740
741 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000742 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000743
Sebastian Redl64b45f72009-01-05 20:52:13 +0000744 case tok::kw___is_pod: // [GNU] unary-type-trait
745 case tok::kw___is_class:
746 case tok::kw___is_enum:
747 case tok::kw___is_union:
748 case tok::kw___is_polymorphic:
749 return ParseUnaryTypeTrait();
750
Chris Lattnerc97c2042007-10-03 22:03:06 +0000751 case tok::at: {
752 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000753 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000754 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000755 case tok::caret:
756 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000757 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000758 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000759 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000760 case tok::l_square:
761 // These can be followed by postfix-expr pieces.
762 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000763 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000764 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 default:
766 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000767 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 // unreachable.
771 abort();
772}
773
774/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
775/// is parsed, this method parses any suffixes that apply.
776///
777/// postfix-expression: [C99 6.5.2]
778/// primary-expression
779/// postfix-expression '[' expression ']'
780/// postfix-expression '(' argument-expression-list[opt] ')'
781/// postfix-expression '.' identifier
782/// postfix-expression '->' identifier
783/// postfix-expression '++'
784/// postfix-expression '--'
785/// '(' type-name ')' '{' initializer-list '}'
786/// '(' type-name ')' '{' initializer-list ',' '}'
787///
788/// argument-expression-list: [C99 6.5.2]
789/// argument-expression
790/// argument-expression-list ',' assignment-expression
791///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000792Parser::OwningExprResult
793Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 // Now that the primary-expression piece of the postfix-expression has been
795 // parsed, see if there are any postfix-expression pieces here.
796 SourceLocation Loc;
797 while (1) {
798 switch (Tok.getKind()) {
799 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000800 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
802 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000803 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000806
807 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000808 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
809 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000810 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000811 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000812
813 // Match the ']'.
814 MatchRHSPunctuation(tok::r_square, Loc);
815 break;
816 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000819 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000820 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000823
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000824 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000825 if (ParseExpressionList(ArgExprs, CommaLocs)) {
826 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000827 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 }
829 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000830
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000832 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
834 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000835 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000836 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000837 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000839
Chris Lattner2ff54262007-07-21 05:18:12 +0000840 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 break;
842 }
843 case tok::arrow: // postfix-expression: p-e '->' identifier
844 case tok::period: { // postfix-expression: p-e '.' identifier
845 tok::TokenKind OpKind = Tok.getKind();
846 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000847
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000848 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000850 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000852
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000853 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000854 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000855 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000856 *Tok.getIdentifierInfo(),
857 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000858 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 ConsumeToken();
860 break;
861 }
862 case tok::plusplus: // postfix-expression: postfix-expression '++'
863 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000864 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000865 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000866 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000867 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 ConsumeToken();
869 break;
870 }
871 }
872}
873
874
875/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
876/// unary-expression: [C99 6.5.3]
877/// 'sizeof' unary-expression
878/// 'sizeof' '(' type-name ')'
879/// [GNU] '__alignof' unary-expression
880/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000881/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000882Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000883 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
884 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000886 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 ConsumeToken();
888
889 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000890 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000891 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 Operand = ParseCastExpression(true);
893 } else {
894 // If it starts with a '(', we know that it is either a parenthesized
895 // type-name, or it is a unary-expression that starts with a compound
896 // literal, or starts with a primary-expression that is a parenthesized
897 // expression.
898 ParenParseOption ExprType = CastExpr;
899 TypeTy *CastTy;
900 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
901 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
904 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000905 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000906 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000907 OpTok.is(tok::kw_sizeof),
908 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000909 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000910
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000911 // If this is a parenthesized expression, it is the start of a
912 // unary-expression, but doesn't include any postfix pieces. Parse these
913 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000914 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000918 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000919 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
920 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000921 /*isType=*/false,
922 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000923 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000924}
925
926/// ParseBuiltinPrimaryExpression
927///
928/// primary-expression: [C99 6.5.1]
929/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
930/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
931/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
932/// assign-expr ')'
933/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
934///
935/// [GNU] offsetof-member-designator:
936/// [GNU] identifier
937/// [GNU] offsetof-member-designator '.' identifier
938/// [GNU] offsetof-member-designator '[' expression ']'
939///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000940Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000941 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
943
944 tok::TokenKind T = Tok.getKind();
945 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
946
947 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000948 if (Tok.isNot(tok::l_paren))
949 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
950 << BuiltinII);
951
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 SourceLocation LParenLoc = ConsumeParen();
953 // TODO: Build AST.
954
955 switch (T) {
956 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000957 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000958 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000959 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000961 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 }
963
964 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000965 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000966
Douglas Gregor809070a2009-02-18 17:45:20 +0000967 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000968
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000969 if (Tok.isNot(tok::r_paren)) {
970 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000971 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000972 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000973 if (Ty.isInvalid())
974 Res = ExprError();
975 else
976 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty.get(),
977 ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000979 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000980 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000981 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +0000982 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000983
984 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000985 return ExprError();
986
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000988 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000989 Diag(Tok, diag::err_expected_ident);
990 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000991 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000992 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000993
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000994 // Keep track of the various subcomponents we see.
995 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +0000996
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000997 Comps.push_back(Action::OffsetOfComponent());
998 Comps.back().isBrackets = false;
999 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1000 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001001
Sebastian Redla55e52c2008-11-25 22:21:31 +00001002 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001004 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001006 Comps.push_back(Action::OffsetOfComponent());
1007 Comps.back().isBrackets = false;
1008 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001009
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001010 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001011 Diag(Tok, diag::err_expected_ident);
1012 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001013 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001014 }
1015 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1016 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001017
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001018 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001020 Comps.push_back(Action::OffsetOfComponent());
1021 Comps.back().isBrackets = true;
1022 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001024 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001026 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001028 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001029
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001030 Comps.back().LocEnd =
1031 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001032 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001033 if (Ty.isInvalid())
1034 Res = ExprError();
1035 else
1036 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1037 Ty.get(), &Comps[0],
1038 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001039 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001041 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001042 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 }
1044 }
1045 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001046 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001047 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001048 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001049 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001050 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001051 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001052 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001054 return ExprError();
1055
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001056 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001057 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001058 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001059 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001062 return ExprError();
1063
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001064 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001065 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001066 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001067 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001068 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001069 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001070 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001071 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001072 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001073 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1074 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001075 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001076 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001078 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001081 return ExprError();
1082
Douglas Gregor809070a2009-02-18 17:45:20 +00001083 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001084
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001085 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001086 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001087 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001088 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001089
1090 if (Ty1.isInvalid() || Ty2.isInvalid())
1091 Res = ExprError();
1092 else
1093 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1094 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001095 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001096 }
1097
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 // These can be followed by postfix-expr pieces because they are
1099 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001100 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001101}
1102
1103/// ParseParenExpression - This parses the unit that starts with a '(' token,
1104/// based on what is allowed by ExprType. The actual thing parsed is returned
1105/// in ExprType.
1106///
1107/// primary-expression: [C99 6.5.1]
1108/// '(' expression ')'
1109/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1110/// postfix-expression: [C99 6.5.2]
1111/// '(' type-name ')' '{' initializer-list '}'
1112/// '(' type-name ')' '{' initializer-list ',' '}'
1113/// cast-expression: [C99 6.5.4]
1114/// '(' type-name ')' cast-expression
1115///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001116Parser::OwningExprResult
1117Parser::ParseParenExpression(ParenParseOption &ExprType,
1118 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001119 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001120 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001122 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001124
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001125 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001127 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001129
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001130 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001131 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1132 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001133 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001134
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001135 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001137 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
1139 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001140 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 RParenLoc = ConsumeParen();
1142 else
1143 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001144
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001145 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 if (!getLang().C99) // Compound literals don't exist in C90.
1147 Diag(OpenLoc, diag::ext_c99_compound_literal);
1148 Result = ParseInitializer();
1149 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001150 if (!Result.isInvalid() && !Ty.isInvalid())
1151 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001152 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001153 return move(Result);
1154 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001155
Chris Lattner42ece642008-12-12 06:00:12 +00001156 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001157 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 // returns the parsed type to the callee.
1159 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001160
1161 if (Ty.isInvalid())
1162 return ExprError();
1163
1164 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001165 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001167
Chris Lattner42ece642008-12-12 06:00:12 +00001168 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1169 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 } else {
1171 Result = ParseExpression();
1172 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001173 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001174 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001178 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001180 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 }
Chris Lattner42ece642008-12-12 06:00:12 +00001182
1183 if (Tok.is(tok::r_paren))
1184 RParenLoc = ConsumeParen();
1185 else
1186 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001187
1188 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189}
1190
1191/// ParseStringLiteralExpression - This handles the various token types that
1192/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1193/// translation phase #6].
1194///
1195/// primary-expression: [C99 6.5.1]
1196/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001197Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001199
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1201 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001202 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001203
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 do {
1205 StringToks.push_back(Tok);
1206 ConsumeStringToken();
1207 } while (isTokenStringLiteral());
1208
1209 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001210 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001212
1213/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1214///
1215/// argument-expression-list:
1216/// assignment-expression
1217/// argument-expression-list , assignment-expression
1218///
1219/// [C++] expression-list:
1220/// [C++] assignment-expression
1221/// [C++] expression-list , assignment-expression
1222///
1223bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1224 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001225 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001226 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001227 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001228
Sebastian Redleffa8d12008-12-10 00:02:53 +00001229 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001230
1231 if (Tok.isNot(tok::comma))
1232 return false;
1233 // Move to the next argument, remember where the comma was.
1234 CommaLocs.push_back(ConsumeToken());
1235 }
1236}
Steve Naroff296e8d52008-08-28 19:20:44 +00001237
Mike Stump98eb8a72009-02-04 22:31:32 +00001238/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1239///
1240/// [clang] block-id:
1241/// [clang] specifier-qualifier-list block-declarator
1242///
1243void Parser::ParseBlockId() {
1244 // Parse the specifier-qualifier-list piece.
1245 DeclSpec DS;
1246 ParseSpecifierQualifierList(DS);
1247
1248 // Parse the block-declarator.
1249 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1250 ParseDeclarator(DeclaratorInfo);
1251 // Inform sema that we are starting a block.
1252 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1253}
1254
Steve Naroff296e8d52008-08-28 19:20:44 +00001255/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001256/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001257///
1258/// block-literal:
1259/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001260/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001261/// [clang] block-args:
1262/// [clang] '(' parameter-list ')'
1263///
Sebastian Redl1d922962008-12-13 15:32:12 +00001264Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001265 assert(Tok.is(tok::caret) && "block literal starts with ^");
1266 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001267
Chris Lattner6b91f002009-03-05 07:32:12 +00001268 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1269 "block literal parsing");
1270
Steve Naroff296e8d52008-08-28 19:20:44 +00001271 // Enter a scope to hold everything within the block. This includes the
1272 // argument decls, decls within the compound expression, etc. This also
1273 // allows determining whether a variable reference inside the block is
1274 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001275 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1276 Scope::BreakScope | Scope::ContinueScope |
1277 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001278
1279 // Inform sema that we are starting a block.
1280 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001281
Steve Naroff296e8d52008-08-28 19:20:44 +00001282 // Parse the return type if present.
1283 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001284 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001285 // FIXME: Since the return type isn't actually parsed, it can't be used to
1286 // fill ParamInfo with an initial valid range, so do it manually.
1287 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001288
Steve Naroff296e8d52008-08-28 19:20:44 +00001289 // If this block has arguments, parse them. There is no ambiguity here with
1290 // the expression case, because the expression case requires a parameter list.
1291 if (Tok.is(tok::l_paren)) {
1292 ParseParenDeclarator(ParamInfo);
1293 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001294 // SetIdentifier sets the source range end, but in this case we're past
1295 // that location.
1296 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001297 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001298 ParamInfo.SetRangeEnd(Tmp);
Steve Naroff296e8d52008-08-28 19:20:44 +00001299 if (ParamInfo.getInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001300 // If there was an error parsing the arguments, they may have
1301 // tried to use ^(x+y) which requires an argument list. Just
1302 // skip the whole block literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001303 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001304 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001305 // Inform sema that we are starting a block.
1306 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1307 } else if (! Tok.is(tok::l_brace)) {
1308 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001309 } else {
1310 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001311 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1312 SourceLocation(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001313 0, 0, 0, CaretLoc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001314 ParamInfo),
1315 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001316 // Inform sema that we are starting a block.
1317 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001318 }
1319
Sebastian Redl1d922962008-12-13 15:32:12 +00001320
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001321 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001322 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001323 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001324 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001325 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001326 } else {
1327 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001328 }
Mike Stump281481d2009-02-02 23:46:21 +00001329 } else {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001330 // Saw something like: ^expr
1331 Diag(Tok, diag::err_expected_expression);
1332 return ExprError();
1333 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001334 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001335}
1336