blob: 94e686b6d7735f10c4cd23f7b10da87e85bd476d [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 {
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, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13 // *, /, %
50 };
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;
78 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
95/// ParseExpression - Simple precedence-based parser for binary/ternary
96/// operators.
97///
98/// 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///
107/// 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 Lattner50dd2892008-02-26 00:51:44 +0000163/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000164///
165/// assignment-operator: one of
166/// = *= /= %= += -= <<= >>= &= ^= |=
167///
168/// expression: [C99 6.5.17]
169/// assignment-expression
170/// expression ',' assignment-expression
171///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000172Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000173 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000174 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000175
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000176 OwningExprResult LHS(ParseCastExpression(false));
177 if (LHS.isInvalid()) return move(LHS);
178
Sebastian Redld8c4e152008-12-11 22:33:27 +0000179 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Fariborz Jahanian397fcc12007-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 Lattnerc97c2042007-10-03 22:03:06 +0000184/// routine is necessary to disambiguate @try-statement from,
185/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000186///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000187Parser::OwningExprResult
188Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl1d922962008-12-13 15:32:12 +0000189 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000190 if (LHS.isInvalid()) return move(LHS);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000191
Sebastian Redld8c4e152008-12-11 22:33:27 +0000192 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000193}
194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
196///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000197Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000198 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000199 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000200
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000201 OwningExprResult LHS(ParseCastExpression(false));
202 if (LHS.isInvalid()) return move(LHS);
203
Sebastian Redld8c4e152008-12-11 22:33:27 +0000204 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205}
206
Chris Lattnerb93fb492008-06-02 21:31:07 +0000207/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
208/// where part of an objc message send has already been parsed. In this case
209/// LBracLoc indicates the location of the '[' of the message send, and either
210/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
211/// message.
212///
213/// Since this handles full assignment-expression's, it handles postfix
214/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000215Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000216Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000217 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000218 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000219 ExprArg ReceiverExpr) {
220 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
221 ReceiverName,
222 move(ReceiverExpr)));
223 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000224 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000225 if (R.isInvalid()) return move(R);
226 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000227}
228
229
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000230Parser::OwningExprResult Parser::ParseConstantExpression() {
231 OwningExprResult LHS(ParseCastExpression(false));
232 if (LHS.isInvalid()) return move(LHS);
233
Sebastian Redld8c4e152008-12-11 22:33:27 +0000234 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
238/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000239Parser::OwningExprResult
240Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
242 SourceLocation ColonLoc;
243
244 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 Redl0e9eabc2008-12-09 13:15:23 +0000248 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000249 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
251 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000252 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 ConsumeToken();
254
255 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000256 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000258 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl0e9eabc2008-12-09 13:15:23 +0000264 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000265 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 } else {
267 // Special case handling of "X ? Y : Z" where Y is empty:
268 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000269 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 Diag(Tok, diag::ext_gnu_conditional_expr);
271 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000272
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000273 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000275 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000276 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Eat the colon.
280 ColonLoc = ConsumeToken();
281 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000284 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000285 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000286 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +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());
292
293 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000294 bool isRightAssoc = ThisPrec == prec::Conditional ||
295 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +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.
299 if (ThisPrec < NextTokPrec ||
300 (ThisPrec == NextTokPrec && isRightAssoc)) {
301 // 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 Redla55e52c2008-11-25 22:21:31 +0000305 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000306 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000307 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000308 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000309
310 NextTokPrec = getBinOpPrecedence(Tok.getKind());
311 }
312 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000313
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000314 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000315 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000317 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
318 OpToken.getKind(), LHS.release(),
319 RHS.release());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000320 else
Steve Narofff69936d2007-09-16 03:34:24 +0000321 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000322 LHS.release(), TernaryMiddle.release(),
323 RHS.release());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
326}
327
328/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
329/// true, parse a unary-expression.
330///
331/// cast-expression: [C99 6.5.4]
332/// unary-expression
333/// '(' type-name ')' cast-expression
334///
335/// unary-expression: [C99 6.5.3]
336/// postfix-expression
337/// '++' unary-expression
338/// '--' unary-expression
339/// unary-operator cast-expression
340/// 'sizeof' unary-expression
341/// 'sizeof' '(' type-name ')'
342/// [GNU] '__alignof' unary-expression
343/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000344/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000345/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000346/// [C++] new-expression
347/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000348///
349/// unary-operator: one of
350/// '&' '*' '+' '-' '~' '!'
351/// [GNU] '__extension__' '__real' '__imag'
352///
353/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000354/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000355/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000356/// constant
357/// string-literal
358/// [C++] boolean-literal [C++ 2.13.5]
359/// '(' expression ')'
360/// '__func__' [C99 6.4.2.2]
361/// [GNU] '__FUNCTION__'
362/// [GNU] '__PRETTY_FUNCTION__'
363/// [GNU] '(' compound-statement ')'
364/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
365/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
366/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
367/// assign-expr ')'
368/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000369/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000370/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000371/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000372/// [OBJC] '@protocol' '(' identifier ')'
373/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000374/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000375/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
376/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
378/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
379/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
380/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000381/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
382/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000383/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000384/// [G++] unary-type-trait '(' type-id ')'
385/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000386/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000387///
388/// constant: [C99 6.4.4]
389/// integer-constant
390/// floating-constant
391/// enumeration-constant -> identifier
392/// character-constant
393///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000394/// id-expression: [C++ 5.1]
395/// unqualified-id
396/// qualified-id [TODO]
397///
398/// unqualified-id: [C++ 5.1]
399/// identifier
400/// operator-function-id
401/// conversion-function-id [TODO]
402/// '~' class-name [TODO]
403/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000404///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000405/// new-expression: [C++ 5.3.4]
406/// '::'[opt] 'new' new-placement[opt] new-type-id
407/// new-initializer[opt]
408/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
409/// new-initializer[opt]
410///
411/// delete-expression: [C++ 5.3.5]
412/// '::'[opt] 'delete' cast-expression
413/// '::'[opt] 'delete' '[' ']' cast-expression
414///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000415/// [GNU] unary-type-trait:
416/// '__has_nothrow_assign' [TODO]
417/// '__has_nothrow_copy' [TODO]
418/// '__has_nothrow_constructor' [TODO]
419/// '__has_trivial_assign' [TODO]
420/// '__has_trivial_copy' [TODO]
421/// '__has_trivial_constructor' [TODO]
422/// '__has_trivial_destructor' [TODO]
423/// '__has_virtual_destructor' [TODO]
424/// '__is_abstract' [TODO]
425/// '__is_class'
426/// '__is_empty' [TODO]
427/// '__is_enum'
428/// '__is_pod'
429/// '__is_polymorphic'
430/// '__is_union'
431///
432/// [GNU] binary-type-trait:
433/// '__is_base_of' [TODO]
434///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000435Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000436 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 tok::TokenKind SavedKind = Tok.getKind();
438
439 // This handles all of cast-expression, unary-expression, postfix-expression,
440 // and primary-expression. We handle them together like this for efficiency
441 // and to simplify handling of an expression starting with a '(' token: which
442 // may be one of a parenthesized expression, cast-expression, compound literal
443 // expression, or statement expression.
444 //
445 // If the parsed tokens consist of a primary-expression, the cases below
446 // call ParsePostfixExpressionSuffix to handle the postfix expression
447 // suffixes. Cases that cannot be followed by postfix exprs should
448 // return without invoking ParsePostfixExpressionSuffix.
449 switch (SavedKind) {
450 case tok::l_paren: {
451 // If this expression is limited to being a unary-expression, the parent can
452 // not start a cast expression.
453 ParenParseOption ParenExprType =
454 isUnaryExpression ? CompoundLiteral : CastExpr;
455 TypeTy *CastTy;
456 SourceLocation LParenLoc = Tok.getLocation();
457 SourceLocation RParenLoc;
458 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000459 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000460
461 switch (ParenExprType) {
462 case SimpleExpr: break; // Nothing else to do.
463 case CompoundStmt: break; // Nothing else to do.
464 case CompoundLiteral:
465 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
466 // postfix-expression exist, parse them now.
467 break;
468 case CastExpr:
469 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
470 // the cast-expression that follows it next.
471 // TODO: For cast expression with CastTy.
472 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000473 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000474 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,
475 Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000476 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000480 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000482
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 // primary-expression
484 case tok::numeric_constant:
485 // constant: integer-constant
486 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000487
Steve Narofff69936d2007-09-16 03:34:24 +0000488 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000490
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000492 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000493
494 case tok::kw_true:
495 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000496 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000497
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000498 case tok::identifier: { // primary-expression: identifier
499 // unqualified-id: identifier
500 // constant: enumeration-constant
Chris Lattner74ba4102009-01-04 22:52:14 +0000501 // Turn a potentially qualified name into a annot_qualtypename or
502 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000503 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000504 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
505 if (TryAnnotateTypeOrScopeToken())
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000506 return ParseCastExpression(isUnaryExpression);
507 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // Consume the identifier so that we can see if it is followed by a '('.
510 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
511 // need to know whether or not this identifier is a function designator or
512 // not.
513 IdentifierInfo &II = *Tok.getIdentifierInfo();
514 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000515 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
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 }
519 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000520 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 ConsumeToken();
522 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000523 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
525 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
526 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000527 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 ConsumeToken();
529 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000530 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 case tok::string_literal: // primary-expression: string-literal
532 case tok::wide_string_literal:
533 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000534 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000536 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 case tok::kw___builtin_va_arg:
538 case tok::kw___builtin_offsetof:
539 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000540 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000542 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000543 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000544 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000545 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case tok::plusplus: // unary-expression: '++' unary-expression
547 case tok::minusminus: { // unary-expression: '--' unary-expression
548 SourceLocation SavedLoc = ConsumeToken();
549 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000550 if (!Res.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000551 Res = Owned(Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind,
552 Res.release()));
553 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 }
555 case tok::amp: // unary-expression: '&' cast-expression
556 case tok::star: // unary-expression: '*' cast-expression
557 case tok::plus: // unary-expression: '+' cast-expression
558 case tok::minus: // unary-expression: '-' cast-expression
559 case tok::tilde: // unary-expression: '~' cast-expression
560 case tok::exclaim: // unary-expression: '!' cast-expression
561 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000562 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 SourceLocation SavedLoc = ConsumeToken();
564 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000565 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000566 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000567 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000568 }
569
Chris Lattner35080842008-02-02 20:20:10 +0000570 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
571 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000572 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000573 SourceLocation SavedLoc = ConsumeToken();
574 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000575 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000576 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000577 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 }
579 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
580 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000581 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
583 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000584 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000585 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 case tok::ampamp: { // unary-expression: '&&' identifier
587 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000588 if (Tok.isNot(tok::identifier))
589 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000592 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 Tok.getIdentifierInfo());
594 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000595 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 }
597 case tok::kw_const_cast:
598 case tok::kw_dynamic_cast:
599 case tok::kw_reinterpret_cast:
600 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000601 Res = ParseCXXCasts();
602 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000603 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000604 case tok::kw_typeid:
605 Res = ParseCXXTypeid();
606 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000607 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000608 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000609 Res = ParseCXXThis();
610 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000611 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000612
613 case tok::kw_char:
614 case tok::kw_wchar_t:
615 case tok::kw_bool:
616 case tok::kw_short:
617 case tok::kw_int:
618 case tok::kw_long:
619 case tok::kw_signed:
620 case tok::kw_unsigned:
621 case tok::kw_float:
622 case tok::kw_double:
623 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000624 case tok::kw_typeof:
625 case tok::annot_qualtypename: {
626 if (!getLang().CPlusPlus) {
627 Diag(Tok, diag::err_expected_expression);
628 return ExprError();
629 }
630
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000631 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
632 //
633 DeclSpec DS;
634 ParseCXXSimpleTypeSpecifier(DS);
635 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000636 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
637 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000638
639 Res = ParseCXXTypeConstructExpression(DS);
640 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000641 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000642 }
643
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000644 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
645 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
646 // template-id
647 Res = ParseCXXIdExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000648 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000649
Chris Lattner74ba4102009-01-04 22:52:14 +0000650 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000651 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
652 // annotates the token, tail recurse.
653 if (TryAnnotateTypeOrScopeToken())
654 return ParseCastExpression(isUnaryExpression);
655
Chris Lattner74ba4102009-01-04 22:52:14 +0000656 // ::new -> [C++] new-expression
657 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000658 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000659 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000660 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000661 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000662 return ParseCXXDeleteExpression(true, CCLoc);
663
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000664 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000665 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000666 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000667 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000668
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000669 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000670 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000671
672 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000673 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000674
Sebastian Redl64b45f72009-01-05 20:52:13 +0000675 case tok::kw___is_pod: // [GNU] unary-type-trait
676 case tok::kw___is_class:
677 case tok::kw___is_enum:
678 case tok::kw___is_union:
679 case tok::kw___is_polymorphic:
680 return ParseUnaryTypeTrait();
681
Chris Lattnerc97c2042007-10-03 22:03:06 +0000682 case tok::at: {
683 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000684 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000685 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000686 case tok::caret:
687 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000688 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000689 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000690 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000691 case tok::l_square:
692 // These can be followed by postfix-expr pieces.
693 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000694 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000695 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 default:
697 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000698 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // unreachable.
702 abort();
703}
704
705/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
706/// is parsed, this method parses any suffixes that apply.
707///
708/// postfix-expression: [C99 6.5.2]
709/// primary-expression
710/// postfix-expression '[' expression ']'
711/// postfix-expression '(' argument-expression-list[opt] ')'
712/// postfix-expression '.' identifier
713/// postfix-expression '->' identifier
714/// postfix-expression '++'
715/// postfix-expression '--'
716/// '(' type-name ')' '{' initializer-list '}'
717/// '(' type-name ')' '{' initializer-list ',' '}'
718///
719/// argument-expression-list: [C99 6.5.2]
720/// argument-expression
721/// argument-expression-list ',' assignment-expression
722///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000723Parser::OwningExprResult
724Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // Now that the primary-expression piece of the postfix-expression has been
726 // parsed, see if there are any postfix-expression pieces here.
727 SourceLocation Loc;
728 while (1) {
729 switch (Tok.getKind()) {
730 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000731 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
733 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000734 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000737
738 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000739 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.release(), Loc,
740 Idx.release(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000741 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000742 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
744 // Match the ']'.
745 MatchRHSPunctuation(tok::r_square, Loc);
746 break;
747 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000748
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000750 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000751 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000754
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000755 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000756 if (ParseExpressionList(ArgExprs, CommaLocs)) {
757 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000758 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
760 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000763 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
765 "Unexpected number of commas!");
Sebastian Redleffa8d12008-12-10 00:02:53 +0000766 LHS = Actions.ActOnCallExpr(CurScope, LHS.release(), Loc,
Douglas Gregor5c37de72008-12-06 00:22:45 +0000767 ArgExprs.take(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000768 ArgExprs.size(), &CommaLocs[0],
769 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000771
Chris Lattner2ff54262007-07-21 05:18:12 +0000772 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 break;
774 }
775 case tok::arrow: // postfix-expression: p-e '->' identifier
776 case tok::period: { // postfix-expression: p-e '.' identifier
777 tok::TokenKind OpKind = Tok.getKind();
778 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000779
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000780 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000782 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000784
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000785 if (!LHS.isInvalid()) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000786 LHS = Actions.ActOnMemberReferenceExpr(CurScope, LHS.release(), OpLoc,
787 OpKind, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 *Tok.getIdentifierInfo());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000789 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 ConsumeToken();
791 break;
792 }
793 case tok::plusplus: // postfix-expression: postfix-expression '++'
794 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000795 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000796 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000797 Tok.getKind(), LHS.release());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000798 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 ConsumeToken();
800 break;
801 }
802 }
803}
804
805
806/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
807/// unary-expression: [C99 6.5.3]
808/// 'sizeof' unary-expression
809/// 'sizeof' '(' type-name ')'
810/// [GNU] '__alignof' unary-expression
811/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000812/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000813Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000814 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
815 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000817 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 ConsumeToken();
819
820 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000821 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000822 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 Operand = ParseCastExpression(true);
824 } else {
825 // If it starts with a '(', we know that it is either a parenthesized
826 // type-name, or it is a unary-expression that starts with a compound
827 // literal, or starts with a primary-expression that is a parenthesized
828 // expression.
829 ParenParseOption ExprType = CastExpr;
830 TypeTy *CastTy;
831 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
832 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
835 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000836 if (ExprType == CastExpr)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000837 return Owned(Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000838 OpTok.is(tok::kw_sizeof),
839 /*isType=*/true, CastTy,
Sebastian Redld8c4e152008-12-11 22:33:27 +0000840 SourceRange(LParenLoc, RParenLoc)));
841
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000842 // If this is a parenthesized expression, it is the start of a
843 // unary-expression, but doesn't include any postfix pieces. Parse these
844 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000845 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000849 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000850 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
851 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000852 /*isType=*/false,
853 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000854 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855}
856
857/// ParseBuiltinPrimaryExpression
858///
859/// primary-expression: [C99 6.5.1]
860/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
861/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
862/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
863/// assign-expr ')'
864/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000865/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000866///
867/// [GNU] offsetof-member-designator:
868/// [GNU] identifier
869/// [GNU] offsetof-member-designator '.' identifier
870/// [GNU] offsetof-member-designator '[' expression ']'
871///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000872Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000873 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
875
876 tok::TokenKind T = Tok.getKind();
877 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
878
879 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000880 if (Tok.isNot(tok::l_paren))
881 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
882 << BuiltinII);
883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 SourceLocation LParenLoc = ConsumeParen();
885 // TODO: Build AST.
886
887 switch (T) {
888 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000889 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000890 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000891 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000893 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 }
895
896 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000897 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000898
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000899 TypeTy *Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000900
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000901 if (Tok.isNot(tok::r_paren)) {
902 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000903 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000904 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000905 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000907 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000908 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000909 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000910 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000911
912 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000913 return ExprError();
914
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000916 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000917 Diag(Tok, diag::err_expected_ident);
918 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000919 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000920 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000921
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000922 // Keep track of the various subcomponents we see.
923 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +0000924
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000925 Comps.push_back(Action::OffsetOfComponent());
926 Comps.back().isBrackets = false;
927 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
928 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000929
Sebastian Redla55e52c2008-11-25 22:21:31 +0000930 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000934 Comps.push_back(Action::OffsetOfComponent());
935 Comps.back().isBrackets = false;
936 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000937
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000938 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000939 Diag(Tok, diag::err_expected_ident);
940 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000941 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000942 }
943 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
944 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000945
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000946 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000948 Comps.push_back(Action::OffsetOfComponent());
949 Comps.back().isBrackets = true;
950 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000952 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000954 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000956 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000957
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000958 Comps.back().LocEnd =
959 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000960 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000961 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, Ty,
962 &Comps[0], Comps.size(),
963 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +0000964 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000966 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000967 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 }
969 }
970 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000971 }
Steve Naroffd04fdd52007-08-03 21:21:27 +0000972 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000973 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000974 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000975 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000976 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +0000977 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000979 return ExprError();
980
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000981 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000983 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000984 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000985 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000987 return ExprError();
988
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000989 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000990 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000991 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000992 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000993 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000994 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000995 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000996 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +0000997 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000998 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
999 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001000 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001001 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001002 case tok::kw___builtin_overload: {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001003 ExprVector ArgExprs(Actions);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001004 llvm::SmallVector<SourceLocation, 8> CommaLocs;
1005
1006 // For each iteration through the loop look for assign-expr followed by a
1007 // comma. If there is no comma, break and attempt to match r-paren.
1008 if (Tok.isNot(tok::r_paren)) {
1009 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001010 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001011 if (ArgExpr.isInvalid()) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00001012 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001013 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001014 } else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001015 ArgExprs.push_back(ArgExpr.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001016
Nate Begemane2ce1d92008-01-17 17:46:27 +00001017 if (Tok.isNot(tok::comma))
1018 break;
1019 // Move to the next argument, remember where the comma was.
1020 CommaLocs.push_back(ConsumeToken());
1021 }
1022 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001023
Nate Begemane2ce1d92008-01-17 17:46:27 +00001024 // Attempt to consume the r-paren
1025 if (Tok.isNot(tok::r_paren)) {
1026 Diag(Tok, diag::err_expected_rparen);
1027 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001028 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001029 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001030 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begemane2ce1d92008-01-17 17:46:27 +00001031 &CommaLocs[0], StartLoc, ConsumeParen());
1032 break;
1033 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +00001035 TypeTy *Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001036
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001038 return ExprError();
1039
Steve Naroff363bcff2007-08-01 23:45:51 +00001040 TypeTy *Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001041
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001042 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001043 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001044 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001045 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001046 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001047 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001048 }
1049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 // These can be followed by postfix-expr pieces because they are
1051 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001052 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001053}
1054
1055/// ParseParenExpression - This parses the unit that starts with a '(' token,
1056/// based on what is allowed by ExprType. The actual thing parsed is returned
1057/// in ExprType.
1058///
1059/// primary-expression: [C99 6.5.1]
1060/// '(' expression ')'
1061/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1062/// postfix-expression: [C99 6.5.2]
1063/// '(' type-name ')' '{' initializer-list '}'
1064/// '(' type-name ')' '{' initializer-list ',' '}'
1065/// cast-expression: [C99 6.5.4]
1066/// '(' type-name ')' cast-expression
1067///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001068Parser::OwningExprResult
1069Parser::ParseParenExpression(ParenParseOption &ExprType,
1070 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001071 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001073 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001075
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001076 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001078 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001080
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001081 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001082 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1083 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001084 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001085
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001086 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 // Otherwise, this is a compound literal expression or cast expression.
1088 TypeTy *Ty = ParseTypeName();
1089
1090 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001091 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 RParenLoc = ConsumeParen();
1093 else
1094 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001095
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001096 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 if (!getLang().C99) // Compound literals don't exist in C90.
1098 Diag(OpenLoc, diag::ext_c99_compound_literal);
1099 Result = ParseInitializer();
1100 ExprType = CompoundLiteral;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001101 if (!Result.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +00001102 return Owned(Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
1103 Result.release()));
Chris Lattner42ece642008-12-12 06:00:12 +00001104 return move(Result);
1105 }
1106
1107 if (ExprType == CastExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 // Note that this doesn't parse the subsequence cast-expression, it just
1109 // returns the parsed type to the callee.
1110 ExprType = CastExpr;
1111 CastTy = Ty;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001112 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 }
Chris Lattner42ece642008-12-12 06:00:12 +00001114
1115 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1116 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 } else {
1118 Result = ParseExpression();
1119 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001120 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Chris Lattner42ece642008-12-12 06:00:12 +00001121 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(),
1122 Result.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001124
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001126 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001128 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 }
Chris Lattner42ece642008-12-12 06:00:12 +00001130
1131 if (Tok.is(tok::r_paren))
1132 RParenLoc = ConsumeParen();
1133 else
1134 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001135
1136 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001137}
1138
1139/// ParseStringLiteralExpression - This handles the various token types that
1140/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1141/// translation phase #6].
1142///
1143/// primary-expression: [C99 6.5.1]
1144/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001145Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001147
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1149 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001150 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001151
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 do {
1153 StringToks.push_back(Tok);
1154 ConsumeStringToken();
1155 } while (isTokenStringLiteral());
1156
1157 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redl20df9b72008-12-11 22:51:44 +00001158 return Owned(Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001159}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001160
1161/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1162///
1163/// argument-expression-list:
1164/// assignment-expression
1165/// argument-expression-list , assignment-expression
1166///
1167/// [C++] expression-list:
1168/// [C++] assignment-expression
1169/// [C++] expression-list , assignment-expression
1170///
1171bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1172 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001173 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001174 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001175 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001176
Sebastian Redleffa8d12008-12-10 00:02:53 +00001177 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001178
1179 if (Tok.isNot(tok::comma))
1180 return false;
1181 // Move to the next argument, remember where the comma was.
1182 CommaLocs.push_back(ConsumeToken());
1183 }
1184}
Steve Naroff296e8d52008-08-28 19:20:44 +00001185
1186/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001187/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001188///
1189/// block-literal:
1190/// [clang] '^' block-args[opt] compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001191/// [clang] block-args:
1192/// [clang] '(' parameter-list ')'
1193///
Sebastian Redl1d922962008-12-13 15:32:12 +00001194Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001195 assert(Tok.is(tok::caret) && "block literal starts with ^");
1196 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001197
Steve Naroff296e8d52008-08-28 19:20:44 +00001198 // Enter a scope to hold everything within the block. This includes the
1199 // argument decls, decls within the compound expression, etc. This also
1200 // allows determining whether a variable reference inside the block is
1201 // within or outside of the block.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001202 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1203 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001204
1205 // Inform sema that we are starting a block.
1206 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001207
Steve Naroff296e8d52008-08-28 19:20:44 +00001208 // Parse the return type if present.
1209 DeclSpec DS;
1210 Declarator ParamInfo(DS, Declarator::PrototypeContext);
Sebastian Redl1d922962008-12-13 15:32:12 +00001211
Steve Naroff296e8d52008-08-28 19:20:44 +00001212 // If this block has arguments, parse them. There is no ambiguity here with
1213 // the expression case, because the expression case requires a parameter list.
1214 if (Tok.is(tok::l_paren)) {
1215 ParseParenDeclarator(ParamInfo);
1216 // Parse the pieces after the identifier as if we had "int(...)".
1217 ParamInfo.SetIdentifier(0, CaretLoc);
1218 if (ParamInfo.getInvalidType()) {
1219 // If there was an error parsing the arguments, they may have tried to use
1220 // ^(x+y) which requires an argument list. Just skip the whole block
1221 // literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001222 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001223 }
1224 } else {
1225 // Otherwise, pretend we saw (void).
1226 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001227 0, 0, 0, CaretLoc));
Steve Naroff296e8d52008-08-28 19:20:44 +00001228 }
1229
1230 // Inform sema that we are starting a block.
Steve Naroff090276f2008-10-10 01:28:17 +00001231 Actions.ActOnBlockArguments(ParamInfo);
Sebastian Redl1d922962008-12-13 15:32:12 +00001232
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001233 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001234 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001235 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001236 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001237 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001238 } else {
1239 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001240 }
1241 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001242 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001243}
1244