blob: dca12e6c051976e97c550a9cfca1dd34f5e641c1 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
Chris Lattnerc951dae2006-08-10 04:23:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerc951dae2006-08-10 04:23:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnercde626a2006-08-12 08:13:25 +000010// 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
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff0ac012832008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redl511ed552008-11-25 22:21:31 +000026#include "AstGuard.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner834618d2006-11-03 07:48:41 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000029using namespace clang;
30
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000031/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000032/// 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 {
36 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, // &
Chris Lattner9916c5c2006-10-27 05:24:37 +000045 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13 // *, /, %
Chris Lattnercde626a2006-08-12 08:13:25 +000050 };
51}
52
53
54/// getBinOpPrecedence - Return the precedence of the specified binary operator
55/// token. This returns:
56///
57static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
58 switch (Kind) {
59 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
Chris Lattnercde626a2006-08-12 08:13:25 +000078 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
82 case tok::greaterequal:
83 case tok::greater: return prec::Relational;
84 case tok::lessless:
85 case tok::greatergreater: return prec::Shift;
86 case tok::plus:
87 case tok::minus: return prec::Additive;
88 case tok::percent:
89 case tok::slash:
90 case tok::star: return prec::Multiplicative;
91 }
92}
93
94
Chris Lattnerce7e21d2006-08-12 17:22:40 +000095/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000096/// operators.
97///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
Chris Lattnercde626a2006-08-12 08:13:25 +0000107/// multiplicative-expression: [C99 6.5.5]
108/// cast-expression
109/// multiplicative-expression '*' cast-expression
110/// multiplicative-expression '/' cast-expression
111/// multiplicative-expression '%' cast-expression
112///
113/// additive-expression: [C99 6.5.6]
114/// multiplicative-expression
115/// additive-expression '+' multiplicative-expression
116/// additive-expression '-' multiplicative-expression
117///
118/// shift-expression: [C99 6.5.7]
119/// additive-expression
120/// shift-expression '<<' additive-expression
121/// shift-expression '>>' additive-expression
122///
123/// relational-expression: [C99 6.5.8]
124/// shift-expression
125/// relational-expression '<' shift-expression
126/// relational-expression '>' shift-expression
127/// relational-expression '<=' shift-expression
128/// relational-expression '>=' shift-expression
129///
130/// equality-expression: [C99 6.5.9]
131/// relational-expression
132/// equality-expression '==' relational-expression
133/// equality-expression '!=' relational-expression
134///
135/// AND-expression: [C99 6.5.10]
136/// equality-expression
137/// AND-expression '&' equality-expression
138///
139/// exclusive-OR-expression: [C99 6.5.11]
140/// AND-expression
141/// exclusive-OR-expression '^' AND-expression
142///
143/// inclusive-OR-expression: [C99 6.5.12]
144/// exclusive-OR-expression
145/// inclusive-OR-expression '|' exclusive-OR-expression
146///
147/// logical-AND-expression: [C99 6.5.13]
148/// inclusive-OR-expression
149/// logical-AND-expression '&&' inclusive-OR-expression
150///
151/// logical-OR-expression: [C99 6.5.14]
152/// logical-AND-expression
153/// logical-OR-expression '||' logical-AND-expression
154///
155/// conditional-expression: [C99 6.5.15]
156/// logical-OR-expression
157/// logical-OR-expression '?' expression ':' conditional-expression
158/// [GNU] logical-OR-expression '?' ':' conditional-expression
159///
160/// assignment-expression: [C99 6.5.16]
161/// conditional-expression
162/// unary-expression assignment-operator assignment-expression
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000163/// [C++] throw-expression [C++ 15]
Chris Lattnercde626a2006-08-12 08:13:25 +0000164///
165/// assignment-operator: one of
166/// = *= /= %= += -= <<= >>= &= ^= |=
167///
168/// expression: [C99 6.5.17]
169/// assignment-expression
170/// expression ',' assignment-expression
171///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000172Parser::ExprResult Parser::ParseExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000173 if (Tok.is(tok::kw_throw))
174 return ParseThrowExpression();
175
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000176 ExprOwner LHS(Actions, ParseCastExpression(false));
177 if (LHS.isInvalid()) return LHS.move();
Chris Lattnercde626a2006-08-12 08:13:25 +0000178
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000179 return ParseRHSOfBinaryExpression(LHS.move(), prec::Comma);
Chris Lattnercde626a2006-08-12 08:13:25 +0000180}
181
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000182/// This routine is called when the '@' is seen and consumed.
183/// Current token is an Identifier and is not a 'try'. This
Chris Lattner644e1b72007-10-03 22:03:06 +0000184/// routine is necessary to disambiguate @try-statement from,
185/// for example, @encode-expression.
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000186///
Fariborz Jahanian90814572007-10-04 20:19:06 +0000187Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000188 ExprOwner LHS(Actions, ParseObjCAtExpression(AtLoc));
189 if (LHS.isInvalid()) return LHS.move();
190
191 return ParseRHSOfBinaryExpression(LHS.move(), prec::Comma);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000192}
193
Chris Lattner0c6c0342006-08-12 18:12:45 +0000194/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
195///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000196Parser::ExprResult Parser::ParseAssignmentExpression() {
Chris Lattnerb7e656b2008-02-26 00:51:44 +0000197 if (Tok.is(tok::kw_throw))
198 return ParseThrowExpression();
199
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000200 ExprOwner LHS(Actions, ParseCastExpression(false));
201 if (LHS.isInvalid()) return LHS.move();
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000202
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000203 return ParseRHSOfBinaryExpression(LHS.move(), prec::Assignment);
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000204}
205
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000206/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
207/// where part of an objc message send has already been parsed. In this case
208/// LBracLoc indicates the location of the '[' of the message send, and either
209/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
210/// message.
211///
212/// Since this handles full assignment-expression's, it handles postfix
213/// expressions and other binary operators for these expressions as well.
214Parser::ExprResult
215Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +0000216 SourceLocation NameLoc,
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000217 IdentifierInfo *ReceiverName,
218 ExprTy *ReceiverExpr) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000219 ExprOwner R(Actions, ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
220 ReceiverName,
221 ReceiverExpr));
222 if (R.isInvalid()) return R.move();
223 R = ParsePostfixExpressionSuffix(R.move());
224 if (R.isInvalid()) return R.move();
225 return ParseRHSOfBinaryExpression(R.move(), 2);
Chris Lattnerfd2fe822008-06-02 21:31:07 +0000226}
227
228
Chris Lattner3b561a32006-08-13 00:12:11 +0000229Parser::ExprResult Parser::ParseConstantExpression() {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000230 ExprOwner LHS(Actions, ParseCastExpression(false));
231 if (LHS.isInvalid()) return LHS.move();
Chris Lattner3b561a32006-08-13 00:12:11 +0000232
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000233 return ParseRHSOfBinaryExpression(LHS.move(), prec::Conditional);
Chris Lattner3b561a32006-08-13 00:12:11 +0000234}
235
Chris Lattnercde626a2006-08-12 08:13:25 +0000236/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
237/// LHS and has a precedence of at least MinPrec.
238Parser::ExprResult
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000239Parser::ParseRHSOfBinaryExpression(ExprResult LHSArg, unsigned MinPrec) {
Chris Lattnercde626a2006-08-12 08:13:25 +0000240 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000241 SourceLocation ColonLoc;
242
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000243 ExprOwner LHS(Actions, LHSArg);
Chris Lattnercde626a2006-08-12 08:13:25 +0000244 while (1) {
245 // If this token has a lower precedence than we are allowed to parse (e.g.
246 // because we are called recursively, or because the token is not a binop),
247 // then we are done!
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000248 if (NextTokPrec < MinPrec)
249 return LHS.move();
Chris Lattnercde626a2006-08-12 08:13:25 +0000250
251 // Consume the operator, saving the operator token for error reporting.
Chris Lattner146762e2007-07-20 16:59:19 +0000252 Token OpToken = Tok;
Chris Lattnercde626a2006-08-12 08:13:25 +0000253 ConsumeToken();
254
Chris Lattner96c3deb2006-08-12 17:13:08 +0000255 // Special case handling for the ternary operator.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000256 ExprOwner TernaryMiddle(Actions, true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000257 if (NextTokPrec == prec::Conditional) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000258 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000259 // Handle this production specially:
260 // logical-OR-expression '?' expression ':' conditional-expression
261 // In particular, the RHS of the '?' is 'expression', not
262 // 'logical-OR-expression' as we might expect.
263 TernaryMiddle = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000264 if (TernaryMiddle.isInvalid())
265 return TernaryMiddle.move();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000266 } else {
267 // Special case handling of "X ? Y : Z" where Y is empty:
268 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000269 TernaryMiddle.reset();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000270 Diag(Tok, diag::ext_gnu_conditional_expr);
271 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000272
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000273 if (Tok.isNot(tok::colon)) {
Chris Lattner96c3deb2006-08-12 17:13:08 +0000274 Diag(Tok, diag::err_expected_colon);
Chris Lattner03c40412008-11-23 23:17:07 +0000275 Diag(OpToken, diag::note_matching) << "?";
Chris Lattner96c3deb2006-08-12 17:13:08 +0000276 return ExprResult(true);
277 }
278
279 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000280 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000281 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000282
283 // Parse another leaf here for the RHS of the operator.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000284 ExprOwner RHS(Actions, ParseCastExpression(false));
285 if (RHS.isInvalid())
286 return RHS.move();
Chris Lattnercde626a2006-08-12 08:13:25 +0000287
288 // Remember the precedence of this operator and get the precedence of the
289 // operator immediately to the right of the RHS.
290 unsigned ThisPrec = NextTokPrec;
291 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000292
293 // Assignment and conditional expressions are right-associative.
Chris Lattnerdcb7cc52007-12-18 06:06:23 +0000294 bool isRightAssoc = ThisPrec == prec::Conditional ||
295 ThisPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000296
297 // Get the precedence of the operator to the right of the RHS. If it binds
298 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000299 if (ThisPrec < NextTokPrec ||
300 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000301 // If this is left-associative, only parse things on the RHS that bind
302 // more tightly than the current operator. If it is left-associative, it
303 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
304 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl511ed552008-11-25 22:21:31 +0000305 // The function takes ownership of the RHS.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000306 RHS = ParseRHSOfBinaryExpression(RHS.move(), ThisPrec + !isRightAssoc);
307 if (RHS.isInvalid())
308 return RHS.move();
Chris Lattnercde626a2006-08-12 08:13:25 +0000309
310 NextTokPrec = getBinOpPrecedence(Tok.getKind());
311 }
312 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl511ed552008-11-25 22:21:31 +0000313
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000314 if (!LHS.isInvalid()) {
Chris Lattner319079c2007-08-31 05:01:50 +0000315 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Douglas Gregor7d5fc7e2008-11-06 23:29:22 +0000317 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000318 OpToken.getKind(), LHS.move(), RHS.move());
Chris Lattner319079c2007-08-31 05:01:50 +0000319 else
Steve Naroff83895f72007-09-16 03:34:24 +0000320 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000321 LHS.move(), TernaryMiddle.move(),
322 RHS.move());
Chris Lattner319079c2007-08-31 05:01:50 +0000323 }
Chris Lattnercde626a2006-08-12 08:13:25 +0000324 }
325}
326
Chris Lattnereaf06592006-08-11 02:02:23 +0000327/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
328/// true, parse a unary-expression.
329///
Chris Lattner4564bc12006-08-10 23:14:52 +0000330/// cast-expression: [C99 6.5.4]
331/// unary-expression
332/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000333///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000334/// unary-expression: [C99 6.5.3]
335/// postfix-expression
336/// '++' unary-expression
337/// '--' unary-expression
338/// unary-operator cast-expression
339/// 'sizeof' unary-expression
340/// 'sizeof' '(' type-name ')'
341/// [GNU] '__alignof' unary-expression
342/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000343/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000344/// [GNU] '&&' identifier
Sebastian Redlbd150f42008-11-21 19:14:01 +0000345/// [C++] new-expression
346/// [C++] delete-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000347///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000348/// unary-operator: one of
349/// '&' '*' '+' '-' '~' '!'
350/// [GNU] '__extension__' '__real' '__imag'
351///
Chris Lattner52a99e52006-08-10 20:56:00 +0000352/// primary-expression: [C99 6.5.1]
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000353/// [C99] identifier
Sebastian Redlc4704762008-11-11 11:37:55 +0000354/// [C++] id-expression
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000355/// constant
356/// string-literal
Bill Wendling4073ed52007-02-13 01:51:42 +0000357/// [C++] boolean-literal [C++ 2.13.5]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000358/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000359/// '__func__' [C99 6.4.2.2]
360/// [GNU] '__FUNCTION__'
361/// [GNU] '__PRETTY_FUNCTION__'
362/// [GNU] '(' compound-statement ')'
363/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
364/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
365/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
366/// assign-expr ')'
367/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor3be4b122008-11-29 04:51:27 +0000368/// [GNU] '__null'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000369/// [OBJC] '[' objc-message-expr ']'
Chris Lattnerb241a1b2008-01-25 18:58:06 +0000370/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian19529ee2007-09-26 17:03:44 +0000371/// [OBJC] '@protocol' '(' identifier ')'
372/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000373/// [OBJC] objc-string-literal
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000374/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
375/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Bill Wendlinga6930032007-06-29 18:21:34 +0000376/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
377/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
378/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
379/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc4704762008-11-11 11:37:55 +0000380/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
381/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidis16c04102008-07-16 07:23:27 +0000382/// [C++] 'this' [C++ 9.3.2]
Steve Naroff0ac012832008-08-28 19:20:44 +0000383/// [clang] '^' block-literal
Chris Lattner52a99e52006-08-10 20:56:00 +0000384///
385/// constant: [C99 6.4.4]
386/// integer-constant
387/// floating-constant
388/// enumeration-constant -> identifier
389/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000390///
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000391/// id-expression: [C++ 5.1]
392/// unqualified-id
393/// qualified-id [TODO]
394///
395/// unqualified-id: [C++ 5.1]
396/// identifier
397/// operator-function-id
398/// conversion-function-id [TODO]
399/// '~' class-name [TODO]
400/// template-id [TODO]
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000401///
Sebastian Redlbd150f42008-11-21 19:14:01 +0000402/// new-expression: [C++ 5.3.4]
403/// '::'[opt] 'new' new-placement[opt] new-type-id
404/// new-initializer[opt]
405/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
406/// new-initializer[opt]
407///
408/// delete-expression: [C++ 5.3.5]
409/// '::'[opt] 'delete' cast-expression
410/// '::'[opt] 'delete' '[' ']' cast-expression
411///
Chris Lattner89c50c62006-08-11 06:41:18 +0000412Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000413 if (getLang().CPlusPlus) {
414 // Annotate typenames and C++ scope specifiers.
Argyrios Kyrtzidis0c4162a2008-11-26 21:51:07 +0000415 // Used only in C++, where the typename can be considered as a functional
416 // style cast ("int(1)").
417 // In C we don't expect identifiers to be treated as typenames; if it's a
418 // typedef name, let it be handled as an identifier and
419 // Actions.ActOnIdentifierExpr will emit the proper diagnostic.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000420 TryAnnotateTypeOrScopeToken();
421 }
422
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000423 ExprOwner Res(Actions);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000424 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000425
Chris Lattner81b576e2006-08-11 02:13:20 +0000426 // This handles all of cast-expression, unary-expression, postfix-expression,
427 // and primary-expression. We handle them together like this for efficiency
428 // and to simplify handling of an expression starting with a '(' token: which
429 // may be one of a parenthesized expression, cast-expression, compound literal
430 // expression, or statement expression.
431 //
432 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000433 // call ParsePostfixExpressionSuffix to handle the postfix expression
434 // suffixes. Cases that cannot be followed by postfix exprs should
435 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000436 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000437 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000438 // If this expression is limited to being a unary-expression, the parent can
439 // not start a cast expression.
440 ParenParseOption ParenExprType =
441 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000442 TypeTy *CastTy;
443 SourceLocation LParenLoc = Tok.getLocation();
444 SourceLocation RParenLoc;
445 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000446 if (Res.isInvalid()) return Res.move();
Chris Lattner89c50c62006-08-11 06:41:18 +0000447
Chris Lattner81b576e2006-08-11 02:13:20 +0000448 switch (ParenExprType) {
449 case SimpleExpr: break; // Nothing else to do.
450 case CompoundStmt: break; // Nothing else to do.
451 case CompoundLiteral:
452 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
453 // postfix-expression exist, parse them now.
454 break;
455 case CastExpr:
456 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
457 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000458 // TODO: For cast expression with CastTy.
459 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000460 if (!Res.isInvalid())
461 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.move());
462 return Res.move();
Chris Lattner81b576e2006-08-11 02:13:20 +0000463 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000464
Chris Lattner20c6a452006-08-12 17:40:43 +0000465 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000466 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattnere550a4e2006-08-24 06:37:51 +0000467 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000468
Chris Lattner52a99e52006-08-10 20:56:00 +0000469 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000470 case tok::numeric_constant:
471 // constant: integer-constant
472 // constant: floating-constant
473
Steve Naroff83895f72007-09-16 03:34:24 +0000474 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000475 ConsumeToken();
476
477 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000478 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000479
Bill Wendling4073ed52007-02-13 01:51:42 +0000480 case tok::kw_true:
481 case tok::kw_false:
482 return ParseCXXBoolLiteral();
483
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000484 case tok::identifier: { // primary-expression: identifier
485 // unqualified-id: identifier
486 // constant: enumeration-constant
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000487
Chris Lattnerac18be92006-11-20 06:49:47 +0000488 // Consume the identifier so that we can see if it is followed by a '('.
489 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
490 // need to know whether or not this identifier is a function designator or
491 // not.
492 IdentifierInfo &II = *Tok.getIdentifierInfo();
493 SourceLocation L = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000494 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner17ed4872006-11-20 04:58:19 +0000495 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000496 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattnerac18be92006-11-20 06:49:47 +0000497 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000498 case tok::char_constant: // constant: character-constant
Steve Naroff83895f72007-09-16 03:34:24 +0000499 Res = Actions.ActOnCharacterConstant(Tok);
Steve Naroffae4143e2007-04-26 20:39:23 +0000500 ConsumeToken();
501 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000502 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattner52a99e52006-08-10 20:56:00 +0000503 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
504 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
505 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner6307f192008-08-10 01:53:14 +0000506 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000507 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000508 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000509 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattner52a99e52006-08-10 20:56:00 +0000510 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000511 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000512 Res = ParseStringLiteralExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000513 if (Res.isInvalid()) return Res.move();
Chris Lattner20c6a452006-08-12 17:40:43 +0000514 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000515 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattnerf8339772006-08-10 22:01:51 +0000516 case tok::kw___builtin_va_arg:
517 case tok::kw___builtin_offsetof:
518 case tok::kw___builtin_choose_expr:
Nate Begeman1e36a852008-01-17 17:46:27 +0000519 case tok::kw___builtin_overload:
Chris Lattnerf8339772006-08-10 22:01:51 +0000520 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000521 return ParseBuiltinPrimaryExpression();
Douglas Gregor3be4b122008-11-29 04:51:27 +0000522 case tok::kw___null:
523 return Actions.ActOnGNUNullExpr(ConsumeToken());
524 break;
Chris Lattner81b576e2006-08-11 02:13:20 +0000525 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000526 case tok::minusminus: { // unary-expression: '--' unary-expression
527 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000528 Res = ParseCastExpression(true);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000529 if (!Res.isInvalid())
530 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.move());
531 return Res.move();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000532 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000533 case tok::amp: // unary-expression: '&' cast-expression
534 case tok::star: // unary-expression: '*' cast-expression
535 case tok::plus: // unary-expression: '+' cast-expression
536 case tok::minus: // unary-expression: '-' cast-expression
537 case tok::tilde: // unary-expression: '~' cast-expression
538 case tok::exclaim: // unary-expression: '!' cast-expression
539 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattnerc43926f2008-02-02 20:20:10 +0000540 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000541 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000542 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000543 if (!Res.isInvalid())
544 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.move());
545 return Res.move();
546 }
547
Chris Lattnerc43926f2008-02-02 20:20:10 +0000548 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
549 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000550 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattnerc43926f2008-02-02 20:20:10 +0000551 SourceLocation SavedLoc = ConsumeToken();
552 Res = ParseCastExpression(false);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000553 if (!Res.isInvalid())
554 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.move());
555 return Res.move();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000556 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000557 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
558 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000559 case tok::kw_alignof:
Chris Lattner81b576e2006-08-11 02:13:20 +0000560 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
561 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000562 // unary-expression: 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000563 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000564 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattnereefa10e2007-05-28 06:56:27 +0000565 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000566 if (Tok.isNot(tok::identifier)) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000567 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000568 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000569 }
Chris Lattnereefa10e2007-05-28 06:56:27 +0000570
571 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff66356bd2007-09-16 14:56:35 +0000572 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattnereefa10e2007-05-28 06:56:27 +0000573 Tok.getIdentifierInfo());
Chris Lattner14a1b642006-10-15 22:33:58 +0000574 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000575 return Res.move();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000576 }
Chris Lattner29375652006-12-04 18:06:35 +0000577 case tok::kw_const_cast:
578 case tok::kw_dynamic_cast:
579 case tok::kw_reinterpret_cast:
580 case tok::kw_static_cast:
Argyrios Kyrtzidis16d63a72008-08-16 19:45:32 +0000581 Res = ParseCXXCasts();
582 // These can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000583 return ParsePostfixExpressionSuffix(Res.move());
Sebastian Redlc4704762008-11-11 11:37:55 +0000584 case tok::kw_typeid:
585 Res = ParseCXXTypeid();
586 // This can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000587 return ParsePostfixExpressionSuffix(Res.move());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000588 case tok::kw_this:
Argyrios Kyrtzidis37779ad2008-08-16 19:34:46 +0000589 Res = ParseCXXThis();
590 // This can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000591 return ParsePostfixExpressionSuffix(Res.move());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000592
593 case tok::kw_char:
594 case tok::kw_wchar_t:
595 case tok::kw_bool:
596 case tok::kw_short:
597 case tok::kw_int:
598 case tok::kw_long:
599 case tok::kw_signed:
600 case tok::kw_unsigned:
601 case tok::kw_float:
602 case tok::kw_double:
603 case tok::kw_void:
604 case tok::kw_typeof: {
605 if (!getLang().CPlusPlus)
606 goto UnhandledToken;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000607 case tok::annot_qualtypename:
608 assert(getLang().CPlusPlus && "Expected C++");
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000609 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
610 //
611 DeclSpec DS;
612 ParseCXXSimpleTypeSpecifier(DS);
613 if (Tok.isNot(tok::l_paren))
Chris Lattner6d29c102008-11-18 07:48:38 +0000614 return Diag(Tok, diag::err_expected_lparen_after_type)
615 << DS.getSourceRange();
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000616
617 Res = ParseCXXTypeConstructExpression(DS);
618 // This can be followed by postfix-expr pieces.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000619 return ParsePostfixExpressionSuffix(Res.move());
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000620 }
621
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000622 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
623 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
624 // template-id
625 Res = ParseCXXIdExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000626 return ParsePostfixExpressionSuffix(Res.move());
Douglas Gregor11d0c4c2008-11-06 22:13:31 +0000627
Sebastian Redldb36b9b2008-12-02 16:35:44 +0000628 case tok::coloncolon: // [C++] new-expression or [C++] delete-expression
Sebastian Redl538787f2008-12-02 17:10:24 +0000629 // If the next token is neither 'new' nor 'delete', the :: would have been
630 // parsed as a scope specifier already.
Sebastian Redldb36b9b2008-12-02 16:35:44 +0000631 if (NextToken().is(tok::kw_new))
632 return ParseCXXNewExpression();
633 else
634 return ParseCXXDeleteExpression();
635
Sebastian Redlbd150f42008-11-21 19:14:01 +0000636 case tok::kw_new: // [C++] new-expression
Sebastian Redlbd150f42008-11-21 19:14:01 +0000637 return ParseCXXNewExpression();
638
639 case tok::kw_delete: // [C++] delete-expression
640 return ParseCXXDeleteExpression();
641
Chris Lattner644e1b72007-10-03 22:03:06 +0000642 case tok::at: {
643 SourceLocation AtLoc = ConsumeToken();
Steve Naroff126b4d82007-10-15 20:55:58 +0000644 return ParseObjCAtExpression(AtLoc);
Chris Lattner644e1b72007-10-03 22:03:06 +0000645 }
Fariborz Jahanian7db004d2007-09-05 19:52:07 +0000646 case tok::l_square:
Steve Naroff126b4d82007-10-15 20:55:58 +0000647 // These can be followed by postfix-expr pieces.
Chris Lattner2fdcddd2008-05-09 05:28:21 +0000648 if (getLang().ObjC1)
649 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
650 // FALL THROUGH.
Steve Naroff0ac012832008-08-28 19:20:44 +0000651 case tok::caret:
652 if (getLang().Blocks)
653 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
654 Diag(Tok, diag::err_expected_expression);
655 return ExprResult(true);
Chris Lattner52a99e52006-08-10 20:56:00 +0000656 default:
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000657 UnhandledToken:
Chris Lattner52a99e52006-08-10 20:56:00 +0000658 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000659 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000660 }
661
Chris Lattner20c6a452006-08-12 17:40:43 +0000662 // unreachable.
663 abort();
664}
665
666/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
667/// is parsed, this method parses any suffixes that apply.
668///
669/// postfix-expression: [C99 6.5.2]
670/// primary-expression
671/// postfix-expression '[' expression ']'
672/// postfix-expression '(' argument-expression-list[opt] ')'
673/// postfix-expression '.' identifier
674/// postfix-expression '->' identifier
675/// postfix-expression '++'
676/// postfix-expression '--'
677/// '(' type-name ')' '{' initializer-list '}'
678/// '(' type-name ')' '{' initializer-list ',' '}'
679///
680/// argument-expression-list: [C99 6.5.2]
681/// argument-expression
682/// argument-expression-list ',' assignment-expression
683///
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000684Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHSArg) {
685 ExprOwner LHS(Actions, LHSArg);
Chris Lattnerf8339772006-08-10 22:01:51 +0000686 // Now that the primary-expression piece of the postfix-expression has been
687 // parsed, see if there are any postfix-expression pieces here.
688 SourceLocation Loc;
689 while (1) {
690 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000691 default: // Not a postfix-expression suffix.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000692 return LHS.move();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000693 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000694 Loc = ConsumeBracket();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000695 ExprOwner Idx(Actions, ParseExpression());
Sebastian Redl511ed552008-11-25 22:21:31 +0000696
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000697 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000698
699 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
700 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.move(), Loc,
701 Idx.move(), RLoc);
702 } else
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000703 LHS = ExprResult(true);
704
Chris Lattner89c50c62006-08-11 06:41:18 +0000705 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000706 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000707 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000708 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000709
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000710 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl511ed552008-11-25 22:21:31 +0000711 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000712 CommaLocsTy CommaLocs;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000713
Chris Lattner04132372006-10-16 06:12:55 +0000714 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000715
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000716 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +0000717 if (ParseExpressionList(ArgExprs, CommaLocs)) {
718 SkipUntil(tok::r_paren);
719 return ExprResult(true);
Chris Lattner0c6c0342006-08-12 18:12:45 +0000720 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000721 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000722
Chris Lattner89c50c62006-08-11 06:41:18 +0000723 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000724 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Chris Lattnere165d942006-08-24 04:40:38 +0000725 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
726 "Unexpected number of commas!");
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000727 LHS = Actions.ActOnCallExpr(CurScope, LHS.move(), Loc,
Douglas Gregorb0846b02008-12-06 00:22:45 +0000728 ArgExprs.take(),
Sebastian Redl511ed552008-11-25 22:21:31 +0000729 ArgExprs.size(), &CommaLocs[0],
730 Tok.getLocation());
Chris Lattnere165d942006-08-24 04:40:38 +0000731 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000732
Chris Lattner5abb82c2007-07-21 05:18:12 +0000733 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000734 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000735 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000736 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000737 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000738 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000739 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000740
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000741 if (Tok.isNot(tok::identifier)) {
Chris Lattner89c50c62006-08-11 06:41:18 +0000742 Diag(Tok, diag::err_expected_ident);
743 return ExprResult(true);
744 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000745
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000746 if (!LHS.isInvalid()) {
747 LHS = Actions.ActOnMemberReferenceExpr(LHS.move(), OpLoc, OpKind,
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000748 Tok.getLocation(),
749 *Tok.getIdentifierInfo());
Sebastian Redl511ed552008-11-25 22:21:31 +0000750 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000751 ConsumeToken();
752 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000753 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000754 case tok::plusplus: // postfix-expression: postfix-expression '++'
755 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000756 if (!LHS.isInvalid()) {
Douglas Gregord08452f2008-11-19 15:42:04 +0000757 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000758 Tok.getKind(), LHS.move());
Sebastian Redl511ed552008-11-25 22:21:31 +0000759 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000760 ConsumeToken();
761 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000762 }
763 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000764}
765
Chris Lattner20c6a452006-08-12 17:40:43 +0000766
Chris Lattner81b576e2006-08-11 02:13:20 +0000767/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
768/// unary-expression: [C99 6.5.3]
769/// 'sizeof' unary-expression
770/// 'sizeof' '(' type-name ')'
771/// [GNU] '__alignof' unary-expression
772/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregord7fc8722008-11-06 15:17:27 +0000773/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000774Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregord7fc8722008-11-06 15:17:27 +0000775 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
776 || Tok.is(tok::kw_alignof)) &&
Chris Lattner81b576e2006-08-11 02:13:20 +0000777 "Not a sizeof/alignof expression!");
Chris Lattner146762e2007-07-20 16:59:19 +0000778 Token OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000779 ConsumeToken();
780
781 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000782 ExprOwner Operand(Actions);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000783 if (Tok.isNot(tok::l_paren)) {
Chris Lattner26115ac2006-08-24 06:10:04 +0000784 Operand = ParseCastExpression(true);
785 } else {
786 // If it starts with a '(', we know that it is either a parenthesized
787 // type-name, or it is a unary-expression that starts with a compound
788 // literal, or starts with a primary-expression that is a parenthesized
789 // expression.
790 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000791 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000792 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000793 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000794
795 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
796 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner47791a42007-11-13 20:50:37 +0000797 if (ExprType == CastExpr)
Sebastian Redl6f282892008-11-11 17:56:53 +0000798 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
799 OpTok.is(tok::kw_sizeof),
800 /*isType=*/true, CastTy,
801 SourceRange(LParenLoc, RParenLoc));
Chris Lattner47791a42007-11-13 20:50:37 +0000802
803 // If this is a parenthesized expression, it is the start of a
804 // unary-expression, but doesn't include any postfix pieces. Parse these
805 // now if present.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000806 Operand = ParsePostfixExpressionSuffix(Operand.move());
Chris Lattner26115ac2006-08-24 06:10:04 +0000807 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000808
Chris Lattner26115ac2006-08-24 06:10:04 +0000809 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000810 if (!Operand.isInvalid())
Sebastian Redl6f282892008-11-11 17:56:53 +0000811 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
812 OpTok.is(tok::kw_sizeof),
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000813 /*isType=*/false, Operand.move(),
Sebastian Redl6f282892008-11-11 17:56:53 +0000814 SourceRange());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000815 return Operand.move();
Chris Lattner81b576e2006-08-11 02:13:20 +0000816}
817
Chris Lattner11124352006-08-12 19:16:08 +0000818/// ParseBuiltinPrimaryExpression
819///
820/// primary-expression: [C99 6.5.1]
821/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
822/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
823/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
824/// assign-expr ')'
825/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman1e36a852008-01-17 17:46:27 +0000826/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner11124352006-08-12 19:16:08 +0000827///
828/// [GNU] offsetof-member-designator:
829/// [GNU] identifier
830/// [GNU] offsetof-member-designator '.' identifier
831/// [GNU] offsetof-member-designator '[' expression ']'
832///
833Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000834 ExprOwner Res(Actions);
Chris Lattner11124352006-08-12 19:16:08 +0000835 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
836
837 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000838 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000839
840 // All of these start with an open paren.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000841 if (Tok.isNot(tok::l_paren)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000842 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Chris Lattner11124352006-08-12 19:16:08 +0000843 return ExprResult(true);
844 }
845
Chris Lattner04132372006-10-16 06:12:55 +0000846 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000847 // TODO: Build AST.
848
Chris Lattner11124352006-08-12 19:16:08 +0000849 switch (T) {
850 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000851 case tok::kw___builtin_va_arg: {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000852 ExprOwner Expr(Actions, ParseAssignmentExpression());
853 if (Expr.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +0000854 SkipUntil(tok::r_paren);
Eli Friedman002ad122008-08-20 22:07:34 +0000855 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000856 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000857
Chris Lattner6d7e6342006-08-15 03:41:14 +0000858 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000859 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000860
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000861 TypeTy *Ty = ParseTypeName();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000862
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000863 if (Tok.isNot(tok::r_paren)) {
864 Diag(Tok, diag::err_expected_rparen);
865 return ExprResult(true);
866 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000867 Res = Actions.ActOnVAArg(StartLoc, Expr.move(), Ty, ConsumeParen());
Chris Lattner11124352006-08-12 19:16:08 +0000868 break;
Anders Carlsson7e13ab82007-10-15 20:28:48 +0000869 }
Chris Lattner687d6092007-08-30 15:51:11 +0000870 case tok::kw___builtin_offsetof: {
Chris Lattnere4ee2df2007-08-30 17:08:45 +0000871 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner687d6092007-08-30 15:51:11 +0000872 TypeTy *Ty = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000873
Chris Lattner6d7e6342006-08-15 03:41:14 +0000874 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000875 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000876
877 // We must have at least one identifier here.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000878 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000879 Diag(Tok, diag::err_expected_ident);
880 SkipUntil(tok::r_paren);
881 return true;
882 }
883
884 // Keep track of the various subcomponents we see.
885 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
886
887 Comps.push_back(Action::OffsetOfComponent());
888 Comps.back().isBrackets = false;
889 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
890 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000891
Sebastian Redl511ed552008-11-25 22:21:31 +0000892 // FIXME: This loop leaks the index expressions on error.
Chris Lattner11124352006-08-12 19:16:08 +0000893 while (1) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000894 if (Tok.is(tok::period)) {
Chris Lattner11124352006-08-12 19:16:08 +0000895 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner687d6092007-08-30 15:51:11 +0000896 Comps.push_back(Action::OffsetOfComponent());
897 Comps.back().isBrackets = false;
898 Comps.back().LocStart = ConsumeToken();
Chris Lattner11124352006-08-12 19:16:08 +0000899
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000900 if (Tok.isNot(tok::identifier)) {
Chris Lattner687d6092007-08-30 15:51:11 +0000901 Diag(Tok, diag::err_expected_ident);
902 SkipUntil(tok::r_paren);
903 return true;
904 }
905 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
906 Comps.back().LocEnd = ConsumeToken();
907
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000908 } else if (Tok.is(tok::l_square)) {
Chris Lattner11124352006-08-12 19:16:08 +0000909 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner687d6092007-08-30 15:51:11 +0000910 Comps.push_back(Action::OffsetOfComponent());
911 Comps.back().isBrackets = true;
912 Comps.back().LocStart = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000913 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000914 if (Res.isInvalid()) {
Chris Lattner11124352006-08-12 19:16:08 +0000915 SkipUntil(tok::r_paren);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000916 return Res.move();
Chris Lattner11124352006-08-12 19:16:08 +0000917 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000918 Comps.back().U.E = Res.move();
Chris Lattner11124352006-08-12 19:16:08 +0000919
Chris Lattner687d6092007-08-30 15:51:11 +0000920 Comps.back().LocEnd =
921 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000922 } else if (Tok.is(tok::r_paren)) {
Steve Naroff66356bd2007-09-16 14:56:35 +0000923 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner5ad4f462007-08-30 15:52:49 +0000924 Comps.size(), ConsumeParen());
925 break;
Chris Lattner11124352006-08-12 19:16:08 +0000926 } else {
Chris Lattner687d6092007-08-30 15:51:11 +0000927 // Error occurred.
928 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000929 }
930 }
931 break;
Chris Lattner687d6092007-08-30 15:51:11 +0000932 }
Steve Naroff9efdabc2007-08-03 21:21:27 +0000933 case tok::kw___builtin_choose_expr: {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000934 ExprOwner Cond(Actions, ParseAssignmentExpression());
935 if (Cond.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000936 SkipUntil(tok::r_paren);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000937 return Cond.move();
Steve Naroff9efdabc2007-08-03 21:21:27 +0000938 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000939 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000940 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000941
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000942 ExprOwner Expr1(Actions, ParseAssignmentExpression());
943 if (Expr1.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000944 SkipUntil(tok::r_paren);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000945 return Expr1.move();
946 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000947 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000948 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000949
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000950 ExprOwner Expr2(Actions, ParseAssignmentExpression());
951 if (Expr2.isInvalid()) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000952 SkipUntil(tok::r_paren);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000953 return Expr2.move();
954 }
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000955 if (Tok.isNot(tok::r_paren)) {
Steve Naroff9efdabc2007-08-03 21:21:27 +0000956 Diag(Tok, diag::err_expected_rparen);
957 return ExprResult(true);
958 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000959 Res = Actions.ActOnChooseExpr(StartLoc, Cond.move(), Expr1.move(),
960 Expr2.move(), ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +0000961 break;
Steve Naroff9efdabc2007-08-03 21:21:27 +0000962 }
Nate Begeman1e36a852008-01-17 17:46:27 +0000963 case tok::kw___builtin_overload: {
Sebastian Redl511ed552008-11-25 22:21:31 +0000964 ExprVector ArgExprs(Actions);
Nate Begeman1e36a852008-01-17 17:46:27 +0000965 llvm::SmallVector<SourceLocation, 8> CommaLocs;
966
967 // For each iteration through the loop look for assign-expr followed by a
968 // comma. If there is no comma, break and attempt to match r-paren.
969 if (Tok.isNot(tok::r_paren)) {
970 while (1) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000971 ExprOwner ArgExpr(Actions, ParseAssignmentExpression());
972 if (ArgExpr.isInvalid()) {
Nate Begeman1e36a852008-01-17 17:46:27 +0000973 SkipUntil(tok::r_paren);
974 return ExprResult(true);
975 } else
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000976 ArgExprs.push_back(ArgExpr.move());
977
Nate Begeman1e36a852008-01-17 17:46:27 +0000978 if (Tok.isNot(tok::comma))
979 break;
980 // Move to the next argument, remember where the comma was.
981 CommaLocs.push_back(ConsumeToken());
982 }
983 }
984
985 // Attempt to consume the r-paren
986 if (Tok.isNot(tok::r_paren)) {
987 Diag(Tok, diag::err_expected_rparen);
988 SkipUntil(tok::r_paren);
989 return ExprResult(true);
990 }
Sebastian Redl511ed552008-11-25 22:21:31 +0000991 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begeman1e36a852008-01-17 17:46:27 +0000992 &CommaLocs[0], StartLoc, ConsumeParen());
993 break;
994 }
Chris Lattner11124352006-08-12 19:16:08 +0000995 case tok::kw___builtin_types_compatible_p:
Steve Naroff788d8642007-08-01 23:45:51 +0000996 TypeTy *Ty1 = ParseTypeName();
Chris Lattner11124352006-08-12 19:16:08 +0000997
Chris Lattner6d7e6342006-08-15 03:41:14 +0000998 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000999 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +00001000
Steve Naroff788d8642007-08-01 23:45:51 +00001001 TypeTy *Ty2 = ParseTypeName();
1002
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001003 if (Tok.isNot(tok::r_paren)) {
Steve Naroff788d8642007-08-01 23:45:51 +00001004 Diag(Tok, diag::err_expected_rparen);
1005 return ExprResult(true);
1006 }
Steve Naroff66356bd2007-09-16 14:56:35 +00001007 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner5ad4f462007-08-30 15:52:49 +00001008 break;
Chris Lattner11124352006-08-12 19:16:08 +00001009 }
1010
Chris Lattner11124352006-08-12 19:16:08 +00001011 // These can be followed by postfix-expr pieces because they are
1012 // primary-expressions.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001013 return ParsePostfixExpressionSuffix(Res.move());
Chris Lattner11124352006-08-12 19:16:08 +00001014}
1015
Chris Lattner4add4e62006-08-11 01:33:00 +00001016/// ParseParenExpression - This parses the unit that starts with a '(' token,
1017/// based on what is allowed by ExprType. The actual thing parsed is returned
1018/// in ExprType.
1019///
1020/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001021/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +00001022/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1023/// postfix-expression: [C99 6.5.2]
1024/// '(' type-name ')' '{' initializer-list '}'
1025/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +00001026/// cast-expression: [C99 6.5.4]
1027/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +00001028///
Chris Lattnere550a4e2006-08-24 06:37:51 +00001029Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
1030 TypeTy *&CastTy,
1031 SourceLocation &RParenLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001032 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +00001033 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001034 ExprOwner Result(Actions, true);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001035 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +00001036
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001037 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattnerf8339772006-08-10 22:01:51 +00001038 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001039 StmtOwner Stmt(Actions, ParseCompoundStatement(true));
Chris Lattner4add4e62006-08-11 01:33:00 +00001040 ExprType = CompoundStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001041
Chris Lattner366727f2007-07-24 16:58:17 +00001042 // If the substmt parsed correctly, build the AST node.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001043 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1044 Result = Actions.ActOnStmtExpr(
1045 OpenLoc, Stmt.move(), Tok.getLocation());
1046
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001047 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001048 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +00001049 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001050
1051 // Match the ')'.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001052 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001053 RParenLoc = ConsumeParen();
1054 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001055 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001056
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001057 if (Tok.is(tok::l_brace)) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +00001058 if (!getLang().C99) // Compound literals don't exist in C90.
1059 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001060 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +00001061 ExprType = CompoundLiteral;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001062 if (!Result.isInvalid())
1063 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
1064 Result.take());
Chris Lattner4add4e62006-08-11 01:33:00 +00001065 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +00001066 // Note that this doesn't parse the subsequence cast-expression, it just
1067 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +00001068 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +00001069 CastTy = Ty;
1070 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +00001071 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001072 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +00001073 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001074 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001075 return Result.move();
Chris Lattner4add4e62006-08-11 01:33:00 +00001076 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +00001077 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +00001078 ExprType = SimpleExpr;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001079 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1080 Result = Actions.ActOnParenExpr(
1081 OpenLoc, Tok.getLocation(), Result.take());
Chris Lattnerf8339772006-08-10 22:01:51 +00001082 }
Chris Lattnerc951dae2006-08-10 04:23:57 +00001083
Chris Lattner4564bc12006-08-10 23:14:52 +00001084 // Match the ')'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001085 if (Result.isInvalid())
Chris Lattner89c50c62006-08-11 06:41:18 +00001086 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001087 else {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001088 if (Tok.is(tok::r_paren))
Chris Lattner04132372006-10-16 06:12:55 +00001089 RParenLoc = ConsumeParen();
1090 else
Chris Lattnere550a4e2006-08-24 06:37:51 +00001091 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +00001092 }
Chris Lattner1b926492006-08-23 06:42:10 +00001093
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001094 return Result.move();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001095}
Chris Lattnerd3e98952006-10-06 05:22:26 +00001096
Chris Lattnerd3e98952006-10-06 05:22:26 +00001097/// ParseStringLiteralExpression - This handles the various token types that
1098/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1099/// translation phase #6].
1100///
1101/// primary-expression: [C99 6.5.1]
1102/// string-literal
1103Parser::ExprResult Parser::ParseStringLiteralExpression() {
1104 assert(isTokenStringLiteral() && "Not a string literal!");
1105
1106 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1107 // considered to be strings for concatenation purposes.
Chris Lattner146762e2007-07-20 16:59:19 +00001108 llvm::SmallVector<Token, 4> StringToks;
Chris Lattnerd3e98952006-10-06 05:22:26 +00001109
Chris Lattnerd3e98952006-10-06 05:22:26 +00001110 do {
Chris Lattnerd3e98952006-10-06 05:22:26 +00001111 StringToks.push_back(Tok);
1112 ConsumeStringToken();
1113 } while (isTokenStringLiteral());
Chris Lattner08f27912006-11-09 06:34:47 +00001114
1115 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroff83895f72007-09-16 03:34:24 +00001116 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001117}
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001118
1119/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1120///
1121/// argument-expression-list:
1122/// assignment-expression
1123/// argument-expression-list , assignment-expression
1124///
1125/// [C++] expression-list:
1126/// [C++] assignment-expression
1127/// [C++] expression-list , assignment-expression
1128///
1129bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1130 while (1) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001131 ExprOwner Expr(Actions, ParseAssignmentExpression());
1132 if (Expr.isInvalid())
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001133 return true;
Argyrios Kyrtzidis22fc2662008-08-18 22:49:40 +00001134
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001135 Exprs.push_back(Expr.move());
Argyrios Kyrtzidis69af9ee2008-08-16 20:03:01 +00001136
1137 if (Tok.isNot(tok::comma))
1138 return false;
1139 // Move to the next argument, remember where the comma was.
1140 CommaLocs.push_back(ConsumeToken());
1141 }
1142}
Steve Naroff0ac012832008-08-28 19:20:44 +00001143
1144/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff7a147c62008-09-16 23:11:46 +00001145/// like ^(int x){ return x+1; }
Steve Naroff0ac012832008-08-28 19:20:44 +00001146///
1147/// block-literal:
1148/// [clang] '^' block-args[opt] compound-statement
Steve Naroff0ac012832008-08-28 19:20:44 +00001149/// [clang] block-args:
1150/// [clang] '(' parameter-list ')'
1151///
1152Parser::ExprResult Parser::ParseBlockLiteralExpression() {
1153 assert(Tok.is(tok::caret) && "block literal starts with ^");
1154 SourceLocation CaretLoc = ConsumeToken();
1155
1156 // Enter a scope to hold everything within the block. This includes the
1157 // argument decls, decls within the compound expression, etc. This also
1158 // allows determining whether a variable reference inside the block is
1159 // within or outside of the block.
1160 EnterScope(Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1161 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001162
1163 // Inform sema that we are starting a block.
1164 Actions.ActOnBlockStart(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001165
1166 // Parse the return type if present.
1167 DeclSpec DS;
1168 Declarator ParamInfo(DS, Declarator::PrototypeContext);
1169
1170 // If this block has arguments, parse them. There is no ambiguity here with
1171 // the expression case, because the expression case requires a parameter list.
1172 if (Tok.is(tok::l_paren)) {
1173 ParseParenDeclarator(ParamInfo);
1174 // Parse the pieces after the identifier as if we had "int(...)".
1175 ParamInfo.SetIdentifier(0, CaretLoc);
1176 if (ParamInfo.getInvalidType()) {
1177 // If there was an error parsing the arguments, they may have tried to use
1178 // ^(x+y) which requires an argument list. Just skip the whole block
1179 // literal.
1180 ExitScope();
1181 return true;
1182 }
1183 } else {
1184 // Otherwise, pretend we saw (void).
1185 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001186 0, 0, 0, CaretLoc));
Steve Naroff0ac012832008-08-28 19:20:44 +00001187 }
1188
1189 // Inform sema that we are starting a block.
Steve Naroff1d95e5a2008-10-10 01:28:17 +00001190 Actions.ActOnBlockArguments(ParamInfo);
Steve Naroff0ac012832008-08-28 19:20:44 +00001191
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001192 ExprOwner Result(Actions, true);
Steve Naroff0ac012832008-08-28 19:20:44 +00001193 if (Tok.is(tok::l_brace)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001194 StmtOwner Stmt(Actions, ParseCompoundStatementBody());
1195 if (!Stmt.isInvalid()) {
1196 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.move(), CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001197 } else {
1198 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff0ac012832008-08-28 19:20:44 +00001199 }
1200 }
Steve Naroff0ac012832008-08-28 19:20:44 +00001201 ExitScope();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001202 return Result.move();
Steve Naroff0ac012832008-08-28 19:20:44 +00001203}
1204