blob: 64c4c31fc46147a469b5a76a454be7ed96767c81 [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
Eli Friedmanadf077f2009-01-27 08:43:38 +0000195/// This routine is called when a leading '__extension__' is seen and
196/// consumed. This is necessary because the token gets consumed in the
197/// process of disambiguating between an expression and a declaration.
198Parser::OwningExprResult
199Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
200 // FIXME: The handling for throw is almost certainly wrong.
201 if (Tok.is(tok::kw_throw))
202 return ParseThrowExpression();
203
204 OwningExprResult LHS(ParseCastExpression(false));
205 if (LHS.isInvalid()) return move(LHS);
206
207 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
208 move_arg(LHS));
209 if (LHS.isInvalid()) return move(LHS);
210
211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
215///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000216Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000217 if (Tok.is(tok::kw_throw))
Sebastian Redl20df9b72008-12-11 22:51:44 +0000218 return ParseThrowExpression();
Chris Lattner50dd2892008-02-26 00:51:44 +0000219
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000220 OwningExprResult LHS(ParseCastExpression(false));
221 if (LHS.isInvalid()) return move(LHS);
222
Sebastian Redld8c4e152008-12-11 22:33:27 +0000223 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224}
225
Chris Lattnerb93fb492008-06-02 21:31:07 +0000226/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
227/// where part of an objc message send has already been parsed. In this case
228/// LBracLoc indicates the location of the '[' of the message send, and either
229/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
230/// message.
231///
232/// Since this handles full assignment-expression's, it handles postfix
233/// expressions and other binary operators for these expressions as well.
Sebastian Redl1d922962008-12-13 15:32:12 +0000234Parser::OwningExprResult
Chris Lattnerb93fb492008-06-02 21:31:07 +0000235Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000236 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000237 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +0000238 ExprArg ReceiverExpr) {
239 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
240 ReceiverName,
241 move(ReceiverExpr)));
242 if (R.isInvalid()) return move(R);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000243 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redl1d922962008-12-13 15:32:12 +0000244 if (R.isInvalid()) return move(R);
245 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000246}
247
248
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000249Parser::OwningExprResult Parser::ParseConstantExpression() {
250 OwningExprResult LHS(ParseCastExpression(false));
251 if (LHS.isInvalid()) return move(LHS);
252
Sebastian Redld8c4e152008-12-11 22:33:27 +0000253 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254}
255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
257/// LHS and has a precedence of at least MinPrec.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000258Parser::OwningExprResult
259Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
261 SourceLocation ColonLoc;
262
263 while (1) {
264 // If this token has a lower precedence than we are allowed to parse (e.g.
265 // because we are called recursively, or because the token is not a binop),
266 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000267 if (NextTokPrec < MinPrec)
Sebastian Redld8c4e152008-12-11 22:33:27 +0000268 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
270 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000271 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 ConsumeToken();
273
274 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000275 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000277 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // Handle this production specially:
279 // logical-OR-expression '?' expression ':' conditional-expression
280 // In particular, the RHS of the '?' is 'expression', not
281 // 'logical-OR-expression' as we might expect.
282 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000283 if (TernaryMiddle.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000284 return move(TernaryMiddle);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 } else {
286 // Special case handling of "X ? Y : Z" where Y is empty:
287 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000288 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 Diag(Tok, diag::ext_gnu_conditional_expr);
290 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000291
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000292 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000294 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redld8c4e152008-12-11 22:33:27 +0000295 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000297
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 // Eat the colon.
299 ColonLoc = ConsumeToken();
300 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000303 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000304 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000305 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
307 // Remember the precedence of this operator and get the precedence of the
308 // operator immediately to the right of the RHS.
309 unsigned ThisPrec = NextTokPrec;
310 NextTokPrec = getBinOpPrecedence(Tok.getKind());
311
312 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000313 bool isRightAssoc = ThisPrec == prec::Conditional ||
314 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315
316 // Get the precedence of the operator to the right of the RHS. If it binds
317 // more tightly with RHS than we do, evaluate it completely first.
318 if (ThisPrec < NextTokPrec ||
319 (ThisPrec == NextTokPrec && isRightAssoc)) {
320 // If this is left-associative, only parse things on the RHS that bind
321 // more tightly than the current operator. If it is left-associative, it
322 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
323 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000324 // The function takes ownership of the RHS.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000325 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000326 if (RHS.isInvalid())
Sebastian Redld8c4e152008-12-11 22:33:27 +0000327 return move(RHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000328
329 NextTokPrec = getBinOpPrecedence(Tok.getKind());
330 }
331 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000332
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000333 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000334 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000335 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000336 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000337 OpToken.getKind(), move_arg(LHS),
338 move_arg(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000339 else
Steve Narofff69936d2007-09-16 03:34:24 +0000340 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000341 move_arg(LHS), move_arg(TernaryMiddle),
342 move_arg(RHS));
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000343 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
345}
346
347/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redlebc07d52009-02-03 20:19:35 +0000348/// true, parse a unary-expression. isAddressOfOperand exists because an
349/// id-expression that is the operand of address-of gets special treatment
350/// due to member pointers.
Reid Spencer5f016e22007-07-11 17:01:13 +0000351///
352/// cast-expression: [C99 6.5.4]
353/// unary-expression
354/// '(' type-name ')' cast-expression
355///
356/// unary-expression: [C99 6.5.3]
357/// postfix-expression
358/// '++' unary-expression
359/// '--' unary-expression
360/// unary-operator cast-expression
361/// 'sizeof' unary-expression
362/// 'sizeof' '(' type-name ')'
363/// [GNU] '__alignof' unary-expression
364/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000365/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000366/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000367/// [C++] new-expression
368/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000369///
370/// unary-operator: one of
371/// '&' '*' '+' '-' '~' '!'
372/// [GNU] '__extension__' '__real' '__imag'
373///
374/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000375/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000376/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// constant
378/// string-literal
379/// [C++] boolean-literal [C++ 2.13.5]
380/// '(' expression ')'
381/// '__func__' [C99 6.4.2.2]
382/// [GNU] '__FUNCTION__'
383/// [GNU] '__PRETTY_FUNCTION__'
384/// [GNU] '(' compound-statement ')'
385/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
386/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
387/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
388/// assign-expr ')'
389/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000390/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000391/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000392/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000393/// [OBJC] '@protocol' '(' identifier ')'
394/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000395/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000396/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
397/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000398/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
399/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
400/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
401/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000402/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
403/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000404/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl64b45f72009-01-05 20:52:13 +0000405/// [G++] unary-type-trait '(' type-id ')'
406/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Naroff296e8d52008-08-28 19:20:44 +0000407/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000408///
409/// constant: [C99 6.4.4]
410/// integer-constant
411/// floating-constant
412/// enumeration-constant -> identifier
413/// character-constant
414///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000415/// id-expression: [C++ 5.1]
416/// unqualified-id
417/// qualified-id [TODO]
418///
419/// unqualified-id: [C++ 5.1]
420/// identifier
421/// operator-function-id
422/// conversion-function-id [TODO]
423/// '~' class-name [TODO]
424/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000425///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000426/// new-expression: [C++ 5.3.4]
427/// '::'[opt] 'new' new-placement[opt] new-type-id
428/// new-initializer[opt]
429/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
430/// new-initializer[opt]
431///
432/// delete-expression: [C++ 5.3.5]
433/// '::'[opt] 'delete' cast-expression
434/// '::'[opt] 'delete' '[' ']' cast-expression
435///
Sebastian Redl64b45f72009-01-05 20:52:13 +0000436/// [GNU] unary-type-trait:
437/// '__has_nothrow_assign' [TODO]
438/// '__has_nothrow_copy' [TODO]
439/// '__has_nothrow_constructor' [TODO]
440/// '__has_trivial_assign' [TODO]
441/// '__has_trivial_copy' [TODO]
442/// '__has_trivial_constructor' [TODO]
443/// '__has_trivial_destructor' [TODO]
444/// '__has_virtual_destructor' [TODO]
445/// '__is_abstract' [TODO]
446/// '__is_class'
447/// '__is_empty' [TODO]
448/// '__is_enum'
449/// '__is_pod'
450/// '__is_polymorphic'
451/// '__is_union'
452///
453/// [GNU] binary-type-trait:
454/// '__is_base_of' [TODO]
455///
Sebastian Redlebc07d52009-02-03 20:19:35 +0000456Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
457 bool isAddressOfOperand) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000458 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 tok::TokenKind SavedKind = Tok.getKind();
460
461 // This handles all of cast-expression, unary-expression, postfix-expression,
462 // and primary-expression. We handle them together like this for efficiency
463 // and to simplify handling of an expression starting with a '(' token: which
464 // may be one of a parenthesized expression, cast-expression, compound literal
465 // expression, or statement expression.
466 //
467 // If the parsed tokens consist of a primary-expression, the cases below
468 // call ParsePostfixExpressionSuffix to handle the postfix expression
469 // suffixes. Cases that cannot be followed by postfix exprs should
470 // return without invoking ParsePostfixExpressionSuffix.
471 switch (SavedKind) {
472 case tok::l_paren: {
473 // If this expression is limited to being a unary-expression, the parent can
474 // not start a cast expression.
475 ParenParseOption ParenExprType =
476 isUnaryExpression ? CompoundLiteral : CastExpr;
477 TypeTy *CastTy;
478 SourceLocation LParenLoc = Tok.getLocation();
479 SourceLocation RParenLoc;
480 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000481 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482
483 switch (ParenExprType) {
484 case SimpleExpr: break; // Nothing else to do.
485 case CompoundStmt: break; // Nothing else to do.
486 case CompoundLiteral:
487 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
488 // postfix-expression exist, parse them now.
489 break;
490 case CastExpr:
491 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
492 // the cast-expression that follows it next.
493 // TODO: For cast expression with CastTy.
494 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000495 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000496 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,
Sebastian Redlb8a6aca2009-01-19 22:31:54 +0000497 move_arg(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000498 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000500
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000502 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // primary-expression
506 case tok::numeric_constant:
507 // constant: integer-constant
508 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000509
Steve Narofff69936d2007-09-16 03:34:24 +0000510 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000512
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000514 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000515
516 case tok::kw_true:
517 case tok::kw_false:
Sebastian Redl20df9b72008-12-11 22:51:44 +0000518 return ParseCXXBoolLiteral();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000520 case tok::identifier: { // primary-expression: identifier
521 // unqualified-id: identifier
522 // constant: enumeration-constant
Chris Lattnerb31757b2009-01-06 05:06:21 +0000523 // Turn a potentially qualified name into a annot_typename or
Chris Lattner74ba4102009-01-04 22:52:14 +0000524 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000525 if (getLang().CPlusPlus) {
Chris Lattnere26ff022009-01-04 23:46:59 +0000526 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
527 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000528 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000529 }
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // Consume the identifier so that we can see if it is followed by a '('.
532 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
533 // need to know whether or not this identifier is a function designator or
534 // not.
535 IdentifierInfo &II = *Tok.getIdentifierInfo();
536 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000537 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000539 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 }
541 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000542 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 ConsumeToken();
544 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000545 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
547 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
548 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000549 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 ConsumeToken();
551 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000552 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 case tok::string_literal: // primary-expression: string-literal
554 case tok::wide_string_literal:
555 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000556 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000558 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 case tok::kw___builtin_va_arg:
560 case tok::kw___builtin_offsetof:
561 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000562 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 case tok::kw___builtin_types_compatible_p:
Sebastian Redld8c4e152008-12-11 22:33:27 +0000564 return ParseBuiltinPrimaryExpression();
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000565 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000566 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000567 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 case tok::plusplus: // unary-expression: '++' unary-expression
569 case tok::minusminus: { // unary-expression: '--' unary-expression
570 SourceLocation SavedLoc = ConsumeToken();
571 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000572 if (!Res.isInvalid())
Sebastian Redl0eb23302009-01-19 00:08:26 +0000573 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move_arg(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000574 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 }
Sebastian Redlebc07d52009-02-03 20:19:35 +0000576 case tok::amp: { // unary-expression: '&' cast-expression
577 // Special treatment because of member pointers
578 SourceLocation SavedLoc = ConsumeToken();
579 Res = ParseCastExpression(false, true);
580 if (!Res.isInvalid())
581 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move_arg(Res));
582 return move(Res);
583 }
584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 case tok::star: // unary-expression: '*' cast-expression
586 case tok::plus: // unary-expression: '+' cast-expression
587 case tok::minus: // unary-expression: '-' cast-expression
588 case tok::tilde: // unary-expression: '~' cast-expression
589 case tok::exclaim: // unary-expression: '!' cast-expression
590 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000591 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 SourceLocation SavedLoc = ConsumeToken();
593 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000594 if (!Res.isInvalid())
Sebastian Redl0eb23302009-01-19 00:08:26 +0000595 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move_arg(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000596 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000597 }
598
Chris Lattner35080842008-02-02 20:20:10 +0000599 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
600 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000601 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000602 SourceLocation SavedLoc = ConsumeToken();
603 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000604 if (!Res.isInvalid())
Sebastian Redl0eb23302009-01-19 00:08:26 +0000605 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move_arg(Res));
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000606 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 }
608 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
609 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000610 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
612 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000613 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000614 return ParseSizeofAlignofExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 case tok::ampamp: { // unary-expression: '&&' identifier
616 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000617 if (Tok.isNot(tok::identifier))
618 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000621 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 Tok.getIdentifierInfo());
623 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000624 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 }
626 case tok::kw_const_cast:
627 case tok::kw_dynamic_cast:
628 case tok::kw_reinterpret_cast:
629 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000630 Res = ParseCXXCasts();
631 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000632 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000633 case tok::kw_typeid:
634 Res = ParseCXXTypeid();
635 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000636 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000637 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000638 Res = ParseCXXThis();
639 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000640 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000641
642 case tok::kw_char:
643 case tok::kw_wchar_t:
644 case tok::kw_bool:
645 case tok::kw_short:
646 case tok::kw_int:
647 case tok::kw_long:
648 case tok::kw_signed:
649 case tok::kw_unsigned:
650 case tok::kw_float:
651 case tok::kw_double:
652 case tok::kw_void:
Chris Lattner2dcaab32009-01-04 22:28:21 +0000653 case tok::kw_typeof:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000654 case tok::annot_typename: {
Chris Lattner2dcaab32009-01-04 22:28:21 +0000655 if (!getLang().CPlusPlus) {
656 Diag(Tok, diag::err_expected_expression);
657 return ExprError();
658 }
659
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000660 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
661 //
662 DeclSpec DS;
663 ParseCXXSimpleTypeSpecifier(DS);
664 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000665 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
666 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000667
668 Res = ParseCXXTypeConstructExpression(DS);
669 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000670 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000671 }
672
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000673 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
674 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
675 // template-id
Sebastian Redlebc07d52009-02-03 20:19:35 +0000676 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000677 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000678
Chris Lattner74ba4102009-01-04 22:52:14 +0000679 case tok::coloncolon: {
Chris Lattner5b454732009-01-05 03:55:46 +0000680 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
681 // annotates the token, tail recurse.
682 if (TryAnnotateTypeOrScopeToken())
Sebastian Redlebc07d52009-02-03 20:19:35 +0000683 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
684
Chris Lattner74ba4102009-01-04 22:52:14 +0000685 // ::new -> [C++] new-expression
686 // ::delete -> [C++] delete-expression
Chris Lattner5b454732009-01-05 03:55:46 +0000687 SourceLocation CCLoc = ConsumeToken();
Chris Lattner59232d32009-01-04 21:25:24 +0000688 if (Tok.is(tok::kw_new))
Chris Lattner5b454732009-01-05 03:55:46 +0000689 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner74ba4102009-01-04 22:52:14 +0000690 if (Tok.is(tok::kw_delete))
Chris Lattner5b454732009-01-05 03:55:46 +0000691 return ParseCXXDeleteExpression(true, CCLoc);
692
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000693 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner5b454732009-01-05 03:55:46 +0000694 Diag(CCLoc, diag::err_expected_expression);
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000695 return ExprError();
Chris Lattner59232d32009-01-04 21:25:24 +0000696 }
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000697
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000698 case tok::kw_new: // [C++] new-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000699 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000700
701 case tok::kw_delete: // [C++] delete-expression
Chris Lattner59232d32009-01-04 21:25:24 +0000702 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000703
Sebastian Redl64b45f72009-01-05 20:52:13 +0000704 case tok::kw___is_pod: // [GNU] unary-type-trait
705 case tok::kw___is_class:
706 case tok::kw___is_enum:
707 case tok::kw___is_union:
708 case tok::kw___is_polymorphic:
709 return ParseUnaryTypeTrait();
710
Chris Lattnerc97c2042007-10-03 22:03:06 +0000711 case tok::at: {
712 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +0000713 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000714 }
Steve Naroff296e8d52008-08-28 19:20:44 +0000715 case tok::caret:
716 if (getLang().Blocks)
Sebastian Redl1d922962008-12-13 15:32:12 +0000717 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Naroff296e8d52008-08-28 19:20:44 +0000718 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000719 return ExprError();
Chris Lattnerfdb548e2008-12-12 19:20:14 +0000720 case tok::l_square:
721 // These can be followed by postfix-expr pieces.
722 if (getLang().ObjC1)
Sebastian Redl1d922962008-12-13 15:32:12 +0000723 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner2dcaab32009-01-04 22:28:21 +0000724 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 default:
726 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000727 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // unreachable.
731 abort();
732}
733
734/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
735/// is parsed, this method parses any suffixes that apply.
736///
737/// postfix-expression: [C99 6.5.2]
738/// primary-expression
739/// postfix-expression '[' expression ']'
740/// postfix-expression '(' argument-expression-list[opt] ')'
741/// postfix-expression '.' identifier
742/// postfix-expression '->' identifier
743/// postfix-expression '++'
744/// postfix-expression '--'
745/// '(' type-name ')' '{' initializer-list '}'
746/// '(' type-name ')' '{' initializer-list ',' '}'
747///
748/// argument-expression-list: [C99 6.5.2]
749/// argument-expression
750/// argument-expression-list ',' assignment-expression
751///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000752Parser::OwningExprResult
753Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 // Now that the primary-expression piece of the postfix-expression has been
755 // parsed, see if there are any postfix-expression pieces here.
756 SourceLocation Loc;
757 while (1) {
758 switch (Tok.getKind()) {
759 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000760 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
762 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000763 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000766
767 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl0eb23302009-01-19 00:08:26 +0000768 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move_arg(LHS), Loc,
769 move_arg(Idx), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000770 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000771 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000772
773 // Match the ']'.
774 MatchRHSPunctuation(tok::r_square, Loc);
775 break;
776 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000777
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000779 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000780 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000781
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000783
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000784 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000785 if (ParseExpressionList(ArgExprs, CommaLocs)) {
786 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000787 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 }
789 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000792 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
794 "Unexpected number of commas!");
Sebastian Redl0eb23302009-01-19 00:08:26 +0000795 LHS = Actions.ActOnCallExpr(CurScope, move_arg(LHS), Loc,
796 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redla55e52c2008-11-25 22:21:31 +0000797 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000799
Chris Lattner2ff54262007-07-21 05:18:12 +0000800 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 break;
802 }
803 case tok::arrow: // postfix-expression: p-e '->' identifier
804 case tok::period: { // postfix-expression: p-e '.' identifier
805 tok::TokenKind OpKind = Tok.getKind();
806 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000807
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000808 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000810 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000812
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000813 if (!LHS.isInvalid()) {
Sebastian Redl0eb23302009-01-19 00:08:26 +0000814 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move_arg(LHS), OpLoc,
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000815 OpKind, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 *Tok.getIdentifierInfo());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000817 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 ConsumeToken();
819 break;
820 }
821 case tok::plusplus: // postfix-expression: postfix-expression '++'
822 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000823 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000824 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl0eb23302009-01-19 00:08:26 +0000825 Tok.getKind(), move_arg(LHS));
Sebastian Redla55e52c2008-11-25 22:21:31 +0000826 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 ConsumeToken();
828 break;
829 }
830 }
831}
832
833
834/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
835/// unary-expression: [C99 6.5.3]
836/// 'sizeof' unary-expression
837/// 'sizeof' '(' type-name ')'
838/// [GNU] '__alignof' unary-expression
839/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000840/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redld8c4e152008-12-11 22:33:27 +0000841Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000842 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
843 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000845 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 ConsumeToken();
847
848 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000849 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000850 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 Operand = ParseCastExpression(true);
852 } else {
853 // If it starts with a '(', we know that it is either a parenthesized
854 // type-name, or it is a unary-expression that starts with a compound
855 // literal, or starts with a primary-expression that is a parenthesized
856 // expression.
857 ParenParseOption ExprType = CastExpr;
858 TypeTy *CastTy;
859 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
860 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000861
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
863 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000864 if (ExprType == CastExpr)
Sebastian Redl0eb23302009-01-19 00:08:26 +0000865 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl05189992008-11-11 17:56:53 +0000866 OpTok.is(tok::kw_sizeof),
867 /*isType=*/true, CastTy,
Sebastian Redl0eb23302009-01-19 00:08:26 +0000868 SourceRange(LParenLoc, RParenLoc));
Sebastian Redld8c4e152008-12-11 22:33:27 +0000869
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000870 // If this is a parenthesized expression, it is the start of a
871 // unary-expression, but doesn't include any postfix pieces. Parse these
872 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000873 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000877 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000878 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
879 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000880 /*isType=*/false,
881 Operand.release(), SourceRange());
Sebastian Redld8c4e152008-12-11 22:33:27 +0000882 return move(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883}
884
885/// ParseBuiltinPrimaryExpression
886///
887/// primary-expression: [C99 6.5.1]
888/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
889/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
890/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
891/// assign-expr ')'
892/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000893/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000894///
895/// [GNU] offsetof-member-designator:
896/// [GNU] identifier
897/// [GNU] offsetof-member-designator '.' identifier
898/// [GNU] offsetof-member-designator '[' expression ']'
899///
Sebastian Redld8c4e152008-12-11 22:33:27 +0000900Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000901 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
903
904 tok::TokenKind T = Tok.getKind();
905 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
906
907 // All of these start with an open paren.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000908 if (Tok.isNot(tok::l_paren))
909 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
910 << BuiltinII);
911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 SourceLocation LParenLoc = ConsumeParen();
913 // TODO: Build AST.
914
915 switch (T) {
916 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000917 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000918 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000919 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000921 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 }
923
924 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000925 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000927 TypeTy *Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000928
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000929 if (Tok.isNot(tok::r_paren)) {
930 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000931 return ExprError();
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000932 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000933 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000935 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000936 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000937 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000938 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000939
940 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +0000941 return ExprError();
942
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000944 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000945 Diag(Tok, diag::err_expected_ident);
946 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000947 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000948 }
Sebastian Redld8c4e152008-12-11 22:33:27 +0000949
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000950 // Keep track of the various subcomponents we see.
951 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redld8c4e152008-12-11 22:33:27 +0000952
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000953 Comps.push_back(Action::OffsetOfComponent());
954 Comps.back().isBrackets = false;
955 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
956 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000957
Sebastian Redla55e52c2008-11-25 22:21:31 +0000958 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000960 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000962 Comps.push_back(Action::OffsetOfComponent());
963 Comps.back().isBrackets = false;
964 Comps.back().LocStart = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000965
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000966 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000967 Diag(Tok, diag::err_expected_ident);
968 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000969 return ExprError();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000970 }
971 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
972 Comps.back().LocEnd = ConsumeToken();
Sebastian Redld8c4e152008-12-11 22:33:27 +0000973
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000974 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000976 Comps.push_back(Action::OffsetOfComponent());
977 Comps.back().isBrackets = true;
978 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000980 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +0000982 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000984 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000985
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000986 Comps.back().LocEnd =
987 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000988 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000989 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, Ty,
990 &Comps[0], Comps.size(),
991 ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +0000992 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000994 // Error occurred.
Sebastian Redld8c4e152008-12-11 22:33:27 +0000995 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 }
997 }
998 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000999 }
Steve Naroffd04fdd52007-08-03 21:21:27 +00001000 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001001 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001002 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001003 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001004 return move(Cond);
Steve Naroffd04fdd52007-08-03 21:21:27 +00001005 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001007 return ExprError();
1008
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001009 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001010 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001011 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001012 return move(Expr1);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001013 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001015 return ExprError();
1016
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001017 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001018 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001019 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001020 return move(Expr2);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001021 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001022 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +00001023 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001024 return ExprError();
Steve Naroffd04fdd52007-08-03 21:21:27 +00001025 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001026 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1027 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001028 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +00001029 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001030 case tok::kw___builtin_overload: {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001031 ExprVector ArgExprs(Actions);
Nate Begemane2ce1d92008-01-17 17:46:27 +00001032 llvm::SmallVector<SourceLocation, 8> CommaLocs;
1033
1034 // For each iteration through the loop look for assign-expr followed by a
1035 // comma. If there is no comma, break and attempt to match r-paren.
1036 if (Tok.isNot(tok::r_paren)) {
1037 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001038 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001039 if (ArgExpr.isInvalid()) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00001040 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001041 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001042 } else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001043 ArgExprs.push_back(ArgExpr.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001044
Nate Begemane2ce1d92008-01-17 17:46:27 +00001045 if (Tok.isNot(tok::comma))
1046 break;
1047 // Move to the next argument, remember where the comma was.
1048 CommaLocs.push_back(ConsumeToken());
1049 }
1050 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001051
Nate Begemane2ce1d92008-01-17 17:46:27 +00001052 // Attempt to consume the r-paren
1053 if (Tok.isNot(tok::r_paren)) {
1054 Diag(Tok, diag::err_expected_rparen);
1055 SkipUntil(tok::r_paren);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001056 return ExprError();
Nate Begemane2ce1d92008-01-17 17:46:27 +00001057 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001058 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begemane2ce1d92008-01-17 17:46:27 +00001059 &CommaLocs[0], StartLoc, ConsumeParen());
1060 break;
1061 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +00001063 TypeTy *Ty1 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001064
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redld8c4e152008-12-11 22:33:27 +00001066 return ExprError();
1067
Steve Naroff363bcff2007-08-01 23:45:51 +00001068 TypeTy *Ty2 = ParseTypeName();
Sebastian Redld8c4e152008-12-11 22:33:27 +00001069
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001070 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001071 Diag(Tok, diag::err_expected_rparen);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001072 return ExprError();
Steve Naroff363bcff2007-08-01 23:45:51 +00001073 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001074 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001075 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001076 }
1077
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 // These can be followed by postfix-expr pieces because they are
1079 // primary-expressions.
Sebastian Redld8c4e152008-12-11 22:33:27 +00001080 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +00001081}
1082
1083/// ParseParenExpression - This parses the unit that starts with a '(' token,
1084/// based on what is allowed by ExprType. The actual thing parsed is returned
1085/// in ExprType.
1086///
1087/// primary-expression: [C99 6.5.1]
1088/// '(' expression ')'
1089/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1090/// postfix-expression: [C99 6.5.2]
1091/// '(' type-name ')' '{' initializer-list '}'
1092/// '(' type-name ')' '{' initializer-list ',' '}'
1093/// cast-expression: [C99 6.5.4]
1094/// '(' type-name ')' cast-expression
1095///
Sebastian Redld8c4e152008-12-11 22:33:27 +00001096Parser::OwningExprResult
1097Parser::ParseParenExpression(ParenParseOption &ExprType,
1098 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001099 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001101 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 CastTy = 0;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001103
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001104 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001106 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001108
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001109 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001110 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1111 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001112 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001113
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001114 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 // Otherwise, this is a compound literal expression or cast expression.
1116 TypeTy *Ty = ParseTypeName();
1117
1118 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001119 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 RParenLoc = ConsumeParen();
1121 else
1122 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001123
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001124 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 if (!getLang().C99) // Compound literals don't exist in C90.
1126 Diag(OpenLoc, diag::ext_c99_compound_literal);
1127 Result = ParseInitializer();
1128 ExprType = CompoundLiteral;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001129 if (!Result.isInvalid())
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001130 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
1131 move_arg(Result));
Chris Lattner42ece642008-12-12 06:00:12 +00001132 return move(Result);
1133 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001134
Chris Lattner42ece642008-12-12 06:00:12 +00001135 if (ExprType == CastExpr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // Note that this doesn't parse the subsequence cast-expression, it just
1137 // returns the parsed type to the callee.
1138 ExprType = CastExpr;
1139 CastTy = Ty;
Sebastian Redld8c4e152008-12-11 22:33:27 +00001140 return OwningExprResult(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 }
Sebastian Redlb8a6aca2009-01-19 22:31:54 +00001142
Chris Lattner42ece642008-12-12 06:00:12 +00001143 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1144 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 } else {
1146 Result = ParseExpression();
1147 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001148 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Chris Lattner42ece642008-12-12 06:00:12 +00001149 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(),
Sebastian Redlcd965b92009-01-18 18:53:16 +00001150 move_arg(Result));
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
Sebastian Redld8c4e152008-12-11 22:33:27 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Match the ')'.
Chris Lattner42ece642008-12-12 06:00:12 +00001154 if (Result.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 SkipUntil(tok::r_paren);
Chris Lattner42ece642008-12-12 06:00:12 +00001156 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
Chris Lattner42ece642008-12-12 06:00:12 +00001158
1159 if (Tok.is(tok::r_paren))
1160 RParenLoc = ConsumeParen();
1161 else
1162 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001163
1164 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165}
1166
1167/// ParseStringLiteralExpression - This handles the various token types that
1168/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1169/// translation phase #6].
1170///
1171/// primary-expression: [C99 6.5.1]
1172/// string-literal
Sebastian Redl20df9b72008-12-11 22:51:44 +00001173Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl20df9b72008-12-11 22:51:44 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1177 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001178 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl20df9b72008-12-11 22:51:44 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 do {
1181 StringToks.push_back(Tok);
1182 ConsumeStringToken();
1183 } while (isTokenStringLiteral());
1184
1185 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd965b92009-01-18 18:53:16 +00001186 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001187}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001188
1189/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1190///
1191/// argument-expression-list:
1192/// assignment-expression
1193/// argument-expression-list , assignment-expression
1194///
1195/// [C++] expression-list:
1196/// [C++] assignment-expression
1197/// [C++] expression-list , assignment-expression
1198///
1199bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1200 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001201 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001202 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001203 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001204
Sebastian Redleffa8d12008-12-10 00:02:53 +00001205 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001206
1207 if (Tok.isNot(tok::comma))
1208 return false;
1209 // Move to the next argument, remember where the comma was.
1210 CommaLocs.push_back(ConsumeToken());
1211 }
1212}
Steve Naroff296e8d52008-08-28 19:20:44 +00001213
1214/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001215/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001216///
1217/// block-literal:
1218/// [clang] '^' block-args[opt] compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001219/// [clang] block-args:
1220/// [clang] '(' parameter-list ')'
1221///
Sebastian Redl1d922962008-12-13 15:32:12 +00001222Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Naroff296e8d52008-08-28 19:20:44 +00001223 assert(Tok.is(tok::caret) && "block literal starts with ^");
1224 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001225
Steve Naroff296e8d52008-08-28 19:20:44 +00001226 // Enter a scope to hold everything within the block. This includes the
1227 // argument decls, decls within the compound expression, etc. This also
1228 // allows determining whether a variable reference inside the block is
1229 // within or outside of the block.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001230 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1231 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001232
1233 // Inform sema that we are starting a block.
1234 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redl1d922962008-12-13 15:32:12 +00001235
Steve Naroff296e8d52008-08-28 19:20:44 +00001236 // Parse the return type if present.
1237 DeclSpec DS;
1238 Declarator ParamInfo(DS, Declarator::PrototypeContext);
Sebastian Redl1d922962008-12-13 15:32:12 +00001239
Steve Naroff296e8d52008-08-28 19:20:44 +00001240 // If this block has arguments, parse them. There is no ambiguity here with
1241 // the expression case, because the expression case requires a parameter list.
1242 if (Tok.is(tok::l_paren)) {
1243 ParseParenDeclarator(ParamInfo);
1244 // Parse the pieces after the identifier as if we had "int(...)".
1245 ParamInfo.SetIdentifier(0, CaretLoc);
1246 if (ParamInfo.getInvalidType()) {
1247 // If there was an error parsing the arguments, they may have tried to use
1248 // ^(x+y) which requires an argument list. Just skip the whole block
1249 // literal.
Sebastian Redl1d922962008-12-13 15:32:12 +00001250 return ExprError();
Steve Naroff296e8d52008-08-28 19:20:44 +00001251 }
1252 } else {
1253 // Otherwise, pretend we saw (void).
1254 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Chris Lattner5af2f352009-01-20 19:11:22 +00001255 0, 0, 0, CaretLoc,
1256 ParamInfo));
Steve Naroff296e8d52008-08-28 19:20:44 +00001257 }
1258
1259 // Inform sema that we are starting a block.
Steve Naroff090276f2008-10-10 01:28:17 +00001260 Actions.ActOnBlockArguments(ParamInfo);
Sebastian Redl1d922962008-12-13 15:32:12 +00001261
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001262 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001263 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001264 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001265 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001266 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001267 } else {
1268 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001269 }
Mike Stump281481d2009-02-02 23:46:21 +00001270 } else {
Fariborz Jahanianff03fbb2009-01-14 19:39:53 +00001271 // Saw something like: ^expr
1272 Diag(Tok, diag::err_expected_expression);
1273 return ExprError();
1274 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001275 return move(Result);
Steve Naroff296e8d52008-08-28 19:20:44 +00001276}
1277