blob: d2e59ece354cf0e919cab833b2a50edcecc24858 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000026#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl22460502009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Reid Spencer5f016e22007-07-11 17:01:13 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor55f6b142009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
59 bool GreaterThanIsOperator) {
Reid Spencer5f016e22007-07-11 17:01:13 +000060 switch (Kind) {
Douglas Gregor55f6b142009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor55f6b142009-02-09 18:46:07 +000092 case tok::greaterequal: return prec::Relational;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl22460502009-02-07 00:15:38 +0000100 case tok::periodstar:
101 case tok::arrowstar: return prec::PointerToMember;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl22460502009-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///
Reid Spencer5f016e22007-07-11 17:01:13 +0000123/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl22460502009-02-07 00:15:38 +0000124/// Note: in C++, apply pm-expression instead of cast-expression
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner50dd2892008-02-26 00:51:44 +0000180/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000181///
182/// assignment-operator: one of
183/// = *= /= %= += -= <<= >>= &= ^= |=
184///
185/// expression: [C99 6.5.17]
186/// assignment-expression
187/// expression ',' assignment-expression
188///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000189Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000190 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000191 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000192
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000193 OwningExprResult LHS(ParseCastExpression(false));
194 if (LHS.isInvalid()) return move(LHS);
195
Sebastian Redld8c4e152008-12-11 22:33:27 +0000196 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Fariborz Jahanian397fcc12007-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 Lattnerc97c2042007-10-03 22:03:06 +0000201/// routine is necessary to disambiguate @try-statement from,
202/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000203///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000204Parser::OwningExprResult
205Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000206 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000207 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000208
Sebastian Redld8c4e152008-12-11 22:33:27 +0000209 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000210}
211
Eli Friedmanadf077f2009-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 Redl76ad2e82009-02-05 15:02:23 +0000225 move(LHS));
Eli Friedmanadf077f2009-01-27 08:43:38 +0000226 if (LHS.isInvalid()) return move(LHS);
227
228 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
229}
230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
232///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000233Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000234 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000235 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000236
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000237 OwningExprResult LHS(ParseCastExpression(false));
238 if (LHS.isInvalid()) return move(LHS);
239
Sebastian Redld8c4e152008-12-11 22:33:27 +0000240 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}
242
Chris Lattnerb93fb492008-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 Redl1d922962008-12-13 15:32:12 +0000251Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000252Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000253 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000254 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-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 Redl2f7ece72008-12-11 21:36:32 +0000260 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000261 if (R.isInvalid()) return move(R);
262 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000263}
264
265
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000266Parser::OwningExprResult Parser::ParseConstantExpression() {
267 OwningExprResult LHS(ParseCastExpression(false));
268 if (LHS.isInvalid()) return move(LHS);
269
Sebastian Redld8c4e152008-12-11 22:33:27 +0000270 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000271}
272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
274/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000275Parser::OwningExprResult
276Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor55f6b142009-02-09 18:46:07 +0000277 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl0e9eabc2008-12-09 13:15:23 +0000284 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000285 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286
287 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000288 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ConsumeToken();
Sebastian Redl22460502009-02-07 00:15:38 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000292 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000294 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl0e9eabc2008-12-09 13:15:23 +0000300 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000301 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 } else {
303 // Special case handling of "X ? Y : Z" where Y is empty:
304 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000305 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 Diag(Tok, diag::ext_gnu_conditional_expr);
307 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000308
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000309 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000311 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000312 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 // Eat the colon.
316 ColonLoc = ConsumeToken();
317 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000320 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000321 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000322 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor55f6b142009-02-09 18:46:07 +0000327 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
329 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000330 bool isRightAssoc = ThisPrec == prec::Conditional ||
331 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redla55e52c2008-11-25 22:21:31 +0000341 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000342 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000343 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000344 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000345
Douglas Gregor55f6b142009-02-09 18:46:07 +0000346 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
348 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000349
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000350 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000351 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000352 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000353 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000354 OpToken.getKind(), move(LHS), move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000355 else
Steve Narofff69936d2007-09-16 03:34:24 +0000356 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000357 move(LHS), move(TernaryMiddle),
358 move(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000359 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
361}
362
363/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor85bb3da2008-11-06 15:17:27 +0000381/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000382/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000383/// [C++] new-expression
384/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000385///
386/// unary-operator: one of
387/// '&' '*' '+' '-' '~' '!'
388/// [GNU] '__extension__' '__real' '__imag'
389///
390/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000391/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000392/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor2d8b2732008-11-29 04:51:27 +0000406/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000407/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000408/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000409/// [OBJC] '@protocol' '(' identifier ')'
410/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000411/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-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]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlc42e1182008-11-11 11:37:55 +0000418/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
419/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000420/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000421/// [G++] unary-type-trait '(' type-id ')'
422/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000423/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000424///
425/// constant: [C99 6.4.4]
426/// integer-constant
427/// floating-constant
428/// enumeration-constant -> identifier
429/// character-constant
430///
Douglas Gregor1cd1b1e2008-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]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000441///
Sebastian Redl4c5d3202008-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 Redl64b45f72009-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 Redlebc07d52009-02-03 20:19:35 +0000472Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
473 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000474 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl2f7ece72008-12-11 21:36:32 +0000497 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl0e9eabc2008-12-09 13:15:23 +0000511 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000512 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000513 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000517 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // primary-expression
521 case tok::numeric_constant:
522 // constant: integer-constant
523 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000524
Steve Narofff69936d2007-09-16 03:34:24 +0000525 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000529 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000530
531 case tok::kw_true:
532 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000533 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000534
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000535 case tok::identifier: { // primary-expression: identifier
536 // unqualified-id: identifier
537 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000538 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000539 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000540 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000541 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
542 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000543 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000544 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4e1d99a2007-10-09 17:41:39 +0000552 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000554 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
556 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000557 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 ConsumeToken();
559 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000560 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd9f69102008-08-10 01:53:14 +0000564 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 ConsumeToken();
566 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000567 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 case tok::string_literal: // primary-expression: string-literal
569 case tok::wide_string_literal:
570 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000571 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000573 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 case tok::kw___builtin_va_arg:
575 case tok::kw___builtin_offsetof:
576 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000577 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000579 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000580 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000581 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000582 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 case tok::plusplus: // unary-expression: '++' unary-expression
584 case tok::minusminus: { // unary-expression: '--' unary-expression
585 SourceLocation SavedLoc = ConsumeToken();
586 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000587 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000588 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000589 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000591 case tok::amp: { // unary-expression: '&' cast-expression
592 // Special treatment because of member pointers
593 SourceLocation SavedLoc = ConsumeToken();
594 Res = ParseCastExpression(false, true);
595 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000596 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redlebc07d52009-02-03 20:19:35 +0000597 return move(Res);
598 }
599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 case tok::star: // unary-expression: '*' cast-expression
601 case tok::plus: // unary-expression: '+' cast-expression
602 case tok::minus: // unary-expression: '-' cast-expression
603 case tok::tilde: // unary-expression: '~' cast-expression
604 case tok::exclaim: // unary-expression: '!' cast-expression
605 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000606 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 SourceLocation SavedLoc = ConsumeToken();
608 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000609 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000610 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000611 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000612 }
613
Chris Lattner35080842008-02-02 20:20:10 +0000614 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
615 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000616 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000617 SourceLocation SavedLoc = ConsumeToken();
618 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000619 if (!Res.isInvalid())
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000620 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000621 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 }
623 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
624 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000625 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
627 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000628 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000629 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 case tok::ampamp: { // unary-expression: '&&' identifier
631 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000632 if (Tok.isNot(tok::identifier))
633 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000636 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 Tok.getIdentifierInfo());
638 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000639 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 }
641 case tok::kw_const_cast:
642 case tok::kw_dynamic_cast:
643 case tok::kw_reinterpret_cast:
644 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000645 Res = ParseCXXCasts();
646 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000647 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000648 case tok::kw_typeid:
649 Res = ParseCXXTypeid();
650 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000651 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000652 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000653 Res = ParseCXXThis();
654 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000655 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000656
657 case tok::kw_char:
658 case tok::kw_wchar_t:
659 case tok::kw_bool:
660 case tok::kw_short:
661 case tok::kw_int:
662 case tok::kw_long:
663 case tok::kw_signed:
664 case tok::kw_unsigned:
665 case tok::kw_float:
666 case tok::kw_double:
667 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000668 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000669 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000670 if (!getLang().CPlusPlus) {
671 Diag(Tok, diag::err_expected_expression);
672 return ExprError();
673 }
674
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000675 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
676 //
677 DeclSpec DS;
678 ParseCXXSimpleTypeSpecifier(DS);
679 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000680 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
681 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000682
683 Res = ParseCXXTypeConstructExpression(DS);
684 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000685 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000686 }
687
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000688 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
689 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
690 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000691 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000692 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000693
Chris Lattner74ba4102009-01-04 22:52:14 +0000694 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000695 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
696 // annotates the token, tail recurse.
697 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000698 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
699
Chris Lattner74ba4102009-01-04 22:52:14 +0000700 // ::new -> [C++] new-expression
701 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000702 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000703 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000704 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000705 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000706 return ParseCXXDeleteExpression(true, CCLoc);
707
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000708 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000709 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000710 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000711 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000712
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000713 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000714 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000715
716 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000717 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000718
Sebastian Redl64b45f72009-01-05 20:52:13 +0000719 case tok::kw___is_pod: // [GNU] unary-type-trait
720 case tok::kw___is_class:
721 case tok::kw___is_enum:
722 case tok::kw___is_union:
723 case tok::kw___is_polymorphic:
724 return ParseUnaryTypeTrait();
725
Chris Lattnerc97c2042007-10-03 22:03:06 +0000726 case tok::at: {
727 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000728 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000729 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000730 case tok::caret:
731 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000732 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000733 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000734 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000735 case tok::l_square:
736 // These can be followed by postfix-expr pieces.
737 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000738 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000739 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 default:
741 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000742 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 // unreachable.
746 abort();
747}
748
749/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
750/// is parsed, this method parses any suffixes that apply.
751///
752/// postfix-expression: [C99 6.5.2]
753/// primary-expression
754/// postfix-expression '[' expression ']'
755/// postfix-expression '(' argument-expression-list[opt] ')'
756/// postfix-expression '.' identifier
757/// postfix-expression '->' identifier
758/// postfix-expression '++'
759/// postfix-expression '--'
760/// '(' type-name ')' '{' initializer-list '}'
761/// '(' type-name ')' '{' initializer-list ',' '}'
762///
763/// argument-expression-list: [C99 6.5.2]
764/// argument-expression
765/// argument-expression-list ',' assignment-expression
766///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000767Parser::OwningExprResult
768Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Now that the primary-expression piece of the postfix-expression has been
770 // parsed, see if there are any postfix-expression pieces here.
771 SourceLocation Loc;
772 while (1) {
773 switch (Tok.getKind()) {
774 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000775 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
777 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000778 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000779
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000781
782 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000783 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
784 move(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000785 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000786 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000787
788 // Match the ']'.
789 MatchRHSPunctuation(tok::r_square, Loc);
790 break;
791 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000794 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000795 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000796
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000798
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000799 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000800 if (ParseExpressionList(ArgExprs, CommaLocs)) {
801 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000802 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 }
804 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000807 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
809 "Unexpected number of commas!");
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000810 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000811 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000812 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000814
Chris Lattner2ff54262007-07-21 05:18:12 +0000815 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 break;
817 }
818 case tok::arrow: // postfix-expression: p-e '->' identifier
819 case tok::period: { // postfix-expression: p-e '.' identifier
820 tok::TokenKind OpKind = Tok.getKind();
821 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000822
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000823 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000825 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000827
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000828 if (!LHS.isInvalid()) {
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000829 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000830 OpKind, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 *Tok.getIdentifierInfo());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000832 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 ConsumeToken();
834 break;
835 }
836 case tok::plusplus: // postfix-expression: postfix-expression '++'
837 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000838 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000839 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +0000840 Tok.getKind(), move(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000841 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 ConsumeToken();
843 break;
844 }
845 }
846}
847
848
849/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
850/// unary-expression: [C99 6.5.3]
851/// 'sizeof' unary-expression
852/// 'sizeof' '(' type-name ')'
853/// [GNU] '__alignof' unary-expression
854/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000855/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000856Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000857 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
858 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000860 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 ConsumeToken();
862
863 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000864 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000865 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 Operand = ParseCastExpression(true);
867 } else {
868 // If it starts with a '(', we know that it is either a parenthesized
869 // type-name, or it is a unary-expression that starts with a compound
870 // literal, or starts with a primary-expression that is a parenthesized
871 // expression.
872 ParenParseOption ExprType = CastExpr;
873 TypeTy *CastTy;
874 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
875 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
878 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000879 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000880 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000881 OpTok.is(tok::kw_sizeof),
882 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000883 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000884
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000885 // If this is a parenthesized expression, it is the start of a
886 // unary-expression, but doesn't include any postfix pieces. Parse these
887 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000888 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000890
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000892 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000893 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
894 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000895 /*isType=*/false,
896 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000897 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898}
899
900/// ParseBuiltinPrimaryExpression
901///
902/// primary-expression: [C99 6.5.1]
903/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
904/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
905/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
906/// assign-expr ')'
907/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000908/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000909///
910/// [GNU] offsetof-member-designator:
911/// [GNU] identifier
912/// [GNU] offsetof-member-designator '.' identifier
913/// [GNU] offsetof-member-designator '[' expression ']'
914///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000915Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000916 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
918
919 tok::TokenKind T = Tok.getKind();
920 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
921
922 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000923 if (Tok.isNot(tok::l_paren))
924 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
925 << BuiltinII);
926
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 SourceLocation LParenLoc = ConsumeParen();
928 // TODO: Build AST.
929
930 switch (T) {
931 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000932 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000933 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000934 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000936 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 }
938
939 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000940 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000942 TypeTy *Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000943
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000944 if (Tok.isNot(tok::r_paren)) {
945 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000946 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000947 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000948 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000950 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000951 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000952 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000953 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000954
955 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000956 return ExprError();
957
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000959 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000960 Diag(Tok, diag::err_expected_ident);
961 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000962 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000963 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000964
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000965 // Keep track of the various subcomponents we see.
966 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +0000967
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000968 Comps.push_back(Action::OffsetOfComponent());
969 Comps.back().isBrackets = false;
970 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
971 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000972
Sebastian Redla55e52c2008-11-25 22:21:31 +0000973 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000975 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000977 Comps.push_back(Action::OffsetOfComponent());
978 Comps.back().isBrackets = false;
979 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000980
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000981 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000982 Diag(Tok, diag::err_expected_ident);
983 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000984 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000985 }
986 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
987 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000988
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000989 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000991 Comps.push_back(Action::OffsetOfComponent());
992 Comps.back().isBrackets = true;
993 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000995 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000997 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000999 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +00001000
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001001 Comps.back().LocEnd =
1002 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001003 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001004 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, Ty,
1005 &Comps[0], Comps.size(),
1006 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001007 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001009 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001010 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 }
1012 }
1013 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +00001014 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001015 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001016 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001017 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001018 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001019 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001020 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001022 return ExprError();
1023
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001024 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001025 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001026 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001027 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001028 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001030 return ExprError();
1031
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001032 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001033 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001034 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001035 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001036 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001037 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001038 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001039 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001040 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001041 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1042 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001043 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001044 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001045 case tok::kw___builtin_overload: {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001046 ExprVector ArgExprs(Actions);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001047 llvm::SmallVector<SourceLocation, 8> CommaLocs;
1048
1049 // For each iteration through the loop look for assign-expr followed by a
1050 // comma. If there is no comma, break and attempt to match r-paren.
1051 if (Tok.isNot(tok::r_paren)) {
1052 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001053 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001054 if (ArgExpr.isInvalid()) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00001055 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001057 } else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001058 ArgExprs.push_back(ArgExpr.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001059
Nate Begemane2ce1d92008-01-17 17:46:27 +00001060 if (Tok.isNot(tok::comma))
1061 break;
1062 // Move to the next argument, remember where the comma was.
1063 CommaLocs.push_back(ConsumeToken());
1064 }
1065 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001066
Nate Begemane2ce1d92008-01-17 17:46:27 +00001067 // Attempt to consume the r-paren
1068 if (Tok.isNot(tok::r_paren)) {
1069 Diag(Tok, diag::err_expected_rparen);
1070 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001071 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001072 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001073 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begemane2ce1d92008-01-17 17:46:27 +00001074 &CommaLocs[0], StartLoc, ConsumeParen());
1075 break;
1076 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +00001078 TypeTy *Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001079
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001081 return ExprError();
1082
Steve Naroff363bcff2007-08-01 23:45:51 +00001083 TypeTy *Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001084
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001085 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001086 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001087 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001088 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001089 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001090 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001091 }
1092
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 // These can be followed by postfix-expr pieces because they are
1094 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001095 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001096}
1097
1098/// ParseParenExpression - This parses the unit that starts with a '(' token,
1099/// based on what is allowed by ExprType. The actual thing parsed is returned
1100/// in ExprType.
1101///
1102/// primary-expression: [C99 6.5.1]
1103/// '(' expression ')'
1104/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1105/// postfix-expression: [C99 6.5.2]
1106/// '(' type-name ')' '{' initializer-list '}'
1107/// '(' type-name ')' '{' initializer-list ',' '}'
1108/// cast-expression: [C99 6.5.4]
1109/// '(' type-name ')' cast-expression
1110///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001111Parser::OwningExprResult
1112Parser::ParseParenExpression(ParenParseOption &ExprType,
1113 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001114 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregorf02da892009-02-09 21:04:56 +00001115 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001117 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001119
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001120 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001122 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001124
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001125 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001126 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1127 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001128 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001129
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001130 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // Otherwise, this is a compound literal expression or cast expression.
1132 TypeTy *Ty = ParseTypeName();
1133
1134 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001135 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 RParenLoc = ConsumeParen();
1137 else
1138 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001139
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001140 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 if (!getLang().C99) // Compound literals don't exist in C90.
1142 Diag(OpenLoc, diag::ext_c99_compound_literal);
1143 Result = ParseInitializer();
1144 ExprType = CompoundLiteral;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001145 if (!Result.isInvalid())
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001146 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001147 move(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001148 return move(Result);
1149 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001150
Chris Lattner42ece642008-12-12 06:00:12 +00001151 if (ExprType == CastExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 // Note that this doesn't parse the subsequence cast-expression, it just
1153 // returns the parsed type to the callee.
1154 ExprType = CastExpr;
1155 CastTy = Ty;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001156 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001158
Chris Lattner42ece642008-12-12 06:00:12 +00001159 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1160 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 } else {
1162 Result = ParseExpression();
1163 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001164 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001165 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001169 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001171 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 }
Chris Lattner42ece642008-12-12 06:00:12 +00001173
1174 if (Tok.is(tok::r_paren))
1175 RParenLoc = ConsumeParen();
1176 else
1177 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001178
1179 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001180}
1181
1182/// ParseStringLiteralExpression - This handles the various token types that
1183/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1184/// translation phase #6].
1185///
1186/// primary-expression: [C99 6.5.1]
1187/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001188Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1192 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001193 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001194
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 do {
1196 StringToks.push_back(Tok);
1197 ConsumeStringToken();
1198 } while (isTokenStringLiteral());
1199
1200 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001201 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001202}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001203
1204/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1205///
1206/// argument-expression-list:
1207/// assignment-expression
1208/// argument-expression-list , assignment-expression
1209///
1210/// [C++] expression-list:
1211/// [C++] assignment-expression
1212/// [C++] expression-list , assignment-expression
1213///
1214bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1215 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001216 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001217 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001218 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001219
Sebastian Redleffa8d12008-12-10 00:02:53 +00001220 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001221
1222 if (Tok.isNot(tok::comma))
1223 return false;
1224 // Move to the next argument, remember where the comma was.
1225 CommaLocs.push_back(ConsumeToken());
1226 }
1227}
Steve Naroff296e8d52008-08-28 19:20:44 +00001228
Mike Stump98eb8a72009-02-04 22:31:32 +00001229/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1230///
1231/// [clang] block-id:
1232/// [clang] specifier-qualifier-list block-declarator
1233///
1234void Parser::ParseBlockId() {
1235 // Parse the specifier-qualifier-list piece.
1236 DeclSpec DS;
1237 ParseSpecifierQualifierList(DS);
1238
1239 // Parse the block-declarator.
1240 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1241 ParseDeclarator(DeclaratorInfo);
1242 // Inform sema that we are starting a block.
1243 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1244}
1245
Steve Naroff296e8d52008-08-28 19:20:44 +00001246/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001247/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001248///
1249/// block-literal:
1250/// [clang] '^' block-args[opt] compound-statement
Mike Stump98eb8a72009-02-04 22:31:32 +00001251/// [clang] '^' block-id compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001252/// [clang] block-args:
1253/// [clang] '(' parameter-list ')'
1254///
Sebastian Redl1d922962008-12-13 15:32:12 +00001255Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001256 assert(Tok.is(tok::caret) && "block literal starts with ^");
1257 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001258
Steve Naroff296e8d52008-08-28 19:20:44 +00001259 // Enter a scope to hold everything within the block. This includes the
1260 // argument decls, decls within the compound expression, etc. This also
1261 // allows determining whether a variable reference inside the block is
1262 // within or outside of the block.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001263 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1264 Scope::BreakScope | Scope::ContinueScope |
1265 Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001266
1267 // Inform sema that we are starting a block.
1268 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001269
Steve Naroff296e8d52008-08-28 19:20:44 +00001270 // Parse the return type if present.
1271 DeclSpec DS;
Mike Stump98eb8a72009-02-04 22:31:32 +00001272 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001273 // FIXME: Since the return type isn't actually parsed, it can't be used to
1274 // fill ParamInfo with an initial valid range, so do it manually.
1275 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redl1d922962008-12-13 15:32:12 +00001276
Steve Naroff296e8d52008-08-28 19:20:44 +00001277 // If this block has arguments, parse them. There is no ambiguity here with
1278 // the expression case, because the expression case requires a parameter list.
1279 if (Tok.is(tok::l_paren)) {
1280 ParseParenDeclarator(ParamInfo);
1281 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redlab197ba2009-02-09 18:23:29 +00001282 // SetIdentifier sets the source range end, but in this case we're past
1283 // that location.
1284 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Naroff296e8d52008-08-28 19:20:44 +00001285 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001286 ParamInfo.SetRangeEnd(Tmp);
Steve Naroff296e8d52008-08-28 19:20:44 +00001287 if (ParamInfo.getInvalidType()) {
Mike Stump98eb8a72009-02-04 22:31:32 +00001288 // If there was an error parsing the arguments, they may have
1289 // tried to use ^(x+y) which requires an argument list. Just
1290 // skip the whole block literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001291 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001292 }
Mike Stump98eb8a72009-02-04 22:31:32 +00001293 // Inform sema that we are starting a block.
1294 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1295 } else if (! Tok.is(tok::l_brace)) {
1296 ParseBlockId();
Steve Naroff296e8d52008-08-28 19:20:44 +00001297 } else {
1298 // Otherwise, pretend we saw (void).
Douglas Gregor965acbb2009-02-18 07:07:28 +00001299 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1300 SourceLocation(),
Chris Lattner5af2f352009-01-20 19:11:22 +00001301 0, 0, 0, CaretLoc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001302 ParamInfo),
1303 CaretLoc);
Mike Stump98eb8a72009-02-04 22:31:32 +00001304 // Inform sema that we are starting a block.
1305 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001306 }
1307
Sebastian Redl1d922962008-12-13 15:32:12 +00001308
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001309 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001310 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001311 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001312 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001313 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001314 } else {
1315 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001316 }
Mike Stump281481d2009-02-02 23:46:21 +00001317 } else {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001318 // Saw something like: ^expr
1319 Diag(Tok, diag::err_expected_expression);
1320 return ExprError();
1321 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001322 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001323}
1324