blob: f299c5246ec675eeb633694e43dc76b149ad58ab [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattner6b91f002009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000027#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/SmallString.h"
30using namespace clang;
31
32/// PrecedenceLevels - These are precedences for the binary/ternary operators in
33/// the C99 grammar. These have been named to relate with the C99 grammar
34/// productions. Low precedences numbers bind more weakly than high numbers.
35namespace prec {
36 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000037 Unknown = 0, // Not binary operator.
38 Comma = 1, // ,
39 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
40 Conditional = 3, // ?
41 LogicalOr = 4, // ||
42 LogicalAnd = 5, // &&
43 InclusiveOr = 6, // |
44 ExclusiveOr = 7, // ^
45 And = 8, // &
46 Equality = 9, // ==, !=
47 Relational = 10, // >=, <=, >, <
48 Shift = 11, // <<, >>
49 Additive = 12, // -, +
50 Multiplicative = 13, // *, /, %
51 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000052 };
53}
54
55
56/// getBinOpPrecedence - Return the precedence of the specified binary operator
57/// token. This returns:
58///
Douglas Gregor55f6b142009-02-09 18:46:07 +000059static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregor3965b7b2009-02-25 23:02:36 +000060 bool GreaterThanIsOperator,
61 bool CPlusPlus0x) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 switch (Kind) {
Douglas Gregor55f6b142009-02-09 18:46:07 +000063 case tok::greater:
Douglas Gregor3965b7b2009-02-25 23:02:36 +000064 // C++ [temp.names]p3:
65 // [...] When parsing a template-argument-list, the first
66 // non-nested > is taken as the ending delimiter rather than a
67 // greater-than operator. [...]
Douglas Gregor55f6b142009-02-09 18:46:07 +000068 if (GreaterThanIsOperator)
69 return prec::Relational;
70 return prec::Unknown;
71
Douglas Gregor3965b7b2009-02-25 23:02:36 +000072 case tok::greatergreater:
73 // C++0x [temp.names]p3:
74 //
75 // [...] Similarly, the first non-nested >> is treated as two
76 // consecutive but distinct > tokens, the first of which is
77 // taken as the end of the template-argument-list and completes
78 // the template-id. [...]
79 if (GreaterThanIsOperator || !CPlusPlus0x)
80 return prec::Shift;
81 return prec::Unknown;
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 default: return prec::Unknown;
84 case tok::comma: return prec::Comma;
85 case tok::equal:
86 case tok::starequal:
87 case tok::slashequal:
88 case tok::percentequal:
89 case tok::plusequal:
90 case tok::minusequal:
91 case tok::lesslessequal:
92 case tok::greatergreaterequal:
93 case tok::ampequal:
94 case tok::caretequal:
95 case tok::pipeequal: return prec::Assignment;
96 case tok::question: return prec::Conditional;
97 case tok::pipepipe: return prec::LogicalOr;
98 case tok::ampamp: return prec::LogicalAnd;
99 case tok::pipe: return prec::InclusiveOr;
100 case tok::caret: return prec::ExclusiveOr;
101 case tok::amp: return prec::And;
102 case tok::exclaimequal:
103 case tok::equalequal: return prec::Equality;
104 case tok::lessequal:
105 case tok::less:
Douglas Gregor55f6b142009-02-09 18:46:07 +0000106 case tok::greaterequal: return prec::Relational;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000107 case tok::lessless: return prec::Shift;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 case tok::plus:
109 case tok::minus: return prec::Additive;
110 case tok::percent:
111 case tok::slash:
112 case tok::star: return prec::Multiplicative;
Sebastian Redl22460502009-02-07 00:15:38 +0000113 case tok::periodstar:
114 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116}
117
118
119/// ParseExpression - Simple precedence-based parser for binary/ternary
120/// operators.
121///
122/// Note: we diverge from the C99 grammar when parsing the assignment-expression
123/// production. C99 specifies that the LHS of an assignment operator should be
124/// parsed as a unary-expression, but consistency dictates that it be a
125/// conditional-expession. In practice, the important thing here is that the
126/// LHS of an assignment has to be an l-value, which productions between
127/// unary-expression and conditional-expression don't produce. Because we want
128/// consistency, we parse the LHS as a conditional-expression, then check for
129/// l-value-ness in semantic analysis stages.
130///
Sebastian Redl22460502009-02-07 00:15:38 +0000131/// pm-expression: [C++ 5.5]
132/// cast-expression
133/// pm-expression '.*' cast-expression
134/// pm-expression '->*' cast-expression
135///
Reid Spencer5f016e22007-07-11 17:01:13 +0000136/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000137/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// cast-expression
139/// multiplicative-expression '*' cast-expression
140/// multiplicative-expression '/' cast-expression
141/// multiplicative-expression '%' cast-expression
142///
143/// additive-expression: [C99 6.5.6]
144/// multiplicative-expression
145/// additive-expression '+' multiplicative-expression
146/// additive-expression '-' multiplicative-expression
147///
148/// shift-expression: [C99 6.5.7]
149/// additive-expression
150/// shift-expression '<<' additive-expression
151/// shift-expression '>>' additive-expression
152///
153/// relational-expression: [C99 6.5.8]
154/// shift-expression
155/// relational-expression '<' shift-expression
156/// relational-expression '>' shift-expression
157/// relational-expression '<=' shift-expression
158/// relational-expression '>=' shift-expression
159///
160/// equality-expression: [C99 6.5.9]
161/// relational-expression
162/// equality-expression '==' relational-expression
163/// equality-expression '!=' relational-expression
164///
165/// AND-expression: [C99 6.5.10]
166/// equality-expression
167/// AND-expression '&' equality-expression
168///
169/// exclusive-OR-expression: [C99 6.5.11]
170/// AND-expression
171/// exclusive-OR-expression '^' AND-expression
172///
173/// inclusive-OR-expression: [C99 6.5.12]
174/// exclusive-OR-expression
175/// inclusive-OR-expression '|' exclusive-OR-expression
176///
177/// logical-AND-expression: [C99 6.5.13]
178/// inclusive-OR-expression
179/// logical-AND-expression '&&' inclusive-OR-expression
180///
181/// logical-OR-expression: [C99 6.5.14]
182/// logical-AND-expression
183/// logical-OR-expression '||' logical-AND-expression
184///
185/// conditional-expression: [C99 6.5.15]
186/// logical-OR-expression
187/// logical-OR-expression '?' expression ':' conditional-expression
188/// [GNU] logical-OR-expression '?' ':' conditional-expression
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000189/// [C++] the third operand is an assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000190///
191/// assignment-expression: [C99 6.5.16]
192/// conditional-expression
193/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000194/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000195///
196/// assignment-operator: one of
197/// = *= /= %= += -= <<= >>= &= ^= |=
198///
199/// expression: [C99 6.5.17]
200/// assignment-expression
201/// expression ',' assignment-expression
202///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000203Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000204 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000205 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000206
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000207 OwningExprResult LHS(ParseCastExpression(false));
208 if (LHS.isInvalid()) return move(LHS);
209
Sebastian Redld8c4e152008-12-11 22:33:27 +0000210 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000213/// This routine is called when the '@' is seen and consumed.
214/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000215/// routine is necessary to disambiguate @try-statement from,
216/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000217///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000218Parser::OwningExprResult
219Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000220 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000221 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000222
Sebastian Redld8c4e152008-12-11 22:33:27 +0000223 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000224}
225
Eli Friedmanadf077f2009-01-27 08:43:38 +0000226/// This routine is called when a leading '__extension__' is seen and
227/// consumed. This is necessary because the token gets consumed in the
228/// process of disambiguating between an expression and a declaration.
229Parser::OwningExprResult
230Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
231 // FIXME: The handling for throw is almost certainly wrong.
232 if (Tok.is(tok::kw_throw))
233 return ParseThrowExpression();
234
235 OwningExprResult LHS(ParseCastExpression(false));
236 if (LHS.isInvalid()) return move(LHS);
237
238 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000239 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000240 if (LHS.isInvalid()) return move(LHS);
241
242 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
243}
244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
246///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000247Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000248 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000249 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000250
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000251 OwningExprResult LHS(ParseCastExpression(false));
252 if (LHS.isInvalid()) return move(LHS);
253
Sebastian Redld8c4e152008-12-11 22:33:27 +0000254 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Chris Lattnerb93fb492008-06-02 21:31:07 +0000257/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
258/// where part of an objc message send has already been parsed. In this case
259/// LBracLoc indicates the location of the '[' of the message send, and either
260/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
261/// message.
262///
263/// Since this handles full assignment-expression's, it handles postfix
264/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000265Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000266Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000267 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000268 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000269 ExprArg ReceiverExpr) {
270 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
271 ReceiverName,
272 move(ReceiverExpr)));
273 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000274 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000275 if (R.isInvalid()) return move(R);
276 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000277}
278
279
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000280Parser::OwningExprResult Parser::ParseConstantExpression() {
281 OwningExprResult LHS(ParseCastExpression(false));
282 if (LHS.isInvalid()) return move(LHS);
283
Sebastian Redld8c4e152008-12-11 22:33:27 +0000284 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000289Parser::OwningExprResult
290Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
292 GreaterThanIsOperator,
293 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 SourceLocation ColonLoc;
295
296 while (1) {
297 // If this token has a lower precedence than we are allowed to parse (e.g.
298 // because we are called recursively, or because the token is not a binop),
299 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000300 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000301 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302
303 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000304 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000308 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000310 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Handle this production specially:
312 // logical-OR-expression '?' expression ':' conditional-expression
313 // In particular, the RHS of the '?' is 'expression', not
314 // 'logical-OR-expression' as we might expect.
315 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000317 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 } else {
319 // Special case handling of "X ? Y : Z" where Y is empty:
320 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000321 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 Diag(Tok, diag::ext_gnu_conditional_expr);
323 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000324
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000325 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000327 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000328 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // Eat the colon.
332 ColonLoc = ConsumeToken();
333 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // Parse another leaf here for the RHS of the operator.
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000336 // ParseCastExpression works here because all RHS expressions in C have it
337 // as a prefix, at least. However, in C++, an assignment-expression could
338 // be a throw-expression, which is not a valid cast-expression.
339 // Therefore we need some special-casing here.
340 // Also note that the third operand of the conditional operator is
341 // an assignment-expression in C++.
342 OwningExprResult RHS(Actions);
343 if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
344 RHS = ParseAssignmentExpression();
345 else
346 RHS = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000347 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000348 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000349
350 // Remember the precedence of this operator and get the precedence of the
351 // operator immediately to the right of the RHS.
352 unsigned ThisPrec = NextTokPrec;
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000353 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
354 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000355
356 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000357 bool isRightAssoc = ThisPrec == prec::Conditional ||
358 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
360 // Get the precedence of the operator to the right of the RHS. If it binds
361 // more tightly with RHS than we do, evaluate it completely first.
362 if (ThisPrec < NextTokPrec ||
363 (ThisPrec == NextTokPrec && isRightAssoc)) {
364 // If this is left-associative, only parse things on the RHS that bind
365 // more tightly than the current operator. If it is left-associative, it
366 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
367 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000368 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000369 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000370 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000371 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000372
Douglas Gregor3965b7b2009-02-25 23:02:36 +0000373 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
374 getLang().CPlusPlus0x);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 }
376 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000377
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000378 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000379 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000380 if (TernaryMiddle.isInvalid()) {
381 // If we're using '>>' as an operator within a template
382 // argument list (in C++98), suggest the addition of
383 // parentheses so that the code remains well-formed in C++0x.
384 if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater))
385 SuggestParentheses(OpToken.getLocation(),
386 diag::warn_cxx0x_right_shift_in_template_arg,
387 SourceRange(Actions.getExprRange(LHS.get()).getBegin(),
388 Actions.getExprRange(RHS.get()).getEnd()));
389
Sebastian Redleffa8d12008-12-10 00:02:53 +0000390 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000391 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000392 } else
Steve Narofff69936d2007-09-16 03:34:24 +0000393 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000394 move(LHS), move(TernaryMiddle),
395 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000396 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 }
398}
399
400/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000401/// true, parse a unary-expression. isAddressOfOperand exists because an
402/// id-expression that is the operand of address-of gets special treatment
403/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000404///
405/// cast-expression: [C99 6.5.4]
406/// unary-expression
407/// '(' type-name ')' cast-expression
408///
409/// unary-expression: [C99 6.5.3]
410/// postfix-expression
411/// '++' unary-expression
412/// '--' unary-expression
413/// unary-operator cast-expression
414/// 'sizeof' unary-expression
415/// 'sizeof' '(' type-name ')'
416/// [GNU] '__alignof' unary-expression
417/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000418/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000420/// [C++] new-expression
421/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000422///
423/// unary-operator: one of
424/// '&' '*' '+' '-' '~' '!'
425/// [GNU] '__extension__' '__real' '__imag'
426///
427/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000428/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000429/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000430/// constant
431/// string-literal
432/// [C++] boolean-literal [C++ 2.13.5]
433/// '(' expression ')'
434/// '__func__' [C99 6.4.2.2]
435/// [GNU] '__FUNCTION__'
436/// [GNU] '__PRETTY_FUNCTION__'
437/// [GNU] '(' compound-statement ')'
438/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
439/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
440/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
441/// assign-expr ')'
442/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000443/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000444/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000445/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000446/// [OBJC] '@protocol' '(' identifier ')'
447/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000448/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000449/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
450/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000451/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
452/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
453/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
454/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000455/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
456/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000457/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000458/// [G++] unary-type-trait '(' type-id ')'
459/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000460/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000461///
462/// constant: [C99 6.4.4]
463/// integer-constant
464/// floating-constant
465/// enumeration-constant -> identifier
466/// character-constant
467///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000468/// id-expression: [C++ 5.1]
469/// unqualified-id
470/// qualified-id [TODO]
471///
472/// unqualified-id: [C++ 5.1]
473/// identifier
474/// operator-function-id
475/// conversion-function-id [TODO]
476/// '~' class-name [TODO]
477/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000478///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000479/// new-expression: [C++ 5.3.4]
480/// '::'[opt] 'new' new-placement[opt] new-type-id
481/// new-initializer[opt]
482/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
483/// new-initializer[opt]
484///
485/// delete-expression: [C++ 5.3.5]
486/// '::'[opt] 'delete' cast-expression
487/// '::'[opt] 'delete' '[' ']' cast-expression
488///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000489/// [GNU] unary-type-trait:
490/// '__has_nothrow_assign' [TODO]
491/// '__has_nothrow_copy' [TODO]
492/// '__has_nothrow_constructor' [TODO]
493/// '__has_trivial_assign' [TODO]
494/// '__has_trivial_copy' [TODO]
Anders Carlsson347ba892009-04-16 00:08:20 +0000495/// '__has_trivial_constructor'
Anders Carlsson072abef2009-04-17 02:34:54 +0000496/// '__has_trivial_destructor'
Sebastian Redl64b45f72009-01-05 20:52:13 +0000497/// '__has_virtual_destructor' [TODO]
498/// '__is_abstract' [TODO]
499/// '__is_class'
500/// '__is_empty' [TODO]
501/// '__is_enum'
502/// '__is_pod'
503/// '__is_polymorphic'
504/// '__is_union'
505///
506/// [GNU] binary-type-trait:
507/// '__is_base_of' [TODO]
508///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000509Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
510 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000511 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 tok::TokenKind SavedKind = Tok.getKind();
513
514 // This handles all of cast-expression, unary-expression, postfix-expression,
515 // and primary-expression. We handle them together like this for efficiency
516 // and to simplify handling of an expression starting with a '(' token: which
517 // may be one of a parenthesized expression, cast-expression, compound literal
518 // expression, or statement expression.
519 //
520 // If the parsed tokens consist of a primary-expression, the cases below
521 // call ParsePostfixExpressionSuffix to handle the postfix expression
522 // suffixes. Cases that cannot be followed by postfix exprs should
523 // return without invoking ParsePostfixExpressionSuffix.
524 switch (SavedKind) {
525 case tok::l_paren: {
526 // If this expression is limited to being a unary-expression, the parent can
527 // not start a cast expression.
528 ParenParseOption ParenExprType =
529 isUnaryExpression ? CompoundLiteral : CastExpr;
530 TypeTy *CastTy;
531 SourceLocation LParenLoc = Tok.getLocation();
532 SourceLocation RParenLoc;
533 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000534 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535
536 switch (ParenExprType) {
537 case SimpleExpr: break; // Nothing else to do.
538 case CompoundStmt: break; // Nothing else to do.
539 case CompoundLiteral:
540 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
541 // postfix-expression exist, parse them now.
542 break;
543 case CastExpr:
544 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
545 // the cast-expression that follows it next.
546 // TODO: For cast expression with CastTy.
547 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000548 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000549 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000550 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000552
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000554 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 // primary-expression
558 case tok::numeric_constant:
559 // constant: integer-constant
560 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000561
Steve Narofff69936d2007-09-16 03:34:24 +0000562 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000566 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000567
568 case tok::kw_true:
569 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000570 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000571
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000572 case tok::identifier: { // primary-expression: identifier
573 // unqualified-id: identifier
574 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000575 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000576 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000577 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000578 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
579 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000580 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000581 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000582
Steve Naroff61f72cb2009-03-09 21:12:44 +0000583 // Support 'Class.property' notation.
584 // We don't use isTokObjCMessageIdentifierReceiver(), since it allows
585 // 'super' (which is inappropriate here).
586 if (getLang().ObjC1 &&
587 Actions.getTypeName(*Tok.getIdentifierInfo(),
588 Tok.getLocation(), CurScope) &&
589 NextToken().is(tok::period)) {
590 IdentifierInfo &ReceiverName = *Tok.getIdentifierInfo();
591 SourceLocation IdentLoc = ConsumeToken();
592 SourceLocation DotLoc = ConsumeToken();
593
594 if (Tok.isNot(tok::identifier)) {
595 Diag(Tok, diag::err_expected_ident);
596 return ExprError();
597 }
598 IdentifierInfo &PropertyName = *Tok.getIdentifierInfo();
599 SourceLocation PropertyLoc = ConsumeToken();
600
601 Res = Actions.ActOnClassPropertyRefExpr(ReceiverName, PropertyName,
602 IdentLoc, PropertyLoc);
Steve Naroffed91f902009-04-02 18:37:59 +0000603 // These can be followed by postfix-expr pieces.
604 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff61f72cb2009-03-09 21:12:44 +0000605 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // Consume the identifier so that we can see if it is followed by a '('.
607 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
608 // need to know whether or not this identifier is a function designator or
609 // not.
610 IdentifierInfo &II = *Tok.getIdentifierInfo();
611 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000612 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000614 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
616 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000617 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 ConsumeToken();
619 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000620 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
622 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
623 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000624 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 ConsumeToken();
626 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000627 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 case tok::string_literal: // primary-expression: string-literal
629 case tok::wide_string_literal:
630 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000631 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000633 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 case tok::kw___builtin_va_arg:
635 case tok::kw___builtin_offsetof:
636 case tok::kw___builtin_choose_expr:
637 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000638 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000639 case tok::kw___null:
Sebastian Redlf53597f2009-03-15 17:47:39 +0000640 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000641 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 case tok::plusplus: // unary-expression: '++' unary-expression
643 case tok::minusminus: { // unary-expression: '--' unary-expression
644 SourceLocation SavedLoc = ConsumeToken();
645 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000646 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000647 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000648 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000650 case tok::amp: { // unary-expression: '&' cast-expression
651 // Special treatment because of member pointers
652 SourceLocation SavedLoc = ConsumeToken();
653 Res = ParseCastExpression(false, true);
654 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000655 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000656 return move(Res);
657 }
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 case tok::star: // unary-expression: '*' cast-expression
660 case tok::plus: // unary-expression: '+' cast-expression
661 case tok::minus: // unary-expression: '-' cast-expression
662 case tok::tilde: // unary-expression: '~' cast-expression
663 case tok::exclaim: // unary-expression: '!' cast-expression
664 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000665 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 SourceLocation SavedLoc = ConsumeToken();
667 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000668 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000669 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000670 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000671 }
672
Chris Lattner35080842008-02-02 20:20:10 +0000673 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
674 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000675 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000676 SourceLocation SavedLoc = ConsumeToken();
677 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000678 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000679 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000680 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
682 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
683 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000684 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
686 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000687 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000688 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 case tok::ampamp: { // unary-expression: '&&' identifier
690 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000691 if (Tok.isNot(tok::identifier))
692 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000695 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 Tok.getIdentifierInfo());
697 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000698 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 }
700 case tok::kw_const_cast:
701 case tok::kw_dynamic_cast:
702 case tok::kw_reinterpret_cast:
703 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000704 Res = ParseCXXCasts();
705 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000706 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000707 case tok::kw_typeid:
708 Res = ParseCXXTypeid();
709 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000710 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000711 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000712 Res = ParseCXXThis();
713 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000714 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000715
716 case tok::kw_char:
717 case tok::kw_wchar_t:
718 case tok::kw_bool:
719 case tok::kw_short:
720 case tok::kw_int:
721 case tok::kw_long:
722 case tok::kw_signed:
723 case tok::kw_unsigned:
724 case tok::kw_float:
725 case tok::kw_double:
726 case tok::kw_void:
Douglas Gregord57959a2009-03-27 23:10:48 +0000727 case tok::kw_typename:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000728 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000729 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000730 if (!getLang().CPlusPlus) {
731 Diag(Tok, diag::err_expected_expression);
732 return ExprError();
733 }
734
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000735 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
736 //
737 DeclSpec DS;
738 ParseCXXSimpleTypeSpecifier(DS);
739 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000740 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
741 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000742
743 Res = ParseCXXTypeConstructExpression(DS);
744 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000745 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000746 }
747
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000748 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
749 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
750 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000751 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000752 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000753
Chris Lattner74ba4102009-01-04 22:52:14 +0000754 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000755 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
756 // annotates the token, tail recurse.
757 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000758 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
759
Chris Lattner74ba4102009-01-04 22:52:14 +0000760 // ::new -> [C++] new-expression
761 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000762 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000763 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000764 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000765 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000766 return ParseCXXDeleteExpression(true, CCLoc);
767
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000768 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000769 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000770 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000771 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000772
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000773 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000774 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000775
776 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000777 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000778
Sebastian Redl64b45f72009-01-05 20:52:13 +0000779 case tok::kw___is_pod: // [GNU] unary-type-trait
780 case tok::kw___is_class:
781 case tok::kw___is_enum:
782 case tok::kw___is_union:
783 case tok::kw___is_polymorphic:
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000784 case tok::kw___is_abstract:
Anders Carlsson347ba892009-04-16 00:08:20 +0000785 case tok::kw___has_trivial_constructor:
Anders Carlsson072abef2009-04-17 02:34:54 +0000786 case tok::kw___has_trivial_destructor:
Sebastian Redl64b45f72009-01-05 20:52:13 +0000787 return ParseUnaryTypeTrait();
788
Chris Lattnerc97c2042007-10-03 22:03:06 +0000789 case tok::at: {
790 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000791 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000792 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000793 case tok::caret:
Chris Lattner9af55002009-03-27 04:18:06 +0000794 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000795 case tok::l_square:
796 // These can be followed by postfix-expr pieces.
797 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000798 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000799 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 default:
801 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000802 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 // unreachable.
806 abort();
807}
808
809/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
810/// is parsed, this method parses any suffixes that apply.
811///
812/// postfix-expression: [C99 6.5.2]
813/// primary-expression
814/// postfix-expression '[' expression ']'
815/// postfix-expression '(' argument-expression-list[opt] ')'
816/// postfix-expression '.' identifier
817/// postfix-expression '->' identifier
818/// postfix-expression '++'
819/// postfix-expression '--'
820/// '(' type-name ')' '{' initializer-list '}'
821/// '(' type-name ')' '{' initializer-list ',' '}'
822///
823/// argument-expression-list: [C99 6.5.2]
824/// argument-expression
825/// argument-expression-list ',' assignment-expression
826///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000827Parser::OwningExprResult
828Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 // Now that the primary-expression piece of the postfix-expression has been
830 // parsed, see if there are any postfix-expression pieces here.
831 SourceLocation Loc;
832 while (1) {
833 switch (Tok.getKind()) {
834 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000835 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
837 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000838 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000841
842 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000843 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
844 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000845 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000846 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000847
848 // Match the ']'.
849 MatchRHSPunctuation(tok::r_square, Loc);
850 break;
851 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000854 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000855 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000856
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000858
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000859 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000860 if (ParseExpressionList(ArgExprs, CommaLocs)) {
861 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000862 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 }
864 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000865
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 // Match the ')'.
Chris Lattner1721a2d2009-04-13 00:10:38 +0000867 if (Tok.isNot(tok::r_paren)) {
868 MatchRHSPunctuation(tok::r_paren, Loc);
869 return ExprError();
870 }
871
872 if (!LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
874 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000875 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000876 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000877 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 }
Chris Lattner1721a2d2009-04-13 00:10:38 +0000879
880 ConsumeParen();
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 break;
882 }
883 case tok::arrow: // postfix-expression: p-e '->' identifier
884 case tok::period: { // postfix-expression: p-e '.' identifier
885 tok::TokenKind OpKind = Tok.getKind();
886 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000887
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000888 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000890 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000892
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000893 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000894 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000895 OpKind, Tok.getLocation(),
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +0000896 *Tok.getIdentifierInfo(),
897 ObjCImpDecl);
Sebastian Redla55e52c2008-11-25 22:21:31 +0000898 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 ConsumeToken();
900 break;
901 }
902 case tok::plusplus: // postfix-expression: postfix-expression '++'
903 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000904 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000905 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000906 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000907 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 ConsumeToken();
909 break;
910 }
911 }
912}
913
914
915/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
916/// unary-expression: [C99 6.5.3]
917/// 'sizeof' unary-expression
918/// 'sizeof' '(' type-name ')'
919/// [GNU] '__alignof' unary-expression
920/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000921/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000922Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000923 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
924 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000926 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 ConsumeToken();
928
929 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000930 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000931 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 Operand = ParseCastExpression(true);
933 } else {
934 // If it starts with a '(', we know that it is either a parenthesized
935 // type-name, or it is a unary-expression that starts with a compound
936 // literal, or starts with a primary-expression that is a parenthesized
937 // expression.
938 ParenParseOption ExprType = CastExpr;
939 TypeTy *CastTy;
940 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
941 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000942
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
944 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000945 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000946 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000947 OpTok.is(tok::kw_sizeof),
948 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000949 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000950
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000951 // If this is a parenthesized expression, it is the start of a
952 // unary-expression, but doesn't include any postfix pieces. Parse these
953 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000954 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000956
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000958 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000959 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
960 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000961 /*isType=*/false,
962 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000963 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000964}
965
966/// ParseBuiltinPrimaryExpression
967///
968/// primary-expression: [C99 6.5.1]
969/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
970/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
971/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
972/// assign-expr ')'
973/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
974///
975/// [GNU] offsetof-member-designator:
976/// [GNU] identifier
977/// [GNU] offsetof-member-designator '.' identifier
978/// [GNU] offsetof-member-designator '[' expression ']'
979///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000980Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000981 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
983
984 tok::TokenKind T = Tok.getKind();
985 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
986
987 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000988 if (Tok.isNot(tok::l_paren))
989 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
990 << BuiltinII);
991
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 SourceLocation LParenLoc = ConsumeParen();
993 // TODO: Build AST.
994
995 switch (T) {
996 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000997 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000998 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000999 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001001 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 }
1003
1004 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001005 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001006
Douglas Gregor809070a2009-02-18 17:45:20 +00001007 TypeResult Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001008
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001009 if (Tok.isNot(tok::r_paren)) {
1010 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001011 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001012 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001013 if (Ty.isInvalid())
1014 Res = ExprError();
1015 else
Sebastian Redlf53597f2009-03-15 17:47:39 +00001016 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001018 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001019 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +00001020 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor809070a2009-02-18 17:45:20 +00001021 TypeResult Ty = ParseTypeName();
Chris Lattnerca7102c2009-03-24 17:21:43 +00001022 if (Ty.isInvalid()) {
1023 SkipUntil(tok::r_paren);
1024 return ExprError();
1025 }
1026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001028 return ExprError();
1029
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001031 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001032 Diag(Tok, diag::err_expected_ident);
1033 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001034 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001035 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001036
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001037 // Keep track of the various subcomponents we see.
1038 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001039
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001040 Comps.push_back(Action::OffsetOfComponent());
1041 Comps.back().isBrackets = false;
1042 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1043 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001044
Sebastian Redla55e52c2008-11-25 22:21:31 +00001045 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001047 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001049 Comps.push_back(Action::OffsetOfComponent());
1050 Comps.back().isBrackets = false;
1051 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001052
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001053 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001054 Diag(Tok, diag::err_expected_ident);
1055 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001057 }
1058 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1059 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001060
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001061 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001063 Comps.push_back(Action::OffsetOfComponent());
1064 Comps.back().isBrackets = true;
1065 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001067 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001069 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001071 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001072
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001073 Comps.back().LocEnd =
1074 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001075 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001076 if (Ty.isInvalid())
1077 Res = ExprError();
1078 else
1079 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1080 Ty.get(), &Comps[0],
1081 Comps.size(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001082 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001084 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001085 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 }
1087 }
1088 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001089 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001090 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001091 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001092 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001093 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001094 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001095 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001097 return ExprError();
1098
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001099 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001100 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001101 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001102 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001103 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001105 return ExprError();
1106
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001107 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001108 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001109 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001110 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001111 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001112 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001113 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001114 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001115 }
Sebastian Redlf53597f2009-03-15 17:47:39 +00001116 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1117 move(Expr2), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001118 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001119 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 case tok::kw___builtin_types_compatible_p:
Douglas Gregor809070a2009-02-18 17:45:20 +00001121 TypeResult Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001122
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001124 return ExprError();
1125
Douglas Gregor809070a2009-02-18 17:45:20 +00001126 TypeResult Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001127
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001128 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001129 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001130 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001131 }
Douglas Gregor809070a2009-02-18 17:45:20 +00001132
1133 if (Ty1.isInvalid() || Ty2.isInvalid())
1134 Res = ExprError();
1135 else
1136 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1137 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001138 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001139 }
1140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 // These can be followed by postfix-expr pieces because they are
1142 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001143 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001144}
1145
1146/// ParseParenExpression - This parses the unit that starts with a '(' token,
1147/// based on what is allowed by ExprType. The actual thing parsed is returned
1148/// in ExprType.
1149///
1150/// primary-expression: [C99 6.5.1]
1151/// '(' expression ')'
1152/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1153/// postfix-expression: [C99 6.5.2]
1154/// '(' type-name ')' '{' initializer-list '}'
1155/// '(' type-name ')' '{' initializer-list ',' '}'
1156/// cast-expression: [C99 6.5.4]
1157/// '(' type-name ')' cast-expression
1158///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001159Parser::OwningExprResult
1160Parser::ParseParenExpression(ParenParseOption &ExprType,
1161 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001162 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001163 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001165 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001167
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001168 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001170 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001172
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001173 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001174 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redlf53597f2009-03-15 17:47:39 +00001175 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001176
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001177 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor809070a2009-02-18 17:45:20 +00001179 TypeResult Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +00001180
1181 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001182 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 RParenLoc = ConsumeParen();
1184 else
1185 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001186
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001187 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 if (!getLang().C99) // Compound literals don't exist in C90.
1189 Diag(OpenLoc, diag::ext_c99_compound_literal);
1190 Result = ParseInitializer();
1191 ExprType = CompoundLiteral;
Douglas Gregor809070a2009-02-18 17:45:20 +00001192 if (!Result.isInvalid() && !Ty.isInvalid())
1193 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001194 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001195 return move(Result);
1196 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001197
Chris Lattner42ece642008-12-12 06:00:12 +00001198 if (ExprType == CastExpr) {
Douglas Gregor809070a2009-02-18 17:45:20 +00001199 // Note that this doesn't parse the subsequent cast-expression, it just
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 // returns the parsed type to the callee.
1201 ExprType = CastExpr;
Douglas Gregor809070a2009-02-18 17:45:20 +00001202
1203 if (Ty.isInvalid())
1204 return ExprError();
1205
1206 CastTy = Ty.get();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001207 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001209
Chris Lattner42ece642008-12-12 06:00:12 +00001210 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1211 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 } else {
1213 Result = ParseExpression();
1214 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001215 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001216 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001220 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001222 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 }
Chris Lattner42ece642008-12-12 06:00:12 +00001224
1225 if (Tok.is(tok::r_paren))
1226 RParenLoc = ConsumeParen();
1227 else
1228 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001229
1230 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231}
1232
1233/// ParseStringLiteralExpression - This handles the various token types that
1234/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1235/// translation phase #6].
1236///
1237/// primary-expression: [C99 6.5.1]
1238/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001239Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1243 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001244 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001245
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 do {
1247 StringToks.push_back(Tok);
1248 ConsumeStringToken();
1249 } while (isTokenStringLiteral());
1250
1251 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001252 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001253}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001254
1255/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1256///
1257/// argument-expression-list:
1258/// assignment-expression
1259/// argument-expression-list , assignment-expression
1260///
1261/// [C++] expression-list:
1262/// [C++] assignment-expression
1263/// [C++] expression-list , assignment-expression
1264///
1265bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1266 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001267 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001268 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001269 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001270
Sebastian Redleffa8d12008-12-10 00:02:53 +00001271 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001272
1273 if (Tok.isNot(tok::comma))
1274 return false;
1275 // Move to the next argument, remember where the comma was.
1276 CommaLocs.push_back(ConsumeToken());
1277 }
1278}
Steve Naroff296e8d52008-08-28 19:20:44 +00001279
Mike Stump98eb8a72009-02-04 22:31:32 +00001280/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1281///
1282/// [clang] block-id:
1283/// [clang] specifier-qualifier-list block-declarator
1284///
1285void Parser::ParseBlockId() {
1286 // Parse the specifier-qualifier-list piece.
1287 DeclSpec DS;
1288 ParseSpecifierQualifierList(DS);
1289
1290 // Parse the block-declarator.
1291 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1292 ParseDeclarator(DeclaratorInfo);
1293 // Inform sema that we are starting a block.
1294 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1295}
1296
Steve Naroff296e8d52008-08-28 19:20:44 +00001297/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001298/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001299///
1300/// block-literal:
1301/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001302/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001303/// [clang] block-args:
1304/// [clang] '(' parameter-list ')'
1305///
Sebastian Redl1d922962008-12-13 15:32:12 +00001306Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001307 assert(Tok.is(tok::caret) && "block literal starts with ^");
1308 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001309
Chris Lattner6b91f002009-03-05 07:32:12 +00001310 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1311 "block literal parsing");
1312
Steve Naroff296e8d52008-08-28 19:20:44 +00001313 // Enter a scope to hold everything within the block. This includes the
1314 // argument decls, decls within the compound expression, etc. This also
1315 // allows determining whether a variable reference inside the block is
1316 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001317 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1318 Scope::BreakScope | Scope::ContinueScope |
1319 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001320
1321 // Inform sema that we are starting a block.
1322 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattner6b91f002009-03-05 07:32:12 +00001323
Steve Naroff296e8d52008-08-28 19:20:44 +00001324 // Parse the return type if present.
1325 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001326 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001327 // FIXME: Since the return type isn't actually parsed, it can't be used to
1328 // fill ParamInfo with an initial valid range, so do it manually.
1329 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001330
Steve Naroff296e8d52008-08-28 19:20:44 +00001331 // If this block has arguments, parse them. There is no ambiguity here with
1332 // the expression case, because the expression case requires a parameter list.
1333 if (Tok.is(tok::l_paren)) {
1334 ParseParenDeclarator(ParamInfo);
1335 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001336 // SetIdentifier sets the source range end, but in this case we're past
1337 // that location.
1338 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001339 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001340 ParamInfo.SetRangeEnd(Tmp);
Chris Lattnereaaebc72009-04-25 08:06:05 +00001341 if (ParamInfo.isInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001342 // If there was an error parsing the arguments, they may have
1343 // tried to use ^(x+y) which requires an argument list. Just
1344 // skip the whole block literal.
Chris Lattner4f2aac32009-04-18 20:05:34 +00001345 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001346 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001347 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001348 // Inform sema that we are starting a block.
1349 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stumpaa771a82009-04-14 18:24:37 +00001350 } else if (!Tok.is(tok::l_brace)) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001351 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001352 } else {
1353 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001354 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1355 SourceLocation(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00001356 0, 0, 0,
1357 false, false, 0, 0,
1358 CaretLoc, ParamInfo),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001359 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001360 // Inform sema that we are starting a block.
1361 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001362 }
1363
Sebastian Redl1d922962008-12-13 15:32:12 +00001364
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001365 OwningExprResult Result(Actions, true);
Chris Lattner9af55002009-03-27 04:18:06 +00001366 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001367 // Saw something like: ^expr
1368 Diag(Tok, diag::err_expected_expression);
Chris Lattner4f2aac32009-04-18 20:05:34 +00001369 Actions.ActOnBlockError(CaretLoc, CurScope);
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001370 return ExprError();
1371 }
Chris Lattner9af55002009-03-27 04:18:06 +00001372
1373 OwningStmtResult Stmt(ParseCompoundStatementBody());
1374 if (!Stmt.isInvalid())
1375 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1376 else
1377 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001378 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001379}
1380