blob: f780cf1a605411fb8e708309e456ebe7eff02c64 [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"
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)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // Handle this production specially:
321 // logical-OR-expression '?' expression ':' conditional-expression
322 // In particular, the RHS of the '?' is 'expression', not
323 // 'logical-OR-expression' as we might expect.
324 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000325 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000326 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 } else {
328 // Special case handling of "X ? Y : Z" where Y is empty:
329 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000330 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 Diag(Tok, diag::ext_gnu_conditional_expr);
332 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000333
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000334 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000336 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000337 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Eat the colon.
341 ColonLoc = ConsumeToken();
342 }
Fariborz Jahanianf071e9b2009-10-23 21:01:39 +0000343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000345 // ParseCastExpression works here because all RHS expressions in C have it
346 // as a prefix, at least. However, in C++, an assignment-expression could
347 // be a throw-expression, which is not a valid cast-expression.
348 // Therefore we need some special-casing here.
349 // Also note that the third operand of the conditional operator is
350 // an assignment-expression in C++.
351 OwningExprResult RHS(Actions);
352 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
353 RHS = ParseAssignmentExpression();
354 else
355 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000356 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000357 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358
359 // Remember the precedence of this operator and get the precedence of the
360 // operator immediately to the right of the RHS.
361 unsigned ThisPrec = NextTokPrec;
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 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000366 bool isRightAssoc = ThisPrec == prec::Conditional ||
367 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368
369 // Get the precedence of the operator to the right of the RHS. If it binds
370 // more tightly with RHS than we do, evaluate it completely first.
371 if (ThisPrec < NextTokPrec ||
372 (ThisPrec == NextTokPrec && isRightAssoc)) {
373 // If this is left-associative, only parse things on the RHS that bind
374 // more tightly than the current operator. If it is left-associative, it
375 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
376 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000377 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000378 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000379 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000380 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000381
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000382 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
383 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 }
385 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000386
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000387 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000388 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000389 if (TernaryMiddle.isInvalid()) {
390 // If we're using '>>' as an operator within a template
391 // argument list (in C++98), suggest the addition of
392 // parentheses so that the code remains well-formed in C++0x.
393 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
394 SuggestParentheses(OpToken.getLocation(),
395 diag::warn_cxx0x_right_shift_in_template_arg,
396 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
397 Actions.getExprRange(RHS.get()).getEnd()));
398
Sebastian Redleffa8d12008-12-10 00:02:53 +0000399 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000400 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000401 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000402 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000403 move(LHS), move(TernaryMiddle),
404 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000405 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 }
407}
408
409/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000410/// true, parse a unary-expression. isAddressOfOperand exists because an
411/// id-expression that is the operand of address-of gets special treatment
412/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000413///
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000414Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000415 bool isAddressOfOperand,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000416 TypeTy *TypeOfCast) {
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000417 bool NotCastExpr;
418 OwningExprResult Res = ParseCastExpression(isUnaryExpression,
419 isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000420 NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000421 TypeOfCast);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000422 if (NotCastExpr)
423 Diag(Tok, diag::err_expected_expression);
424 return move(Res);
425}
426
427/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
428/// true, parse a unary-expression. isAddressOfOperand exists because an
429/// id-expression that is the operand of address-of gets special treatment
430/// due to member pointers. NotCastExpr is set to true if the token is not the
431/// start of a cast-expression, and no diagnostic is emitted in this case.
432///
Reid Spencer5f016e22007-07-11 17:01:13 +0000433/// cast-expression: [C99 6.5.4]
434/// unary-expression
435/// '(' type-name ')' cast-expression
436///
437/// unary-expression: [C99 6.5.3]
438/// postfix-expression
439/// '++' unary-expression
440/// '--' unary-expression
441/// unary-operator cast-expression
442/// 'sizeof' unary-expression
443/// 'sizeof' '(' type-name ')'
444/// [GNU] '__alignof' unary-expression
445/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000446/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000447/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000448/// [C++] new-expression
449/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000450///
451/// unary-operator: one of
452/// '&' '*' '+' '-' '~' '!'
453/// [GNU] '__extension__' '__real' '__imag'
454///
455/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000456/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000457/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000458/// constant
459/// string-literal
460/// [C++] boolean-literal [C++ 2.13.5]
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000461/// [C++0x] 'nullptr' [C++0x 2.14.7]
Reid Spencer5f016e22007-07-11 17:01:13 +0000462/// '(' expression ')'
463/// '__func__' [C99 6.4.2.2]
464/// [GNU] '__FUNCTION__'
465/// [GNU] '__PRETTY_FUNCTION__'
466/// [GNU] '(' compound-statement ')'
467/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
468/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
469/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
470/// assign-expr ')'
471/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000472/// [GNU] '__null'
Mike Stump1eb44332009-09-09 15:08:12 +0000473/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000474/// [OBJC] '@selector' '(' objc-selector-arg ')'
Mike Stump1eb44332009-09-09 15:08:12 +0000475/// [OBJC] '@protocol' '(' identifier ')'
476/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000477/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000478/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
479/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000480/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
481/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
482/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
483/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000484/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
485/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000486/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000487/// [G++] unary-type-trait '(' type-id ')'
488/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000489/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000490///
491/// constant: [C99 6.4.4]
492/// integer-constant
493/// floating-constant
494/// enumeration-constant -> identifier
495/// character-constant
496///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000497/// id-expression: [C++ 5.1]
498/// unqualified-id
499/// qualified-id [TODO]
500///
501/// unqualified-id: [C++ 5.1]
502/// identifier
503/// operator-function-id
504/// conversion-function-id [TODO]
505/// '~' class-name [TODO]
506/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000507///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000508/// new-expression: [C++ 5.3.4]
509/// '::'[opt] 'new' new-placement[opt] new-type-id
510/// new-initializer[opt]
511/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
512/// new-initializer[opt]
513///
514/// delete-expression: [C++ 5.3.5]
515/// '::'[opt] 'delete' cast-expression
516/// '::'[opt] 'delete' '[' ']' cast-expression
517///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000518/// [GNU] unary-type-trait:
519/// '__has_nothrow_assign' [TODO]
520/// '__has_nothrow_copy' [TODO]
521/// '__has_nothrow_constructor' [TODO]
522/// '__has_trivial_assign' [TODO]
523/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000524/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000525/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000526/// '__has_virtual_destructor' [TODO]
527/// '__is_abstract' [TODO]
528/// '__is_class'
529/// '__is_empty' [TODO]
530/// '__is_enum'
531/// '__is_pod'
532/// '__is_polymorphic'
533/// '__is_union'
534///
535/// [GNU] binary-type-trait:
536/// '__is_base_of' [TODO]
537///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000538Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000539 bool isAddressOfOperand,
Nate Begeman2ef13e52009-08-10 23:49:36 +0000540 bool &NotCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000541 TypeTy *TypeOfCast) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000542 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 tok::TokenKind SavedKind = Tok.getKind();
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000544 NotCastExpr = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 // This handles all of cast-expression, unary-expression, postfix-expression,
547 // and primary-expression. We handle them together like this for efficiency
548 // and to simplify handling of an expression starting with a '(' token: which
549 // may be one of a parenthesized expression, cast-expression, compound literal
550 // expression, or statement expression.
551 //
552 // If the parsed tokens consist of a primary-expression, the cases below
553 // call ParsePostfixExpressionSuffix to handle the postfix expression
554 // suffixes. Cases that cannot be followed by postfix exprs should
555 // return without invoking ParsePostfixExpressionSuffix.
556 switch (SavedKind) {
557 case tok::l_paren: {
558 // If this expression is limited to being a unary-expression, the parent can
559 // not start a cast expression.
560 ParenParseOption ParenExprType =
561 isUnaryExpression ? CompoundLiteral : CastExpr;
562 TypeTy *CastTy;
563 SourceLocation LParenLoc = Tok.getLocation();
564 SourceLocation RParenLoc;
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000565 Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +0000566 TypeOfCast, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000567 if (Res.isInvalid()) return move(Res);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 switch (ParenExprType) {
570 case SimpleExpr: break; // Nothing else to do.
571 case CompoundStmt: break; // Nothing else to do.
572 case CompoundLiteral:
573 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
574 // postfix-expression exist, parse them now.
575 break;
576 case CastExpr:
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +0000577 // We have parsed the cast-expression and no postfix-expr pieces are
578 // following.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000579 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000581
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000583 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 // primary-expression
587 case tok::numeric_constant:
588 // constant: integer-constant
589 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000590
Steve Narofff69936d2007-09-16 03:34:24 +0000591 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000595 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000596
597 case tok::kw_true:
598 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000599 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000600
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000601 case tok::kw_nullptr:
602 return Actions.ActOnCXXNullPtrLiteral(ConsumeToken());
603
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000604 case tok::identifier: { // primary-expression: identifier
605 // unqualified-id: identifier
606 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000607 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000608 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000609 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000610 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
611 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000612 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000613 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000614
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000615 // Consume the identifier so that we can see if it is followed by a '(' or
616 // '.'.
617 IdentifierInfo &II = *Tok.getIdentifierInfo();
618 SourceLocation ILoc = ConsumeToken();
619
620 // Support 'Class.property' notation. We don't use
621 // isTokObjCMessageIdentifierReceiver(), since it allows 'super' (which is
622 // inappropriate here).
623 if (getLang().ObjC1 && Tok.is(tok::period) &&
624 Actions.getTypeName(II, ILoc, CurScope)) {
Steve Naroff61f72cb2009-03-09 21:12:44 +0000625 SourceLocation DotLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000626
Steve Naroff61f72cb2009-03-09 21:12:44 +0000627 if (Tok.isNot(tok::identifier)) {
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000628 Diag(Tok, diag::err_expected_property_name);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000629 return ExprError();
630 }
631 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
632 SourceLocation PropertyLoc = ConsumeToken();
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000633
634 Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName,
635 ILoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000636 // These can be followed by postfix-expr pieces.
637 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000638 }
Chris Lattnerb7c3fd72009-10-25 17:04:48 +0000639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
641 // need to know whether or not this identifier is a function designator or
642 // not.
Douglas Gregor02a24ee2009-11-03 16:56:39 +0000643 UnqualifiedId Name;
644 CXXScopeSpec ScopeSpec;
645 Name.setIdentifier(&II, ILoc);
646 Res = Actions.ActOnIdExpression(CurScope, ScopeSpec, Name,
647 Tok.is(tok::l_paren), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000649 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 }
651 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000652 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 ConsumeToken();
654 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000655 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
657 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
658 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000659 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 ConsumeToken();
661 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000662 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case tok::string_literal: // primary-expression: string-literal
664 case tok::wide_string_literal:
665 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000666 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000668 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 case tok::kw___builtin_va_arg:
670 case tok::kw___builtin_offsetof:
671 case tok::kw___builtin_choose_expr:
672 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000673 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000674 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000675 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000676 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 case tok::plusplus: // unary-expression: '++' unary-expression
678 case tok::minusminus: { // unary-expression: '--' unary-expression
679 SourceLocation SavedLoc = ConsumeToken();
680 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000681 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000682 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000683 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000685 case tok::amp: { // unary-expression: '&' cast-expression
686 // Special treatment because of member pointers
687 SourceLocation SavedLoc = ConsumeToken();
688 Res = ParseCastExpression(false, true);
689 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000690 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000691 return move(Res);
692 }
693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 case tok::star: // unary-expression: '*' cast-expression
695 case tok::plus: // unary-expression: '+' cast-expression
696 case tok::minus: // unary-expression: '-' cast-expression
697 case tok::tilde: // unary-expression: '~' cast-expression
698 case tok::exclaim: // unary-expression: '!' cast-expression
699 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000700 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 SourceLocation SavedLoc = ConsumeToken();
702 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000703 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000704 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000705 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000706 }
707
Chris Lattner35080842008-02-02 20:20:10 +0000708 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
709 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000710 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000711 SourceLocation SavedLoc = ConsumeToken();
712 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000713 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000714 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000715 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 }
717 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
718 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000719 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
721 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000722 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000723 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 case tok::ampamp: { // unary-expression: '&&' identifier
725 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000726 if (Tok.isNot(tok::identifier))
727 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000728
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000730 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 Tok.getIdentifierInfo());
732 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000733 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 }
735 case tok::kw_const_cast:
736 case tok::kw_dynamic_cast:
737 case tok::kw_reinterpret_cast:
738 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000739 Res = ParseCXXCasts();
740 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000741 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000742 case tok::kw_typeid:
743 Res = ParseCXXTypeid();
744 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000745 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000746 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000747 Res = ParseCXXThis();
748 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000749 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000750
751 case tok::kw_char:
752 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000753 case tok::kw_char16_t:
754 case tok::kw_char32_t:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000755 case tok::kw_bool:
756 case tok::kw_short:
757 case tok::kw_int:
758 case tok::kw_long:
759 case tok::kw_signed:
760 case tok::kw_unsigned:
761 case tok::kw_float:
762 case tok::kw_double:
763 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000764 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000765 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000766 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000767 if (!getLang().CPlusPlus) {
768 Diag(Tok, diag::err_expected_expression);
769 return ExprError();
770 }
Eli Friedman2e0cdb42009-06-11 00:33:41 +0000771
772 if (SavedKind == tok::kw_typename) {
773 // postfix-expression: typename-specifier '(' expression-list[opt] ')'
774 if (!TryAnnotateTypeOrScopeToken())
775 return ExprError();
776 }
777
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000778 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
779 //
780 DeclSpec DS;
781 ParseCXXSimpleTypeSpecifier(DS);
782 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000783 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
784 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000785
786 Res = ParseCXXTypeConstructExpression(DS);
787 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000788 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000789 }
790
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000791 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
792 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000793 case tok::annot_template_id: // [C++] template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000794 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000795 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000796
Chris Lattner74ba4102009-01-04 22:52:14 +0000797 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000798 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
799 // annotates the token, tail recurse.
800 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000801 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
802
Chris Lattner74ba4102009-01-04 22:52:14 +0000803 // ::new -> [C++] new-expression
804 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000805 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000806 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000807 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000808 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000809 return ParseCXXDeleteExpression(true, CCLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000811 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000812 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000813 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000814 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000815
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000816 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000817 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000818
819 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000820 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000821
Sebastian Redl64b45f72009-01-05 20:52:13 +0000822 case tok::kw___is_pod: // [GNU] unary-type-trait
823 case tok::kw___is_class:
824 case tok::kw___is_enum:
825 case tok::kw___is_union:
Eli Friedman1d954f62009-08-15 21:55:26 +0000826 case tok::kw___is_empty:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000827 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000828 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000829 case tok::kw___has_trivial_constructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000830 case tok::kw___has_trivial_copy:
831 case tok::kw___has_trivial_assign:
Anders Carlsson072abef2009-04-17 02:34:54 +0000832 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000833 return ParseUnaryTypeTrait();
834
Chris Lattnerc97c2042007-10-03 22:03:06 +0000835 case tok::at: {
836 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000837 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000838 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000839 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000840 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000841 case tok::l_square:
842 // These can be followed by postfix-expr pieces.
843 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000844 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000845 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 default:
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +0000847 NotCastExpr = true;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000848 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000850
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 // unreachable.
852 abort();
853}
854
855/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
856/// is parsed, this method parses any suffixes that apply.
857///
858/// postfix-expression: [C99 6.5.2]
859/// primary-expression
860/// postfix-expression '[' expression ']'
861/// postfix-expression '(' argument-expression-list[opt] ')'
862/// postfix-expression '.' identifier
863/// postfix-expression '->' identifier
864/// postfix-expression '++'
865/// postfix-expression '--'
866/// '(' type-name ')' '{' initializer-list '}'
867/// '(' type-name ')' '{' initializer-list ',' '}'
868///
869/// argument-expression-list: [C99 6.5.2]
870/// argument-expression
871/// argument-expression-list ',' assignment-expression
872///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000873Parser::OwningExprResult
874Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 // Now that the primary-expression piece of the postfix-expression has been
876 // parsed, see if there are any postfix-expression pieces here.
877 SourceLocation Loc;
878 while (1) {
879 switch (Tok.getKind()) {
880 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000881 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
883 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000884 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000887
888 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000889 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
890 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000891 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000892 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000893
894 // Match the ']'.
895 MatchRHSPunctuation(tok::r_square, Loc);
896 break;
897 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000900 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000901 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000902
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000904
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000905 if (Tok.is(tok::code_completion)) {
906 Actions.CodeCompleteCall(CurScope, LHS.get(), 0, 0);
907 ConsumeToken();
908 }
909
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000910 if (Tok.isNot(tok::r_paren)) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +0000911 if (ParseExpressionList(ArgExprs, CommaLocs, &Action::CodeCompleteCall,
912 LHS.get())) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000913 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000914 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
916 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000917
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000919 if (Tok.isNot(tok::r_paren)) {
920 MatchRHSPunctuation(tok::r_paren, Loc);
921 return ExprError();
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Chris Lattner1721a2d2009-04-13 00:10:38 +0000924 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
926 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000927 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000928 move_arg(ArgExprs), CommaLocs.data(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000929 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner1721a2d2009-04-13 00:10:38 +0000932 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 break;
934 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000935 case tok::arrow:
936 case tok::period: {
937 // postfix-expression: p-e '->' template[opt] id-expression
938 // postfix-expression: p-e '.' template[opt] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 tok::TokenKind OpKind = Tok.getKind();
940 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000941
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000942 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000943 Action::TypeTy *ObjectType = 0;
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000944 if (getLang().CPlusPlus && !LHS.isInvalid()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000945 LHS = Actions.ActOnStartCXXMemberReference(CurScope, move(LHS),
946 OpLoc, OpKind, ObjectType);
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000947 if (LHS.isInvalid())
948 break;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000949 ParseOptionalCXXScopeSpecifier(SS, ObjectType, false);
Douglas Gregorfe85ced2009-08-06 03:17:00 +0000950 }
951
Douglas Gregor81b747b2009-09-17 21:32:03 +0000952 if (Tok.is(tok::code_completion)) {
953 // Code completion for a member access expression.
954 Actions.CodeCompleteMemberReferenceExpr(CurScope, LHS.get(),
955 OpLoc, OpKind == tok::arrow);
956
957 ConsumeToken();
958 }
959
Douglas Gregor2d1c2142009-11-03 19:44:04 +0000960 UnqualifiedId Name;
961 if (ParseUnqualifiedId(SS,
962 /*EnteringContext=*/false,
963 /*AllowDestructorName=*/true,
964 /*AllowConstructorName=*/false,
965 ObjectType,
966 Name))
967 return ExprError();
968
969 if (!LHS.isInvalid())
970 LHS = Actions.ActOnMemberAccessExpr(CurScope, move(LHS), OpLoc, OpKind,
971 SS, Name, ObjCImpDecl,
972 Tok.is(tok::l_paren));
973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 break;
975 }
976 case tok::plusplus: // postfix-expression: postfix-expression '++'
977 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000978 if (!LHS.isInvalid()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000979 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000980 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000981 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 ConsumeToken();
983 break;
984 }
985 }
986}
987
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +0000988/// ParseExprAfterTypeofSizeofAlignof - We parsed a typeof/sizeof/alignof and
989/// we are at the start of an expression or a parenthesized type-id.
990/// OpTok is the operand token (typeof/sizeof/alignof). Returns the expression
991/// (isCastExpr == false) or the type (isCastExpr == true).
992///
993/// unary-expression: [C99 6.5.3]
994/// 'sizeof' unary-expression
995/// 'sizeof' '(' type-name ')'
996/// [GNU] '__alignof' unary-expression
997/// [GNU] '__alignof' '(' type-name ')'
998/// [C++0x] 'alignof' '(' type-id ')'
999///
1000/// [GNU] typeof-specifier:
1001/// typeof ( expressions )
1002/// typeof ( type-name )
1003/// [GNU/C++] typeof unary-expression
1004///
1005Parser::OwningExprResult
1006Parser::ParseExprAfterTypeofSizeofAlignof(const Token &OpTok,
1007 bool &isCastExpr,
1008 TypeTy *&CastTy,
1009 SourceRange &CastRange) {
Mike Stump1eb44332009-09-09 15:08:12 +00001010
1011 assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) ||
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001012 OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof)) &&
1013 "Not a typeof/sizeof/alignof expression!");
1014
1015 OwningExprResult Operand(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001017 // If the operand doesn't start with an '(', it must be an expression.
1018 if (Tok.isNot(tok::l_paren)) {
1019 isCastExpr = false;
1020 if (OpTok.is(tok::kw_typeof) && !getLang().CPlusPlus) {
1021 Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo();
1022 return ExprError();
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Douglas Gregore0762c92009-06-19 23:52:42 +00001025 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001026 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001027 // operand (Clause 5) [...]
1028 //
1029 // The GNU typeof and alignof extensions also behave as unevaluated
1030 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001031 EnterExpressionEvaluationContext Unevaluated(Actions,
1032 Action::Unevaluated);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001033 Operand = ParseCastExpression(true/*isUnaryExpression*/);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001034 } else {
1035 // If it starts with a '(', we know that it is either a parenthesized
1036 // type-name, or it is a unary-expression that starts with a compound
1037 // literal, or starts with a primary-expression that is a parenthesized
1038 // expression.
1039 ParenParseOption ExprType = CastExpr;
1040 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregore0762c92009-06-19 23:52:42 +00001042 // C++0x [expr.sizeof]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00001043 // [...] The operand is either an expression, which is an unevaluated
Douglas Gregore0762c92009-06-19 23:52:42 +00001044 // operand (Clause 5) [...]
1045 //
1046 // The GNU typeof and alignof extensions also behave as unevaluated
1047 // operands.
Douglas Gregorac7610d2009-06-22 20:57:11 +00001048 EnterExpressionEvaluationContext Unevaluated(Actions,
1049 Action::Unevaluated);
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001050 Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/,
1051 0/*TypeOfCast*/,
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001052 CastTy, RParenLoc);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001053 CastRange = SourceRange(LParenLoc, RParenLoc);
1054
1055 // If ParseParenExpression parsed a '(typename)' sequence only, then this is
1056 // a type.
1057 if (ExprType == CastExpr) {
1058 isCastExpr = true;
1059 return ExprEmpty();
1060 }
1061
Mike Stump1eb44332009-09-09 15:08:12 +00001062 // If this is a parenthesized expression, it is the start of a
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001063 // unary-expression, but doesn't include any postfix pieces. Parse these
1064 // now if present.
1065 Operand = ParsePostfixExpressionSuffix(move(Operand));
1066 }
1067
1068 // If we get here, the operand to the typeof/sizeof/alignof was an expresion.
1069 isCastExpr = false;
1070 return move(Operand);
1071}
1072
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
1075/// unary-expression: [C99 6.5.3]
1076/// 'sizeof' unary-expression
1077/// 'sizeof' '(' type-name ')'
1078/// [GNU] '__alignof' unary-expression
1079/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001080/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +00001081Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +00001082 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
1083 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +00001085 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001088 bool isCastExpr;
1089 TypeTy *CastTy;
1090 SourceRange CastRange;
1091 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
1092 isCastExpr,
1093 CastTy,
1094 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001095
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001096 if (isCastExpr)
1097 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1098 OpTok.is(tok::kw_sizeof),
1099 /*isType=*/true, CastTy,
1100 CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001101
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001103 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +00001104 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
1105 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +00001106 /*isType=*/false,
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00001107 Operand.release(), CastRange);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001108 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
1111/// ParseBuiltinPrimaryExpression
1112///
1113/// primary-expression: [C99 6.5.1]
1114/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
1115/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
1116/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
1117/// assign-expr ')'
1118/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001119///
Reid Spencer5f016e22007-07-11 17:01:13 +00001120/// [GNU] offsetof-member-designator:
1121/// [GNU] identifier
1122/// [GNU] offsetof-member-designator '.' identifier
1123/// [GNU] offsetof-member-designator '[' expression ']'
1124///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001125Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001126 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
1128
1129 tok::TokenKind T = Tok.getKind();
1130 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
1131
1132 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001133 if (Tok.isNot(tok::l_paren))
1134 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
1135 << BuiltinII);
1136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 SourceLocation LParenLoc = ConsumeParen();
1138 // TODO: Build AST.
1139
1140 switch (T) {
1141 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001142 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001143 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001144 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001146 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 }
1148
1149 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001150 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001151
Douglas Gregor809070a2009-02-18 17:45:20 +00001152 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001153
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001154 if (Tok.isNot(tok::r_paren)) {
1155 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001156 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001157 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001158 if (Ty.isInvalid())
1159 Res = ExprError();
1160 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001161 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001163 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001164 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001165 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001166 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001167 if (Ty.isInvalid()) {
1168 SkipUntil(tok::r_paren);
1169 return ExprError();
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001173 return ExprError();
1174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001176 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001177 Diag(Tok, diag::err_expected_ident);
1178 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001179 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001180 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001181
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001182 // Keep track of the various subcomponents we see.
1183 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001184
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001185 Comps.push_back(Action::OffsetOfComponent());
1186 Comps.back().isBrackets = false;
1187 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1188 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001189
Sebastian Redla55e52c2008-11-25 22:21:31 +00001190 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001192 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001194 Comps.push_back(Action::OffsetOfComponent());
1195 Comps.back().isBrackets = false;
1196 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001197
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001198 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001199 Diag(Tok, diag::err_expected_ident);
1200 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001201 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001202 }
1203 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1204 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001205
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001206 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001208 Comps.push_back(Action::OffsetOfComponent());
1209 Comps.back().isBrackets = true;
1210 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001212 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001214 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001216 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001217
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001218 Comps.back().LocEnd =
1219 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Eli Friedman309fe0d2009-06-27 20:38:33 +00001220 } else {
1221 if (Tok.isNot(tok::r_paren)) {
1222 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00001223 Res = ExprError();
Eli Friedman309fe0d2009-06-27 20:38:33 +00001224 } else if (Ty.isInvalid()) {
1225 Res = ExprError();
1226 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001227 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1228 Ty.get(), &Comps[0],
Douglas Gregor809070a2009-02-18 17:45:20 +00001229 Comps.size(), ConsumeParen());
Eli Friedman309fe0d2009-06-27 20:38:33 +00001230 }
Chris Lattner6eb21092007-08-30 15:52:49 +00001231 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 }
1233 }
1234 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001235 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001236 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001237 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001238 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001239 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001240 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001241 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001243 return ExprError();
1244
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001245 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001246 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001247 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001248 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001249 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001251 return ExprError();
1252
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001253 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001254 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001255 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001256 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001257 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001258 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001259 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001260 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001261 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001262 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1263 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001264 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001265 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001267 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001270 return ExprError();
1271
Douglas Gregor809070a2009-02-18 17:45:20 +00001272 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001273
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001274 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001275 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001276 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001277 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001278
1279 if (Ty1.isInvalid() || Ty2.isInvalid())
1280 Res = ExprError();
1281 else
1282 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1283 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001284 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001285 }
1286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 // These can be followed by postfix-expr pieces because they are
1288 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001289 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001290}
1291
1292/// ParseParenExpression - This parses the unit that starts with a '(' token,
1293/// based on what is allowed by ExprType. The actual thing parsed is returned
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001294/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type,
1295/// not the parsed cast-expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001296///
1297/// primary-expression: [C99 6.5.1]
1298/// '(' expression ')'
1299/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1300/// postfix-expression: [C99 6.5.2]
1301/// '(' type-name ')' '{' initializer-list '}'
1302/// '(' type-name ')' '{' initializer-list ',' '}'
1303/// cast-expression: [C99 6.5.4]
1304/// '(' type-name ')' cast-expression
1305///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001306Parser::OwningExprResult
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001307Parser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr,
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001308 TypeTy *TypeOfCast, TypeTy *&CastTy,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001309 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001310 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001311 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001313 OwningExprResult Result(Actions, true);
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001314 bool isAmbiguousTypeId;
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001316
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001317 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 Diag(Tok, diag::ext_gnu_statement_expr);
Sean Huntbbd37c62009-11-21 08:43:09 +00001319 OwningStmtResult Stmt(ParseCompoundStatement(0, true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001321
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001322 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001323 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001324 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001325
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001326 } else if (ExprType >= CompoundLiteral &&
1327 isTypeIdInParens(isAmbiguousTypeId)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // Otherwise, this is a compound literal expression or cast expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Argyrios Kyrtzidisf58f45e2009-05-22 10:24:42 +00001331 // In C++, if the type-id is ambiguous we disambiguate based on context.
1332 // If stopIfCastExpr is true the context is a typeof/sizeof/alignof
1333 // in which case we should treat it as type-id.
1334 // if stopIfCastExpr is false, we need to determine the context past the
1335 // parens, so we defer to ParseCXXAmbiguousParenExpression for that.
1336 if (isAmbiguousTypeId && !stopIfCastExpr)
1337 return ParseCXXAmbiguousParenExpression(ExprType, CastTy,
1338 OpenLoc, RParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregor809070a2009-02-18 17:45:20 +00001340 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001341
1342 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001343 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 RParenLoc = ConsumeParen();
1345 else
1346 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001347
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001348 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 ExprType = CompoundLiteral;
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001350 return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc);
Chris Lattner42ece642008-12-12 06:00:12 +00001351 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001352
Chris Lattner42ece642008-12-12 06:00:12 +00001353 if (ExprType == CastExpr) {
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001354 // We parsed '(' type-name ')' and the thing after it wasn't a '{'.
Douglas Gregor809070a2009-02-18 17:45:20 +00001355
1356 if (Ty.isInvalid())
1357 return ExprError();
1358
1359 CastTy = Ty.get();
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001360
1361 if (stopIfCastExpr) {
1362 // Note that this doesn't parse the subsequent cast-expression, it just
1363 // returns the parsed type to the callee.
1364 return OwningExprResult(Actions);
1365 }
1366
1367 // Parse the cast-expression that follows it next.
1368 // TODO: For cast expression with CastTy.
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001369 Result = ParseCastExpression(false, false, CastTy);
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001370 if (!Result.isInvalid())
Nate Begeman2ef13e52009-08-10 23:49:36 +00001371 Result = Actions.ActOnCastExpr(CurScope, OpenLoc, CastTy, RParenLoc,
1372 move(Result));
Argyrios Kyrtzidis0350ca52009-05-22 10:23:40 +00001373 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001375
Chris Lattner42ece642008-12-12 06:00:12 +00001376 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1377 return ExprError();
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001378 } else if (TypeOfCast) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00001379 // Parse the expression-list.
1380 ExprVector ArgExprs(Actions);
1381 CommaLocsTy CommaLocs;
1382
1383 if (!ParseExpressionList(ArgExprs, CommaLocs)) {
1384 ExprType = SimpleExpr;
Fariborz Jahanianf88f7ab2009-11-25 01:26:41 +00001385 Result = Actions.ActOnParenOrParenListExpr(OpenLoc, Tok.getLocation(),
1386 move_arg(ArgExprs), TypeOfCast);
Nate Begeman2ef13e52009-08-10 23:49:36 +00001387 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 } else {
1389 Result = ParseExpression();
1390 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001391 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001392 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001394
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001396 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001398 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 }
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattner42ece642008-12-12 06:00:12 +00001401 if (Tok.is(tok::r_paren))
1402 RParenLoc = ConsumeParen();
1403 else
1404 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001405
1406 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407}
1408
Argyrios Kyrtzidisd974a7b2009-05-22 10:24:05 +00001409/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name
1410/// and we are at the left brace.
1411///
1412/// postfix-expression: [C99 6.5.2]
1413/// '(' type-name ')' '{' initializer-list '}'
1414/// '(' type-name ')' '{' initializer-list ',' '}'
1415///
1416Parser::OwningExprResult
1417Parser::ParseCompoundLiteralExpression(TypeTy *Ty,
1418 SourceLocation LParenLoc,
1419 SourceLocation RParenLoc) {
1420 assert(Tok.is(tok::l_brace) && "Not a compound literal!");
1421 if (!getLang().C99) // Compound literals don't exist in C90.
1422 Diag(LParenLoc, diag::ext_c99_compound_literal);
1423 OwningExprResult Result = ParseInitializer();
1424 if (!Result.isInvalid() && Ty)
1425 return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, move(Result));
1426 return move(Result);
1427}
1428
Reid Spencer5f016e22007-07-11 17:01:13 +00001429/// ParseStringLiteralExpression - This handles the various token types that
1430/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1431/// translation phase #6].
1432///
1433/// primary-expression: [C99 6.5.1]
1434/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001435Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001437
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1439 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001440 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 do {
1443 StringToks.push_back(Tok);
1444 ConsumeStringToken();
1445 } while (isTokenStringLiteral());
1446
1447 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001448 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001449}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001450
1451/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1452///
1453/// argument-expression-list:
1454/// assignment-expression
1455/// argument-expression-list , assignment-expression
1456///
1457/// [C++] expression-list:
1458/// [C++] assignment-expression
1459/// [C++] expression-list , assignment-expression
1460///
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001461bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs,
1462 void (Action::*Completer)(Scope *S,
1463 void *Data,
1464 ExprTy **Args,
1465 unsigned NumArgs),
1466 void *Data) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001467 while (1) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00001468 if (Tok.is(tok::code_completion)) {
1469 if (Completer)
1470 (Actions.*Completer)(CurScope, Data, Exprs.data(), Exprs.size());
1471 ConsumeToken();
1472 }
1473
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001474 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001475 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001476 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001477
Sebastian Redleffa8d12008-12-10 00:02:53 +00001478 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001479
1480 if (Tok.isNot(tok::comma))
1481 return false;
1482 // Move to the next argument, remember where the comma was.
1483 CommaLocs.push_back(ConsumeToken());
1484 }
1485}
Steve Naroff296e8d52008-08-28 19:20:44 +00001486
Mike Stump98eb8a72009-02-04 22:31:32 +00001487/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1488///
1489/// [clang] block-id:
1490/// [clang] specifier-qualifier-list block-declarator
1491///
1492void Parser::ParseBlockId() {
1493 // Parse the specifier-qualifier-list piece.
1494 DeclSpec DS;
1495 ParseSpecifierQualifierList(DS);
1496
1497 // Parse the block-declarator.
1498 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1499 ParseDeclarator(DeclaratorInfo);
Mike Stump19c30c02009-04-29 19:03:13 +00001500
Mike Stump6c92fa72009-04-29 21:40:37 +00001501 // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes.
1502 DeclaratorInfo.AddAttributes(DS.TakeAttributes(),
1503 SourceLocation());
1504
Mike Stump19c30c02009-04-29 19:03:13 +00001505 if (Tok.is(tok::kw___attribute)) {
1506 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001507 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001508 DeclaratorInfo.AddAttributes(AttrList, Loc);
1509 }
1510
Mike Stump98eb8a72009-02-04 22:31:32 +00001511 // Inform sema that we are starting a block.
1512 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1513}
1514
Steve Naroff296e8d52008-08-28 19:20:44 +00001515/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001516/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001517///
1518/// block-literal:
1519/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001520/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001521/// [clang] block-args:
1522/// [clang] '(' parameter-list ')'
1523///
Sebastian Redl1d922962008-12-13 15:32:12 +00001524Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001525 assert(Tok.is(tok::caret) && "block literal starts with ^");
1526 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001527
Chris Lattner6b91f002009-03-05 07:32:12 +00001528 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1529 "block literal parsing");
1530
Mike Stump1eb44332009-09-09 15:08:12 +00001531 // Enter a scope to hold everything within the block. This includes the
Steve Naroff296e8d52008-08-28 19:20:44 +00001532 // argument decls, decls within the compound expression, etc. This also
1533 // allows determining whether a variable reference inside the block is
1534 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001535 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1536 Scope::BreakScope | Scope::ContinueScope |
1537 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001538
1539 // Inform sema that we are starting a block.
1540 Actions.ActOnBlockStart(CaretLoc, CurScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Steve Naroff296e8d52008-08-28 19:20:44 +00001542 // Parse the return type if present.
1543 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001544 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001545 // FIXME: Since the return type isn't actually parsed, it can't be used to
1546 // fill ParamInfo with an initial valid range, so do it manually.
1547 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001548
Steve Naroff296e8d52008-08-28 19:20:44 +00001549 // If this block has arguments, parse them. There is no ambiguity here with
1550 // the expression case, because the expression case requires a parameter list.
1551 if (Tok.is(tok::l_paren)) {
1552 ParseParenDeclarator(ParamInfo);
1553 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001554 // SetIdentifier sets the source range end, but in this case we're past
1555 // that location.
1556 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001557 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001558 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001559 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001560 // If there was an error parsing the arguments, they may have
1561 // tried to use ^(x+y) which requires an argument list. Just
1562 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001563 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001564 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001565 }
Mike Stump19c30c02009-04-29 19:03:13 +00001566
1567 if (Tok.is(tok::kw___attribute)) {
1568 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001569 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001570 ParamInfo.AddAttributes(AttrList, Loc);
1571 }
1572
Mike Stump98eb8a72009-02-04 22:31:32 +00001573 // Inform sema that we are starting a block.
1574 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001575 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001576 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001577 } else {
1578 // Otherwise, pretend we saw (void).
Mike Stump1eb44332009-09-09 15:08:12 +00001579 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00001580 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001581 0, 0, 0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00001582 false, SourceLocation(),
1583 false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00001584 CaretLoc, CaretLoc,
1585 ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001586 CaretLoc);
Mike Stump19c30c02009-04-29 19:03:13 +00001587
1588 if (Tok.is(tok::kw___attribute)) {
1589 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001590 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Mike Stump19c30c02009-04-29 19:03:13 +00001591 ParamInfo.AddAttributes(AttrList, Loc);
1592 }
1593
Mike Stump98eb8a72009-02-04 22:31:32 +00001594 // Inform sema that we are starting a block.
1595 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001596 }
1597
Sebastian Redl1d922962008-12-13 15:32:12 +00001598
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001599 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001600 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001601 // Saw something like: ^expr
1602 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001603 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001604 return ExprError();
1605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Chris Lattner9af55002009-03-27 04:18:06 +00001607 OwningStmtResult Stmt(ParseCompoundStatementBody());
1608 if (!Stmt.isInvalid())
1609 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1610 else
1611 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001612 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001613}