blob: b6c2a71e089d004d672a85984838205a25a0a5db [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 Lattnerdaa5c002008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000026#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl95216a62009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Chris Lattner4b009652007-07-25 00:24:17 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor8e458f42009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
59 bool GreaterThanIsOperator) {
Chris Lattner4b009652007-07-25 00:24:17 +000060 switch (Kind) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000061 case tok::greater:
62 // The '>' token can act as either an operator or as the ending
63 // token for a template argument list.
64 // FIXME: '>>' is similar, for error recovery and C++0x.
65 if (GreaterThanIsOperator)
66 return prec::Relational;
67 return prec::Unknown;
68
Chris Lattner4b009652007-07-25 00:24:17 +000069 default: return prec::Unknown;
70 case tok::comma: return prec::Comma;
71 case tok::equal:
72 case tok::starequal:
73 case tok::slashequal:
74 case tok::percentequal:
75 case tok::plusequal:
76 case tok::minusequal:
77 case tok::lesslessequal:
78 case tok::greatergreaterequal:
79 case tok::ampequal:
80 case tok::caretequal:
81 case tok::pipeequal: return prec::Assignment;
82 case tok::question: return prec::Conditional;
83 case tok::pipepipe: return prec::LogicalOr;
84 case tok::ampamp: return prec::LogicalAnd;
85 case tok::pipe: return prec::InclusiveOr;
86 case tok::caret: return prec::ExclusiveOr;
87 case tok::amp: return prec::And;
88 case tok::exclaimequal:
89 case tok::equalequal: return prec::Equality;
90 case tok::lessequal:
91 case tok::less:
Douglas Gregor8e458f42009-02-09 18:46:07 +000092 case tok::greaterequal: return prec::Relational;
Chris Lattner4b009652007-07-25 00:24:17 +000093 case tok::lessless:
94 case tok::greatergreater: return prec::Shift;
95 case tok::plus:
96 case tok::minus: return prec::Additive;
97 case tok::percent:
98 case tok::slash:
99 case tok::star: return prec::Multiplicative;
Sebastian Redl95216a62009-02-07 00:15:38 +0000100 case tok::periodstar:
101 case tok::arrowstar: return prec::PointerToMember;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 }
103}
104
105
106/// ParseExpression - Simple precedence-based parser for binary/ternary
107/// operators.
108///
109/// Note: we diverge from the C99 grammar when parsing the assignment-expression
110/// production. C99 specifies that the LHS of an assignment operator should be
111/// parsed as a unary-expression, but consistency dictates that it be a
112/// conditional-expession. In practice, the important thing here is that the
113/// LHS of an assignment has to be an l-value, which productions between
114/// unary-expression and conditional-expression don't produce. Because we want
115/// consistency, we parse the LHS as a conditional-expression, then check for
116/// l-value-ness in semantic analysis stages.
117///
Sebastian Redl95216a62009-02-07 00:15:38 +0000118/// pm-expression: [C++ 5.5]
119/// cast-expression
120/// pm-expression '.*' cast-expression
121/// pm-expression '->*' cast-expression
122///
Chris Lattner4b009652007-07-25 00:24:17 +0000123/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl95216a62009-02-07 00:15:38 +0000124/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000125/// cast-expression
126/// multiplicative-expression '*' cast-expression
127/// multiplicative-expression '/' cast-expression
128/// multiplicative-expression '%' cast-expression
129///
130/// additive-expression: [C99 6.5.6]
131/// multiplicative-expression
132/// additive-expression '+' multiplicative-expression
133/// additive-expression '-' multiplicative-expression
134///
135/// shift-expression: [C99 6.5.7]
136/// additive-expression
137/// shift-expression '<<' additive-expression
138/// shift-expression '>>' additive-expression
139///
140/// relational-expression: [C99 6.5.8]
141/// shift-expression
142/// relational-expression '<' shift-expression
143/// relational-expression '>' shift-expression
144/// relational-expression '<=' shift-expression
145/// relational-expression '>=' shift-expression
146///
147/// equality-expression: [C99 6.5.9]
148/// relational-expression
149/// equality-expression '==' relational-expression
150/// equality-expression '!=' relational-expression
151///
152/// AND-expression: [C99 6.5.10]
153/// equality-expression
154/// AND-expression '&' equality-expression
155///
156/// exclusive-OR-expression: [C99 6.5.11]
157/// AND-expression
158/// exclusive-OR-expression '^' AND-expression
159///
160/// inclusive-OR-expression: [C99 6.5.12]
161/// exclusive-OR-expression
162/// inclusive-OR-expression '|' exclusive-OR-expression
163///
164/// logical-AND-expression: [C99 6.5.13]
165/// inclusive-OR-expression
166/// logical-AND-expression '&&' inclusive-OR-expression
167///
168/// logical-OR-expression: [C99 6.5.14]
169/// logical-AND-expression
170/// logical-OR-expression '||' logical-AND-expression
171///
172/// conditional-expression: [C99 6.5.15]
173/// logical-OR-expression
174/// logical-OR-expression '?' expression ':' conditional-expression
175/// [GNU] logical-OR-expression '?' ':' conditional-expression
176///
177/// assignment-expression: [C99 6.5.16]
178/// conditional-expression
179/// unary-expression assignment-operator assignment-expression
Chris Lattnera7447ba2008-02-26 00:51:44 +0000180/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000181///
182/// assignment-operator: one of
183/// = *= /= %= += -= <<= >>= &= ^= |=
184///
185/// expression: [C99 6.5.17]
186/// assignment-expression
187/// expression ',' assignment-expression
188///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000189Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000190 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000191 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000192
Sebastian Redl14ca7412008-12-11 21:36:32 +0000193 OwningExprResult LHS(ParseCastExpression(false));
194 if (LHS.isInvalid()) return move(LHS);
195
Sebastian Redla6817a02008-12-11 22:33:27 +0000196 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000197}
198
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000199/// This routine is called when the '@' is seen and consumed.
200/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000201/// routine is necessary to disambiguate @try-statement from,
202/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000203///
Sebastian Redla6817a02008-12-11 22:33:27 +0000204Parser::OwningExprResult
205Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redla2deb432008-12-13 15:32:12 +0000206 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000207 if (LHS.isInvalid()) return move(LHS);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000208
Sebastian Redla6817a02008-12-11 22:33:27 +0000209 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000210}
211
Eli Friedmanc4772072009-01-27 08:43:38 +0000212/// This routine is called when a leading '__extension__' is seen and
213/// consumed. This is necessary because the token gets consumed in the
214/// process of disambiguating between an expression and a declaration.
215Parser::OwningExprResult
216Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
217 // FIXME: The handling for throw is almost certainly wrong.
218 if (Tok.is(tok::kw_throw))
219 return ParseThrowExpression();
220
221 OwningExprResult LHS(ParseCastExpression(false));
222 if (LHS.isInvalid()) return move(LHS);
223
224 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl81db6682009-02-05 15:02:23 +0000225 move(LHS));
Eli Friedmanc4772072009-01-27 08:43:38 +0000226 if (LHS.isInvalid()) return move(LHS);
227
228 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
229}
230
Chris Lattner4b009652007-07-25 00:24:17 +0000231/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
232///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000233Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000234 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000235 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000236
Sebastian Redl14ca7412008-12-11 21:36:32 +0000237 OwningExprResult LHS(ParseCastExpression(false));
238 if (LHS.isInvalid()) return move(LHS);
239
Sebastian Redla6817a02008-12-11 22:33:27 +0000240 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000241}
242
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000243/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
244/// where part of an objc message send has already been parsed. In this case
245/// LBracLoc indicates the location of the '[' of the message send, and either
246/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
247/// message.
248///
249/// Since this handles full assignment-expression's, it handles postfix
250/// expressions and other binary operators for these expressions as well.
Sebastian Redla2deb432008-12-13 15:32:12 +0000251Parser::OwningExprResult
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000252Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000253 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000254 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-12-13 15:32:12 +0000255 ExprArg ReceiverExpr) {
256 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
257 ReceiverName,
258 move(ReceiverExpr)));
259 if (R.isInvalid()) return move(R);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000260 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redla2deb432008-12-13 15:32:12 +0000261 if (R.isInvalid()) return move(R);
262 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000263}
264
265
Sebastian Redl14ca7412008-12-11 21:36:32 +0000266Parser::OwningExprResult Parser::ParseConstantExpression() {
267 OwningExprResult LHS(ParseCastExpression(false));
268 if (LHS.isInvalid()) return move(LHS);
269
Sebastian Redla6817a02008-12-11 22:33:27 +0000270 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000271}
272
Chris Lattner4b009652007-07-25 00:24:17 +0000273/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
274/// LHS and has a precedence of at least MinPrec.
Sebastian Redla6817a02008-12-11 22:33:27 +0000275Parser::OwningExprResult
276Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000277 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 SourceLocation ColonLoc;
279
280 while (1) {
281 // If this token has a lower precedence than we are allowed to parse (e.g.
282 // because we are called recursively, or because the token is not a binop),
283 // then we are done!
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000284 if (NextTokPrec < MinPrec)
Sebastian Redla6817a02008-12-11 22:33:27 +0000285 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000286
287 // Consume the operator, saving the operator token for error reporting.
288 Token OpToken = Tok;
289 ConsumeToken();
Sebastian Redl95216a62009-02-07 00:15:38 +0000290
Chris Lattner4b009652007-07-25 00:24:17 +0000291 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000292 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000293 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000294 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000295 // Handle this production specially:
296 // logical-OR-expression '?' expression ':' conditional-expression
297 // In particular, the RHS of the '?' is 'expression', not
298 // 'logical-OR-expression' as we might expect.
299 TernaryMiddle = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000300 if (TernaryMiddle.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000301 return move(TernaryMiddle);
Chris Lattner4b009652007-07-25 00:24:17 +0000302 } else {
303 // Special case handling of "X ? Y : Z" where Y is empty:
304 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000305 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 Diag(Tok, diag::ext_gnu_conditional_expr);
307 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000308
Chris Lattner4d7d2342007-10-09 17:41:39 +0000309 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000310 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000311 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redla6817a02008-12-11 22:33:27 +0000312 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000313 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000314
Chris Lattner4b009652007-07-25 00:24:17 +0000315 // Eat the colon.
316 ColonLoc = ConsumeToken();
317 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 // Parse another leaf here for the RHS of the operator.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000320 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000321 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000322 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000323
324 // Remember the precedence of this operator and get the precedence of the
325 // operator immediately to the right of the RHS.
326 unsigned ThisPrec = NextTokPrec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000327 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000328
329 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000330 bool isRightAssoc = ThisPrec == prec::Conditional ||
331 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +0000332
333 // Get the precedence of the operator to the right of the RHS. If it binds
334 // more tightly with RHS than we do, evaluate it completely first.
335 if (ThisPrec < NextTokPrec ||
336 (ThisPrec == NextTokPrec && isRightAssoc)) {
337 // If this is left-associative, only parse things on the RHS that bind
338 // more tightly than the current operator. If it is left-associative, it
339 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
340 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000341 // The function takes ownership of the RHS.
Sebastian Redla6817a02008-12-11 22:33:27 +0000342 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000343 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000344 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000345
Douglas Gregor8e458f42009-02-09 18:46:07 +0000346 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000347 }
348 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000349
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000350 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000351 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000352 if (TernaryMiddle.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000353 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000354 OpToken.getKind(), move(LHS), move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000355 else
Steve Naroff87d58b42007-09-16 03:34:24 +0000356 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000357 move(LHS), move(TernaryMiddle),
358 move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000359 }
Chris Lattner4b009652007-07-25 00:24:17 +0000360 }
361}
362
363/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl0c9da212009-02-03 20:19:35 +0000364/// true, parse a unary-expression. isAddressOfOperand exists because an
365/// id-expression that is the operand of address-of gets special treatment
366/// due to member pointers.
Chris Lattner4b009652007-07-25 00:24:17 +0000367///
368/// cast-expression: [C99 6.5.4]
369/// unary-expression
370/// '(' type-name ')' cast-expression
371///
372/// unary-expression: [C99 6.5.3]
373/// postfix-expression
374/// '++' unary-expression
375/// '--' unary-expression
376/// unary-operator cast-expression
377/// 'sizeof' unary-expression
378/// 'sizeof' '(' type-name ')'
379/// [GNU] '__alignof' unary-expression
380/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000381/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000382/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000383/// [C++] new-expression
384/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000385///
386/// unary-operator: one of
387/// '&' '*' '+' '-' '~' '!'
388/// [GNU] '__extension__' '__real' '__imag'
389///
390/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000391/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000392/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000393/// constant
394/// string-literal
395/// [C++] boolean-literal [C++ 2.13.5]
396/// '(' expression ')'
397/// '__func__' [C99 6.4.2.2]
398/// [GNU] '__FUNCTION__'
399/// [GNU] '__PRETTY_FUNCTION__'
400/// [GNU] '(' compound-statement ')'
401/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
402/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
403/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
404/// assign-expr ')'
405/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregorad4b3792008-11-29 04:51:27 +0000406/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000407/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000408/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000409/// [OBJC] '@protocol' '(' identifier ')'
410/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000411/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000412/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
413/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000414/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
415/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
416/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
417/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000418/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
419/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000420/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000421/// [G++] unary-type-trait '(' type-id ')'
422/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000423/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000424///
425/// constant: [C99 6.4.4]
426/// integer-constant
427/// floating-constant
428/// enumeration-constant -> identifier
429/// character-constant
430///
Douglas Gregore60e5d32008-11-06 22:13:31 +0000431/// id-expression: [C++ 5.1]
432/// unqualified-id
433/// qualified-id [TODO]
434///
435/// unqualified-id: [C++ 5.1]
436/// identifier
437/// operator-function-id
438/// conversion-function-id [TODO]
439/// '~' class-name [TODO]
440/// template-id [TODO]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000441///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000442/// new-expression: [C++ 5.3.4]
443/// '::'[opt] 'new' new-placement[opt] new-type-id
444/// new-initializer[opt]
445/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
446/// new-initializer[opt]
447///
448/// delete-expression: [C++ 5.3.5]
449/// '::'[opt] 'delete' cast-expression
450/// '::'[opt] 'delete' '[' ']' cast-expression
451///
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000452/// [GNU] unary-type-trait:
453/// '__has_nothrow_assign' [TODO]
454/// '__has_nothrow_copy' [TODO]
455/// '__has_nothrow_constructor' [TODO]
456/// '__has_trivial_assign' [TODO]
457/// '__has_trivial_copy' [TODO]
458/// '__has_trivial_constructor' [TODO]
459/// '__has_trivial_destructor' [TODO]
460/// '__has_virtual_destructor' [TODO]
461/// '__is_abstract' [TODO]
462/// '__is_class'
463/// '__is_empty' [TODO]
464/// '__is_enum'
465/// '__is_pod'
466/// '__is_polymorphic'
467/// '__is_union'
468///
469/// [GNU] binary-type-trait:
470/// '__is_base_of' [TODO]
471///
Sebastian Redl0c9da212009-02-03 20:19:35 +0000472Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
473 bool isAddressOfOperand) {
Sebastian Redl62261042008-12-09 20:22:58 +0000474 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000475 tok::TokenKind SavedKind = Tok.getKind();
476
477 // This handles all of cast-expression, unary-expression, postfix-expression,
478 // and primary-expression. We handle them together like this for efficiency
479 // and to simplify handling of an expression starting with a '(' token: which
480 // may be one of a parenthesized expression, cast-expression, compound literal
481 // expression, or statement expression.
482 //
483 // If the parsed tokens consist of a primary-expression, the cases below
484 // call ParsePostfixExpressionSuffix to handle the postfix expression
485 // suffixes. Cases that cannot be followed by postfix exprs should
486 // return without invoking ParsePostfixExpressionSuffix.
487 switch (SavedKind) {
488 case tok::l_paren: {
489 // If this expression is limited to being a unary-expression, the parent can
490 // not start a cast expression.
491 ParenParseOption ParenExprType =
492 isUnaryExpression ? CompoundLiteral : CastExpr;
493 TypeTy *CastTy;
494 SourceLocation LParenLoc = Tok.getLocation();
495 SourceLocation RParenLoc;
496 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000497 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000498
499 switch (ParenExprType) {
500 case SimpleExpr: break; // Nothing else to do.
501 case CompoundStmt: break; // Nothing else to do.
502 case CompoundLiteral:
503 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
504 // postfix-expression exist, parse them now.
505 break;
506 case CastExpr:
507 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
508 // the cast-expression that follows it next.
509 // TODO: For cast expression with CastTy.
510 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000511 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000512 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000513 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000514 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000515
Chris Lattner4b009652007-07-25 00:24:17 +0000516 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000517 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000519
Chris Lattner4b009652007-07-25 00:24:17 +0000520 // primary-expression
521 case tok::numeric_constant:
522 // constant: integer-constant
523 // constant: floating-constant
Sebastian Redl14ca7412008-12-11 21:36:32 +0000524
Steve Naroff87d58b42007-09-16 03:34:24 +0000525 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000526 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000527
Chris Lattner4b009652007-07-25 00:24:17 +0000528 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000529 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000530
531 case tok::kw_true:
532 case tok::kw_false:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000533 return ParseCXXBoolLiteral();
Chris Lattner4b009652007-07-25 00:24:17 +0000534
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535 case tok::identifier: { // primary-expression: identifier
536 // unqualified-id: identifier
537 // constant: enumeration-constant
Chris Lattner5d7eace2009-01-06 05:06:21 +0000538 // Turn a potentially qualified name into a annot_typename or
Chris Lattner68751c42009-01-04 22:52:14 +0000539 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner1e015942009-01-04 23:23:14 +0000540 if (getLang().CPlusPlus) {
Chris Lattner914660b2009-01-04 23:46:59 +0000541 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
542 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000543 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner1e015942009-01-04 23:23:14 +0000544 }
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000545
Chris Lattner4b009652007-07-25 00:24:17 +0000546 // Consume the identifier so that we can see if it is followed by a '('.
547 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
548 // need to know whether or not this identifier is a function designator or
549 // not.
550 IdentifierInfo &II = *Tok.getIdentifierInfo();
551 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000552 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
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 }
556 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000557 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000558 ConsumeToken();
559 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000560 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000561 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
562 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
563 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner69909292008-08-10 01:53:14 +0000564 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000565 ConsumeToken();
566 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000567 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000568 case tok::string_literal: // primary-expression: string-literal
569 case tok::wide_string_literal:
570 Res = ParseStringLiteralExpression();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000571 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000572 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl14ca7412008-12-11 21:36:32 +0000573 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000574 case tok::kw___builtin_va_arg:
575 case tok::kw___builtin_offsetof:
576 case tok::kw___builtin_choose_expr:
577 case tok::kw___builtin_types_compatible_p:
Sebastian Redla6817a02008-12-11 22:33:27 +0000578 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000579 case tok::kw___null:
Sebastian Redl14ca7412008-12-11 21:36:32 +0000580 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregorad4b3792008-11-29 04:51:27 +0000581 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000582 case tok::plusplus: // unary-expression: '++' unary-expression
583 case tok::minusminus: { // unary-expression: '--' unary-expression
584 SourceLocation SavedLoc = ConsumeToken();
585 Res = ParseCastExpression(true);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000586 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000587 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000588 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000589 }
Sebastian Redl0c9da212009-02-03 20:19:35 +0000590 case tok::amp: { // unary-expression: '&' cast-expression
591 // Special treatment because of member pointers
592 SourceLocation SavedLoc = ConsumeToken();
593 Res = ParseCastExpression(false, true);
594 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000595 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl0c9da212009-02-03 20:19:35 +0000596 return move(Res);
597 }
598
Chris Lattner4b009652007-07-25 00:24:17 +0000599 case tok::star: // unary-expression: '*' cast-expression
600 case tok::plus: // unary-expression: '+' cast-expression
601 case tok::minus: // unary-expression: '-' cast-expression
602 case tok::tilde: // unary-expression: '~' cast-expression
603 case tok::exclaim: // unary-expression: '!' cast-expression
604 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000605 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000606 SourceLocation SavedLoc = ConsumeToken();
607 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000608 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000609 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000610 return move(Res);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000611 }
612
Chris Lattner6cf92942008-02-02 20:20:10 +0000613 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
614 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000615 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000616 SourceLocation SavedLoc = ConsumeToken();
617 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000618 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000619 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000620 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000621 }
622 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
623 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000624 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000625 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
626 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000627 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000628 return ParseSizeofAlignofExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000629 case tok::ampamp: { // unary-expression: '&&' identifier
630 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000631 if (Tok.isNot(tok::identifier))
632 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000633
Chris Lattner4b009652007-07-25 00:24:17 +0000634 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000635 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000636 Tok.getIdentifierInfo());
637 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000638 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000639 }
640 case tok::kw_const_cast:
641 case tok::kw_dynamic_cast:
642 case tok::kw_reinterpret_cast:
643 case tok::kw_static_cast:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000644 Res = ParseCXXCasts();
645 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000646 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000647 case tok::kw_typeid:
648 Res = ParseCXXTypeid();
649 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000650 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000651 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000652 Res = ParseCXXThis();
653 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000654 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000655
656 case tok::kw_char:
657 case tok::kw_wchar_t:
658 case tok::kw_bool:
659 case tok::kw_short:
660 case tok::kw_int:
661 case tok::kw_long:
662 case tok::kw_signed:
663 case tok::kw_unsigned:
664 case tok::kw_float:
665 case tok::kw_double:
666 case tok::kw_void:
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000667 case tok::kw_typeof:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000668 case tok::annot_typename: {
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000669 if (!getLang().CPlusPlus) {
670 Diag(Tok, diag::err_expected_expression);
671 return ExprError();
672 }
673
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000674 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
675 //
676 DeclSpec DS;
677 ParseCXXSimpleTypeSpecifier(DS);
678 if (Tok.isNot(tok::l_paren))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000679 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
680 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000681
682 Res = ParseCXXTypeConstructExpression(DS);
683 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000684 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000685 }
686
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000687 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
688 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
689 // template-id
Sebastian Redl0c9da212009-02-03 20:19:35 +0000690 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000691 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000692
Chris Lattner68751c42009-01-04 22:52:14 +0000693 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000694 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
695 // annotates the token, tail recurse.
696 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000697 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
698
Chris Lattner68751c42009-01-04 22:52:14 +0000699 // ::new -> [C++] new-expression
700 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000701 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000702 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000703 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000704 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000705 return ParseCXXDeleteExpression(true, CCLoc);
706
Chris Lattner1e015942009-01-04 23:23:14 +0000707 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000708 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000709 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000710 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000711
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000712 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000713 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000714
715 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000716 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000717
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000718 case tok::kw___is_pod: // [GNU] unary-type-trait
719 case tok::kw___is_class:
720 case tok::kw___is_enum:
721 case tok::kw___is_union:
722 case tok::kw___is_polymorphic:
723 return ParseUnaryTypeTrait();
724
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000725 case tok::at: {
726 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000727 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000728 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000729 case tok::caret:
730 if (getLang().Blocks)
Sebastian Redla2deb432008-12-13 15:32:12 +0000731 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Narofffd5b19d2008-08-28 19:20:44 +0000732 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000733 return ExprError();
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000734 case tok::l_square:
735 // These can be followed by postfix-expr pieces.
736 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000737 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000738 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000739 default:
740 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000741 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000742 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000743
Chris Lattner4b009652007-07-25 00:24:17 +0000744 // unreachable.
745 abort();
746}
747
748/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
749/// is parsed, this method parses any suffixes that apply.
750///
751/// postfix-expression: [C99 6.5.2]
752/// primary-expression
753/// postfix-expression '[' expression ']'
754/// postfix-expression '(' argument-expression-list[opt] ')'
755/// postfix-expression '.' identifier
756/// postfix-expression '->' identifier
757/// postfix-expression '++'
758/// postfix-expression '--'
759/// '(' type-name ')' '{' initializer-list '}'
760/// '(' type-name ')' '{' initializer-list ',' '}'
761///
762/// argument-expression-list: [C99 6.5.2]
763/// argument-expression
764/// argument-expression-list ',' assignment-expression
765///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000766Parser::OwningExprResult
767Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000768 // Now that the primary-expression piece of the postfix-expression has been
769 // parsed, see if there are any postfix-expression pieces here.
770 SourceLocation Loc;
771 while (1) {
772 switch (Tok.getKind()) {
773 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000774 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000775 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
776 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000777 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000778
Chris Lattner4b009652007-07-25 00:24:17 +0000779 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000780
781 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000782 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
783 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000784 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000785 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000786
787 // Match the ']'.
788 MatchRHSPunctuation(tok::r_square, Loc);
789 break;
790 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000791
Chris Lattner4b009652007-07-25 00:24:17 +0000792 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000793 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000794 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000795
Chris Lattner4b009652007-07-25 00:24:17 +0000796 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000797
Chris Lattner4d7d2342007-10-09 17:41:39 +0000798 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000799 if (ParseExpressionList(ArgExprs, CommaLocs)) {
800 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000801 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
803 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000804
Chris Lattner4b009652007-07-25 00:24:17 +0000805 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000806 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000807 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
808 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000809 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl8b769972009-01-19 00:08:26 +0000810 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redl6008ac32008-11-25 22:21:31 +0000811 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000812 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000813
Chris Lattner4b009652007-07-25 00:24:17 +0000814 MatchRHSPunctuation(tok::r_paren, Loc);
815 break;
816 }
817 case tok::arrow: // postfix-expression: p-e '->' identifier
818 case tok::period: { // postfix-expression: p-e '.' identifier
819 tok::TokenKind OpKind = Tok.getKind();
820 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000821
Chris Lattner4d7d2342007-10-09 17:41:39 +0000822 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000823 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000824 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000825 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000826
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000827 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000828 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000829 OpKind, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000830 *Tok.getIdentifierInfo());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000831 }
Chris Lattner4b009652007-07-25 00:24:17 +0000832 ConsumeToken();
833 break;
834 }
835 case tok::plusplus: // postfix-expression: postfix-expression '++'
836 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000837 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000838 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000839 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000840 }
Chris Lattner4b009652007-07-25 00:24:17 +0000841 ConsumeToken();
842 break;
843 }
844 }
845}
846
847
848/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
849/// unary-expression: [C99 6.5.3]
850/// 'sizeof' unary-expression
851/// 'sizeof' '(' type-name ')'
852/// [GNU] '__alignof' unary-expression
853/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000854/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000855Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +0000856 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
857 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000858 "Not a sizeof/alignof expression!");
859 Token OpTok = Tok;
860 ConsumeToken();
861
862 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl62261042008-12-09 20:22:58 +0000863 OwningExprResult Operand(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000864 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000865 Operand = ParseCastExpression(true);
866 } else {
867 // If it starts with a '(', we know that it is either a parenthesized
868 // type-name, or it is a unary-expression that starts with a compound
869 // literal, or starts with a primary-expression that is a parenthesized
870 // expression.
871 ParenParseOption ExprType = CastExpr;
872 TypeTy *CastTy;
873 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
874 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +0000875
Chris Lattner4b009652007-07-25 00:24:17 +0000876 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
877 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000878 if (ExprType == CastExpr)
Sebastian Redl8b769972009-01-19 00:08:26 +0000879 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000880 OpTok.is(tok::kw_sizeof),
881 /*isType=*/true, CastTy,
Sebastian Redl8b769972009-01-19 00:08:26 +0000882 SourceRange(LParenLoc, RParenLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000883
Chris Lattner48553562007-11-13 20:50:37 +0000884 // If this is a parenthesized expression, it is the start of a
885 // unary-expression, but doesn't include any postfix pieces. Parse these
886 // now if present.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000887 Operand = ParsePostfixExpressionSuffix(move(Operand));
Chris Lattner4b009652007-07-25 00:24:17 +0000888 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000889
Chris Lattner4b009652007-07-25 00:24:17 +0000890 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000891 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000892 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
893 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000894 /*isType=*/false,
895 Operand.release(), SourceRange());
Sebastian Redla6817a02008-12-11 22:33:27 +0000896 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +0000897}
898
899/// ParseBuiltinPrimaryExpression
900///
901/// primary-expression: [C99 6.5.1]
902/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
903/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
904/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
905/// assign-expr ')'
906/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
907///
908/// [GNU] offsetof-member-designator:
909/// [GNU] identifier
910/// [GNU] offsetof-member-designator '.' identifier
911/// [GNU] offsetof-member-designator '[' expression ']'
912///
Sebastian Redla6817a02008-12-11 22:33:27 +0000913Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000914 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000915 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
916
917 tok::TokenKind T = Tok.getKind();
918 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
919
920 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +0000921 if (Tok.isNot(tok::l_paren))
922 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
923 << BuiltinII);
924
Chris Lattner4b009652007-07-25 00:24:17 +0000925 SourceLocation LParenLoc = ConsumeParen();
926 // TODO: Build AST.
927
928 switch (T) {
929 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000930 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000931 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000932 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000933 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000934 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000935 }
936
937 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000938 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000939
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000940 TypeResult Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000941
Anders Carlsson36760332007-10-15 20:28:48 +0000942 if (Tok.isNot(tok::r_paren)) {
943 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +0000944 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +0000945 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000946 if (Ty.isInvalid())
947 Res = ExprError();
948 else
949 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty.get(),
950 ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +0000951 break;
Anders Carlsson36760332007-10-15 20:28:48 +0000952 }
Chris Lattner69638b12007-08-30 15:51:11 +0000953 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +0000954 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000955 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000956
957 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000958 return ExprError();
959
Chris Lattner4b009652007-07-25 00:24:17 +0000960 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000961 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000962 Diag(Tok, diag::err_expected_ident);
963 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000964 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000965 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000966
Chris Lattner69638b12007-08-30 15:51:11 +0000967 // Keep track of the various subcomponents we see.
968 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +0000969
Chris Lattner69638b12007-08-30 15:51:11 +0000970 Comps.push_back(Action::OffsetOfComponent());
971 Comps.back().isBrackets = false;
972 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
973 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000974
Sebastian Redl6008ac32008-11-25 22:21:31 +0000975 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +0000976 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000977 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000978 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +0000979 Comps.push_back(Action::OffsetOfComponent());
980 Comps.back().isBrackets = false;
981 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000982
Chris Lattner4d7d2342007-10-09 17:41:39 +0000983 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000984 Diag(Tok, diag::err_expected_ident);
985 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000986 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000987 }
988 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
989 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000990
Chris Lattner4d7d2342007-10-09 17:41:39 +0000991 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000992 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +0000993 Comps.push_back(Action::OffsetOfComponent());
994 Comps.back().isBrackets = true;
995 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +0000996 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000997 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000998 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000999 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001000 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001001 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +00001002
Chris Lattner69638b12007-08-30 15:51:11 +00001003 Comps.back().LocEnd =
1004 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001005 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001006 if (Ty.isInvalid())
1007 Res = ExprError();
1008 else
1009 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1010 Ty.get(), &Comps[0],
1011 Comps.size(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001012 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001013 } else {
Chris Lattner69638b12007-08-30 15:51:11 +00001014 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +00001015 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001016 }
1017 }
1018 break;
Chris Lattner69638b12007-08-30 15:51:11 +00001019 }
Steve Naroff93c53012007-08-03 21:21:27 +00001020 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001021 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001022 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001023 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001024 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001025 }
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
Sebastian Redl14ca7412008-12-11 21:36:32 +00001029 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001030 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001031 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001032 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001033 }
Chris Lattner4b009652007-07-25 00:24:17 +00001034 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001035 return ExprError();
1036
Sebastian Redl14ca7412008-12-11 21:36:32 +00001037 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001038 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001039 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001040 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001041 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001042 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001043 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001044 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001045 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001046 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1047 Expr2.release(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001048 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001049 }
Chris Lattner4b009652007-07-25 00:24:17 +00001050 case tok::kw___builtin_types_compatible_p:
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001051 TypeResult Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001052
Chris Lattner4b009652007-07-25 00:24:17 +00001053 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001054 return ExprError();
1055
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001056 TypeResult Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001057
Chris Lattner4d7d2342007-10-09 17:41:39 +00001058 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001059 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001060 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001061 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001062
1063 if (Ty1.isInvalid() || Ty2.isInvalid())
1064 Res = ExprError();
1065 else
1066 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1067 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001068 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001069 }
1070
Chris Lattner4b009652007-07-25 00:24:17 +00001071 // These can be followed by postfix-expr pieces because they are
1072 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001073 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001074}
1075
1076/// ParseParenExpression - This parses the unit that starts with a '(' token,
1077/// based on what is allowed by ExprType. The actual thing parsed is returned
1078/// in ExprType.
1079///
1080/// primary-expression: [C99 6.5.1]
1081/// '(' expression ')'
1082/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1083/// postfix-expression: [C99 6.5.2]
1084/// '(' type-name ')' '{' initializer-list '}'
1085/// '(' type-name ')' '{' initializer-list ',' '}'
1086/// cast-expression: [C99 6.5.4]
1087/// '(' type-name ')' cast-expression
1088///
Sebastian Redla6817a02008-12-11 22:33:27 +00001089Parser::OwningExprResult
1090Parser::ParseParenExpression(ParenParseOption &ExprType,
1091 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001092 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregoraf0d0092009-02-09 21:04:56 +00001093 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001094 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001095 OwningExprResult Result(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001096 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001097
Chris Lattner4d7d2342007-10-09 17:41:39 +00001098 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001099 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001100 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001101 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001102
Chris Lattner4b009652007-07-25 00:24:17 +00001103 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001104 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1105 Result = Actions.ActOnStmtExpr(
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001106 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001107
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001108 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001109 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001110 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +00001111
1112 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001113 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001114 RParenLoc = ConsumeParen();
1115 else
1116 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001117
Chris Lattner4d7d2342007-10-09 17:41:39 +00001118 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001119 if (!getLang().C99) // Compound literals don't exist in C90.
1120 Diag(OpenLoc, diag::ext_c99_compound_literal);
1121 Result = ParseInitializer();
1122 ExprType = CompoundLiteral;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001123 if (!Result.isInvalid() && !Ty.isInvalid())
1124 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001125 move(Result));
Chris Lattnercde12fd2008-12-12 06:00:12 +00001126 return move(Result);
1127 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001128
Chris Lattnercde12fd2008-12-12 06:00:12 +00001129 if (ExprType == CastExpr) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001130 // Note that this doesn't parse the subsequent cast-expression, it just
Chris Lattner4b009652007-07-25 00:24:17 +00001131 // returns the parsed type to the callee.
1132 ExprType = CastExpr;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001133
1134 if (Ty.isInvalid())
1135 return ExprError();
1136
1137 CastTy = Ty.get();
Sebastian Redla6817a02008-12-11 22:33:27 +00001138 return OwningExprResult(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001139 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001140
Chris Lattnercde12fd2008-12-12 06:00:12 +00001141 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1142 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001143 } else {
1144 Result = ParseExpression();
1145 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001146 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001147 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001148 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001149
Chris Lattner4b009652007-07-25 00:24:17 +00001150 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001151 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001152 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001153 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001154 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001155
1156 if (Tok.is(tok::r_paren))
1157 RParenLoc = ConsumeParen();
1158 else
1159 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001160
1161 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001162}
1163
1164/// ParseStringLiteralExpression - This handles the various token types that
1165/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1166/// translation phase #6].
1167///
1168/// primary-expression: [C99 6.5.1]
1169/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001170Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001171 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001172
Chris Lattner4b009652007-07-25 00:24:17 +00001173 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1174 // considered to be strings for concatenation purposes.
1175 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001176
Chris Lattner4b009652007-07-25 00:24:17 +00001177 do {
1178 StringToks.push_back(Tok);
1179 ConsumeStringToken();
1180 } while (isTokenStringLiteral());
1181
1182 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001183 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001184}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001185
1186/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1187///
1188/// argument-expression-list:
1189/// assignment-expression
1190/// argument-expression-list , assignment-expression
1191///
1192/// [C++] expression-list:
1193/// [C++] assignment-expression
1194/// [C++] expression-list , assignment-expression
1195///
1196bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1197 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001198 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001199 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001200 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001201
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001202 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001203
1204 if (Tok.isNot(tok::comma))
1205 return false;
1206 // Move to the next argument, remember where the comma was.
1207 CommaLocs.push_back(ConsumeToken());
1208 }
1209}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001210
Mike Stumpc1fddff2009-02-04 22:31:32 +00001211/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1212///
1213/// [clang] block-id:
1214/// [clang] specifier-qualifier-list block-declarator
1215///
1216void Parser::ParseBlockId() {
1217 // Parse the specifier-qualifier-list piece.
1218 DeclSpec DS;
1219 ParseSpecifierQualifierList(DS);
1220
1221 // Parse the block-declarator.
1222 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1223 ParseDeclarator(DeclaratorInfo);
1224 // Inform sema that we are starting a block.
1225 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1226}
1227
Steve Narofffd5b19d2008-08-28 19:20:44 +00001228/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001229/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001230///
1231/// block-literal:
1232/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001233/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001234/// [clang] block-args:
1235/// [clang] '(' parameter-list ')'
1236///
Sebastian Redla2deb432008-12-13 15:32:12 +00001237Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001238 assert(Tok.is(tok::caret) && "block literal starts with ^");
1239 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001240
Steve Narofffd5b19d2008-08-28 19:20:44 +00001241 // Enter a scope to hold everything within the block. This includes the
1242 // argument decls, decls within the compound expression, etc. This also
1243 // allows determining whether a variable reference inside the block is
1244 // within or outside of the block.
Sebastian Redl0c986032009-02-09 18:23:29 +00001245 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1246 Scope::BreakScope | Scope::ContinueScope |
1247 Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001248
1249 // Inform sema that we are starting a block.
1250 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001251
Steve Narofffd5b19d2008-08-28 19:20:44 +00001252 // Parse the return type if present.
1253 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001254 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redl0c986032009-02-09 18:23:29 +00001255 // FIXME: Since the return type isn't actually parsed, it can't be used to
1256 // fill ParamInfo with an initial valid range, so do it manually.
1257 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redla2deb432008-12-13 15:32:12 +00001258
Steve Narofffd5b19d2008-08-28 19:20:44 +00001259 // If this block has arguments, parse them. There is no ambiguity here with
1260 // the expression case, because the expression case requires a parameter list.
1261 if (Tok.is(tok::l_paren)) {
1262 ParseParenDeclarator(ParamInfo);
1263 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redl0c986032009-02-09 18:23:29 +00001264 // SetIdentifier sets the source range end, but in this case we're past
1265 // that location.
1266 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001267 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001268 ParamInfo.SetRangeEnd(Tmp);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001269 if (ParamInfo.getInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001270 // If there was an error parsing the arguments, they may have
1271 // tried to use ^(x+y) which requires an argument list. Just
1272 // skip the whole block literal.
Sebastian Redla2deb432008-12-13 15:32:12 +00001273 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001274 }
Mike Stumpc1fddff2009-02-04 22:31:32 +00001275 // Inform sema that we are starting a block.
1276 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1277 } else if (! Tok.is(tok::l_brace)) {
1278 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001279 } else {
1280 // Otherwise, pretend we saw (void).
Douglas Gregor88a25f82009-02-18 07:07:28 +00001281 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1282 SourceLocation(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00001283 0, 0, 0, CaretLoc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001284 ParamInfo),
1285 CaretLoc);
Mike Stumpc1fddff2009-02-04 22:31:32 +00001286 // Inform sema that we are starting a block.
1287 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001288 }
1289
Sebastian Redla2deb432008-12-13 15:32:12 +00001290
Sebastian Redl62261042008-12-09 20:22:58 +00001291 OwningExprResult Result(Actions, true);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001292 if (Tok.is(tok::l_brace)) {
Sebastian Redl10c32952008-12-11 19:30:53 +00001293 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001294 if (!Stmt.isInvalid()) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001295 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001296 } else {
1297 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001298 }
Mike Stump677b3f42009-02-02 23:46:21 +00001299 } else {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001300 // Saw something like: ^expr
1301 Diag(Tok, diag::err_expected_expression);
1302 return ExprError();
1303 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001304 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001305}
1306