blob: 3280730564fd364609086e9b6a5d46da2428334e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Narofffd5b19d2008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnere533c7d2009-03-05 07:32:12 +000025#include "clang/Basic/PrettyStackTrace.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000026#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000027#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-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 // .*, ->*
Chris Lattner4b009652007-07-25 00:24:17 +000052 };
53}
54
55
56/// getBinOpPrecedence - Return the precedence of the specified binary operator
57/// token. This returns:
58///
Douglas Gregor8e458f42009-02-09 18:46:07 +000059static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
Douglas Gregorf2d87392009-02-25 23:02:36 +000060 bool GreaterThanIsOperator,
61 bool CPlusPlus0x) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 switch (Kind) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000063 case tok::greater:
Douglas Gregorf2d87392009-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 Gregor8e458f42009-02-09 18:46:07 +000068 if (GreaterThanIsOperator)
69 return prec::Relational;
70 return prec::Unknown;
71
Douglas Gregorf2d87392009-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor8e458f42009-02-09 18:46:07 +0000106 case tok::greaterequal: return prec::Relational;
Douglas Gregorf2d87392009-02-25 23:02:36 +0000107 case tok::lessless: return prec::Shift;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-02-07 00:15:38 +0000113 case tok::periodstar:
114 case tok::arrowstar: return prec::PointerToMember;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl95216a62009-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///
Chris Lattner4b009652007-07-25 00:24:17 +0000136/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl95216a62009-02-07 00:15:38 +0000137/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbd261962009-04-16 17:51:27 +0000189/// [C++] the third operand is an assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000190///
191/// assignment-expression: [C99 6.5.16]
192/// conditional-expression
193/// unary-expression assignment-operator assignment-expression
Chris Lattnera7447ba2008-02-26 00:51:44 +0000194/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000195///
196/// assignment-operator: one of
197/// = *= /= %= += -= <<= >>= &= ^= |=
198///
199/// expression: [C99 6.5.17]
200/// assignment-expression
201/// expression ',' assignment-expression
202///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000203Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000204 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000205 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000206
Sebastian Redl14ca7412008-12-11 21:36:32 +0000207 OwningExprResult LHS(ParseCastExpression(false));
208 if (LHS.isInvalid()) return move(LHS);
209
Sebastian Redla6817a02008-12-11 22:33:27 +0000210 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000211}
212
Fariborz Jahanian64b864e2007-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 Lattnerb82d6ef2007-10-03 22:03:06 +0000215/// routine is necessary to disambiguate @try-statement from,
216/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000217///
Sebastian Redla6817a02008-12-11 22:33:27 +0000218Parser::OwningExprResult
219Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redla2deb432008-12-13 15:32:12 +0000220 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000221 if (LHS.isInvalid()) return move(LHS);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000222
Sebastian Redla6817a02008-12-11 22:33:27 +0000223 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000224}
225
Eli Friedmanc4772072009-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 Redl81db6682009-02-05 15:02:23 +0000239 move(LHS));
Eli Friedmanc4772072009-01-27 08:43:38 +0000240 if (LHS.isInvalid()) return move(LHS);
241
242 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
243}
244
Chris Lattner4b009652007-07-25 00:24:17 +0000245/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
246///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000247Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000248 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000249 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000250
Sebastian Redl14ca7412008-12-11 21:36:32 +0000251 OwningExprResult LHS(ParseCastExpression(false));
252 if (LHS.isInvalid()) return move(LHS);
253
Sebastian Redla6817a02008-12-11 22:33:27 +0000254 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000255}
256
Chris Lattnerbfcf4772008-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 Redla2deb432008-12-13 15:32:12 +0000265Parser::OwningExprResult
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000266Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000267 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000268 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-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 Redl14ca7412008-12-11 21:36:32 +0000274 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redla2deb432008-12-13 15:32:12 +0000275 if (R.isInvalid()) return move(R);
276 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000277}
278
279
Sebastian Redl14ca7412008-12-11 21:36:32 +0000280Parser::OwningExprResult Parser::ParseConstantExpression() {
281 OwningExprResult LHS(ParseCastExpression(false));
282 if (LHS.isInvalid()) return move(LHS);
283
Sebastian Redla6817a02008-12-11 22:33:27 +0000284 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000285}
286
Chris Lattner4b009652007-07-25 00:24:17 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
Sebastian Redla6817a02008-12-11 22:33:27 +0000289Parser::OwningExprResult
290Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregorf2d87392009-02-25 23:02:36 +0000291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(),
292 GreaterThanIsOperator,
293 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000300 if (NextTokPrec < MinPrec)
Sebastian Redla6817a02008-12-11 22:33:27 +0000301 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000302
303 // Consume the operator, saving the operator token for error reporting.
304 Token OpToken = Tok;
305 ConsumeToken();
Sebastian Redl95216a62009-02-07 00:15:38 +0000306
Chris Lattner4b009652007-07-25 00:24:17 +0000307 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000308 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000309 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000310 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000317 return move(TernaryMiddle);
Chris Lattner4b009652007-07-25 00:24:17 +0000318 } else {
319 // Special case handling of "X ? Y : Z" where Y is empty:
320 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000321 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000322 Diag(Tok, diag::ext_gnu_conditional_expr);
323 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000324
Chris Lattner4d7d2342007-10-09 17:41:39 +0000325 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000326 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000327 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redla6817a02008-12-11 22:33:27 +0000328 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000329 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000330
Chris Lattner4b009652007-07-25 00:24:17 +0000331 // Eat the colon.
332 ColonLoc = ConsumeToken();
333 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000334
Chris Lattner4b009652007-07-25 00:24:17 +0000335 // Parse another leaf here for the RHS of the operator.
Sebastian Redlbd261962009-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 Redlbb4dae72008-12-09 13:15:23 +0000347 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000348 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorf2d87392009-02-25 23:02:36 +0000353 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
354 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +0000355
356 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000357 bool isRightAssoc = ThisPrec == prec::Conditional ||
358 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl6008ac32008-11-25 22:21:31 +0000368 // The function takes ownership of the RHS.
Sebastian Redla6817a02008-12-11 22:33:27 +0000369 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000370 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000371 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000372
Douglas Gregorf2d87392009-02-25 23:02:36 +0000373 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator,
374 getLang().CPlusPlus0x);
Chris Lattner4b009652007-07-25 00:24:17 +0000375 }
376 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000377
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000378 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000379 // Combine the LHS and RHS into the LHS (e.g. build AST).
Douglas Gregor3bb30002009-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 Redl6f1ee232008-12-10 00:02:53 +0000390 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000391 OpToken.getKind(), move(LHS), move(RHS));
Douglas Gregor3bb30002009-02-26 21:00:50 +0000392 } else
Steve Naroff87d58b42007-09-16 03:34:24 +0000393 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000394 move(LHS), move(TernaryMiddle),
395 move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000396 }
Chris Lattner4b009652007-07-25 00:24:17 +0000397 }
398}
399
400/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl0c9da212009-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.
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor658b4442008-11-06 15:17:27 +0000418/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000419/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000420/// [C++] new-expression
421/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000422///
423/// unary-operator: one of
424/// '&' '*' '+' '-' '~' '!'
425/// [GNU] '__extension__' '__real' '__imag'
426///
427/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000428/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000429/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorad4b3792008-11-29 04:51:27 +0000443/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000444/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000445/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000446/// [OBJC] '@protocol' '(' identifier ')'
447/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000448/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-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]
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlb93b49c2008-11-11 11:37:55 +0000455/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
456/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000457/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000458/// [G++] unary-type-trait '(' type-id ')'
459/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000460/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000461///
462/// constant: [C99 6.4.4]
463/// integer-constant
464/// floating-constant
465/// enumeration-constant -> identifier
466/// character-constant
467///
Douglas Gregore60e5d32008-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]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000478///
Sebastian Redl19fec9d2008-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 Redl39c0f6f2009-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 Carlssonc6363712009-04-16 00:08:20 +0000495/// '__has_trivial_constructor'
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000496/// '__has_trivial_destructor' [TODO]
497/// '__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 Redl0c9da212009-02-03 20:19:35 +0000509Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
510 bool isAddressOfOperand) {
Sebastian Redl62261042008-12-09 20:22:58 +0000511 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl14ca7412008-12-11 21:36:32 +0000534 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000548 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000549 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000550 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000551 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000552
Chris Lattner4b009652007-07-25 00:24:17 +0000553 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000554 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000555 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000556
Chris Lattner4b009652007-07-25 00:24:17 +0000557 // primary-expression
558 case tok::numeric_constant:
559 // constant: integer-constant
560 // constant: floating-constant
Sebastian Redl14ca7412008-12-11 21:36:32 +0000561
Steve Naroff87d58b42007-09-16 03:34:24 +0000562 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000563 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000564
Chris Lattner4b009652007-07-25 00:24:17 +0000565 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000566 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000567
568 case tok::kw_true:
569 case tok::kw_false:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000570 return ParseCXXBoolLiteral();
Chris Lattner4b009652007-07-25 00:24:17 +0000571
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000572 case tok::identifier: { // primary-expression: identifier
573 // unqualified-id: identifier
574 // constant: enumeration-constant
Chris Lattner5d7eace2009-01-06 05:06:21 +0000575 // Turn a potentially qualified name into a annot_typename or
Chris Lattner68751c42009-01-04 22:52:14 +0000576 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner1e015942009-01-04 23:23:14 +0000577 if (getLang().CPlusPlus) {
Chris Lattner914660b2009-01-04 23:46:59 +0000578 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
579 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000580 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner1e015942009-01-04 23:23:14 +0000581 }
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000582
Steve Naroff73ec9322009-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 Naroffc5ba83c2009-04-02 18:37:59 +0000603 // These can be followed by postfix-expr pieces.
604 return ParsePostfixExpressionSuffix(move(Res));
Steve Naroff73ec9322009-03-09 21:12:44 +0000605 }
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner4d7d2342007-10-09 17:41:39 +0000612 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000613 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000614 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000615 }
616 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000617 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000618 ConsumeToken();
619 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000620 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner69909292008-08-10 01:53:14 +0000624 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000625 ConsumeToken();
626 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000627 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000628 case tok::string_literal: // primary-expression: string-literal
629 case tok::wide_string_literal:
630 Res = ParseStringLiteralExpression();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000631 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000632 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl14ca7412008-12-11 21:36:32 +0000633 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +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 Redla6817a02008-12-11 22:33:27 +0000638 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000639 case tok::kw___null:
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000640 return Actions.ActOnGNUNullExpr(ConsumeToken());
Douglas Gregorad4b3792008-11-29 04:51:27 +0000641 break;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000646 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000647 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000648 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000649 }
Sebastian Redl0c9da212009-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 Redl81db6682009-02-05 15:02:23 +0000655 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl0c9da212009-02-03 20:19:35 +0000656 return move(Res);
657 }
658
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6cf92942008-02-02 20:20:10 +0000665 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000666 SourceLocation SavedLoc = ConsumeToken();
667 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000668 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000669 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000670 return move(Res);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000671 }
672
Chris Lattner6cf92942008-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 Lattnerdaa5c002008-10-20 06:45:43 +0000675 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000676 SourceLocation SavedLoc = ConsumeToken();
677 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000678 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000679 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000680 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000681 }
682 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
683 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000684 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000685 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
686 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000687 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000688 return ParseSizeofAlignofExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000689 case tok::ampamp: { // unary-expression: '&&' identifier
690 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000691 if (Tok.isNot(tok::identifier))
692 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000693
Chris Lattner4b009652007-07-25 00:24:17 +0000694 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000695 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000696 Tok.getIdentifierInfo());
697 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000698 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +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:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000704 Res = ParseCXXCasts();
705 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000706 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000707 case tok::kw_typeid:
708 Res = ParseCXXTypeid();
709 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000710 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000711 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000712 Res = ParseCXXThis();
713 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000714 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-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 Gregord3022602009-03-27 23:10:48 +0000727 case tok::kw_typename:
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000728 case tok::kw_typeof:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000729 case tok::annot_typename: {
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000730 if (!getLang().CPlusPlus) {
731 Diag(Tok, diag::err_expected_expression);
732 return ExprError();
733 }
734
Argiris Kirtzidis7a1e7412008-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 Redl14ca7412008-12-11 21:36:32 +0000740 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
741 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000742
743 Res = ParseCXXTypeConstructExpression(DS);
744 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000745 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000746 }
747
Argiris Kirtzidis311db8c2008-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 Redl0c9da212009-02-03 20:19:35 +0000751 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000752 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000753
Chris Lattner68751c42009-01-04 22:52:14 +0000754 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000755 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
756 // annotates the token, tail recurse.
757 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000758 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
759
Chris Lattner68751c42009-01-04 22:52:14 +0000760 // ::new -> [C++] new-expression
761 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000762 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000763 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000764 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000765 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000766 return ParseCXXDeleteExpression(true, CCLoc);
767
Chris Lattner1e015942009-01-04 23:23:14 +0000768 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000769 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000770 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000771 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000772
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000773 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000774 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000775
776 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000777 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000778
Sebastian Redl39c0f6f2009-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 Carlsson1dae87f2009-03-22 01:52:17 +0000784 case tok::kw___is_abstract:
Anders Carlssonc6363712009-04-16 00:08:20 +0000785 case tok::kw___has_trivial_constructor:
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000786 return ParseUnaryTypeTrait();
787
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000788 case tok::at: {
789 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000790 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000791 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000792 case tok::caret:
Chris Lattnerc14c7f02009-03-27 04:18:06 +0000793 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000794 case tok::l_square:
795 // These can be followed by postfix-expr pieces.
796 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000797 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000798 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000799 default:
800 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000801 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000803
Chris Lattner4b009652007-07-25 00:24:17 +0000804 // unreachable.
805 abort();
806}
807
808/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
809/// is parsed, this method parses any suffixes that apply.
810///
811/// postfix-expression: [C99 6.5.2]
812/// primary-expression
813/// postfix-expression '[' expression ']'
814/// postfix-expression '(' argument-expression-list[opt] ')'
815/// postfix-expression '.' identifier
816/// postfix-expression '->' identifier
817/// postfix-expression '++'
818/// postfix-expression '--'
819/// '(' type-name ')' '{' initializer-list '}'
820/// '(' type-name ')' '{' initializer-list ',' '}'
821///
822/// argument-expression-list: [C99 6.5.2]
823/// argument-expression
824/// argument-expression-list ',' assignment-expression
825///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000826Parser::OwningExprResult
827Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000828 // Now that the primary-expression piece of the postfix-expression has been
829 // parsed, see if there are any postfix-expression pieces here.
830 SourceLocation Loc;
831 while (1) {
832 switch (Tok.getKind()) {
833 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000834 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000835 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
836 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000837 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000838
Chris Lattner4b009652007-07-25 00:24:17 +0000839 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000840
841 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000842 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
843 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000844 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000845 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000846
847 // Match the ']'.
848 MatchRHSPunctuation(tok::r_square, Loc);
849 break;
850 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000851
Chris Lattner4b009652007-07-25 00:24:17 +0000852 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000853 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000854 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000855
Chris Lattner4b009652007-07-25 00:24:17 +0000856 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000857
Chris Lattner4d7d2342007-10-09 17:41:39 +0000858 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000859 if (ParseExpressionList(ArgExprs, CommaLocs)) {
860 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000861 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000862 }
863 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000864
Chris Lattner4b009652007-07-25 00:24:17 +0000865 // Match the ')'.
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000866 if (Tok.isNot(tok::r_paren)) {
867 MatchRHSPunctuation(tok::r_paren, Loc);
868 return ExprError();
869 }
870
871 if (!LHS.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000872 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
873 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000874 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl8b769972009-01-19 00:08:26 +0000875 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redl6008ac32008-11-25 22:21:31 +0000876 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000877 }
Chris Lattner0c07d9a2009-04-13 00:10:38 +0000878
879 ConsumeParen();
Chris Lattner4b009652007-07-25 00:24:17 +0000880 break;
881 }
882 case tok::arrow: // postfix-expression: p-e '->' identifier
883 case tok::period: { // postfix-expression: p-e '.' identifier
884 tok::TokenKind OpKind = Tok.getKind();
885 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000886
Chris Lattner4d7d2342007-10-09 17:41:39 +0000887 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000888 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000889 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000890 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000891
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000892 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000893 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000894 OpKind, Tok.getLocation(),
Fariborz Jahanian0cc2ac12009-03-04 22:30:12 +0000895 *Tok.getIdentifierInfo(),
896 ObjCImpDecl);
Sebastian Redl6008ac32008-11-25 22:21:31 +0000897 }
Chris Lattner4b009652007-07-25 00:24:17 +0000898 ConsumeToken();
899 break;
900 }
901 case tok::plusplus: // postfix-expression: postfix-expression '++'
902 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000903 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000904 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000905 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000906 }
Chris Lattner4b009652007-07-25 00:24:17 +0000907 ConsumeToken();
908 break;
909 }
910 }
911}
912
913
914/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
915/// unary-expression: [C99 6.5.3]
916/// 'sizeof' unary-expression
917/// 'sizeof' '(' type-name ')'
918/// [GNU] '__alignof' unary-expression
919/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000920/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000921Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +0000922 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
923 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000924 "Not a sizeof/alignof expression!");
925 Token OpTok = Tok;
926 ConsumeToken();
927
928 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl62261042008-12-09 20:22:58 +0000929 OwningExprResult Operand(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000930 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000931 Operand = ParseCastExpression(true);
932 } else {
933 // If it starts with a '(', we know that it is either a parenthesized
934 // type-name, or it is a unary-expression that starts with a compound
935 // literal, or starts with a primary-expression that is a parenthesized
936 // expression.
937 ParenParseOption ExprType = CastExpr;
938 TypeTy *CastTy;
939 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
940 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +0000941
Chris Lattner4b009652007-07-25 00:24:17 +0000942 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
943 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000944 if (ExprType == CastExpr)
Sebastian Redl8b769972009-01-19 00:08:26 +0000945 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000946 OpTok.is(tok::kw_sizeof),
947 /*isType=*/true, CastTy,
Sebastian Redl8b769972009-01-19 00:08:26 +0000948 SourceRange(LParenLoc, RParenLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000949
Chris Lattner48553562007-11-13 20:50:37 +0000950 // If this is a parenthesized expression, it is the start of a
951 // unary-expression, but doesn't include any postfix pieces. Parse these
952 // now if present.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000953 Operand = ParsePostfixExpressionSuffix(move(Operand));
Chris Lattner4b009652007-07-25 00:24:17 +0000954 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000955
Chris Lattner4b009652007-07-25 00:24:17 +0000956 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000957 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000958 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
959 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000960 /*isType=*/false,
961 Operand.release(), SourceRange());
Sebastian Redla6817a02008-12-11 22:33:27 +0000962 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +0000963}
964
965/// ParseBuiltinPrimaryExpression
966///
967/// primary-expression: [C99 6.5.1]
968/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
969/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
970/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
971/// assign-expr ')'
972/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
973///
974/// [GNU] offsetof-member-designator:
975/// [GNU] identifier
976/// [GNU] offsetof-member-designator '.' identifier
977/// [GNU] offsetof-member-designator '[' expression ']'
978///
Sebastian Redla6817a02008-12-11 22:33:27 +0000979Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000980 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000981 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
982
983 tok::TokenKind T = Tok.getKind();
984 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
985
986 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +0000987 if (Tok.isNot(tok::l_paren))
988 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
989 << BuiltinII);
990
Chris Lattner4b009652007-07-25 00:24:17 +0000991 SourceLocation LParenLoc = ConsumeParen();
992 // TODO: Build AST.
993
994 switch (T) {
995 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000996 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000997 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000998 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000999 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001000 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001001 }
1002
1003 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001004 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001005
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001006 TypeResult Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001007
Anders Carlsson36760332007-10-15 20:28:48 +00001008 if (Tok.isNot(tok::r_paren)) {
1009 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001010 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +00001011 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001012 if (Ty.isInvalid())
1013 Res = ExprError();
1014 else
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001015 Res = Actions.ActOnVAArg(StartLoc, move(Expr), Ty.get(), ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +00001016 break;
Anders Carlsson36760332007-10-15 20:28:48 +00001017 }
Chris Lattner69638b12007-08-30 15:51:11 +00001018 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +00001019 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001020 TypeResult Ty = ParseTypeName();
Chris Lattner7d5caf22009-03-24 17:21:43 +00001021 if (Ty.isInvalid()) {
1022 SkipUntil(tok::r_paren);
1023 return ExprError();
1024 }
1025
Chris Lattner4b009652007-07-25 00:24:17 +00001026 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001027 return ExprError();
1028
Chris Lattner4b009652007-07-25 00:24:17 +00001029 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001030 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001031 Diag(Tok, diag::err_expected_ident);
1032 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001033 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001034 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001035
Chris Lattner69638b12007-08-30 15:51:11 +00001036 // Keep track of the various subcomponents we see.
1037 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +00001038
Chris Lattner69638b12007-08-30 15:51:11 +00001039 Comps.push_back(Action::OffsetOfComponent());
1040 Comps.back().isBrackets = false;
1041 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1042 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001043
Sebastian Redl6008ac32008-11-25 22:21:31 +00001044 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +00001045 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001046 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001047 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +00001048 Comps.push_back(Action::OffsetOfComponent());
1049 Comps.back().isBrackets = false;
1050 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001051
Chris Lattner4d7d2342007-10-09 17:41:39 +00001052 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +00001053 Diag(Tok, diag::err_expected_ident);
1054 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001055 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +00001056 }
1057 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
1058 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +00001059
Chris Lattner4d7d2342007-10-09 17:41:39 +00001060 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001061 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +00001062 Comps.push_back(Action::OffsetOfComponent());
1063 Comps.back().isBrackets = true;
1064 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +00001065 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001066 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001067 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001068 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001069 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001070 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +00001071
Chris Lattner69638b12007-08-30 15:51:11 +00001072 Comps.back().LocEnd =
1073 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001074 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001075 if (Ty.isInvalid())
1076 Res = ExprError();
1077 else
1078 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1079 Ty.get(), &Comps[0],
1080 Comps.size(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001081 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001082 } else {
Chris Lattner69638b12007-08-30 15:51:11 +00001083 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +00001084 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001085 }
1086 }
1087 break;
Chris Lattner69638b12007-08-30 15:51:11 +00001088 }
Steve Naroff93c53012007-08-03 21:21:27 +00001089 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001090 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001091 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001092 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001093 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001094 }
Chris Lattner4b009652007-07-25 00:24:17 +00001095 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001096 return ExprError();
1097
Sebastian Redl14ca7412008-12-11 21:36:32 +00001098 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001099 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001100 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001101 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001102 }
Chris Lattner4b009652007-07-25 00:24:17 +00001103 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001104 return ExprError();
1105
Sebastian Redl14ca7412008-12-11 21:36:32 +00001106 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001107 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001108 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001109 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001110 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001111 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001112 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001113 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001114 }
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001115 Res = Actions.ActOnChooseExpr(StartLoc, move(Cond), move(Expr1),
1116 move(Expr2), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001117 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001118 }
Chris Lattner4b009652007-07-25 00:24:17 +00001119 case tok::kw___builtin_types_compatible_p:
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001120 TypeResult Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001121
Chris Lattner4b009652007-07-25 00:24:17 +00001122 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001123 return ExprError();
1124
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001125 TypeResult Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001126
Chris Lattner4d7d2342007-10-09 17:41:39 +00001127 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001128 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001129 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001130 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001131
1132 if (Ty1.isInvalid() || Ty2.isInvalid())
1133 Res = ExprError();
1134 else
1135 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1136 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001137 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001138 }
1139
Chris Lattner4b009652007-07-25 00:24:17 +00001140 // These can be followed by postfix-expr pieces because they are
1141 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001142 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001143}
1144
1145/// ParseParenExpression - This parses the unit that starts with a '(' token,
1146/// based on what is allowed by ExprType. The actual thing parsed is returned
1147/// in ExprType.
1148///
1149/// primary-expression: [C99 6.5.1]
1150/// '(' expression ')'
1151/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1152/// postfix-expression: [C99 6.5.2]
1153/// '(' type-name ')' '{' initializer-list '}'
1154/// '(' type-name ')' '{' initializer-list ',' '}'
1155/// cast-expression: [C99 6.5.4]
1156/// '(' type-name ')' cast-expression
1157///
Sebastian Redla6817a02008-12-11 22:33:27 +00001158Parser::OwningExprResult
1159Parser::ParseParenExpression(ParenParseOption &ExprType,
1160 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001161 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregoraf0d0092009-02-09 21:04:56 +00001162 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001163 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001164 OwningExprResult Result(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001165 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001166
Chris Lattner4d7d2342007-10-09 17:41:39 +00001167 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001169 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001170 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001171
Chris Lattner4b009652007-07-25 00:24:17 +00001172 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001173 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00001174 Result = Actions.ActOnStmtExpr(OpenLoc, move(Stmt), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001175
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001176 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001177 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001178 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +00001179
1180 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001181 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001182 RParenLoc = ConsumeParen();
1183 else
1184 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001185
Chris Lattner4d7d2342007-10-09 17:41:39 +00001186 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001187 if (!getLang().C99) // Compound literals don't exist in C90.
1188 Diag(OpenLoc, diag::ext_c99_compound_literal);
1189 Result = ParseInitializer();
1190 ExprType = CompoundLiteral;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001191 if (!Result.isInvalid() && !Ty.isInvalid())
1192 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001193 move(Result));
Chris Lattnercde12fd2008-12-12 06:00:12 +00001194 return move(Result);
1195 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001196
Chris Lattnercde12fd2008-12-12 06:00:12 +00001197 if (ExprType == CastExpr) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001198 // Note that this doesn't parse the subsequent cast-expression, it just
Chris Lattner4b009652007-07-25 00:24:17 +00001199 // returns the parsed type to the callee.
1200 ExprType = CastExpr;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001201
1202 if (Ty.isInvalid())
1203 return ExprError();
1204
1205 CastTy = Ty.get();
Sebastian Redla6817a02008-12-11 22:33:27 +00001206 return OwningExprResult(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001207 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001208
Chris Lattnercde12fd2008-12-12 06:00:12 +00001209 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1210 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001211 } else {
1212 Result = ParseExpression();
1213 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001214 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001215 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001216 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001217
Chris Lattner4b009652007-07-25 00:24:17 +00001218 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001219 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001220 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001221 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001222 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001223
1224 if (Tok.is(tok::r_paren))
1225 RParenLoc = ConsumeParen();
1226 else
1227 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001228
1229 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001230}
1231
1232/// ParseStringLiteralExpression - This handles the various token types that
1233/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1234/// translation phase #6].
1235///
1236/// primary-expression: [C99 6.5.1]
1237/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001238Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001239 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001240
Chris Lattner4b009652007-07-25 00:24:17 +00001241 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1242 // considered to be strings for concatenation purposes.
1243 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001244
Chris Lattner4b009652007-07-25 00:24:17 +00001245 do {
1246 StringToks.push_back(Tok);
1247 ConsumeStringToken();
1248 } while (isTokenStringLiteral());
1249
1250 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001251 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001252}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001253
1254/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1255///
1256/// argument-expression-list:
1257/// assignment-expression
1258/// argument-expression-list , assignment-expression
1259///
1260/// [C++] expression-list:
1261/// [C++] assignment-expression
1262/// [C++] expression-list , assignment-expression
1263///
1264bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1265 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001266 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001267 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001268 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001269
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001270 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001271
1272 if (Tok.isNot(tok::comma))
1273 return false;
1274 // Move to the next argument, remember where the comma was.
1275 CommaLocs.push_back(ConsumeToken());
1276 }
1277}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001278
Mike Stumpc1fddff2009-02-04 22:31:32 +00001279/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1280///
1281/// [clang] block-id:
1282/// [clang] specifier-qualifier-list block-declarator
1283///
1284void Parser::ParseBlockId() {
1285 // Parse the specifier-qualifier-list piece.
1286 DeclSpec DS;
1287 ParseSpecifierQualifierList(DS);
1288
1289 // Parse the block-declarator.
1290 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1291 ParseDeclarator(DeclaratorInfo);
1292 // Inform sema that we are starting a block.
1293 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1294}
1295
Steve Narofffd5b19d2008-08-28 19:20:44 +00001296/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001297/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001298///
1299/// block-literal:
1300/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001301/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001302/// [clang] block-args:
1303/// [clang] '(' parameter-list ')'
1304///
Sebastian Redla2deb432008-12-13 15:32:12 +00001305Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001306 assert(Tok.is(tok::caret) && "block literal starts with ^");
1307 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001308
Chris Lattnere533c7d2009-03-05 07:32:12 +00001309 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc,
1310 "block literal parsing");
1311
Steve Narofffd5b19d2008-08-28 19:20:44 +00001312 // Enter a scope to hold everything within the block. This includes the
1313 // argument decls, decls within the compound expression, etc. This also
1314 // allows determining whether a variable reference inside the block is
1315 // within or outside of the block.
Sebastian Redl0c986032009-02-09 18:23:29 +00001316 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1317 Scope::BreakScope | Scope::ContinueScope |
1318 Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001319
1320 // Inform sema that we are starting a block.
1321 Actions.ActOnBlockStart(CaretLoc, CurScope);
Chris Lattnere533c7d2009-03-05 07:32:12 +00001322
Steve Narofffd5b19d2008-08-28 19:20:44 +00001323 // Parse the return type if present.
1324 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001325 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redl0c986032009-02-09 18:23:29 +00001326 // FIXME: Since the return type isn't actually parsed, it can't be used to
1327 // fill ParamInfo with an initial valid range, so do it manually.
1328 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redla2deb432008-12-13 15:32:12 +00001329
Steve Narofffd5b19d2008-08-28 19:20:44 +00001330 // If this block has arguments, parse them. There is no ambiguity here with
1331 // the expression case, because the expression case requires a parameter list.
1332 if (Tok.is(tok::l_paren)) {
1333 ParseParenDeclarator(ParamInfo);
1334 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redl0c986032009-02-09 18:23:29 +00001335 // SetIdentifier sets the source range end, but in this case we're past
1336 // that location.
1337 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001338 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001339 ParamInfo.SetRangeEnd(Tmp);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001340 if (ParamInfo.getInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001341 // If there was an error parsing the arguments, they may have
1342 // tried to use ^(x+y) which requires an argument list. Just
1343 // skip the whole block literal.
Sebastian Redla2deb432008-12-13 15:32:12 +00001344 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001345 }
Mike Stumpc1fddff2009-02-04 22:31:32 +00001346 // Inform sema that we are starting a block.
1347 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Mike Stump1214b582009-04-14 18:24:37 +00001348 } else if (!Tok.is(tok::l_brace)) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001349 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001350 } else {
1351 // Otherwise, pretend we saw (void).
Douglas Gregor88a25f82009-02-18 07:07:28 +00001352 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1353 SourceLocation(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00001354 0, 0, 0, CaretLoc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001355 ParamInfo),
1356 CaretLoc);
Mike Stumpc1fddff2009-02-04 22:31:32 +00001357 // Inform sema that we are starting a block.
1358 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001359 }
1360
Sebastian Redla2deb432008-12-13 15:32:12 +00001361
Sebastian Redl62261042008-12-09 20:22:58 +00001362 OwningExprResult Result(Actions, true);
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001363 if (!Tok.is(tok::l_brace)) {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001364 // Saw something like: ^expr
1365 Diag(Tok, diag::err_expected_expression);
1366 return ExprError();
1367 }
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001368
1369 OwningStmtResult Stmt(ParseCompoundStatementBody());
1370 if (!Stmt.isInvalid())
1371 Result = Actions.ActOnBlockStmtExpr(CaretLoc, move(Stmt), CurScope);
1372 else
1373 Actions.ActOnBlockError(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001374 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001375}
1376