blob: 2536cee1cbdbcee3e28e678630fea01704ac1701 [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 Lattnerd167ca02009-12-10 00:21:05 +000026#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Mike Stump1eb44332009-09-09 15:08:12 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000059 bool GreaterThanIsOperator,
60 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000062 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000063 // C++ [temp.names]p3:
64 // [...] When parsing a template-argument-list, the first
65 // non-nested > is taken as the ending delimiter rather than a
66 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000067 if (GreaterThanIsOperator)
68 return prec::Relational;
69 return prec::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +000070
Douglas Gregor3965b7b2009-02-25 23:02:36 +000071 case tok::greatergreater:
72 // C++0x [temp.names]p3:
73 //
74 // [...] Similarly, the first non-nested >> is treated as two
75 // consecutive but distinct > tokens, the first of which is
76 // taken as the end of the template-argument-list and completes
77 // the template-id. [...]
78 if (GreaterThanIsOperator || !CPlusPlus0x)
79 return prec::Shift;
80 return prec::Unknown;
81
Reid Spencer5f016e22007-07-11 17:01:13 +000082 default: return prec::Unknown;
83 case tok::comma: return prec::Comma;
84 case tok::equal:
85 case tok::starequal:
86 case tok::slashequal:
87 case tok::percentequal:
88 case tok::plusequal:
89 case tok::minusequal:
90 case tok::lesslessequal:
91 case tok::greatergreaterequal:
92 case tok::ampequal:
93 case tok::caretequal:
94 case tok::pipeequal: return prec::Assignment;
95 case tok::question: return prec::Conditional;
96 case tok::pipepipe: return prec::LogicalOr;
97 case tok::ampamp: return prec::LogicalAnd;
98 case tok::pipe: return prec::InclusiveOr;
99 case tok::caret: return prec::ExclusiveOr;
100 case tok::amp: return prec::And;
101 case tok::exclaimequal:
102 case tok::equalequal: return prec::Equality;
103 case tok::lessequal:
104 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000105 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000106 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 case tok::plus:
108 case tok::minus: return prec::Additive;
109 case tok::percent:
110 case tok::slash:
111 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000112 case tok::periodstar:
113 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
117
118/// ParseExpression - Simple precedence-based parser for binary/ternary
119/// operators.
120///
121/// Note: we diverge from the C99 grammar when parsing the assignment-expression
122/// production. C99 specifies that the LHS of an assignment operator should be
123/// parsed as a unary-expression, but consistency dictates that it be a
124/// conditional-expession. In practice, the important thing here is that the
125/// LHS of an assignment has to be an l-value, which productions between
126/// unary-expression and conditional-expression don't produce. Because we want
127/// consistency, we parse the LHS as a conditional-expression, then check for
128/// l-value-ness in semantic analysis stages.
129///
Sebastian Redl22460502009-02-07 00:15:38 +0000130/// pm-expression: [C++ 5.5]
131/// cast-expression
132/// pm-expression '.*' cast-expression
133/// pm-expression '->*' cast-expression
134///
Reid Spencer5f016e22007-07-11 17:01:13 +0000135/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000136/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000137/// cast-expression
138/// multiplicative-expression '*' cast-expression
139/// multiplicative-expression '/' cast-expression
140/// multiplicative-expression '%' cast-expression
141///
142/// additive-expression: [C99 6.5.6]
143/// multiplicative-expression
144/// additive-expression '+' multiplicative-expression
145/// additive-expression '-' multiplicative-expression
146///
147/// shift-expression: [C99 6.5.7]
148/// additive-expression
149/// shift-expression '<<' additive-expression
150/// shift-expression '>>' additive-expression
151///
152/// relational-expression: [C99 6.5.8]
153/// shift-expression
154/// relational-expression '<' shift-expression
155/// relational-expression '>' shift-expression
156/// relational-expression '<=' shift-expression
157/// relational-expression '>=' shift-expression
158///
159/// equality-expression: [C99 6.5.9]
160/// relational-expression
161/// equality-expression '==' relational-expression
162/// equality-expression '!=' relational-expression
163///
164/// AND-expression: [C99 6.5.10]
165/// equality-expression
166/// AND-expression '&' equality-expression
167///
168/// exclusive-OR-expression: [C99 6.5.11]
169/// AND-expression
170/// exclusive-OR-expression '^' AND-expression
171///
172/// inclusive-OR-expression: [C99 6.5.12]
173/// exclusive-OR-expression
174/// inclusive-OR-expression '|' exclusive-OR-expression
175///
176/// logical-AND-expression: [C99 6.5.13]
177/// inclusive-OR-expression
178/// logical-AND-expression '&&' inclusive-OR-expression
179///
180/// logical-OR-expression: [C99 6.5.14]
181/// logical-AND-expression
182/// logical-OR-expression '||' logical-AND-expression
183///
184/// conditional-expression: [C99 6.5.15]
185/// logical-OR-expression
186/// logical-OR-expression '?' expression ':' conditional-expression
187/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000188/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000189///
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() {
Douglas Gregor791215b2009-09-21 20:51:25 +0000203 if (Tok.is(tok::code_completion)) {
204 Actions.CodeCompleteOrdinaryName(CurScope);
205 ConsumeToken();
206 }
207
Mike Stump6ce0c392009-05-15 21:47:08 +0000208 OwningExprResult LHS(ParseAssignmentExpression());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000209 if (LHS.isInvalid()) return move(LHS);
210
Sebastian Redld8c4e152008-12-11 22:33:27 +0000211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
Mike Stump1eb44332009-09-09 15:08:12 +0000214/// This routine is called when the '@' is seen and consumed.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000215/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000216/// routine is necessary to disambiguate @try-statement from,
217/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000218///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000219Parser::OwningExprResult
220Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000221 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000222 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000223
Sebastian Redld8c4e152008-12-11 22:33:27 +0000224 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000225}
226
Eli Friedmanadf077f2009-01-27 08:43:38 +0000227/// This routine is called when a leading '__extension__' is seen and
228/// consumed. This is necessary because the token gets consumed in the
229/// process of disambiguating between an expression and a declaration.
230Parser::OwningExprResult
231Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000232 OwningExprResult LHS(Actions, true);
233 {
234 // Silence extension warnings in the sub-expression
235 ExtensionRAIIObject O(Diags);
236
237 LHS = ParseCastExpression(false);
238 if (LHS.isInvalid()) return move(LHS);
239 }
Eli Friedmanadf077f2009-01-27 08:43:38 +0000240
241 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000242 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000243 if (LHS.isInvalid()) return move(LHS);
244
245 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
246}
247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
249///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000250Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000251 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000252 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000253
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000254 OwningExprResult LHS(ParseCastExpression(false));
255 if (LHS.isInvalid()) return move(LHS);
256
Sebastian Redld8c4e152008-12-11 22:33:27 +0000257 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Chris Lattnerb93fb492008-06-02 21:31:07 +0000260/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
261/// where part of an objc message send has already been parsed. In this case
262/// LBracLoc indicates the location of the '[' of the message send, and either
263/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
264/// message.
265///
266/// Since this handles full assignment-expression's, it handles postfix
267/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000268Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000269Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000270 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000271 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000272 ExprArg ReceiverExpr) {
273 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
274 ReceiverName,
275 move(ReceiverExpr)));
276 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000277 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000278 if (R.isInvalid()) return move(R);
279 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000280}
281
282
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000283Parser::OwningExprResult Parser::ParseConstantExpression() {
Douglas Gregore0762c92009-06-19 23:52:42 +0000284 // C++ [basic.def.odr]p2:
Mike Stump1eb44332009-09-09 15:08:12 +0000285 // An expression is potentially evaluated unless it appears where an
Douglas Gregore0762c92009-06-19 23:52:42 +0000286 // integral constant expression is required (see 5.19) [...].
Douglas Gregorac7610d2009-06-22 20:57:11 +0000287 EnterExpressionEvaluationContext Unevaluated(Actions,
288 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000290 OwningExprResult LHS(ParseCastExpression(false));
291 if (LHS.isInvalid()) return move(LHS);
292
Sebastian Redld8c4e152008-12-11 22:33:27 +0000293 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
297/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000298Parser::OwningExprResult
299Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Mike Stump1eb44332009-09-09 15:08:12 +0000300 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000301 GreaterThanIsOperator,
302 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 SourceLocation ColonLoc;
304
305 while (1) {
306 // If this token has a lower precedence than we are allowed to parse (e.g.
307 // because we are called recursively, or because the token is not a binop),
308 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000309 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000310 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000311
312 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000313 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000317 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000319 if (Tok.isNot(tok::colon)) {
Chris Lattnera69d0ed2009-12-10 02:02:58 +0000320 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
321 ColonProtectionRAIIObject X(*this);
322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // Handle this production specially:
324 // logical-OR-expression '?' expression ':' conditional-expression
325 // In particular, the RHS of the '?' is 'expression', not
326 // 'logical-OR-expression' as we might expect.
327 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000328 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000329 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 } else {
331 // Special case handling of "X ? Y : Z" where Y is empty:
332 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000333 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 Diag(Tok, diag::ext_gnu_conditional_expr);
335 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000336
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000337 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000339 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000340 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 // Eat the colon.
344 ColonLoc = ConsumeToken();
345 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000348 // ParseCastExpression works here because all RHS expressions in C have it
349 // as a prefix, at least. However, in C++, an assignment-expression could
350 // be a throw-expression, which is not a valid cast-expression.
351 // Therefore we need some special-casing here.
352 // Also note that the third operand of the conditional operator is
353 // an assignment-expression in C++.
354 OwningExprResult RHS(Actions);
355 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
356 RHS = ParseAssignmentExpression();
357 else
358 RHS = ParseCastExpression(false);
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
362 // Remember the precedence of this operator and get the precedence of the
363 // operator immediately to the right of the RHS.
364 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000365 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
366 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
368 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000369 bool isRightAssoc = ThisPrec == prec::Conditional ||
370 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
372 // Get the precedence of the operator to the right of the RHS. If it binds
373 // more tightly with RHS than we do, evaluate it completely first.
374 if (ThisPrec < NextTokPrec ||
375 (ThisPrec == NextTokPrec && isRightAssoc)) {
376 // If this is left-associative, only parse things on the RHS that bind
377 // more tightly than the current operator. If it is left-associative, it
378 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
379 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000380 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000381 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000382 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000383 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000384
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000385 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
386 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 }
388 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000389
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000390 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000391 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000392 if (TernaryMiddle.isInvalid()) {
393 // If we're using '>>' as an operator within a template
394 // argument list (in C++98), suggest the addition of
395 // parentheses so that the code remains well-formed in C++0x.
396 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
397 SuggestParentheses(OpToken.getLocation(),
398 diag::warn_cxx0x_right_shift_in_template_arg,
399 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
400 Actions.getExprRange(RHS.get()).getEnd()));
401
Sebastian Redleffa8d12008-12-10 00:02:53 +0000402 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000403 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000404 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000405 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000406 move(LHS), move(TernaryMiddle),
407 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000408 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 }
410}
411
412/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000413/// true, parse a unary-expression. isAddressOfOperand exists because an
414/// id-expression that is the operand of address-of gets special treatment
415/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000416///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000417Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000418 bool isAddressOfOperand,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000419 TypeTy *TypeOfCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000420 bool NotCastExpr;
421 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
422 isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000423 NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000424 TypeOfCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000425 if (NotCastExpr)
426 Diag(Tok, diag::err_expected_expression);
427 return move(Res);
428}
429
430/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
431/// true, parse a unary-expression. isAddressOfOperand exists because an
432/// id-expression that is the operand of address-of gets special treatment
433/// due to member pointers. NotCastExpr is set to true if the token is not the
434/// start of a cast-expression, and no diagnostic is emitted in this case.
435///
Reid Spencer5f016e22007-07-11 17:01:13 +0000436/// cast-expression: [C99 6.5.4]
437/// unary-expression
438/// '(' type-name ')' cast-expression
439///
440/// unary-expression: [C99 6.5.3]
441/// postfix-expression
442/// '++' unary-expression
443/// '--' unary-expression
444/// unary-operator cast-expression
445/// 'sizeof' unary-expression
446/// 'sizeof' '(' type-name ')'
447/// [GNU] '__alignof' unary-expression
448/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000449/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000450/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000451/// [C++] new-expression
452/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000453///
454/// unary-operator: one of
455/// '&' '*' '+' '-' '~' '!'
456/// [GNU] '__extension__' '__real' '__imag'
457///
458/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000459/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000460/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000461/// constant
462/// string-literal
463/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000464/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000465/// '(' expression ')'
466/// '__func__' [C99 6.4.2.2]
467/// [GNU] '__FUNCTION__'
468/// [GNU] '__PRETTY_FUNCTION__'
469/// [GNU] '(' compound-statement ')'
470/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
471/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
472/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
473/// assign-expr ')'
474/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000475/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000476/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000477/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000478/// [OBJC] '@protocol' '(' identifier ')'
479/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000480/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000481/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
482/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000483/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
484/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
485/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
486/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000487/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
488/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000489/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000490/// [G++] unary-type-trait '(' type-id ')'
491/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000492/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000493///
494/// constant: [C99 6.4.4]
495/// integer-constant
496/// floating-constant
497/// enumeration-constant -> identifier
498/// character-constant
499///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000500/// id-expression: [C++ 5.1]
501/// unqualified-id
502/// qualified-id [TODO]
503///
504/// unqualified-id: [C++ 5.1]
505/// identifier
506/// operator-function-id
507/// conversion-function-id [TODO]
508/// '~' class-name [TODO]
509/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000510///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000511/// new-expression: [C++ 5.3.4]
512/// '::'[opt] 'new' new-placement[opt] new-type-id
513/// new-initializer[opt]
514/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
515/// new-initializer[opt]
516///
517/// delete-expression: [C++ 5.3.5]
518/// '::'[opt] 'delete' cast-expression
519/// '::'[opt] 'delete' '[' ']' cast-expression
520///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000521/// [GNU] unary-type-trait:
522/// '__has_nothrow_assign' [TODO]
523/// '__has_nothrow_copy' [TODO]
524/// '__has_nothrow_constructor' [TODO]
525/// '__has_trivial_assign' [TODO]
526/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000527/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000528/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000529/// '__has_virtual_destructor' [TODO]
530/// '__is_abstract' [TODO]
531/// '__is_class'
532/// '__is_empty' [TODO]
533/// '__is_enum'
534/// '__is_pod'
535/// '__is_polymorphic'
536/// '__is_union'
537///
538/// [GNU] binary-type-trait:
539/// '__is_base_of' [TODO]
540///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000541Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000542 bool isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000543 bool &NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000544 TypeTy *TypeOfCast) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000545 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000547 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // This handles all of cast-expression, unary-expression, postfix-expression,
550 // and primary-expression. We handle them together like this for efficiency
551 // and to simplify handling of an expression starting with a '(' token: which
552 // may be one of a parenthesized expression, cast-expression, compound literal
553 // expression, or statement expression.
554 //
555 // If the parsed tokens consist of a primary-expression, the cases below
556 // call ParsePostfixExpressionSuffix to handle the postfix expression
557 // suffixes. Cases that cannot be followed by postfix exprs should
558 // return without invoking ParsePostfixExpressionSuffix.
559 switch (SavedKind) {
560 case tok::l_paren: {
561 // If this expression is limited to being a unary-expression, the parent can
562 // not start a cast expression.
563 ParenParseOption ParenExprType =
564 isUnaryExpression ? CompoundLiteral : CastExpr;
565 TypeTy *CastTy;
566 SourceLocation LParenLoc = Tok.getLocation();
567 SourceLocation RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000568 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000569 TypeOfCast, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000570 if (Res.isInvalid()) return move(Res);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 switch (ParenExprType) {
573 case SimpleExpr: break; // Nothing else to do.
574 case CompoundStmt: break; // Nothing else to do.
575 case CompoundLiteral:
576 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
577 // postfix-expression exist, parse them now.
578 break;
579 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000580 // We have parsed the cast-expression and no postfix-expr pieces are
581 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000582 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000586 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000588
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 // primary-expression
590 case tok::numeric_constant:
591 // constant: integer-constant
592 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000593
Steve Narofff69936d2007-09-16 03:34:24 +0000594 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000598 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000599
600 case tok::kw_true:
601 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000602 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000603
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000604 case tok::kw_nullptr:
605 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
606
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000607 case tok::identifier: { // primary-expression: identifier
608 // unqualified-id: identifier
609 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000610 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000611 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000612 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000613 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
614 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000615 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000616 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000617
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000618 // Consume the identifier so that we can see if it is followed by a '(' or
619 // '.'.
620 IdentifierInfo &II = *Tok.getIdentifierInfo();
621 SourceLocation ILoc = ConsumeToken();
622
623 // Support 'Class.property' notation. We don't use
624 // isTokObjCMessageIdentifierReceiver(), since it allows 'super' (which is
625 // inappropriate here).
626 if (getLang().ObjC1 && Tok.is(tok::period) &&
627 Actions.getTypeName(II, ILoc, CurScope)) {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000628 SourceLocation DotLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000629
Steve Naroff61f72cb2009-03-09 21:12:44 +0000630 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000631 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000632 return ExprError();
633 }
634 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
635 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000636
637 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
638 ILoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000639 // These can be followed by postfix-expr pieces.
640 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000641 }
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
644 // need to know whether or not this identifier is a function designator or
645 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000646 UnqualifiedId Name;
647 CXXScopeSpec ScopeSpec;
648 Name.setIdentifier(&II, ILoc);
649 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name,
650 Tok.is(tok::l_paren), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000652 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 }
654 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000655 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 ConsumeToken();
657 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000658 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
660 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
661 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000662 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 ConsumeToken();
664 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000665 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 case tok::string_literal: // primary-expression: string-literal
667 case tok::wide_string_literal:
668 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000669 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000671 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 case tok::kw___builtin_va_arg:
673 case tok::kw___builtin_offsetof:
674 case tok::kw___builtin_choose_expr:
675 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000676 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000677 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000678 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000679 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 case tok::plusplus: // unary-expression: '++' unary-expression
681 case tok::minusminus: { // unary-expression: '--' unary-expression
682 SourceLocation SavedLoc = ConsumeToken();
683 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000684 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000685 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000686 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000688 case tok::amp: { // unary-expression: '&' cast-expression
689 // Special treatment because of member pointers
690 SourceLocation SavedLoc = ConsumeToken();
691 Res = ParseCastExpression(false, true);
692 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000693 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000694 return move(Res);
695 }
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 case tok::star: // unary-expression: '*' cast-expression
698 case tok::plus: // unary-expression: '+' cast-expression
699 case tok::minus: // unary-expression: '-' cast-expression
700 case tok::tilde: // unary-expression: '~' cast-expression
701 case tok::exclaim: // unary-expression: '!' cast-expression
702 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000703 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 SourceLocation SavedLoc = ConsumeToken();
705 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000706 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000707 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000708 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000709 }
710
Chris Lattner35080842008-02-02 20:20:10 +0000711 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
712 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000713 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000714 SourceLocation SavedLoc = ConsumeToken();
715 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000716 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000717 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000718 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 }
720 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
721 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000722 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
724 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000725 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000726 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 case tok::ampamp: { // unary-expression: '&&' identifier
728 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000729 if (Tok.isNot(tok::identifier))
730 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000733 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 Tok.getIdentifierInfo());
735 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000736 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 }
738 case tok::kw_const_cast:
739 case tok::kw_dynamic_cast:
740 case tok::kw_reinterpret_cast:
741 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000742 Res = ParseCXXCasts();
743 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000744 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000745 case tok::kw_typeid:
746 Res = ParseCXXTypeid();
747 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000748 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000749 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000750 Res = ParseCXXThis();
751 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000752 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000753
754 case tok::kw_char:
755 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000756 case tok::kw_char16_t:
757 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000758 case tok::kw_bool:
759 case tok::kw_short:
760 case tok::kw_int:
761 case tok::kw_long:
762 case tok::kw_signed:
763 case tok::kw_unsigned:
764 case tok::kw_float:
765 case tok::kw_double:
766 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000767 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000768 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000769 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000770 if (!getLang().CPlusPlus) {
771 Diag(Tok, diag::err_expected_expression);
772 return ExprError();
773 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000774
775 if (SavedKind == tok::kw_typename) {
776 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
777 if (!TryAnnotateTypeOrScopeToken())
778 return ExprError();
779 }
780
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000781 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
782 //
783 DeclSpec DS;
784 ParseCXXSimpleTypeSpecifier(DS);
785 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000786 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
787 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000788
789 Res = ParseCXXTypeConstructExpression(DS);
790 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000791 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000792 }
793
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000794 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
795 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000796 case tok::annot_template_id: // [C++] template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000797 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000798 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000799
Chris Lattner74ba4102009-01-04 22:52:14 +0000800 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000801 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
802 // annotates the token, tail recurse.
803 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000804 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
805
Chris Lattner74ba4102009-01-04 22:52:14 +0000806 // ::new -> [C++] new-expression
807 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000808 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000809 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000810 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000811 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000812 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000814 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000815 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000816 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000817 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000818
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000819 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000820 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000821
822 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000823 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000824
Sebastian Redl64b45f72009-01-05 20:52:13 +0000825 case tok::kw___is_pod: // [GNU] unary-type-trait
826 case tok::kw___is_class:
827 case tok::kw___is_enum:
828 case tok::kw___is_union:
Eli Friedman1d954f62009-08-15 21:55:26 +0000829 case tok::kw___is_empty:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000830 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000831 case tok::kw___is_abstract:
Sebastian Redlccf43502009-12-03 00:13:20 +0000832 case tok::kw___is_literal:
Anders Carlsson347ba892009-04-16 00:08:20 +0000833 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000834 case tok::kw___has_trivial_copy:
835 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +0000836 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000837 return ParseUnaryTypeTrait();
838
Chris Lattnerc97c2042007-10-03 22:03:06 +0000839 case tok::at: {
840 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000841 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000842 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000843 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000844 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000845 case tok::l_square:
846 // These can be followed by postfix-expr pieces.
847 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000848 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000849 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000851 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000852 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000854
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 // unreachable.
856 abort();
857}
858
859/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
860/// is parsed, this method parses any suffixes that apply.
861///
862/// postfix-expression: [C99 6.5.2]
863/// primary-expression
864/// postfix-expression '[' expression ']'
865/// postfix-expression '(' argument-expression-list[opt] ')'
866/// postfix-expression '.' identifier
867/// postfix-expression '->' identifier
868/// postfix-expression '++'
869/// postfix-expression '--'
870/// '(' type-name ')' '{' initializer-list '}'
871/// '(' type-name ')' '{' initializer-list ',' '}'
872///
873/// argument-expression-list: [C99 6.5.2]
874/// argument-expression
875/// argument-expression-list ',' assignment-expression
876///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000877Parser::OwningExprResult
878Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // Now that the primary-expression piece of the postfix-expression has been
880 // parsed, see if there are any postfix-expression pieces here.
881 SourceLocation Loc;
882 while (1) {
883 switch (Tok.getKind()) {
884 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000885 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
887 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000888 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000889
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000891
892 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000893 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
894 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000895 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000896 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000897
898 // Match the ']'.
899 MatchRHSPunctuation(tok::r_square, Loc);
900 break;
901 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000904 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000905 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000906
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000908
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000909 if (Tok.is(tok::code_completion)) {
910 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
911 ConsumeToken();
912 }
913
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000914 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000915 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
916 LHS.get())) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000917 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000918 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 }
920 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000921
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000923 if (Tok.isNot(tok::r_paren)) {
924 MatchRHSPunctuation(tok::r_paren, Loc);
925 return ExprError();
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattner1721a2d2009-04-13 00:10:38 +0000928 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
930 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000931 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000932 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000933 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner1721a2d2009-04-13 00:10:38 +0000936 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 break;
938 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000939 case tok::arrow:
940 case tok::period: {
941 // postfix-expression: p-e '->' template[opt] id-expression
942 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 tok::TokenKind OpKind = Tok.getKind();
944 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000945
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000946 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000947 Action::TypeTy *ObjectType = 0;
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000948 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000949 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
950 OpLoc, OpKind, ObjectType);
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000951 if (LHS.isInvalid())
952 break;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000953 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false);
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000954 }
955
Douglas Gregor81b747b2009-09-17 21:32:03 +0000956 if (Tok.is(tok::code_completion)) {
957 // Code completion for a member access expression.
958 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
959 OpLoc, OpKind == tok::arrow);
960
961 ConsumeToken();
962 }
963
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000964 UnqualifiedId Name;
965 if (ParseUnqualifiedId(SS,
966 /*EnteringContext=*/false,
967 /*AllowDestructorName=*/true,
968 /*AllowConstructorName=*/false,
969 ObjectType,
970 Name))
971 return ExprError();
972
973 if (!LHS.isInvalid())
974 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc, OpKind,
975 SS, Name, ObjCImpDecl,
976 Tok.is(tok::l_paren));
977
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 break;
979 }
980 case tok::plusplus: // postfix-expression: postfix-expression '++'
981 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982 if (!LHS.isInvalid()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000983 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000984 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000985 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 ConsumeToken();
987 break;
988 }
989 }
990}
991
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000992/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
993/// we are at the start of an expression or a parenthesized type-id.
994/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
995/// (isCastExpr == false) or the type (isCastExpr == true).
996///
997/// unary-expression: [C99 6.5.3]
998/// 'sizeof' unary-expression
999/// 'sizeof' '(' type-name ')'
1000/// [GNU] '__alignof' unary-expression
1001/// [GNU] '__alignof' '(' type-name ')'
1002/// [C++0x] 'alignof' '(' type-id ')'
1003///
1004/// [GNU] typeof-specifier:
1005/// typeof ( expressions )
1006/// typeof ( type-name )
1007/// [GNU/C++] typeof unary-expression
1008///
1009Parser::OwningExprResult
1010Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1011 bool &isCastExpr,
1012 TypeTy *&CastTy,
1013 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001014
1015 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001016 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1017 "Not a typeof/sizeof/alignof expression!");
1018
1019 OwningExprResult Operand(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001021 // If the operand doesn't start with an '(', it must be an expression.
1022 if (Tok.isNot(tok::l_paren)) {
1023 isCastExpr = false;
1024 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1025 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1026 return ExprError();
1027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Douglas Gregore0762c92009-06-19 23:52:42 +00001029 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001030 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001031 // operand (Clause 5) [...]
1032 //
1033 // The GNU typeof and alignof extensions also behave as unevaluated
1034 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001035 EnterExpressionEvaluationContext Unevaluated(Actions,
1036 Action::Unevaluated);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001037 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001038 } else {
1039 // If it starts with a '(', we know that it is either a parenthesized
1040 // type-name, or it is a unary-expression that starts with a compound
1041 // literal, or starts with a primary-expression that is a parenthesized
1042 // expression.
1043 ParenParseOption ExprType = CastExpr;
1044 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Douglas Gregore0762c92009-06-19 23:52:42 +00001046 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001047 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001048 // operand (Clause 5) [...]
1049 //
1050 // The GNU typeof and alignof extensions also behave as unevaluated
1051 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001052 EnterExpressionEvaluationContext Unevaluated(Actions,
1053 Action::Unevaluated);
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001054 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1055 0/*TypeOfCast*/,
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001056 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001057 CastRange = SourceRange(LParenLoc, RParenLoc);
1058
1059 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1060 // a type.
1061 if (ExprType == CastExpr) {
1062 isCastExpr = true;
1063 return ExprEmpty();
1064 }
1065
Mike Stump1eb44332009-09-09 15:08:12 +00001066 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001067 // unary-expression, but doesn't include any postfix pieces. Parse these
1068 // now if present.
1069 Operand = ParsePostfixExpressionSuffix(move(Operand));
1070 }
1071
1072 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1073 isCastExpr = false;
1074 return move(Operand);
1075}
1076
Reid Spencer5f016e22007-07-11 17:01:13 +00001077
1078/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1079/// unary-expression: [C99 6.5.3]
1080/// 'sizeof' unary-expression
1081/// 'sizeof' '(' type-name ')'
1082/// [GNU] '__alignof' unary-expression
1083/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001084/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +00001085Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001086 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1087 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001089 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001092 bool isCastExpr;
1093 TypeTy *CastTy;
1094 SourceRange CastRange;
1095 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1096 isCastExpr,
1097 CastTy,
1098 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001099
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001100 if (isCastExpr)
1101 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1102 OpTok.is(tok::kw_sizeof),
1103 /*isType=*/true, CastTy,
1104 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001107 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001108 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1109 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001110 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001111 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001112 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
1115/// ParseBuiltinPrimaryExpression
1116///
1117/// primary-expression: [C99 6.5.1]
1118/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1119/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1120/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1121/// assign-expr ')'
1122/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001123///
Reid Spencer5f016e22007-07-11 17:01:13 +00001124/// [GNU] offsetof-member-designator:
1125/// [GNU] identifier
1126/// [GNU] offsetof-member-designator '.' identifier
1127/// [GNU] offsetof-member-designator '[' expression ']'
1128///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001129Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001130 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1132
1133 tok::TokenKind T = Tok.getKind();
1134 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1135
1136 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001137 if (Tok.isNot(tok::l_paren))
1138 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1139 << BuiltinII);
1140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 SourceLocation LParenLoc = ConsumeParen();
1142 // TODO: Build AST.
1143
1144 switch (T) {
1145 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001146 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001147 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001148 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001150 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
1152
1153 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001154 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
Douglas Gregor809070a2009-02-18 17:45:20 +00001156 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001157
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001158 if (Tok.isNot(tok::r_paren)) {
1159 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001160 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001161 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001162 if (Ty.isInvalid())
1163 Res = ExprError();
1164 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001165 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001167 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001168 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001169 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001170 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001171 if (Ty.isInvalid()) {
1172 SkipUntil(tok::r_paren);
1173 return ExprError();
1174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001177 return ExprError();
1178
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001180 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001181 Diag(Tok, diag::err_expected_ident);
1182 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001183 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001184 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001185
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001186 // Keep track of the various subcomponents we see.
1187 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001188
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001189 Comps.push_back(Action::OffsetOfComponent());
1190 Comps.back().isBrackets = false;
1191 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1192 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
Sebastian Redla55e52c2008-11-25 22:21:31 +00001194 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001196 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001198 Comps.push_back(Action::OffsetOfComponent());
1199 Comps.back().isBrackets = false;
1200 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001201
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001202 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001203 Diag(Tok, diag::err_expected_ident);
1204 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001205 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001206 }
1207 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1208 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001209
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001210 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001212 Comps.push_back(Action::OffsetOfComponent());
1213 Comps.back().isBrackets = true;
1214 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001216 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001218 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001220 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001221
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001222 Comps.back().LocEnd =
1223 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman309fe0d2009-06-27 20:38:33 +00001224 } else {
1225 if (Tok.isNot(tok::r_paren)) {
1226 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00001227 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001228 } else if (Ty.isInvalid()) {
1229 Res = ExprError();
1230 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001231 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1232 Ty.get(), &Comps[0],
Douglas Gregor809070a2009-02-18 17:45:20 +00001233 Comps.size(), ConsumeParen());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001234 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001235 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 }
1237 }
1238 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001239 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001240 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001241 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001242 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001243 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001244 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001245 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001247 return ExprError();
1248
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001249 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001250 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001251 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001252 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001253 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001255 return ExprError();
1256
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001257 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001258 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001259 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001260 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001261 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001262 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001263 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001264 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001265 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001266 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1267 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001268 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001269 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001271 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001274 return ExprError();
1275
Douglas Gregor809070a2009-02-18 17:45:20 +00001276 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001277
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001278 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001279 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001280 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001281 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001282
1283 if (Ty1.isInvalid() || Ty2.isInvalid())
1284 Res = ExprError();
1285 else
1286 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1287 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001288 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001289 }
1290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 // These can be followed by postfix-expr pieces because they are
1292 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001293 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001294}
1295
1296/// ParseParenExpression - This parses the unit that starts with a '(' token,
1297/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001298/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1299/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001300///
1301/// primary-expression: [C99 6.5.1]
1302/// '(' expression ')'
1303/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1304/// postfix-expression: [C99 6.5.2]
1305/// '(' type-name ')' '{' initializer-list '}'
1306/// '(' type-name ')' '{' initializer-list ',' '}'
1307/// cast-expression: [C99 6.5.4]
1308/// '(' type-name ')' cast-expression
1309///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001310Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001311Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001312 TypeTy *TypeOfCast, TypeTy *&CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001313 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001314 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001315 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001317 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001318 bool isAmbiguousTypeId;
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001320
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001321 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 Diag(Tok, diag::ext_gnu_statement_expr);
Sean Huntbbd37c62009-11-21 08:43:09 +00001323 OwningStmtResult Stmt(ParseCompoundStatement(0, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001325
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001326 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001328 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001330 } else if (ExprType >= CompoundLiteral &&
1331 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001335 // In C++, if the type-id is ambiguous we disambiguate based on context.
1336 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1337 // in which case we should treat it as type-id.
1338 // if stopIfCastExpr is false, we need to determine the context past the
1339 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1340 if (isAmbiguousTypeId && !stopIfCastExpr)
1341 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1342 OpenLoc, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor809070a2009-02-18 17:45:20 +00001344 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001345
1346 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001347 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 RParenLoc = ConsumeParen();
1349 else
1350 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001351
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001352 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001354 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001355 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001356
Chris Lattner42ece642008-12-12 06:00:12 +00001357 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001358 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001359
1360 if (Ty.isInvalid())
1361 return ExprError();
1362
1363 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001364
1365 if (stopIfCastExpr) {
1366 // Note that this doesn't parse the subsequent cast-expression, it just
1367 // returns the parsed type to the callee.
1368 return OwningExprResult(Actions);
1369 }
1370
1371 // Parse the cast-expression that follows it next.
1372 // TODO: For cast expression with CastTy.
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001373 Result = ParseCastExpression(false, false, CastTy);
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001374 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001375 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1376 move(Result));
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001377 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001379
Chris Lattner42ece642008-12-12 06:00:12 +00001380 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1381 return ExprError();
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001382 } else if (TypeOfCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00001383 // Parse the expression-list.
1384 ExprVector ArgExprs(Actions);
1385 CommaLocsTy CommaLocs;
1386
1387 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1388 ExprType = SimpleExpr;
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001389 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
1390 move_arg(ArgExprs), TypeOfCast);
Nate Begeman2ef13e52009-08-10 23:49:36 +00001391 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 } else {
1393 Result = ParseExpression();
1394 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001395 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001396 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001400 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001402 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 }
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Chris Lattner42ece642008-12-12 06:00:12 +00001405 if (Tok.is(tok::r_paren))
1406 RParenLoc = ConsumeParen();
1407 else
1408 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001409
1410 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001411}
1412
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001413/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1414/// and we are at the left brace.
1415///
1416/// postfix-expression: [C99 6.5.2]
1417/// '(' type-name ')' '{' initializer-list '}'
1418/// '(' type-name ')' '{' initializer-list ',' '}'
1419///
1420Parser::OwningExprResult
1421Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1422 SourceLocation LParenLoc,
1423 SourceLocation RParenLoc) {
1424 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1425 if (!getLang().C99) // Compound literals don't exist in C90.
1426 Diag(LParenLoc, diag::ext_c99_compound_literal);
1427 OwningExprResult Result = ParseInitializer();
1428 if (!Result.isInvalid() && Ty)
1429 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1430 return move(Result);
1431}
1432
Reid Spencer5f016e22007-07-11 17:01:13 +00001433/// ParseStringLiteralExpression - This handles the various token types that
1434/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1435/// translation phase #6].
1436///
1437/// primary-expression: [C99 6.5.1]
1438/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001439Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1443 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001444 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001445
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 do {
1447 StringToks.push_back(Tok);
1448 ConsumeStringToken();
1449 } while (isTokenStringLiteral());
1450
1451 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001452 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001453}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001454
1455/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1456///
1457/// argument-expression-list:
1458/// assignment-expression
1459/// argument-expression-list , assignment-expression
1460///
1461/// [C++] expression-list:
1462/// [C++] assignment-expression
1463/// [C++] expression-list , assignment-expression
1464///
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001465bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1466 void (Action::*Completer)(Scope *S,
1467 void *Data,
1468 ExprTy **Args,
1469 unsigned NumArgs),
1470 void *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001471 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001472 if (Tok.is(tok::code_completion)) {
1473 if (Completer)
1474 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1475 ConsumeToken();
1476 }
1477
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001478 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001479 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001480 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001481
Sebastian Redleffa8d12008-12-10 00:02:53 +00001482 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001483
1484 if (Tok.isNot(tok::comma))
1485 return false;
1486 // Move to the next argument, remember where the comma was.
1487 CommaLocs.push_back(ConsumeToken());
1488 }
1489}
Steve Naroff296e8d52008-08-28 19:20:44 +00001490
Mike Stump98eb8a72009-02-04 22:31:32 +00001491/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1492///
1493/// [clang] block-id:
1494/// [clang] specifier-qualifier-list block-declarator
1495///
1496void Parser::ParseBlockId() {
1497 // Parse the specifier-qualifier-list piece.
1498 DeclSpec DS;
1499 ParseSpecifierQualifierList(DS);
1500
1501 // Parse the block-declarator.
1502 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1503 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001504
Mike Stump6c92fa72009-04-29 21:40:37 +00001505 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1506 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1507 SourceLocation());
1508
Mike Stump19c30c02009-04-29 19:03:13 +00001509 if (Tok.is(tok::kw___attribute)) {
1510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001512 DeclaratorInfo.AddAttributes(AttrList, Loc);
1513 }
1514
Mike Stump98eb8a72009-02-04 22:31:32 +00001515 // Inform sema that we are starting a block.
1516 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1517}
1518
Steve Naroff296e8d52008-08-28 19:20:44 +00001519/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001520/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001521///
1522/// block-literal:
1523/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001524/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001525/// [clang] block-args:
1526/// [clang] '(' parameter-list ')'
1527///
Sebastian Redl1d922962008-12-13 15:32:12 +00001528Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001529 assert(Tok.is(tok::caret) && "block literal starts with ^");
1530 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001531
Chris Lattner6b91f002009-03-05 07:32:12 +00001532 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1533 "block literal parsing");
1534
Mike Stump1eb44332009-09-09 15:08:12 +00001535 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00001536 // argument decls, decls within the compound expression, etc. This also
1537 // allows determining whether a variable reference inside the block is
1538 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001539 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1540 Scope::BreakScope | Scope::ContinueScope |
1541 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001542
1543 // Inform sema that we are starting a block.
1544 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Steve Naroff296e8d52008-08-28 19:20:44 +00001546 // Parse the return type if present.
1547 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001548 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001549 // FIXME: Since the return type isn't actually parsed, it can't be used to
1550 // fill ParamInfo with an initial valid range, so do it manually.
1551 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001552
Steve Naroff296e8d52008-08-28 19:20:44 +00001553 // If this block has arguments, parse them. There is no ambiguity here with
1554 // the expression case, because the expression case requires a parameter list.
1555 if (Tok.is(tok::l_paren)) {
1556 ParseParenDeclarator(ParamInfo);
1557 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001558 // SetIdentifier sets the source range end, but in this case we're past
1559 // that location.
1560 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001561 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001562 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001563 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001564 // If there was an error parsing the arguments, they may have
1565 // tried to use ^(x+y) which requires an argument list. Just
1566 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001567 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001568 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001569 }
Mike Stump19c30c02009-04-29 19:03:13 +00001570
1571 if (Tok.is(tok::kw___attribute)) {
1572 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001573 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001574 ParamInfo.AddAttributes(AttrList, Loc);
1575 }
1576
Mike Stump98eb8a72009-02-04 22:31:32 +00001577 // Inform sema that we are starting a block.
1578 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001579 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001580 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001581 } else {
1582 // Otherwise, pretend we saw (void).
Mike Stump1eb44332009-09-09 15:08:12 +00001583 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00001584 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001585 0, 0, 0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00001586 false, SourceLocation(),
1587 false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00001588 CaretLoc, CaretLoc,
1589 ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001590 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001591
1592 if (Tok.is(tok::kw___attribute)) {
1593 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001594 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001595 ParamInfo.AddAttributes(AttrList, Loc);
1596 }
1597
Mike Stump98eb8a72009-02-04 22:31:32 +00001598 // Inform sema that we are starting a block.
1599 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001600 }
1601
Sebastian Redl1d922962008-12-13 15:32:12 +00001602
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001603 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001604 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001605 // Saw something like: ^expr
1606 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001607 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001608 return ExprError();
1609 }
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Chris Lattner9af55002009-03-27 04:18:06 +00001611 OwningStmtResult Stmt(ParseCompoundStatementBody());
1612 if (!Stmt.isInvalid())
1613 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1614 else
1615 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001616 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001617}