blob: 20a8359d1d3a36f61ced16bba65913aa67996d64 [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);
Steve Naroffed91f902009-04-02 18:37:59 +0000592 // These can be followed by postfix-expr pieces.
593 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000594 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 // Consume the identifier so that we can see if it is followed by a '('.
596 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
597 // need to know whether or not this identifier is a function designator or
598 // not.
599 IdentifierInfo &II = *Tok.getIdentifierInfo();
600 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000601 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000603 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 }
605 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000606 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 ConsumeToken();
608 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000609 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
611 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
612 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000613 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 ConsumeToken();
615 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000616 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 case tok::string_literal: // primary-expression: string-literal
618 case tok::wide_string_literal:
619 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000620 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000622 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 case tok::kw___builtin_va_arg:
624 case tok::kw___builtin_offsetof:
625 case tok::kw___builtin_choose_expr:
626 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000627 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000628 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000629 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000630 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 case tok::plusplus: // unary-expression: '++' unary-expression
632 case tok::minusminus: { // unary-expression: '--' unary-expression
633 SourceLocation SavedLoc = ConsumeToken();
634 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000635 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000636 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000637 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000639 case tok::amp: { // unary-expression: '&' cast-expression
640 // Special treatment because of member pointers
641 SourceLocation SavedLoc = ConsumeToken();
642 Res = ParseCastExpression(false, true);
643 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000644 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000645 return move(Res);
646 }
647
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 case tok::star: // unary-expression: '*' cast-expression
649 case tok::plus: // unary-expression: '+' cast-expression
650 case tok::minus: // unary-expression: '-' cast-expression
651 case tok::tilde: // unary-expression: '~' cast-expression
652 case tok::exclaim: // unary-expression: '!' cast-expression
653 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000654 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 SourceLocation SavedLoc = ConsumeToken();
656 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000657 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000658 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000659 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000660 }
661
Chris Lattner35080842008-02-02 20:20:10 +0000662 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
663 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000664 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000665 SourceLocation SavedLoc = ConsumeToken();
666 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000667 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000668 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000669 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 }
671 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
672 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000673 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
675 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000676 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000677 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 case tok::ampamp: { // unary-expression: '&&' identifier
679 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000680 if (Tok.isNot(tok::identifier))
681 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000684 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 Tok.getIdentifierInfo());
686 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000687 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 }
689 case tok::kw_const_cast:
690 case tok::kw_dynamic_cast:
691 case tok::kw_reinterpret_cast:
692 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000693 Res = ParseCXXCasts();
694 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000695 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000696 case tok::kw_typeid:
697 Res = ParseCXXTypeid();
698 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000699 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000700 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000701 Res = ParseCXXThis();
702 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000703 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000704
705 case tok::kw_char:
706 case tok::kw_wchar_t:
707 case tok::kw_bool:
708 case tok::kw_short:
709 case tok::kw_int:
710 case tok::kw_long:
711 case tok::kw_signed:
712 case tok::kw_unsigned:
713 case tok::kw_float:
714 case tok::kw_double:
715 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000716 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000717 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000718 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000719 if (!getLang().CPlusPlus) {
720 Diag(Tok, diag::err_expected_expression);
721 return ExprError();
722 }
723
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000724 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
725 //
726 DeclSpec DS;
727 ParseCXXSimpleTypeSpecifier(DS);
728 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000729 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
730 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000731
732 Res = ParseCXXTypeConstructExpression(DS);
733 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000734 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000735 }
736
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000737 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
738 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
739 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000740 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000741 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000742
Chris Lattner74ba4102009-01-04 22:52:14 +0000743 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000744 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
745 // annotates the token, tail recurse.
746 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000747 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
748
Chris Lattner74ba4102009-01-04 22:52:14 +0000749 // ::new -> [C++] new-expression
750 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000751 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000752 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000753 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000754 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000755 return ParseCXXDeleteExpression(true, CCLoc);
756
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000757 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000758 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000759 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000760 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000761
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000762 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000763 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000764
765 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000766 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000767
Sebastian Redl64b45f72009-01-05 20:52:13 +0000768 case tok::kw___is_pod: // [GNU] unary-type-trait
769 case tok::kw___is_class:
770 case tok::kw___is_enum:
771 case tok::kw___is_union:
772 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000773 case tok::kw___is_abstract:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000774 return ParseUnaryTypeTrait();
775
Chris Lattnerc97c2042007-10-03 22:03:06 +0000776 case tok::at: {
777 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000778 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000779 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000780 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000781 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000782 case tok::l_square:
783 // These can be followed by postfix-expr pieces.
784 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000785 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000786 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 default:
788 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000789 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 // unreachable.
793 abort();
794}
795
796/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
797/// is parsed, this method parses any suffixes that apply.
798///
799/// postfix-expression: [C99 6.5.2]
800/// primary-expression
801/// postfix-expression '[' expression ']'
802/// postfix-expression '(' argument-expression-list[opt] ')'
803/// postfix-expression '.' identifier
804/// postfix-expression '->' identifier
805/// postfix-expression '++'
806/// postfix-expression '--'
807/// '(' type-name ')' '{' initializer-list '}'
808/// '(' type-name ')' '{' initializer-list ',' '}'
809///
810/// argument-expression-list: [C99 6.5.2]
811/// argument-expression
812/// argument-expression-list ',' assignment-expression
813///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000814Parser::OwningExprResult
815Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 // Now that the primary-expression piece of the postfix-expression has been
817 // parsed, see if there are any postfix-expression pieces here.
818 SourceLocation Loc;
819 while (1) {
820 switch (Tok.getKind()) {
821 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000822 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
824 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000825 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000826
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000828
829 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000830 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
831 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000832 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000833 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000834
835 // Match the ']'.
836 MatchRHSPunctuation(tok::r_square, Loc);
837 break;
838 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000841 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000842 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000843
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000845
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000846 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000847 if (ParseExpressionList(ArgExprs, CommaLocs)) {
848 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000849 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 }
851 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000854 if (Tok.isNot(tok::r_paren)) {
855 MatchRHSPunctuation(tok::r_paren, Loc);
856 return ExprError();
857 }
858
859 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
861 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000862 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000863 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000864 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000866
867 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 break;
869 }
870 case tok::arrow: // postfix-expression: p-e '->' identifier
871 case tok::period: { // postfix-expression: p-e '.' identifier
872 tok::TokenKind OpKind = Tok.getKind();
873 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000874
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000875 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000877 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000879
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000880 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000881 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000882 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000883 *Tok.getIdentifierInfo(),
884 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000885 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 ConsumeToken();
887 break;
888 }
889 case tok::plusplus: // postfix-expression: postfix-expression '++'
890 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000891 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000892 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000893 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000894 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 ConsumeToken();
896 break;
897 }
898 }
899}
900
901
902/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
903/// unary-expression: [C99 6.5.3]
904/// 'sizeof' unary-expression
905/// 'sizeof' '(' type-name ')'
906/// [GNU] '__alignof' unary-expression
907/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000908/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000909Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000910 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
911 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000913 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 ConsumeToken();
915
916 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000917 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000918 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 Operand = ParseCastExpression(true);
920 } else {
921 // If it starts with a '(', we know that it is either a parenthesized
922 // type-name, or it is a unary-expression that starts with a compound
923 // literal, or starts with a primary-expression that is a parenthesized
924 // expression.
925 ParenParseOption ExprType = CastExpr;
926 TypeTy *CastTy;
927 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
928 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
931 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000932 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000933 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000934 OpTok.is(tok::kw_sizeof),
935 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000936 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000937
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000938 // If this is a parenthesized expression, it is the start of a
939 // unary-expression, but doesn't include any postfix pieces. Parse these
940 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000941 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000945 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000946 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
947 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000948 /*isType=*/false,
949 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000950 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
953/// ParseBuiltinPrimaryExpression
954///
955/// primary-expression: [C99 6.5.1]
956/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
957/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
958/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
959/// assign-expr ')'
960/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
961///
962/// [GNU] offsetof-member-designator:
963/// [GNU] identifier
964/// [GNU] offsetof-member-designator '.' identifier
965/// [GNU] offsetof-member-designator '[' expression ']'
966///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000967Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000968 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
970
971 tok::TokenKind T = Tok.getKind();
972 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
973
974 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000975 if (Tok.isNot(tok::l_paren))
976 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
977 << BuiltinII);
978
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 SourceLocation LParenLoc = ConsumeParen();
980 // TODO: Build AST.
981
982 switch (T) {
983 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000984 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000985 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000986 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000988 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 }
990
991 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000992 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000993
Douglas Gregor809070a2009-02-18 17:45:20 +0000994 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000995
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000996 if (Tok.isNot(tok::r_paren)) {
997 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000998 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000999 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001000 if (Ty.isInvalid())
1001 Res = ExprError();
1002 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001003 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001005 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001006 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001007 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001008 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001009 if (Ty.isInvalid()) {
1010 SkipUntil(tok::r_paren);
1011 return ExprError();
1012 }
1013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001015 return ExprError();
1016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001018 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001019 Diag(Tok, diag::err_expected_ident);
1020 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001021 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001022 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001023
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001024 // Keep track of the various subcomponents we see.
1025 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001026
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001027 Comps.push_back(Action::OffsetOfComponent());
1028 Comps.back().isBrackets = false;
1029 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1030 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001031
Sebastian Redla55e52c2008-11-25 22:21:31 +00001032 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001034 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001036 Comps.push_back(Action::OffsetOfComponent());
1037 Comps.back().isBrackets = false;
1038 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001039
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001040 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001041 Diag(Tok, diag::err_expected_ident);
1042 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001043 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001044 }
1045 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1046 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001047
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001048 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001050 Comps.push_back(Action::OffsetOfComponent());
1051 Comps.back().isBrackets = true;
1052 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001054 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001058 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001059
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001060 Comps.back().LocEnd =
1061 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001062 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001063 if (Ty.isInvalid())
1064 Res = ExprError();
1065 else
1066 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1067 Ty.get(), &Comps[0],
1068 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001069 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001071 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001072 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 }
1074 }
1075 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001076 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001077 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001078 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001079 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001080 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001081 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001082 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001084 return ExprError();
1085
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001086 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001087 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001088 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001089 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001090 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001092 return ExprError();
1093
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001094 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001095 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001096 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001097 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001098 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001099 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001100 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001101 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001102 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001103 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1104 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001105 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001106 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001108 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001109
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001111 return ExprError();
1112
Douglas Gregor809070a2009-02-18 17:45:20 +00001113 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001114
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001115 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001116 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001117 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001118 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001119
1120 if (Ty1.isInvalid() || Ty2.isInvalid())
1121 Res = ExprError();
1122 else
1123 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1124 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001125 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001126 }
1127
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 // These can be followed by postfix-expr pieces because they are
1129 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001130 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001131}
1132
1133/// ParseParenExpression - This parses the unit that starts with a '(' token,
1134/// based on what is allowed by ExprType. The actual thing parsed is returned
1135/// in ExprType.
1136///
1137/// primary-expression: [C99 6.5.1]
1138/// '(' expression ')'
1139/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1140/// postfix-expression: [C99 6.5.2]
1141/// '(' type-name ')' '{' initializer-list '}'
1142/// '(' type-name ')' '{' initializer-list ',' '}'
1143/// cast-expression: [C99 6.5.4]
1144/// '(' type-name ')' cast-expression
1145///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001146Parser::OwningExprResult
1147Parser::ParseParenExpression(ParenParseOption &ExprType,
1148 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001149 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001150 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001152 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001154
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001155 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001157 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001159
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001160 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001161 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001162 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001163
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001164 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001166 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
1168 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001169 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 RParenLoc = ConsumeParen();
1171 else
1172 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001173
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001174 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 if (!getLang().C99) // Compound literals don't exist in C90.
1176 Diag(OpenLoc, diag::ext_c99_compound_literal);
1177 Result = ParseInitializer();
1178 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001179 if (!Result.isInvalid() && !Ty.isInvalid())
1180 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001181 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001182 return move(Result);
1183 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001184
Chris Lattner42ece642008-12-12 06:00:12 +00001185 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001186 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 // returns the parsed type to the callee.
1188 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001189
1190 if (Ty.isInvalid())
1191 return ExprError();
1192
1193 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001194 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001196
Chris Lattner42ece642008-12-12 06:00:12 +00001197 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1198 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 } else {
1200 Result = ParseExpression();
1201 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001202 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001203 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001205
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001207 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001209 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
Chris Lattner42ece642008-12-12 06:00:12 +00001211
1212 if (Tok.is(tok::r_paren))
1213 RParenLoc = ConsumeParen();
1214 else
1215 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001216
1217 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
1220/// ParseStringLiteralExpression - This handles the various token types that
1221/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1222/// translation phase #6].
1223///
1224/// primary-expression: [C99 6.5.1]
1225/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001226Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1230 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001231 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001232
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 do {
1234 StringToks.push_back(Tok);
1235 ConsumeStringToken();
1236 } while (isTokenStringLiteral());
1237
1238 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001239 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001240}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001241
1242/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1243///
1244/// argument-expression-list:
1245/// assignment-expression
1246/// argument-expression-list , assignment-expression
1247///
1248/// [C++] expression-list:
1249/// [C++] assignment-expression
1250/// [C++] expression-list , assignment-expression
1251///
1252bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1253 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001254 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001255 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001256 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001257
Sebastian Redleffa8d12008-12-10 00:02:53 +00001258 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001259
1260 if (Tok.isNot(tok::comma))
1261 return false;
1262 // Move to the next argument, remember where the comma was.
1263 CommaLocs.push_back(ConsumeToken());
1264 }
1265}
Steve Naroff296e8d52008-08-28 19:20:44 +00001266
Mike Stump98eb8a72009-02-04 22:31:32 +00001267/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1268///
1269/// [clang] block-id:
1270/// [clang] specifier-qualifier-list block-declarator
1271///
1272void Parser::ParseBlockId() {
1273 // Parse the specifier-qualifier-list piece.
1274 DeclSpec DS;
1275 ParseSpecifierQualifierList(DS);
1276
1277 // Parse the block-declarator.
1278 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1279 ParseDeclarator(DeclaratorInfo);
1280 // Inform sema that we are starting a block.
1281 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1282}
1283
Steve Naroff296e8d52008-08-28 19:20:44 +00001284/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001285/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001286///
1287/// block-literal:
1288/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001289/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001290/// [clang] block-args:
1291/// [clang] '(' parameter-list ')'
1292///
Sebastian Redl1d922962008-12-13 15:32:12 +00001293Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001294 assert(Tok.is(tok::caret) && "block literal starts with ^");
1295 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001296
Chris Lattner6b91f002009-03-05 07:32:12 +00001297 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1298 "block literal parsing");
1299
Steve Naroff296e8d52008-08-28 19:20:44 +00001300 // Enter a scope to hold everything within the block. This includes the
1301 // argument decls, decls within the compound expression, etc. This also
1302 // allows determining whether a variable reference inside the block is
1303 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001304 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1305 Scope::BreakScope | Scope::ContinueScope |
1306 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001307
1308 // Inform sema that we are starting a block.
1309 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001310
Steve Naroff296e8d52008-08-28 19:20:44 +00001311 // Parse the return type if present.
1312 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001313 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001314 // FIXME: Since the return type isn't actually parsed, it can't be used to
1315 // fill ParamInfo with an initial valid range, so do it manually.
1316 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001317
Steve Naroff296e8d52008-08-28 19:20:44 +00001318 // If this block has arguments, parse them. There is no ambiguity here with
1319 // the expression case, because the expression case requires a parameter list.
1320 if (Tok.is(tok::l_paren)) {
1321 ParseParenDeclarator(ParamInfo);
1322 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001323 // SetIdentifier sets the source range end, but in this case we're past
1324 // that location.
1325 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001326 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001327 ParamInfo.SetRangeEnd(Tmp);
Steve Naroff296e8d52008-08-28 19:20:44 +00001328 if (ParamInfo.getInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001329 // If there was an error parsing the arguments, they may have
1330 // tried to use ^(x+y) which requires an argument list. Just
1331 // skip the whole block literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001332 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001333 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001334 // Inform sema that we are starting a block.
1335 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001336 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001337 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001338 } else {
1339 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001340 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1341 SourceLocation(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001342 0, 0, 0, CaretLoc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001343 ParamInfo),
1344 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001345 // Inform sema that we are starting a block.
1346 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001347 }
1348
Sebastian Redl1d922962008-12-13 15:32:12 +00001349
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001350 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001351 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001352 // Saw something like: ^expr
1353 Diag(Tok, diag::err_expected_expression);
1354 return ExprError();
1355 }
Chris Lattner9af55002009-03-27 04:18:06 +00001356
1357 OwningStmtResult Stmt(ParseCompoundStatementBody());
1358 if (!Stmt.isInvalid())
1359 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1360 else
1361 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001362 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001363}
1364