blob: 5133af8f7f5710dbb385514a7ebaccb4e373437e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Narofffd5b19d2008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000026#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
Sebastian Redl95216a62009-02-07 00:15:38 +000036 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13, // *, /, %
50 PointerToMember = 14 // .*, ->*
Chris Lattner4b009652007-07-25 00:24:17 +000051 };
52}
53
54
55/// getBinOpPrecedence - Return the precedence of the specified binary operator
56/// token. This returns:
57///
Douglas Gregor8e458f42009-02-09 18:46:07 +000058static prec::Level getBinOpPrecedence(tok::TokenKind Kind,
59 bool GreaterThanIsOperator) {
Chris Lattner4b009652007-07-25 00:24:17 +000060 switch (Kind) {
Douglas Gregor8e458f42009-02-09 18:46:07 +000061 case tok::greater:
62 // The '>' token can act as either an operator or as the ending
63 // token for a template argument list.
64 // FIXME: '>>' is similar, for error recovery and C++0x.
65 if (GreaterThanIsOperator)
66 return prec::Relational;
67 return prec::Unknown;
68
Chris Lattner4b009652007-07-25 00:24:17 +000069 default: return prec::Unknown;
70 case tok::comma: return prec::Comma;
71 case tok::equal:
72 case tok::starequal:
73 case tok::slashequal:
74 case tok::percentequal:
75 case tok::plusequal:
76 case tok::minusequal:
77 case tok::lesslessequal:
78 case tok::greatergreaterequal:
79 case tok::ampequal:
80 case tok::caretequal:
81 case tok::pipeequal: return prec::Assignment;
82 case tok::question: return prec::Conditional;
83 case tok::pipepipe: return prec::LogicalOr;
84 case tok::ampamp: return prec::LogicalAnd;
85 case tok::pipe: return prec::InclusiveOr;
86 case tok::caret: return prec::ExclusiveOr;
87 case tok::amp: return prec::And;
88 case tok::exclaimequal:
89 case tok::equalequal: return prec::Equality;
90 case tok::lessequal:
91 case tok::less:
Douglas Gregor8e458f42009-02-09 18:46:07 +000092 case tok::greaterequal: return prec::Relational;
Chris Lattner4b009652007-07-25 00:24:17 +000093 case tok::lessless:
94 case tok::greatergreater: return prec::Shift;
95 case tok::plus:
96 case tok::minus: return prec::Additive;
97 case tok::percent:
98 case tok::slash:
99 case tok::star: return prec::Multiplicative;
Sebastian Redl95216a62009-02-07 00:15:38 +0000100 case tok::periodstar:
101 case tok::arrowstar: return prec::PointerToMember;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 }
103}
104
105
106/// ParseExpression - Simple precedence-based parser for binary/ternary
107/// operators.
108///
109/// Note: we diverge from the C99 grammar when parsing the assignment-expression
110/// production. C99 specifies that the LHS of an assignment operator should be
111/// parsed as a unary-expression, but consistency dictates that it be a
112/// conditional-expession. In practice, the important thing here is that the
113/// LHS of an assignment has to be an l-value, which productions between
114/// unary-expression and conditional-expression don't produce. Because we want
115/// consistency, we parse the LHS as a conditional-expression, then check for
116/// l-value-ness in semantic analysis stages.
117///
Sebastian Redl95216a62009-02-07 00:15:38 +0000118/// pm-expression: [C++ 5.5]
119/// cast-expression
120/// pm-expression '.*' cast-expression
121/// pm-expression '->*' cast-expression
122///
Chris Lattner4b009652007-07-25 00:24:17 +0000123/// multiplicative-expression: [C99 6.5.5]
Sebastian Redl95216a62009-02-07 00:15:38 +0000124/// Note: in C++, apply pm-expression instead of cast-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000125/// cast-expression
126/// multiplicative-expression '*' cast-expression
127/// multiplicative-expression '/' cast-expression
128/// multiplicative-expression '%' cast-expression
129///
130/// additive-expression: [C99 6.5.6]
131/// multiplicative-expression
132/// additive-expression '+' multiplicative-expression
133/// additive-expression '-' multiplicative-expression
134///
135/// shift-expression: [C99 6.5.7]
136/// additive-expression
137/// shift-expression '<<' additive-expression
138/// shift-expression '>>' additive-expression
139///
140/// relational-expression: [C99 6.5.8]
141/// shift-expression
142/// relational-expression '<' shift-expression
143/// relational-expression '>' shift-expression
144/// relational-expression '<=' shift-expression
145/// relational-expression '>=' shift-expression
146///
147/// equality-expression: [C99 6.5.9]
148/// relational-expression
149/// equality-expression '==' relational-expression
150/// equality-expression '!=' relational-expression
151///
152/// AND-expression: [C99 6.5.10]
153/// equality-expression
154/// AND-expression '&' equality-expression
155///
156/// exclusive-OR-expression: [C99 6.5.11]
157/// AND-expression
158/// exclusive-OR-expression '^' AND-expression
159///
160/// inclusive-OR-expression: [C99 6.5.12]
161/// exclusive-OR-expression
162/// inclusive-OR-expression '|' exclusive-OR-expression
163///
164/// logical-AND-expression: [C99 6.5.13]
165/// inclusive-OR-expression
166/// logical-AND-expression '&&' inclusive-OR-expression
167///
168/// logical-OR-expression: [C99 6.5.14]
169/// logical-AND-expression
170/// logical-OR-expression '||' logical-AND-expression
171///
172/// conditional-expression: [C99 6.5.15]
173/// logical-OR-expression
174/// logical-OR-expression '?' expression ':' conditional-expression
175/// [GNU] logical-OR-expression '?' ':' conditional-expression
176///
177/// assignment-expression: [C99 6.5.16]
178/// conditional-expression
179/// unary-expression assignment-operator assignment-expression
Chris Lattnera7447ba2008-02-26 00:51:44 +0000180/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000181///
182/// assignment-operator: one of
183/// = *= /= %= += -= <<= >>= &= ^= |=
184///
185/// expression: [C99 6.5.17]
186/// assignment-expression
187/// expression ',' assignment-expression
188///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000189Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000190 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000191 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000192
Sebastian Redl14ca7412008-12-11 21:36:32 +0000193 OwningExprResult LHS(ParseCastExpression(false));
194 if (LHS.isInvalid()) return move(LHS);
195
Sebastian Redla6817a02008-12-11 22:33:27 +0000196 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000197}
198
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000199/// This routine is called when the '@' is seen and consumed.
200/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000201/// routine is necessary to disambiguate @try-statement from,
202/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000203///
Sebastian Redla6817a02008-12-11 22:33:27 +0000204Parser::OwningExprResult
205Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redla2deb432008-12-13 15:32:12 +0000206 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000207 if (LHS.isInvalid()) return move(LHS);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000208
Sebastian Redla6817a02008-12-11 22:33:27 +0000209 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000210}
211
Eli Friedmanc4772072009-01-27 08:43:38 +0000212/// This routine is called when a leading '__extension__' is seen and
213/// consumed. This is necessary because the token gets consumed in the
214/// process of disambiguating between an expression and a declaration.
215Parser::OwningExprResult
216Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
217 // FIXME: The handling for throw is almost certainly wrong.
218 if (Tok.is(tok::kw_throw))
219 return ParseThrowExpression();
220
221 OwningExprResult LHS(ParseCastExpression(false));
222 if (LHS.isInvalid()) return move(LHS);
223
224 LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
Sebastian Redl81db6682009-02-05 15:02:23 +0000225 move(LHS));
Eli Friedmanc4772072009-01-27 08:43:38 +0000226 if (LHS.isInvalid()) return move(LHS);
227
228 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
229}
230
Chris Lattner4b009652007-07-25 00:24:17 +0000231/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
232///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000233Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000234 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000235 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000236
Sebastian Redl14ca7412008-12-11 21:36:32 +0000237 OwningExprResult LHS(ParseCastExpression(false));
238 if (LHS.isInvalid()) return move(LHS);
239
Sebastian Redla6817a02008-12-11 22:33:27 +0000240 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000241}
242
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000243/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
244/// where part of an objc message send has already been parsed. In this case
245/// LBracLoc indicates the location of the '[' of the message send, and either
246/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
247/// message.
248///
249/// Since this handles full assignment-expression's, it handles postfix
250/// expressions and other binary operators for these expressions as well.
Sebastian Redla2deb432008-12-13 15:32:12 +0000251Parser::OwningExprResult
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000252Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000253 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000254 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-12-13 15:32:12 +0000255 ExprArg ReceiverExpr) {
256 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
257 ReceiverName,
258 move(ReceiverExpr)));
259 if (R.isInvalid()) return move(R);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000260 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redla2deb432008-12-13 15:32:12 +0000261 if (R.isInvalid()) return move(R);
262 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000263}
264
265
Sebastian Redl14ca7412008-12-11 21:36:32 +0000266Parser::OwningExprResult Parser::ParseConstantExpression() {
267 OwningExprResult LHS(ParseCastExpression(false));
268 if (LHS.isInvalid()) return move(LHS);
269
Sebastian Redla6817a02008-12-11 22:33:27 +0000270 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000271}
272
Chris Lattner4b009652007-07-25 00:24:17 +0000273/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
274/// LHS and has a precedence of at least MinPrec.
Sebastian Redla6817a02008-12-11 22:33:27 +0000275Parser::OwningExprResult
276Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Douglas Gregor8e458f42009-02-09 18:46:07 +0000277 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 SourceLocation ColonLoc;
279
280 while (1) {
281 // If this token has a lower precedence than we are allowed to parse (e.g.
282 // because we are called recursively, or because the token is not a binop),
283 // then we are done!
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000284 if (NextTokPrec < MinPrec)
Sebastian Redla6817a02008-12-11 22:33:27 +0000285 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000286
287 // Consume the operator, saving the operator token for error reporting.
288 Token OpToken = Tok;
289 ConsumeToken();
Sebastian Redl95216a62009-02-07 00:15:38 +0000290
Chris Lattner4b009652007-07-25 00:24:17 +0000291 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000292 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000293 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000294 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000295 // Handle this production specially:
296 // logical-OR-expression '?' expression ':' conditional-expression
297 // In particular, the RHS of the '?' is 'expression', not
298 // 'logical-OR-expression' as we might expect.
299 TernaryMiddle = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000300 if (TernaryMiddle.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000301 return move(TernaryMiddle);
Chris Lattner4b009652007-07-25 00:24:17 +0000302 } else {
303 // Special case handling of "X ? Y : Z" where Y is empty:
304 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000305 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 Diag(Tok, diag::ext_gnu_conditional_expr);
307 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000308
Chris Lattner4d7d2342007-10-09 17:41:39 +0000309 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000310 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000311 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redla6817a02008-12-11 22:33:27 +0000312 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000313 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000314
Chris Lattner4b009652007-07-25 00:24:17 +0000315 // Eat the colon.
316 ColonLoc = ConsumeToken();
317 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000318
Chris Lattner4b009652007-07-25 00:24:17 +0000319 // Parse another leaf here for the RHS of the operator.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000320 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000321 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000322 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000323
324 // Remember the precedence of this operator and get the precedence of the
325 // operator immediately to the right of the RHS.
326 unsigned ThisPrec = NextTokPrec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000327 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000328
329 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000330 bool isRightAssoc = ThisPrec == prec::Conditional ||
331 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +0000332
333 // Get the precedence of the operator to the right of the RHS. If it binds
334 // more tightly with RHS than we do, evaluate it completely first.
335 if (ThisPrec < NextTokPrec ||
336 (ThisPrec == NextTokPrec && isRightAssoc)) {
337 // If this is left-associative, only parse things on the RHS that bind
338 // more tightly than the current operator. If it is left-associative, it
339 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
340 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000341 // The function takes ownership of the RHS.
Sebastian Redla6817a02008-12-11 22:33:27 +0000342 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000343 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000344 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000345
Douglas Gregor8e458f42009-02-09 18:46:07 +0000346 NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator);
Chris Lattner4b009652007-07-25 00:24:17 +0000347 }
348 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000349
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000350 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000351 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000352 if (TernaryMiddle.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000353 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000354 OpToken.getKind(), move(LHS), move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000355 else
Steve Naroff87d58b42007-09-16 03:34:24 +0000356 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000357 move(LHS), move(TernaryMiddle),
358 move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000359 }
Chris Lattner4b009652007-07-25 00:24:17 +0000360 }
361}
362
363/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl0c9da212009-02-03 20:19:35 +0000364/// true, parse a unary-expression. isAddressOfOperand exists because an
365/// id-expression that is the operand of address-of gets special treatment
366/// due to member pointers.
Chris Lattner4b009652007-07-25 00:24:17 +0000367///
368/// cast-expression: [C99 6.5.4]
369/// unary-expression
370/// '(' type-name ')' cast-expression
371///
372/// unary-expression: [C99 6.5.3]
373/// postfix-expression
374/// '++' unary-expression
375/// '--' unary-expression
376/// unary-operator cast-expression
377/// 'sizeof' unary-expression
378/// 'sizeof' '(' type-name ')'
379/// [GNU] '__alignof' unary-expression
380/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000381/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000382/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000383/// [C++] new-expression
384/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000385///
386/// unary-operator: one of
387/// '&' '*' '+' '-' '~' '!'
388/// [GNU] '__extension__' '__real' '__imag'
389///
390/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000391/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000392/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000393/// constant
394/// string-literal
395/// [C++] boolean-literal [C++ 2.13.5]
396/// '(' expression ')'
397/// '__func__' [C99 6.4.2.2]
398/// [GNU] '__FUNCTION__'
399/// [GNU] '__PRETTY_FUNCTION__'
400/// [GNU] '(' compound-statement ')'
401/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
402/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
403/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
404/// assign-expr ')'
405/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregorad4b3792008-11-29 04:51:27 +0000406/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000407/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000408/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000409/// [OBJC] '@protocol' '(' identifier ')'
410/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000411/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000412/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
413/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000414/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
415/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
416/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
417/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000418/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
419/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000420/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000421/// [G++] unary-type-trait '(' type-id ')'
422/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000423/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000424///
425/// constant: [C99 6.4.4]
426/// integer-constant
427/// floating-constant
428/// enumeration-constant -> identifier
429/// character-constant
430///
Douglas Gregore60e5d32008-11-06 22:13:31 +0000431/// id-expression: [C++ 5.1]
432/// unqualified-id
433/// qualified-id [TODO]
434///
435/// unqualified-id: [C++ 5.1]
436/// identifier
437/// operator-function-id
438/// conversion-function-id [TODO]
439/// '~' class-name [TODO]
440/// template-id [TODO]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000441///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000442/// new-expression: [C++ 5.3.4]
443/// '::'[opt] 'new' new-placement[opt] new-type-id
444/// new-initializer[opt]
445/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
446/// new-initializer[opt]
447///
448/// delete-expression: [C++ 5.3.5]
449/// '::'[opt] 'delete' cast-expression
450/// '::'[opt] 'delete' '[' ']' cast-expression
451///
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000452/// [GNU] unary-type-trait:
453/// '__has_nothrow_assign' [TODO]
454/// '__has_nothrow_copy' [TODO]
455/// '__has_nothrow_constructor' [TODO]
456/// '__has_trivial_assign' [TODO]
457/// '__has_trivial_copy' [TODO]
458/// '__has_trivial_constructor' [TODO]
459/// '__has_trivial_destructor' [TODO]
460/// '__has_virtual_destructor' [TODO]
461/// '__is_abstract' [TODO]
462/// '__is_class'
463/// '__is_empty' [TODO]
464/// '__is_enum'
465/// '__is_pod'
466/// '__is_polymorphic'
467/// '__is_union'
468///
469/// [GNU] binary-type-trait:
470/// '__is_base_of' [TODO]
471///
Sebastian Redl0c9da212009-02-03 20:19:35 +0000472Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
473 bool isAddressOfOperand) {
Sebastian Redl62261042008-12-09 20:22:58 +0000474 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000475 tok::TokenKind SavedKind = Tok.getKind();
476
477 // This handles all of cast-expression, unary-expression, postfix-expression,
478 // and primary-expression. We handle them together like this for efficiency
479 // and to simplify handling of an expression starting with a '(' token: which
480 // may be one of a parenthesized expression, cast-expression, compound literal
481 // expression, or statement expression.
482 //
483 // If the parsed tokens consist of a primary-expression, the cases below
484 // call ParsePostfixExpressionSuffix to handle the postfix expression
485 // suffixes. Cases that cannot be followed by postfix exprs should
486 // return without invoking ParsePostfixExpressionSuffix.
487 switch (SavedKind) {
488 case tok::l_paren: {
489 // If this expression is limited to being a unary-expression, the parent can
490 // not start a cast expression.
491 ParenParseOption ParenExprType =
492 isUnaryExpression ? CompoundLiteral : CastExpr;
493 TypeTy *CastTy;
494 SourceLocation LParenLoc = Tok.getLocation();
495 SourceLocation RParenLoc;
496 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000497 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000498
499 switch (ParenExprType) {
500 case SimpleExpr: break; // Nothing else to do.
501 case CompoundStmt: break; // Nothing else to do.
502 case CompoundLiteral:
503 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
504 // postfix-expression exist, parse them now.
505 break;
506 case CastExpr:
507 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
508 // the cast-expression that follows it next.
509 // TODO: For cast expression with CastTy.
510 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000511 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000512 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000513 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000514 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000515
Chris Lattner4b009652007-07-25 00:24:17 +0000516 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000517 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000519
Chris Lattner4b009652007-07-25 00:24:17 +0000520 // primary-expression
521 case tok::numeric_constant:
522 // constant: integer-constant
523 // constant: floating-constant
Sebastian Redl14ca7412008-12-11 21:36:32 +0000524
Steve Naroff87d58b42007-09-16 03:34:24 +0000525 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000526 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000527
Chris Lattner4b009652007-07-25 00:24:17 +0000528 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000529 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000530
531 case tok::kw_true:
532 case tok::kw_false:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000533 return ParseCXXBoolLiteral();
Chris Lattner4b009652007-07-25 00:24:17 +0000534
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535 case tok::identifier: { // primary-expression: identifier
536 // unqualified-id: identifier
537 // constant: enumeration-constant
Chris Lattner5d7eace2009-01-06 05:06:21 +0000538 // Turn a potentially qualified name into a annot_typename or
Chris Lattner68751c42009-01-04 22:52:14 +0000539 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner1e015942009-01-04 23:23:14 +0000540 if (getLang().CPlusPlus) {
Chris Lattner914660b2009-01-04 23:46:59 +0000541 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
542 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000543 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner1e015942009-01-04 23:23:14 +0000544 }
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000545
Chris Lattner4b009652007-07-25 00:24:17 +0000546 // Consume the identifier so that we can see if it is followed by a '('.
547 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
548 // need to know whether or not this identifier is a function designator or
549 // not.
550 IdentifierInfo &II = *Tok.getIdentifierInfo();
551 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000552 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000553 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000554 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000555 }
556 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000557 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000558 ConsumeToken();
559 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000560 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000561 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
562 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
563 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner69909292008-08-10 01:53:14 +0000564 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000565 ConsumeToken();
566 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000567 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000568 case tok::string_literal: // primary-expression: string-literal
569 case tok::wide_string_literal:
570 Res = ParseStringLiteralExpression();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000571 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000572 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl14ca7412008-12-11 21:36:32 +0000573 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000574 case tok::kw___builtin_va_arg:
575 case tok::kw___builtin_offsetof:
576 case tok::kw___builtin_choose_expr:
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000577 case tok::kw___builtin_overload:
Chris Lattner4b009652007-07-25 00:24:17 +0000578 case tok::kw___builtin_types_compatible_p:
Sebastian Redla6817a02008-12-11 22:33:27 +0000579 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000580 case tok::kw___null:
Sebastian Redl14ca7412008-12-11 21:36:32 +0000581 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregorad4b3792008-11-29 04:51:27 +0000582 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000583 case tok::plusplus: // unary-expression: '++' unary-expression
584 case tok::minusminus: { // unary-expression: '--' unary-expression
585 SourceLocation SavedLoc = ConsumeToken();
586 Res = ParseCastExpression(true);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000587 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000588 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000589 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000590 }
Sebastian Redl0c9da212009-02-03 20:19:35 +0000591 case tok::amp: { // unary-expression: '&' cast-expression
592 // Special treatment because of member pointers
593 SourceLocation SavedLoc = ConsumeToken();
594 Res = ParseCastExpression(false, true);
595 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000596 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl0c9da212009-02-03 20:19:35 +0000597 return move(Res);
598 }
599
Chris Lattner4b009652007-07-25 00:24:17 +0000600 case tok::star: // unary-expression: '*' cast-expression
601 case tok::plus: // unary-expression: '+' cast-expression
602 case tok::minus: // unary-expression: '-' cast-expression
603 case tok::tilde: // unary-expression: '~' cast-expression
604 case tok::exclaim: // unary-expression: '!' cast-expression
605 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000606 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000607 SourceLocation SavedLoc = ConsumeToken();
608 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000609 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000610 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000611 return move(Res);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000612 }
613
Chris Lattner6cf92942008-02-02 20:20:10 +0000614 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
615 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000616 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000617 SourceLocation SavedLoc = ConsumeToken();
618 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000619 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000620 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000621 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000622 }
623 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
624 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000625 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000626 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
627 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000628 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000629 return ParseSizeofAlignofExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000630 case tok::ampamp: { // unary-expression: '&&' identifier
631 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000632 if (Tok.isNot(tok::identifier))
633 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000634
Chris Lattner4b009652007-07-25 00:24:17 +0000635 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000636 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000637 Tok.getIdentifierInfo());
638 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000639 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000640 }
641 case tok::kw_const_cast:
642 case tok::kw_dynamic_cast:
643 case tok::kw_reinterpret_cast:
644 case tok::kw_static_cast:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000645 Res = ParseCXXCasts();
646 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000647 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000648 case tok::kw_typeid:
649 Res = ParseCXXTypeid();
650 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000651 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000652 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000653 Res = ParseCXXThis();
654 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000655 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000656
657 case tok::kw_char:
658 case tok::kw_wchar_t:
659 case tok::kw_bool:
660 case tok::kw_short:
661 case tok::kw_int:
662 case tok::kw_long:
663 case tok::kw_signed:
664 case tok::kw_unsigned:
665 case tok::kw_float:
666 case tok::kw_double:
667 case tok::kw_void:
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000668 case tok::kw_typeof:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000669 case tok::annot_typename: {
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000670 if (!getLang().CPlusPlus) {
671 Diag(Tok, diag::err_expected_expression);
672 return ExprError();
673 }
674
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000675 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
676 //
677 DeclSpec DS;
678 ParseCXXSimpleTypeSpecifier(DS);
679 if (Tok.isNot(tok::l_paren))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000680 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
681 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000682
683 Res = ParseCXXTypeConstructExpression(DS);
684 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000685 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000686 }
687
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000688 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
689 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
690 // template-id
Sebastian Redl0c9da212009-02-03 20:19:35 +0000691 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000692 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000693
Chris Lattner68751c42009-01-04 22:52:14 +0000694 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000695 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
696 // annotates the token, tail recurse.
697 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000698 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
699
Chris Lattner68751c42009-01-04 22:52:14 +0000700 // ::new -> [C++] new-expression
701 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000702 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000703 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000704 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000705 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000706 return ParseCXXDeleteExpression(true, CCLoc);
707
Chris Lattner1e015942009-01-04 23:23:14 +0000708 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000709 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000710 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000711 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000712
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000713 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000714 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000715
716 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000717 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000718
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000719 case tok::kw___is_pod: // [GNU] unary-type-trait
720 case tok::kw___is_class:
721 case tok::kw___is_enum:
722 case tok::kw___is_union:
723 case tok::kw___is_polymorphic:
724 return ParseUnaryTypeTrait();
725
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000726 case tok::at: {
727 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000728 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000729 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000730 case tok::caret:
731 if (getLang().Blocks)
Sebastian Redla2deb432008-12-13 15:32:12 +0000732 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Narofffd5b19d2008-08-28 19:20:44 +0000733 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000734 return ExprError();
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000735 case tok::l_square:
736 // These can be followed by postfix-expr pieces.
737 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000738 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000739 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000740 default:
741 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000742 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000743 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000744
Chris Lattner4b009652007-07-25 00:24:17 +0000745 // unreachable.
746 abort();
747}
748
749/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
750/// is parsed, this method parses any suffixes that apply.
751///
752/// postfix-expression: [C99 6.5.2]
753/// primary-expression
754/// postfix-expression '[' expression ']'
755/// postfix-expression '(' argument-expression-list[opt] ')'
756/// postfix-expression '.' identifier
757/// postfix-expression '->' identifier
758/// postfix-expression '++'
759/// postfix-expression '--'
760/// '(' type-name ')' '{' initializer-list '}'
761/// '(' type-name ')' '{' initializer-list ',' '}'
762///
763/// argument-expression-list: [C99 6.5.2]
764/// argument-expression
765/// argument-expression-list ',' assignment-expression
766///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000767Parser::OwningExprResult
768Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000769 // Now that the primary-expression piece of the postfix-expression has been
770 // parsed, see if there are any postfix-expression pieces here.
771 SourceLocation Loc;
772 while (1) {
773 switch (Tok.getKind()) {
774 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000775 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000776 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
777 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000778 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000779
Chris Lattner4b009652007-07-25 00:24:17 +0000780 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000781
782 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000783 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
784 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000785 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000786 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000787
788 // Match the ']'.
789 MatchRHSPunctuation(tok::r_square, Loc);
790 break;
791 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000792
Chris Lattner4b009652007-07-25 00:24:17 +0000793 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000794 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000795 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000796
Chris Lattner4b009652007-07-25 00:24:17 +0000797 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000798
Chris Lattner4d7d2342007-10-09 17:41:39 +0000799 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000800 if (ParseExpressionList(ArgExprs, CommaLocs)) {
801 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000802 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000803 }
804 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000805
Chris Lattner4b009652007-07-25 00:24:17 +0000806 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000807 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000808 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
809 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000810 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl8b769972009-01-19 00:08:26 +0000811 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redl6008ac32008-11-25 22:21:31 +0000812 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000813 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000814
Chris Lattner4b009652007-07-25 00:24:17 +0000815 MatchRHSPunctuation(tok::r_paren, Loc);
816 break;
817 }
818 case tok::arrow: // postfix-expression: p-e '->' identifier
819 case tok::period: { // postfix-expression: p-e '.' identifier
820 tok::TokenKind OpKind = Tok.getKind();
821 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000822
Chris Lattner4d7d2342007-10-09 17:41:39 +0000823 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000824 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000825 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000826 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000827
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000828 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000829 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000830 OpKind, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000831 *Tok.getIdentifierInfo());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000832 }
Chris Lattner4b009652007-07-25 00:24:17 +0000833 ConsumeToken();
834 break;
835 }
836 case tok::plusplus: // postfix-expression: postfix-expression '++'
837 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000838 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000839 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000840 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000841 }
Chris Lattner4b009652007-07-25 00:24:17 +0000842 ConsumeToken();
843 break;
844 }
845 }
846}
847
848
849/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
850/// unary-expression: [C99 6.5.3]
851/// 'sizeof' unary-expression
852/// 'sizeof' '(' type-name ')'
853/// [GNU] '__alignof' unary-expression
854/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000855/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000856Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +0000857 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
858 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000859 "Not a sizeof/alignof expression!");
860 Token OpTok = Tok;
861 ConsumeToken();
862
863 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl62261042008-12-09 20:22:58 +0000864 OwningExprResult Operand(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000865 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000866 Operand = ParseCastExpression(true);
867 } else {
868 // If it starts with a '(', we know that it is either a parenthesized
869 // type-name, or it is a unary-expression that starts with a compound
870 // literal, or starts with a primary-expression that is a parenthesized
871 // expression.
872 ParenParseOption ExprType = CastExpr;
873 TypeTy *CastTy;
874 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
875 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +0000876
Chris Lattner4b009652007-07-25 00:24:17 +0000877 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
878 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000879 if (ExprType == CastExpr)
Sebastian Redl8b769972009-01-19 00:08:26 +0000880 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000881 OpTok.is(tok::kw_sizeof),
882 /*isType=*/true, CastTy,
Sebastian Redl8b769972009-01-19 00:08:26 +0000883 SourceRange(LParenLoc, RParenLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000884
Chris Lattner48553562007-11-13 20:50:37 +0000885 // If this is a parenthesized expression, it is the start of a
886 // unary-expression, but doesn't include any postfix pieces. Parse these
887 // now if present.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000888 Operand = ParsePostfixExpressionSuffix(move(Operand));
Chris Lattner4b009652007-07-25 00:24:17 +0000889 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000890
Chris Lattner4b009652007-07-25 00:24:17 +0000891 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000892 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000893 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
894 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000895 /*isType=*/false,
896 Operand.release(), SourceRange());
Sebastian Redla6817a02008-12-11 22:33:27 +0000897 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +0000898}
899
900/// ParseBuiltinPrimaryExpression
901///
902/// primary-expression: [C99 6.5.1]
903/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
904/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
905/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
906/// assign-expr ')'
907/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000908/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000909///
910/// [GNU] offsetof-member-designator:
911/// [GNU] identifier
912/// [GNU] offsetof-member-designator '.' identifier
913/// [GNU] offsetof-member-designator '[' expression ']'
914///
Sebastian Redla6817a02008-12-11 22:33:27 +0000915Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000916 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000917 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
918
919 tok::TokenKind T = Tok.getKind();
920 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
921
922 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +0000923 if (Tok.isNot(tok::l_paren))
924 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
925 << BuiltinII);
926
Chris Lattner4b009652007-07-25 00:24:17 +0000927 SourceLocation LParenLoc = ConsumeParen();
928 // TODO: Build AST.
929
930 switch (T) {
931 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000932 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000933 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000934 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000935 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000936 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000937 }
938
939 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000940 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000941
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000942 TypeResult Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000943
Anders Carlsson36760332007-10-15 20:28:48 +0000944 if (Tok.isNot(tok::r_paren)) {
945 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +0000946 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +0000947 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000948 if (Ty.isInvalid())
949 Res = ExprError();
950 else
951 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty.get(),
952 ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +0000953 break;
Anders Carlsson36760332007-10-15 20:28:48 +0000954 }
Chris Lattner69638b12007-08-30 15:51:11 +0000955 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +0000956 SourceLocation TypeLoc = Tok.getLocation();
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000957 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000958
959 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000960 return ExprError();
961
Chris Lattner4b009652007-07-25 00:24:17 +0000962 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000963 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000964 Diag(Tok, diag::err_expected_ident);
965 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000966 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000967 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000968
Chris Lattner69638b12007-08-30 15:51:11 +0000969 // Keep track of the various subcomponents we see.
970 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +0000971
Chris Lattner69638b12007-08-30 15:51:11 +0000972 Comps.push_back(Action::OffsetOfComponent());
973 Comps.back().isBrackets = false;
974 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
975 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000976
Sebastian Redl6008ac32008-11-25 22:21:31 +0000977 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +0000978 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000979 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000980 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +0000981 Comps.push_back(Action::OffsetOfComponent());
982 Comps.back().isBrackets = false;
983 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000984
Chris Lattner4d7d2342007-10-09 17:41:39 +0000985 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000986 Diag(Tok, diag::err_expected_ident);
987 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000988 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000989 }
990 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
991 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000992
Chris Lattner4d7d2342007-10-09 17:41:39 +0000993 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000994 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +0000995 Comps.push_back(Action::OffsetOfComponent());
996 Comps.back().isBrackets = true;
997 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +0000998 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000999 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001000 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001001 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +00001002 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001003 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +00001004
Chris Lattner69638b12007-08-30 15:51:11 +00001005 Comps.back().LocEnd =
1006 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +00001007 } else if (Tok.is(tok::r_paren)) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001008 if (Ty.isInvalid())
1009 Res = ExprError();
1010 else
1011 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc,
1012 Ty.get(), &Comps[0],
1013 Comps.size(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001014 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001015 } else {
Chris Lattner69638b12007-08-30 15:51:11 +00001016 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +00001017 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001018 }
1019 }
1020 break;
Chris Lattner69638b12007-08-30 15:51:11 +00001021 }
Steve Naroff93c53012007-08-03 21:21:27 +00001022 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001023 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001024 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001025 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001026 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001027 }
Chris Lattner4b009652007-07-25 00:24:17 +00001028 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001029 return ExprError();
1030
Sebastian Redl14ca7412008-12-11 21:36:32 +00001031 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001032 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001033 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001034 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001035 }
Chris Lattner4b009652007-07-25 00:24:17 +00001036 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001037 return ExprError();
1038
Sebastian Redl14ca7412008-12-11 21:36:32 +00001039 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001040 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001041 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001042 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001043 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001044 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001045 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001046 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001047 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001048 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1049 Expr2.release(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001050 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001051 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001052 case tok::kw___builtin_overload: {
Sebastian Redl6008ac32008-11-25 22:21:31 +00001053 ExprVector ArgExprs(Actions);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001054 llvm::SmallVector<SourceLocation, 8> CommaLocs;
1055
1056 // For each iteration through the loop look for assign-expr followed by a
1057 // comma. If there is no comma, break and attempt to match r-paren.
1058 if (Tok.isNot(tok::r_paren)) {
1059 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001060 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001061 if (ArgExpr.isInvalid()) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001062 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001063 return ExprError();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001064 } else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001065 ArgExprs.push_back(ArgExpr.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001066
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001067 if (Tok.isNot(tok::comma))
1068 break;
1069 // Move to the next argument, remember where the comma was.
1070 CommaLocs.push_back(ConsumeToken());
1071 }
1072 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001073
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001074 // Attempt to consume the r-paren
1075 if (Tok.isNot(tok::r_paren)) {
1076 Diag(Tok, diag::err_expected_rparen);
1077 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001078 return ExprError();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001079 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001080 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001081 &CommaLocs[0], StartLoc, ConsumeParen());
1082 break;
1083 }
Chris Lattner4b009652007-07-25 00:24:17 +00001084 case tok::kw___builtin_types_compatible_p:
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001085 TypeResult Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001086
Chris Lattner4b009652007-07-25 00:24:17 +00001087 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001088 return ExprError();
1089
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001090 TypeResult Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001091
Chris Lattner4d7d2342007-10-09 17:41:39 +00001092 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001093 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001094 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001095 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001096
1097 if (Ty1.isInvalid() || Ty2.isInvalid())
1098 Res = ExprError();
1099 else
1100 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1.get(), Ty2.get(),
1101 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001102 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001103 }
1104
Chris Lattner4b009652007-07-25 00:24:17 +00001105 // These can be followed by postfix-expr pieces because they are
1106 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001107 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001108}
1109
1110/// ParseParenExpression - This parses the unit that starts with a '(' token,
1111/// based on what is allowed by ExprType. The actual thing parsed is returned
1112/// in ExprType.
1113///
1114/// primary-expression: [C99 6.5.1]
1115/// '(' expression ')'
1116/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1117/// postfix-expression: [C99 6.5.2]
1118/// '(' type-name ')' '{' initializer-list '}'
1119/// '(' type-name ')' '{' initializer-list ',' '}'
1120/// cast-expression: [C99 6.5.4]
1121/// '(' type-name ')' cast-expression
1122///
Sebastian Redla6817a02008-12-11 22:33:27 +00001123Parser::OwningExprResult
1124Parser::ParseParenExpression(ParenParseOption &ExprType,
1125 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001126 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Douglas Gregoraf0d0092009-02-09 21:04:56 +00001127 GreaterThanIsOperatorScope G(GreaterThanIsOperator, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001128 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001129 OwningExprResult Result(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001130 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001131
Chris Lattner4d7d2342007-10-09 17:41:39 +00001132 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001133 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001134 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001135 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001136
Chris Lattner4b009652007-07-25 00:24:17 +00001137 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001138 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1139 Result = Actions.ActOnStmtExpr(
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001140 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001141
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001142 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001143 // Otherwise, this is a compound literal expression or cast expression.
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001144 TypeResult Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +00001145
1146 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001147 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001148 RParenLoc = ConsumeParen();
1149 else
1150 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001151
Chris Lattner4d7d2342007-10-09 17:41:39 +00001152 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001153 if (!getLang().C99) // Compound literals don't exist in C90.
1154 Diag(OpenLoc, diag::ext_c99_compound_literal);
1155 Result = ParseInitializer();
1156 ExprType = CompoundLiteral;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001157 if (!Result.isInvalid() && !Ty.isInvalid())
1158 return Actions.ActOnCompoundLiteral(OpenLoc, Ty.get(), RParenLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001159 move(Result));
Chris Lattnercde12fd2008-12-12 06:00:12 +00001160 return move(Result);
1161 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001162
Chris Lattnercde12fd2008-12-12 06:00:12 +00001163 if (ExprType == CastExpr) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001164 // Note that this doesn't parse the subsequent cast-expression, it just
Chris Lattner4b009652007-07-25 00:24:17 +00001165 // returns the parsed type to the callee.
1166 ExprType = CastExpr;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00001167
1168 if (Ty.isInvalid())
1169 return ExprError();
1170
1171 CastTy = Ty.get();
Sebastian Redla6817a02008-12-11 22:33:27 +00001172 return OwningExprResult(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001173 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001174
Chris Lattnercde12fd2008-12-12 06:00:12 +00001175 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1176 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001177 } else {
1178 Result = ParseExpression();
1179 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001180 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001181 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001182 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001183
Chris Lattner4b009652007-07-25 00:24:17 +00001184 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001185 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001186 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001187 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001188 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001189
1190 if (Tok.is(tok::r_paren))
1191 RParenLoc = ConsumeParen();
1192 else
1193 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001194
1195 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001196}
1197
1198/// ParseStringLiteralExpression - This handles the various token types that
1199/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1200/// translation phase #6].
1201///
1202/// primary-expression: [C99 6.5.1]
1203/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001204Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001205 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001206
Chris Lattner4b009652007-07-25 00:24:17 +00001207 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1208 // considered to be strings for concatenation purposes.
1209 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001210
Chris Lattner4b009652007-07-25 00:24:17 +00001211 do {
1212 StringToks.push_back(Tok);
1213 ConsumeStringToken();
1214 } while (isTokenStringLiteral());
1215
1216 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001217 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001218}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001219
1220/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1221///
1222/// argument-expression-list:
1223/// assignment-expression
1224/// argument-expression-list , assignment-expression
1225///
1226/// [C++] expression-list:
1227/// [C++] assignment-expression
1228/// [C++] expression-list , assignment-expression
1229///
1230bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1231 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001232 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001233 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001234 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001235
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001236 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001237
1238 if (Tok.isNot(tok::comma))
1239 return false;
1240 // Move to the next argument, remember where the comma was.
1241 CommaLocs.push_back(ConsumeToken());
1242 }
1243}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001244
Mike Stumpc1fddff2009-02-04 22:31:32 +00001245/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1246///
1247/// [clang] block-id:
1248/// [clang] specifier-qualifier-list block-declarator
1249///
1250void Parser::ParseBlockId() {
1251 // Parse the specifier-qualifier-list piece.
1252 DeclSpec DS;
1253 ParseSpecifierQualifierList(DS);
1254
1255 // Parse the block-declarator.
1256 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1257 ParseDeclarator(DeclaratorInfo);
1258 // Inform sema that we are starting a block.
1259 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1260}
1261
Steve Narofffd5b19d2008-08-28 19:20:44 +00001262/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001263/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001264///
1265/// block-literal:
1266/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001267/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001268/// [clang] block-args:
1269/// [clang] '(' parameter-list ')'
1270///
Sebastian Redla2deb432008-12-13 15:32:12 +00001271Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001272 assert(Tok.is(tok::caret) && "block literal starts with ^");
1273 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001274
Steve Narofffd5b19d2008-08-28 19:20:44 +00001275 // Enter a scope to hold everything within the block. This includes the
1276 // argument decls, decls within the compound expression, etc. This also
1277 // allows determining whether a variable reference inside the block is
1278 // within or outside of the block.
Sebastian Redl0c986032009-02-09 18:23:29 +00001279 ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope |
1280 Scope::BreakScope | Scope::ContinueScope |
1281 Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001282
1283 // Inform sema that we are starting a block.
1284 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001285
Steve Narofffd5b19d2008-08-28 19:20:44 +00001286 // Parse the return type if present.
1287 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001288 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redl0c986032009-02-09 18:23:29 +00001289 // FIXME: Since the return type isn't actually parsed, it can't be used to
1290 // fill ParamInfo with an initial valid range, so do it manually.
1291 ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation()));
Sebastian Redla2deb432008-12-13 15:32:12 +00001292
Steve Narofffd5b19d2008-08-28 19:20:44 +00001293 // If this block has arguments, parse them. There is no ambiguity here with
1294 // the expression case, because the expression case requires a parameter list.
1295 if (Tok.is(tok::l_paren)) {
1296 ParseParenDeclarator(ParamInfo);
1297 // Parse the pieces after the identifier as if we had "int(...)".
Sebastian Redl0c986032009-02-09 18:23:29 +00001298 // SetIdentifier sets the source range end, but in this case we're past
1299 // that location.
1300 SourceLocation Tmp = ParamInfo.getSourceRange().getEnd();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001301 ParamInfo.SetIdentifier(0, CaretLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001302 ParamInfo.SetRangeEnd(Tmp);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001303 if (ParamInfo.getInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001304 // If there was an error parsing the arguments, they may have
1305 // tried to use ^(x+y) which requires an argument list. Just
1306 // skip the whole block literal.
Sebastian Redla2deb432008-12-13 15:32:12 +00001307 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001308 }
Mike Stumpc1fddff2009-02-04 22:31:32 +00001309 // Inform sema that we are starting a block.
1310 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1311 } else if (! Tok.is(tok::l_brace)) {
1312 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001313 } else {
1314 // Otherwise, pretend we saw (void).
Douglas Gregor88a25f82009-02-18 07:07:28 +00001315 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
1316 SourceLocation(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00001317 0, 0, 0, CaretLoc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001318 ParamInfo),
1319 CaretLoc);
Mike Stumpc1fddff2009-02-04 22:31:32 +00001320 // Inform sema that we are starting a block.
1321 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001322 }
1323
Sebastian Redla2deb432008-12-13 15:32:12 +00001324
Sebastian Redl62261042008-12-09 20:22:58 +00001325 OwningExprResult Result(Actions, true);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001326 if (Tok.is(tok::l_brace)) {
Sebastian Redl10c32952008-12-11 19:30:53 +00001327 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001328 if (!Stmt.isInvalid()) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001329 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001330 } else {
1331 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001332 }
Mike Stump677b3f42009-02-02 23:46:21 +00001333 } else {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001334 // Saw something like: ^expr
1335 Diag(Tok, diag::err_expected_expression);
1336 return ExprError();
1337 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001338 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001339}
1340