blob: c0b28775ceeb892e90d131b4a5fff4d00f0cc1db [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
Steve Naroff61f72cb2009-03-09 21:12:44 +0000572 // Support 'Class.property' notation.
573 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
574 // 'super' (which is inappropriate here).
575 if (getLang().ObjC1 &&
576 Actions.getTypeName(*Tok.getIdentifierInfo(),
577 Tok.getLocation(), CurScope) &&
578 NextToken().is(tok::period)) {
579 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
580 SourceLocation IdentLoc = ConsumeToken();
581 SourceLocation DotLoc = ConsumeToken();
582
583 if (Tok.isNot(tok::identifier)) {
584 Diag(Tok, diag::err_expected_ident);
585 return ExprError();
586 }
587 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
588 SourceLocation PropertyLoc = ConsumeToken();
589
590 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
591 IdentLoc, PropertyLoc);
592 return move(Res);
593 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 // Consume the identifier so that we can see if it is followed by a '('.
595 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
596 // need to know whether or not this identifier is a function designator or
597 // not.
598 IdentifierInfo &II = *Tok.getIdentifierInfo();
599 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000600 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000602 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 }
604 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000605 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 ConsumeToken();
607 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000608 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
610 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
611 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000612 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 ConsumeToken();
614 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000615 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 case tok::string_literal: // primary-expression: string-literal
617 case tok::wide_string_literal:
618 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000619 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000621 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 case tok::kw___builtin_va_arg:
623 case tok::kw___builtin_offsetof:
624 case tok::kw___builtin_choose_expr:
625 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000626 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000627 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000628 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000629 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 case tok::plusplus: // unary-expression: '++' unary-expression
631 case tok::minusminus: { // unary-expression: '--' unary-expression
632 SourceLocation SavedLoc = ConsumeToken();
633 Res = ParseCastExpression(true);
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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000638 case tok::amp: { // unary-expression: '&' cast-expression
639 // Special treatment because of member pointers
640 SourceLocation SavedLoc = ConsumeToken();
641 Res = ParseCastExpression(false, true);
642 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000643 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000644 return move(Res);
645 }
646
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 case tok::star: // unary-expression: '*' cast-expression
648 case tok::plus: // unary-expression: '+' cast-expression
649 case tok::minus: // unary-expression: '-' cast-expression
650 case tok::tilde: // unary-expression: '~' cast-expression
651 case tok::exclaim: // unary-expression: '!' cast-expression
652 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000653 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 SourceLocation SavedLoc = ConsumeToken();
655 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000656 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000657 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000658 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000659 }
660
Chris Lattner35080842008-02-02 20:20:10 +0000661 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
662 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000663 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000664 SourceLocation SavedLoc = ConsumeToken();
665 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000666 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000667 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000668 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 }
670 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
671 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000672 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
674 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000675 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000676 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 case tok::ampamp: { // unary-expression: '&&' identifier
678 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000679 if (Tok.isNot(tok::identifier))
680 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000683 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 Tok.getIdentifierInfo());
685 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000686 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 }
688 case tok::kw_const_cast:
689 case tok::kw_dynamic_cast:
690 case tok::kw_reinterpret_cast:
691 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000692 Res = ParseCXXCasts();
693 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000694 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000695 case tok::kw_typeid:
696 Res = ParseCXXTypeid();
697 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000698 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000699 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000700 Res = ParseCXXThis();
701 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000702 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000703
704 case tok::kw_char:
705 case tok::kw_wchar_t:
706 case tok::kw_bool:
707 case tok::kw_short:
708 case tok::kw_int:
709 case tok::kw_long:
710 case tok::kw_signed:
711 case tok::kw_unsigned:
712 case tok::kw_float:
713 case tok::kw_double:
714 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000715 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000716 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000717 if (!getLang().CPlusPlus) {
718 Diag(Tok, diag::err_expected_expression);
719 return ExprError();
720 }
721
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000722 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
723 //
724 DeclSpec DS;
725 ParseCXXSimpleTypeSpecifier(DS);
726 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000727 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
728 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000729
730 Res = ParseCXXTypeConstructExpression(DS);
731 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000732 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000733 }
734
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000735 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
736 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
737 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000738 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000739 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000740
Chris Lattner74ba4102009-01-04 22:52:14 +0000741 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000742 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
743 // annotates the token, tail recurse.
744 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000745 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
746
Chris Lattner74ba4102009-01-04 22:52:14 +0000747 // ::new -> [C++] new-expression
748 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000749 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000750 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000751 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000752 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000753 return ParseCXXDeleteExpression(true, CCLoc);
754
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000755 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000756 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000757 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000758 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000759
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000760 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000761 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000762
763 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000764 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000765
Sebastian Redl64b45f72009-01-05 20:52:13 +0000766 case tok::kw___is_pod: // [GNU] unary-type-trait
767 case tok::kw___is_class:
768 case tok::kw___is_enum:
769 case tok::kw___is_union:
770 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000771 case tok::kw___is_abstract:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000772 return ParseUnaryTypeTrait();
773
Chris Lattnerc97c2042007-10-03 22:03:06 +0000774 case tok::at: {
775 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000776 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000777 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000778 case tok::caret:
779 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000780 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000781 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000782 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000783 case tok::l_square:
784 // These can be followed by postfix-expr pieces.
785 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000786 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000787 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 default:
789 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000790 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 // unreachable.
794 abort();
795}
796
797/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
798/// is parsed, this method parses any suffixes that apply.
799///
800/// postfix-expression: [C99 6.5.2]
801/// primary-expression
802/// postfix-expression '[' expression ']'
803/// postfix-expression '(' argument-expression-list[opt] ')'
804/// postfix-expression '.' identifier
805/// postfix-expression '->' identifier
806/// postfix-expression '++'
807/// postfix-expression '--'
808/// '(' type-name ')' '{' initializer-list '}'
809/// '(' type-name ')' '{' initializer-list ',' '}'
810///
811/// argument-expression-list: [C99 6.5.2]
812/// argument-expression
813/// argument-expression-list ',' assignment-expression
814///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000815Parser::OwningExprResult
816Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 // Now that the primary-expression piece of the postfix-expression has been
818 // parsed, see if there are any postfix-expression pieces here.
819 SourceLocation Loc;
820 while (1) {
821 switch (Tok.getKind()) {
822 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000823 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
825 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000826 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000827
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000829
830 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000831 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
832 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000833 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000834 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000835
836 // Match the ']'.
837 MatchRHSPunctuation(tok::r_square, Loc);
838 break;
839 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000840
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000842 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000843 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000846
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000847 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000848 if (ParseExpressionList(ArgExprs, CommaLocs)) {
849 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000850 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 }
852 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000855 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
857 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000858 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000859 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000860 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000862
Chris Lattner2ff54262007-07-21 05:18:12 +0000863 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 break;
865 }
866 case tok::arrow: // postfix-expression: p-e '->' identifier
867 case tok::period: { // postfix-expression: p-e '.' identifier
868 tok::TokenKind OpKind = Tok.getKind();
869 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000870
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000871 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000873 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000875
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000876 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000877 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000878 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000879 *Tok.getIdentifierInfo(),
880 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000881 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 ConsumeToken();
883 break;
884 }
885 case tok::plusplus: // postfix-expression: postfix-expression '++'
886 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000887 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000888 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000889 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000890 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 ConsumeToken();
892 break;
893 }
894 }
895}
896
897
898/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
899/// unary-expression: [C99 6.5.3]
900/// 'sizeof' unary-expression
901/// 'sizeof' '(' type-name ')'
902/// [GNU] '__alignof' unary-expression
903/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000904/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000905Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000906 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
907 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000909 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 ConsumeToken();
911
912 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000913 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000914 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 Operand = ParseCastExpression(true);
916 } else {
917 // If it starts with a '(', we know that it is either a parenthesized
918 // type-name, or it is a unary-expression that starts with a compound
919 // literal, or starts with a primary-expression that is a parenthesized
920 // expression.
921 ParenParseOption ExprType = CastExpr;
922 TypeTy *CastTy;
923 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
924 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000925
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
927 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000928 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000929 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000930 OpTok.is(tok::kw_sizeof),
931 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000932 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000933
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000934 // If this is a parenthesized expression, it is the start of a
935 // unary-expression, but doesn't include any postfix pieces. Parse these
936 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000937 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000941 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000942 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
943 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000944 /*isType=*/false,
945 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000946 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000947}
948
949/// ParseBuiltinPrimaryExpression
950///
951/// primary-expression: [C99 6.5.1]
952/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
953/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
954/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
955/// assign-expr ')'
956/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
957///
958/// [GNU] offsetof-member-designator:
959/// [GNU] identifier
960/// [GNU] offsetof-member-designator '.' identifier
961/// [GNU] offsetof-member-designator '[' expression ']'
962///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000963Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000964 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
966
967 tok::TokenKind T = Tok.getKind();
968 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
969
970 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000971 if (Tok.isNot(tok::l_paren))
972 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
973 << BuiltinII);
974
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 SourceLocation LParenLoc = ConsumeParen();
976 // TODO: Build AST.
977
978 switch (T) {
979 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000980 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000981 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000984 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 }
986
987 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000988 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000989
Douglas Gregor809070a2009-02-18 17:45:20 +0000990 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000991
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000992 if (Tok.isNot(tok::r_paren)) {
993 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000994 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000995 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000996 if (Ty.isInvalid())
997 Res = ExprError();
998 else
Sebastian Redlf53597f2009-03-15 17:47:39 +0000999 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001001 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001002 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001003 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001004 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001005 if (Ty.isInvalid()) {
1006 SkipUntil(tok::r_paren);
1007 return ExprError();
1008 }
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001011 return ExprError();
1012
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001014 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001015 Diag(Tok, diag::err_expected_ident);
1016 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001017 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001018 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001019
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001020 // Keep track of the various subcomponents we see.
1021 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001022
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001023 Comps.push_back(Action::OffsetOfComponent());
1024 Comps.back().isBrackets = false;
1025 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1026 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001027
Sebastian Redla55e52c2008-11-25 22:21:31 +00001028 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001030 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001032 Comps.push_back(Action::OffsetOfComponent());
1033 Comps.back().isBrackets = false;
1034 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001035
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001036 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001037 Diag(Tok, diag::err_expected_ident);
1038 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001039 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001040 }
1041 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1042 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001043
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001044 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001046 Comps.push_back(Action::OffsetOfComponent());
1047 Comps.back().isBrackets = true;
1048 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001050 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001052 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001054 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001055
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001056 Comps.back().LocEnd =
1057 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001058 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001059 if (Ty.isInvalid())
1060 Res = ExprError();
1061 else
1062 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1063 Ty.get(), &Comps[0],
1064 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001065 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001067 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001068 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 }
1070 }
1071 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001072 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001073 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001074 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001075 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001076 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001077 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001078 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001080 return ExprError();
1081
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001082 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001083 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001084 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001085 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001086 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001088 return ExprError();
1089
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001090 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001091 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001092 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001093 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001094 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001095 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001096 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001097 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001098 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001099 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1100 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001101 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001102 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001104 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001107 return ExprError();
1108
Douglas Gregor809070a2009-02-18 17:45:20 +00001109 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001110
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001111 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001112 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001113 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001114 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001115
1116 if (Ty1.isInvalid() || Ty2.isInvalid())
1117 Res = ExprError();
1118 else
1119 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1120 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001121 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001122 }
1123
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 // These can be followed by postfix-expr pieces because they are
1125 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001126 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001127}
1128
1129/// ParseParenExpression - This parses the unit that starts with a '(' token,
1130/// based on what is allowed by ExprType. The actual thing parsed is returned
1131/// in ExprType.
1132///
1133/// primary-expression: [C99 6.5.1]
1134/// '(' expression ')'
1135/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1136/// postfix-expression: [C99 6.5.2]
1137/// '(' type-name ')' '{' initializer-list '}'
1138/// '(' type-name ')' '{' initializer-list ',' '}'
1139/// cast-expression: [C99 6.5.4]
1140/// '(' type-name ')' cast-expression
1141///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001142Parser::OwningExprResult
1143Parser::ParseParenExpression(ParenParseOption &ExprType,
1144 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001145 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001146 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001148 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001150
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001151 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001153 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001155
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001156 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001157 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001158 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001159
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001160 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001162 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
1164 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001165 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 RParenLoc = ConsumeParen();
1167 else
1168 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001169
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001170 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 if (!getLang().C99) // Compound literals don't exist in C90.
1172 Diag(OpenLoc, diag::ext_c99_compound_literal);
1173 Result = ParseInitializer();
1174 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001175 if (!Result.isInvalid() && !Ty.isInvalid())
1176 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001177 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001178 return move(Result);
1179 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001180
Chris Lattner42ece642008-12-12 06:00:12 +00001181 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001182 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 // returns the parsed type to the callee.
1184 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001185
1186 if (Ty.isInvalid())
1187 return ExprError();
1188
1189 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001190 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001192
Chris Lattner42ece642008-12-12 06:00:12 +00001193 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1194 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 } else {
1196 Result = ParseExpression();
1197 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001198 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001199 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001203 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001205 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
Chris Lattner42ece642008-12-12 06:00:12 +00001207
1208 if (Tok.is(tok::r_paren))
1209 RParenLoc = ConsumeParen();
1210 else
1211 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001212
1213 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214}
1215
1216/// ParseStringLiteralExpression - This handles the various token types that
1217/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1218/// translation phase #6].
1219///
1220/// primary-expression: [C99 6.5.1]
1221/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001222Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001224
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1226 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001227 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 do {
1230 StringToks.push_back(Tok);
1231 ConsumeStringToken();
1232 } while (isTokenStringLiteral());
1233
1234 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001235 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001236}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001237
1238/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1239///
1240/// argument-expression-list:
1241/// assignment-expression
1242/// argument-expression-list , assignment-expression
1243///
1244/// [C++] expression-list:
1245/// [C++] assignment-expression
1246/// [C++] expression-list , assignment-expression
1247///
1248bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1249 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001250 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001251 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001252 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001253
Sebastian Redleffa8d12008-12-10 00:02:53 +00001254 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001255
1256 if (Tok.isNot(tok::comma))
1257 return false;
1258 // Move to the next argument, remember where the comma was.
1259 CommaLocs.push_back(ConsumeToken());
1260 }
1261}
Steve Naroff296e8d52008-08-28 19:20:44 +00001262
Mike Stump98eb8a72009-02-04 22:31:32 +00001263/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1264///
1265/// [clang] block-id:
1266/// [clang] specifier-qualifier-list block-declarator
1267///
1268void Parser::ParseBlockId() {
1269 // Parse the specifier-qualifier-list piece.
1270 DeclSpec DS;
1271 ParseSpecifierQualifierList(DS);
1272
1273 // Parse the block-declarator.
1274 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1275 ParseDeclarator(DeclaratorInfo);
1276 // Inform sema that we are starting a block.
1277 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1278}
1279
Steve Naroff296e8d52008-08-28 19:20:44 +00001280/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001281/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001282///
1283/// block-literal:
1284/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001285/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001286/// [clang] block-args:
1287/// [clang] '(' parameter-list ')'
1288///
Sebastian Redl1d922962008-12-13 15:32:12 +00001289Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001290 assert(Tok.is(tok::caret) && "block literal starts with ^");
1291 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001292
Chris Lattner6b91f002009-03-05 07:32:12 +00001293 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1294 "block literal parsing");
1295
Steve Naroff296e8d52008-08-28 19:20:44 +00001296 // Enter a scope to hold everything within the block. This includes the
1297 // argument decls, decls within the compound expression, etc. This also
1298 // allows determining whether a variable reference inside the block is
1299 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001300 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1301 Scope::BreakScope | Scope::ContinueScope |
1302 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001303
1304 // Inform sema that we are starting a block.
1305 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001306
Steve Naroff296e8d52008-08-28 19:20:44 +00001307 // Parse the return type if present.
1308 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001309 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001310 // FIXME: Since the return type isn't actually parsed, it can't be used to
1311 // fill ParamInfo with an initial valid range, so do it manually.
1312 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001313
Steve Naroff296e8d52008-08-28 19:20:44 +00001314 // If this block has arguments, parse them. There is no ambiguity here with
1315 // the expression case, because the expression case requires a parameter list.
1316 if (Tok.is(tok::l_paren)) {
1317 ParseParenDeclarator(ParamInfo);
1318 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001319 // SetIdentifier sets the source range end, but in this case we're past
1320 // that location.
1321 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001322 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001323 ParamInfo.SetRangeEnd(Tmp);
Steve Naroff296e8d52008-08-28 19:20:44 +00001324 if (ParamInfo.getInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001325 // If there was an error parsing the arguments, they may have
1326 // tried to use ^(x+y) which requires an argument list. Just
1327 // skip the whole block literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001328 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001329 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001330 // Inform sema that we are starting a block.
1331 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1332 } else if (! Tok.is(tok::l_brace)) {
1333 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001334 } else {
1335 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001336 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1337 SourceLocation(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001338 0, 0, 0, CaretLoc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001339 ParamInfo),
1340 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001341 // Inform sema that we are starting a block.
1342 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001343 }
1344
Sebastian Redl1d922962008-12-13 15:32:12 +00001345
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001346 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001347 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001348 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001349 if (!Stmt.isInvalid()) {
Sebastian Redlf53597f2009-03-15 17:47:39 +00001350 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001351 } else {
1352 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001353 }
Mike Stump281481d2009-02-02 23:46:21 +00001354 } else {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001355 // Saw something like: ^expr
1356 Diag(Tok, diag::err_expected_expression);
1357 return ExprError();
1358 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001359 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001360}
1361