blob: 597c50c08a6c8287af8c5b5384b6ff72384c6c8b [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 {
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 Lattnera7447ba2008-02-26 00:51:44 +0000163/// [C++] throw-expression [C++ 15]
Chris Lattner4b009652007-07-25 00:24:17 +0000164///
165/// assignment-operator: one of
166/// = *= /= %= += -= <<= >>= &= ^= |=
167///
168/// expression: [C99 6.5.17]
169/// assignment-expression
170/// expression ',' assignment-expression
171///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000172Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000173 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000174 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000175
Sebastian Redl14ca7412008-12-11 21:36:32 +0000176 OwningExprResult LHS(ParseCastExpression(false));
177 if (LHS.isInvalid()) return move(LHS);
178
Sebastian Redla6817a02008-12-11 22:33:27 +0000179 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Chris Lattner4b009652007-07-25 00:24:17 +0000180}
181
Fariborz Jahanian64b864e2007-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 Lattnerb82d6ef2007-10-03 22:03:06 +0000184/// routine is necessary to disambiguate @try-statement from,
185/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000186///
Sebastian Redla6817a02008-12-11 22:33:27 +0000187Parser::OwningExprResult
188Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redla2deb432008-12-13 15:32:12 +0000189 OwningExprResult LHS(ParseObjCAtExpression(AtLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000190 if (LHS.isInvalid()) return move(LHS);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000191
Sebastian Redla6817a02008-12-11 22:33:27 +0000192 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000193}
194
Eli Friedmanc4772072009-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__,
Sebastian Redl81db6682009-02-05 15:02:23 +0000208 move(LHS));
Eli Friedmanc4772072009-01-27 08:43:38 +0000209 if (LHS.isInvalid()) return move(LHS);
210
211 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
212}
213
Chris Lattner4b009652007-07-25 00:24:17 +0000214/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
215///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000216Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattnera7447ba2008-02-26 00:51:44 +0000217 if (Tok.is(tok::kw_throw))
Sebastian Redl39d4f022008-12-11 22:51:44 +0000218 return ParseThrowExpression();
Chris Lattnera7447ba2008-02-26 00:51:44 +0000219
Sebastian Redl14ca7412008-12-11 21:36:32 +0000220 OwningExprResult LHS(ParseCastExpression(false));
221 if (LHS.isInvalid()) return move(LHS);
222
Sebastian Redla6817a02008-12-11 22:33:27 +0000223 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment);
Chris Lattner4b009652007-07-25 00:24:17 +0000224}
225
Chris Lattnerbfcf4772008-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 Redla2deb432008-12-13 15:32:12 +0000234Parser::OwningExprResult
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000235Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000236 SourceLocation NameLoc,
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000237 IdentifierInfo *ReceiverName,
Sebastian Redla2deb432008-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 Redl14ca7412008-12-11 21:36:32 +0000243 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redla2deb432008-12-13 15:32:12 +0000244 if (R.isInvalid()) return move(R);
245 return ParseRHSOfBinaryExpression(move(R), prec::Assignment);
Chris Lattnerbfcf4772008-06-02 21:31:07 +0000246}
247
248
Sebastian Redl14ca7412008-12-11 21:36:32 +0000249Parser::OwningExprResult Parser::ParseConstantExpression() {
250 OwningExprResult LHS(ParseCastExpression(false));
251 if (LHS.isInvalid()) return move(LHS);
252
Sebastian Redla6817a02008-12-11 22:33:27 +0000253 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional);
Chris Lattner4b009652007-07-25 00:24:17 +0000254}
255
Chris Lattner4b009652007-07-25 00:24:17 +0000256/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
257/// LHS and has a precedence of at least MinPrec.
Sebastian Redla6817a02008-12-11 22:33:27 +0000258Parser::OwningExprResult
259Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000267 if (NextTokPrec < MinPrec)
Sebastian Redla6817a02008-12-11 22:33:27 +0000268 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000269
270 // Consume the operator, saving the operator token for error reporting.
271 Token OpToken = Tok;
272 ConsumeToken();
273
274 // Special case handling for the ternary operator.
Sebastian Redl62261042008-12-09 20:22:58 +0000275 OwningExprResult TernaryMiddle(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000276 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000277 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Redlbb4dae72008-12-09 13:15:23 +0000283 if (TernaryMiddle.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000284 return move(TernaryMiddle);
Chris Lattner4b009652007-07-25 00:24:17 +0000285 } else {
286 // Special case handling of "X ? Y : Z" where Y is empty:
287 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl62261042008-12-09 20:22:58 +0000288 TernaryMiddle = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000289 Diag(Tok, diag::ext_gnu_conditional_expr);
290 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000291
Chris Lattner4d7d2342007-10-09 17:41:39 +0000292 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000293 Diag(Tok, diag::err_expected_colon);
Chris Lattner921342c2008-11-23 23:17:07 +0000294 Diag(OpToken, diag::note_matching) << "?";
Sebastian Redla6817a02008-12-11 22:33:27 +0000295 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000296 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000297
Chris Lattner4b009652007-07-25 00:24:17 +0000298 // Eat the colon.
299 ColonLoc = ConsumeToken();
300 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000301
Chris Lattner4b009652007-07-25 00:24:17 +0000302 // Parse another leaf here for the RHS of the operator.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000303 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000304 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000305 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner464c7f62007-12-18 06:06:23 +0000313 bool isRightAssoc = ThisPrec == prec::Conditional ||
314 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl6008ac32008-11-25 22:21:31 +0000324 // The function takes ownership of the RHS.
Sebastian Redla6817a02008-12-11 22:33:27 +0000325 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000326 if (RHS.isInvalid())
Sebastian Redla6817a02008-12-11 22:33:27 +0000327 return move(RHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000328
329 NextTokPrec = getBinOpPrecedence(Tok.getKind());
330 }
331 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redl6008ac32008-11-25 22:21:31 +0000332
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000333 if (!LHS.isInvalid()) {
Chris Lattner4a149b62007-08-31 05:01:50 +0000334 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000335 if (TernaryMiddle.isInvalid())
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000336 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000337 OpToken.getKind(), move(LHS), move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000338 else
Steve Naroff87d58b42007-09-16 03:34:24 +0000339 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +0000340 move(LHS), move(TernaryMiddle),
341 move(RHS));
Chris Lattner4a149b62007-08-31 05:01:50 +0000342 }
Chris Lattner4b009652007-07-25 00:24:17 +0000343 }
344}
345
346/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
Sebastian Redl0c9da212009-02-03 20:19:35 +0000347/// true, parse a unary-expression. isAddressOfOperand exists because an
348/// id-expression that is the operand of address-of gets special treatment
349/// due to member pointers.
Chris Lattner4b009652007-07-25 00:24:17 +0000350///
351/// cast-expression: [C99 6.5.4]
352/// unary-expression
353/// '(' type-name ')' cast-expression
354///
355/// unary-expression: [C99 6.5.3]
356/// postfix-expression
357/// '++' unary-expression
358/// '--' unary-expression
359/// unary-operator cast-expression
360/// 'sizeof' unary-expression
361/// 'sizeof' '(' type-name ')'
362/// [GNU] '__alignof' unary-expression
363/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000364/// [C++0x] 'alignof' '(' type-id ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000365/// [GNU] '&&' identifier
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000366/// [C++] new-expression
367/// [C++] delete-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000368///
369/// unary-operator: one of
370/// '&' '*' '+' '-' '~' '!'
371/// [GNU] '__extension__' '__real' '__imag'
372///
373/// primary-expression: [C99 6.5.1]
Douglas Gregore60e5d32008-11-06 22:13:31 +0000374/// [C99] identifier
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000375/// [C++] id-expression
Chris Lattner4b009652007-07-25 00:24:17 +0000376/// constant
377/// string-literal
378/// [C++] boolean-literal [C++ 2.13.5]
379/// '(' expression ')'
380/// '__func__' [C99 6.4.2.2]
381/// [GNU] '__FUNCTION__'
382/// [GNU] '__PRETTY_FUNCTION__'
383/// [GNU] '(' compound-statement ')'
384/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
385/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
386/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
387/// assign-expr ')'
388/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregorad4b3792008-11-29 04:51:27 +0000389/// [GNU] '__null'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000390/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000391/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000392/// [OBJC] '@protocol' '(' identifier ')'
393/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000394/// [OBJC] objc-string-literal
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000395/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
396/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000397/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
398/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
399/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
400/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000401/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
402/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argiris Kirtzidisfdde2762008-07-16 07:23:27 +0000403/// [C++] 'this' [C++ 9.3.2]
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000404/// [G++] unary-type-trait '(' type-id ')'
405/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO]
Steve Narofffd5b19d2008-08-28 19:20:44 +0000406/// [clang] '^' block-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000407///
408/// constant: [C99 6.4.4]
409/// integer-constant
410/// floating-constant
411/// enumeration-constant -> identifier
412/// character-constant
413///
Douglas Gregore60e5d32008-11-06 22:13:31 +0000414/// id-expression: [C++ 5.1]
415/// unqualified-id
416/// qualified-id [TODO]
417///
418/// unqualified-id: [C++ 5.1]
419/// identifier
420/// operator-function-id
421/// conversion-function-id [TODO]
422/// '~' class-name [TODO]
423/// template-id [TODO]
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000424///
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000425/// new-expression: [C++ 5.3.4]
426/// '::'[opt] 'new' new-placement[opt] new-type-id
427/// new-initializer[opt]
428/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
429/// new-initializer[opt]
430///
431/// delete-expression: [C++ 5.3.5]
432/// '::'[opt] 'delete' cast-expression
433/// '::'[opt] 'delete' '[' ']' cast-expression
434///
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000435/// [GNU] unary-type-trait:
436/// '__has_nothrow_assign' [TODO]
437/// '__has_nothrow_copy' [TODO]
438/// '__has_nothrow_constructor' [TODO]
439/// '__has_trivial_assign' [TODO]
440/// '__has_trivial_copy' [TODO]
441/// '__has_trivial_constructor' [TODO]
442/// '__has_trivial_destructor' [TODO]
443/// '__has_virtual_destructor' [TODO]
444/// '__is_abstract' [TODO]
445/// '__is_class'
446/// '__is_empty' [TODO]
447/// '__is_enum'
448/// '__is_pod'
449/// '__is_polymorphic'
450/// '__is_union'
451///
452/// [GNU] binary-type-trait:
453/// '__is_base_of' [TODO]
454///
Sebastian Redl0c9da212009-02-03 20:19:35 +0000455Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression,
456 bool isAddressOfOperand) {
Sebastian Redl62261042008-12-09 20:22:58 +0000457 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000458 tok::TokenKind SavedKind = Tok.getKind();
459
460 // This handles all of cast-expression, unary-expression, postfix-expression,
461 // and primary-expression. We handle them together like this for efficiency
462 // and to simplify handling of an expression starting with a '(' token: which
463 // may be one of a parenthesized expression, cast-expression, compound literal
464 // expression, or statement expression.
465 //
466 // If the parsed tokens consist of a primary-expression, the cases below
467 // call ParsePostfixExpressionSuffix to handle the postfix expression
468 // suffixes. Cases that cannot be followed by postfix exprs should
469 // return without invoking ParsePostfixExpressionSuffix.
470 switch (SavedKind) {
471 case tok::l_paren: {
472 // If this expression is limited to being a unary-expression, the parent can
473 // not start a cast expression.
474 ParenParseOption ParenExprType =
475 isUnaryExpression ? CompoundLiteral : CastExpr;
476 TypeTy *CastTy;
477 SourceLocation LParenLoc = Tok.getLocation();
478 SourceLocation RParenLoc;
479 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000480 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000481
482 switch (ParenExprType) {
483 case SimpleExpr: break; // Nothing else to do.
484 case CompoundStmt: break; // Nothing else to do.
485 case CompoundLiteral:
486 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
487 // postfix-expression exist, parse them now.
488 break;
489 case CastExpr:
490 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
491 // the cast-expression that follows it next.
492 // TODO: For cast expression with CastTy.
493 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000494 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000495 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000496 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000498
Chris Lattner4b009652007-07-25 00:24:17 +0000499 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000500 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000501 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000502
Chris Lattner4b009652007-07-25 00:24:17 +0000503 // primary-expression
504 case tok::numeric_constant:
505 // constant: integer-constant
506 // constant: floating-constant
Sebastian Redl14ca7412008-12-11 21:36:32 +0000507
Steve Naroff87d58b42007-09-16 03:34:24 +0000508 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000509 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000510
Chris Lattner4b009652007-07-25 00:24:17 +0000511 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000512 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000513
514 case tok::kw_true:
515 case tok::kw_false:
Sebastian Redl39d4f022008-12-11 22:51:44 +0000516 return ParseCXXBoolLiteral();
Chris Lattner4b009652007-07-25 00:24:17 +0000517
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000518 case tok::identifier: { // primary-expression: identifier
519 // unqualified-id: identifier
520 // constant: enumeration-constant
Chris Lattner5d7eace2009-01-06 05:06:21 +0000521 // Turn a potentially qualified name into a annot_typename or
Chris Lattner68751c42009-01-04 22:52:14 +0000522 // annot_cxxscope if it would be valid. This handles things like x::y, etc.
Chris Lattner1e015942009-01-04 23:23:14 +0000523 if (getLang().CPlusPlus) {
Chris Lattner914660b2009-01-04 23:46:59 +0000524 // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse.
525 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000526 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
Chris Lattner1e015942009-01-04 23:23:14 +0000527 }
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000528
Chris Lattner4b009652007-07-25 00:24:17 +0000529 // Consume the identifier so that we can see if it is followed by a '('.
530 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
531 // need to know whether or not this identifier is a function designator or
532 // not.
533 IdentifierInfo &II = *Tok.getIdentifierInfo();
534 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000535 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000536 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000537 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000538 }
539 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000540 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000541 ConsumeToken();
542 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000543 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000544 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
545 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
546 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner69909292008-08-10 01:53:14 +0000547 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000548 ConsumeToken();
549 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000550 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000551 case tok::string_literal: // primary-expression: string-literal
552 case tok::wide_string_literal:
553 Res = ParseStringLiteralExpression();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000554 if (Res.isInvalid()) return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000555 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl14ca7412008-12-11 21:36:32 +0000556 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +0000557 case tok::kw___builtin_va_arg:
558 case tok::kw___builtin_offsetof:
559 case tok::kw___builtin_choose_expr:
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000560 case tok::kw___builtin_overload:
Chris Lattner4b009652007-07-25 00:24:17 +0000561 case tok::kw___builtin_types_compatible_p:
Sebastian Redla6817a02008-12-11 22:33:27 +0000562 return ParseBuiltinPrimaryExpression();
Douglas Gregorad4b3792008-11-29 04:51:27 +0000563 case tok::kw___null:
Sebastian Redl14ca7412008-12-11 21:36:32 +0000564 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregorad4b3792008-11-29 04:51:27 +0000565 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000566 case tok::plusplus: // unary-expression: '++' unary-expression
567 case tok::minusminus: { // unary-expression: '--' unary-expression
568 SourceLocation SavedLoc = ConsumeToken();
569 Res = ParseCastExpression(true);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000570 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000571 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000572 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000573 }
Sebastian Redl0c9da212009-02-03 20:19:35 +0000574 case tok::amp: { // unary-expression: '&' cast-expression
575 // Special treatment because of member pointers
576 SourceLocation SavedLoc = ConsumeToken();
577 Res = ParseCastExpression(false, true);
578 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000579 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl0c9da212009-02-03 20:19:35 +0000580 return move(Res);
581 }
582
Chris Lattner4b009652007-07-25 00:24:17 +0000583 case tok::star: // unary-expression: '*' cast-expression
584 case tok::plus: // unary-expression: '+' cast-expression
585 case tok::minus: // unary-expression: '-' cast-expression
586 case tok::tilde: // unary-expression: '~' cast-expression
587 case tok::exclaim: // unary-expression: '!' cast-expression
588 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000589 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000590 SourceLocation SavedLoc = ConsumeToken();
591 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000592 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000593 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000594 return move(Res);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000595 }
596
Chris Lattner6cf92942008-02-02 20:20:10 +0000597 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
598 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000599 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner6cf92942008-02-02 20:20:10 +0000600 SourceLocation SavedLoc = ConsumeToken();
601 Res = ParseCastExpression(false);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000602 if (!Res.isInvalid())
Sebastian Redl81db6682009-02-05 15:02:23 +0000603 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, move(Res));
Sebastian Redl14ca7412008-12-11 21:36:32 +0000604 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000605 }
606 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
607 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000608 case tok::kw_alignof:
Chris Lattner4b009652007-07-25 00:24:17 +0000609 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
610 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000611 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000612 return ParseSizeofAlignofExpression();
Chris Lattner4b009652007-07-25 00:24:17 +0000613 case tok::ampamp: { // unary-expression: '&&' identifier
614 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000615 if (Tok.isNot(tok::identifier))
616 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000617
Chris Lattner4b009652007-07-25 00:24:17 +0000618 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000619 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000620 Tok.getIdentifierInfo());
621 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000622 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000623 }
624 case tok::kw_const_cast:
625 case tok::kw_dynamic_cast:
626 case tok::kw_reinterpret_cast:
627 case tok::kw_static_cast:
Argiris Kirtzidis4963ee42008-08-16 19:45:32 +0000628 Res = ParseCXXCasts();
629 // These can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000630 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000631 case tok::kw_typeid:
632 Res = ParseCXXTypeid();
633 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000634 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000635 case tok::kw_this:
Argiris Kirtzidis8f8a52a2008-08-16 19:34:46 +0000636 Res = ParseCXXThis();
637 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000638 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000639
640 case tok::kw_char:
641 case tok::kw_wchar_t:
642 case tok::kw_bool:
643 case tok::kw_short:
644 case tok::kw_int:
645 case tok::kw_long:
646 case tok::kw_signed:
647 case tok::kw_unsigned:
648 case tok::kw_float:
649 case tok::kw_double:
650 case tok::kw_void:
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000651 case tok::kw_typeof:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000652 case tok::annot_typename: {
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000653 if (!getLang().CPlusPlus) {
654 Diag(Tok, diag::err_expected_expression);
655 return ExprError();
656 }
657
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000658 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
659 //
660 DeclSpec DS;
661 ParseCXXSimpleTypeSpecifier(DS);
662 if (Tok.isNot(tok::l_paren))
Sebastian Redl14ca7412008-12-11 21:36:32 +0000663 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
664 << DS.getSourceRange());
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000665
666 Res = ParseCXXTypeConstructExpression(DS);
667 // This can be followed by postfix-expr pieces.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000668 return ParsePostfixExpressionSuffix(move(Res));
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000669 }
670
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000671 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
672 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
673 // template-id
Sebastian Redl0c9da212009-02-03 20:19:35 +0000674 Res = ParseCXXIdExpression(isAddressOfOperand);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000675 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregore60e5d32008-11-06 22:13:31 +0000676
Chris Lattner68751c42009-01-04 22:52:14 +0000677 case tok::coloncolon: {
Chris Lattner94a15bd2009-01-05 03:55:46 +0000678 // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken
679 // annotates the token, tail recurse.
680 if (TryAnnotateTypeOrScopeToken())
Sebastian Redl0c9da212009-02-03 20:19:35 +0000681 return ParseCastExpression(isUnaryExpression, isAddressOfOperand);
682
Chris Lattner68751c42009-01-04 22:52:14 +0000683 // ::new -> [C++] new-expression
684 // ::delete -> [C++] delete-expression
Chris Lattner94a15bd2009-01-05 03:55:46 +0000685 SourceLocation CCLoc = ConsumeToken();
Chris Lattnere7de3612009-01-04 21:25:24 +0000686 if (Tok.is(tok::kw_new))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000687 return ParseCXXNewExpression(true, CCLoc);
Chris Lattner68751c42009-01-04 22:52:14 +0000688 if (Tok.is(tok::kw_delete))
Chris Lattner94a15bd2009-01-05 03:55:46 +0000689 return ParseCXXDeleteExpression(true, CCLoc);
690
Chris Lattner1e015942009-01-04 23:23:14 +0000691 // This is not a type name or scope specifier, it is an invalid expression.
Chris Lattner94a15bd2009-01-05 03:55:46 +0000692 Diag(CCLoc, diag::err_expected_expression);
Chris Lattner1e015942009-01-04 23:23:14 +0000693 return ExprError();
Chris Lattnere7de3612009-01-04 21:25:24 +0000694 }
Sebastian Redl7c5955a2008-12-02 16:35:44 +0000695
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000696 case tok::kw_new: // [C++] new-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000697 return ParseCXXNewExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000698
699 case tok::kw_delete: // [C++] delete-expression
Chris Lattnere7de3612009-01-04 21:25:24 +0000700 return ParseCXXDeleteExpression(false, Tok.getLocation());
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000701
Sebastian Redl39c0f6f2009-01-05 20:52:13 +0000702 case tok::kw___is_pod: // [GNU] unary-type-trait
703 case tok::kw___is_class:
704 case tok::kw___is_enum:
705 case tok::kw___is_union:
706 case tok::kw___is_polymorphic:
707 return ParseUnaryTypeTrait();
708
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000709 case tok::at: {
710 SourceLocation AtLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +0000711 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000712 }
Steve Narofffd5b19d2008-08-28 19:20:44 +0000713 case tok::caret:
714 if (getLang().Blocks)
Sebastian Redla2deb432008-12-13 15:32:12 +0000715 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression());
Steve Narofffd5b19d2008-08-28 19:20:44 +0000716 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000717 return ExprError();
Chris Lattnerf5dfe9c2008-12-12 19:20:14 +0000718 case tok::l_square:
719 // These can be followed by postfix-expr pieces.
720 if (getLang().ObjC1)
Sebastian Redla2deb432008-12-13 15:32:12 +0000721 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattnerc2a1c992009-01-04 22:28:21 +0000722 // FALL THROUGH.
Chris Lattner4b009652007-07-25 00:24:17 +0000723 default:
724 Diag(Tok, diag::err_expected_expression);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000725 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000726 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000727
Chris Lattner4b009652007-07-25 00:24:17 +0000728 // unreachable.
729 abort();
730}
731
732/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
733/// is parsed, this method parses any suffixes that apply.
734///
735/// postfix-expression: [C99 6.5.2]
736/// primary-expression
737/// postfix-expression '[' expression ']'
738/// postfix-expression '(' argument-expression-list[opt] ')'
739/// postfix-expression '.' identifier
740/// postfix-expression '->' identifier
741/// postfix-expression '++'
742/// postfix-expression '--'
743/// '(' type-name ')' '{' initializer-list '}'
744/// '(' type-name ')' '{' initializer-list ',' '}'
745///
746/// argument-expression-list: [C99 6.5.2]
747/// argument-expression
748/// argument-expression-list ',' assignment-expression
749///
Sebastian Redl14ca7412008-12-11 21:36:32 +0000750Parser::OwningExprResult
751Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000752 // Now that the primary-expression piece of the postfix-expression has been
753 // parsed, see if there are any postfix-expression pieces here.
754 SourceLocation Loc;
755 while (1) {
756 switch (Tok.getKind()) {
757 default: // Not a postfix-expression suffix.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000758 return move(LHS);
Chris Lattner4b009652007-07-25 00:24:17 +0000759 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
760 Loc = ConsumeBracket();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000761 OwningExprResult Idx(ParseExpression());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000762
Chris Lattner4b009652007-07-25 00:24:17 +0000763 SourceLocation RLoc = Tok.getLocation();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000764
765 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000766 LHS = Actions.ActOnArraySubscriptExpr(CurScope, move(LHS), Loc,
767 move(Idx), RLoc);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000768 } else
Sebastian Redl14ca7412008-12-11 21:36:32 +0000769 LHS = ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000770
771 // Match the ']'.
772 MatchRHSPunctuation(tok::r_square, Loc);
773 break;
774 }
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000775
Chris Lattner4b009652007-07-25 00:24:17 +0000776 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redl6008ac32008-11-25 22:21:31 +0000777 ExprVector ArgExprs(Actions);
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000778 CommaLocsTy CommaLocs;
Sebastian Redl14ca7412008-12-11 21:36:32 +0000779
Chris Lattner4b009652007-07-25 00:24:17 +0000780 Loc = ConsumeParen();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000781
Chris Lattner4d7d2342007-10-09 17:41:39 +0000782 if (Tok.isNot(tok::r_paren)) {
Argiris Kirtzidis83dce112008-08-16 20:03:01 +0000783 if (ParseExpressionList(ArgExprs, CommaLocs)) {
784 SkipUntil(tok::r_paren);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000785 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000786 }
787 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000788
Chris Lattner4b009652007-07-25 00:24:17 +0000789 // Match the ')'.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000790 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000791 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
792 "Unexpected number of commas!");
Sebastian Redl81db6682009-02-05 15:02:23 +0000793 LHS = Actions.ActOnCallExpr(CurScope, move(LHS), Loc,
Sebastian Redl8b769972009-01-19 00:08:26 +0000794 move_arg(ArgExprs), &CommaLocs[0],
Sebastian Redl6008ac32008-11-25 22:21:31 +0000795 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000796 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000797
Chris Lattner4b009652007-07-25 00:24:17 +0000798 MatchRHSPunctuation(tok::r_paren, Loc);
799 break;
800 }
801 case tok::arrow: // postfix-expression: p-e '->' identifier
802 case tok::period: { // postfix-expression: p-e '.' identifier
803 tok::TokenKind OpKind = Tok.getKind();
804 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000805
Chris Lattner4d7d2342007-10-09 17:41:39 +0000806 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000807 Diag(Tok, diag::err_expected_ident);
Sebastian Redl14ca7412008-12-11 21:36:32 +0000808 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000809 }
Sebastian Redl14ca7412008-12-11 21:36:32 +0000810
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000811 if (!LHS.isInvalid()) {
Sebastian Redl81db6682009-02-05 15:02:23 +0000812 LHS = Actions.ActOnMemberReferenceExpr(CurScope, move(LHS), OpLoc,
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000813 OpKind, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000814 *Tok.getIdentifierInfo());
Sebastian Redl6008ac32008-11-25 22:21:31 +0000815 }
Chris Lattner4b009652007-07-25 00:24:17 +0000816 ConsumeToken();
817 break;
818 }
819 case tok::plusplus: // postfix-expression: postfix-expression '++'
820 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000821 if (!LHS.isInvalid()) {
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000822 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redl81db6682009-02-05 15:02:23 +0000823 Tok.getKind(), move(LHS));
Sebastian Redl6008ac32008-11-25 22:21:31 +0000824 }
Chris Lattner4b009652007-07-25 00:24:17 +0000825 ConsumeToken();
826 break;
827 }
828 }
829}
830
831
832/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
833/// unary-expression: [C99 6.5.3]
834/// 'sizeof' unary-expression
835/// 'sizeof' '(' type-name ')'
836/// [GNU] '__alignof' unary-expression
837/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor658b4442008-11-06 15:17:27 +0000838/// [C++0x] 'alignof' '(' type-id ')'
Sebastian Redla6817a02008-12-11 22:33:27 +0000839Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor658b4442008-11-06 15:17:27 +0000840 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
841 || Tok.is(tok::kw_alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000842 "Not a sizeof/alignof expression!");
843 Token OpTok = Tok;
844 ConsumeToken();
845
846 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl62261042008-12-09 20:22:58 +0000847 OwningExprResult Operand(Actions);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000848 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000849 Operand = ParseCastExpression(true);
850 } else {
851 // If it starts with a '(', we know that it is either a parenthesized
852 // type-name, or it is a unary-expression that starts with a compound
853 // literal, or starts with a primary-expression that is a parenthesized
854 // expression.
855 ParenParseOption ExprType = CastExpr;
856 TypeTy *CastTy;
857 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
858 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +0000859
Chris Lattner4b009652007-07-25 00:24:17 +0000860 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
861 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000862 if (ExprType == CastExpr)
Sebastian Redl8b769972009-01-19 00:08:26 +0000863 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000864 OpTok.is(tok::kw_sizeof),
865 /*isType=*/true, CastTy,
Sebastian Redl8b769972009-01-19 00:08:26 +0000866 SourceRange(LParenLoc, RParenLoc));
Sebastian Redla6817a02008-12-11 22:33:27 +0000867
Chris Lattner48553562007-11-13 20:50:37 +0000868 // If this is a parenthesized expression, it is the start of a
869 // unary-expression, but doesn't include any postfix pieces. Parse these
870 // now if present.
Sebastian Redl14ca7412008-12-11 21:36:32 +0000871 Operand = ParsePostfixExpressionSuffix(move(Operand));
Chris Lattner4b009652007-07-25 00:24:17 +0000872 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000873
Chris Lattner4b009652007-07-25 00:24:17 +0000874 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000875 if (!Operand.isInvalid())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000876 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
877 OpTok.is(tok::kw_sizeof),
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000878 /*isType=*/false,
879 Operand.release(), SourceRange());
Sebastian Redla6817a02008-12-11 22:33:27 +0000880 return move(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +0000881}
882
883/// ParseBuiltinPrimaryExpression
884///
885/// primary-expression: [C99 6.5.1]
886/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
887/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
888/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
889/// assign-expr ')'
890/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000891/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000892///
893/// [GNU] offsetof-member-designator:
894/// [GNU] identifier
895/// [GNU] offsetof-member-designator '.' identifier
896/// [GNU] offsetof-member-designator '[' expression ']'
897///
Sebastian Redla6817a02008-12-11 22:33:27 +0000898Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl62261042008-12-09 20:22:58 +0000899 OwningExprResult Res(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000900 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
901
902 tok::TokenKind T = Tok.getKind();
903 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
904
905 // All of these start with an open paren.
Sebastian Redla6817a02008-12-11 22:33:27 +0000906 if (Tok.isNot(tok::l_paren))
907 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id)
908 << BuiltinII);
909
Chris Lattner4b009652007-07-25 00:24:17 +0000910 SourceLocation LParenLoc = ConsumeParen();
911 // TODO: Build AST.
912
913 switch (T) {
914 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000915 case tok::kw___builtin_va_arg: {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000916 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000917 if (Expr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000918 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000919 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000920 }
921
922 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000923 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000924
Anders Carlsson36760332007-10-15 20:28:48 +0000925 TypeTy *Ty = ParseTypeName();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000926
Anders Carlsson36760332007-10-15 20:28:48 +0000927 if (Tok.isNot(tok::r_paren)) {
928 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +0000929 return ExprError();
Anders Carlsson36760332007-10-15 20:28:48 +0000930 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000931 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +0000932 break;
Anders Carlsson36760332007-10-15 20:28:48 +0000933 }
Chris Lattner69638b12007-08-30 15:51:11 +0000934 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +0000935 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner69638b12007-08-30 15:51:11 +0000936 TypeTy *Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000937
938 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +0000939 return ExprError();
940
Chris Lattner4b009652007-07-25 00:24:17 +0000941 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000942 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000943 Diag(Tok, diag::err_expected_ident);
944 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000945 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000946 }
Sebastian Redla6817a02008-12-11 22:33:27 +0000947
Chris Lattner69638b12007-08-30 15:51:11 +0000948 // Keep track of the various subcomponents we see.
949 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
Sebastian Redla6817a02008-12-11 22:33:27 +0000950
Chris Lattner69638b12007-08-30 15:51:11 +0000951 Comps.push_back(Action::OffsetOfComponent());
952 Comps.back().isBrackets = false;
953 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
954 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000955
Sebastian Redl6008ac32008-11-25 22:21:31 +0000956 // FIXME: This loop leaks the index expressions on error.
Chris Lattner4b009652007-07-25 00:24:17 +0000957 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000958 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000959 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +0000960 Comps.push_back(Action::OffsetOfComponent());
961 Comps.back().isBrackets = false;
962 Comps.back().LocStart = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000963
Chris Lattner4d7d2342007-10-09 17:41:39 +0000964 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000965 Diag(Tok, diag::err_expected_ident);
966 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000967 return ExprError();
Chris Lattner69638b12007-08-30 15:51:11 +0000968 }
969 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
970 Comps.back().LocEnd = ConsumeToken();
Sebastian Redla6817a02008-12-11 22:33:27 +0000971
Chris Lattner4d7d2342007-10-09 17:41:39 +0000972 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000973 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +0000974 Comps.push_back(Action::OffsetOfComponent());
975 Comps.back().isBrackets = true;
976 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +0000977 Res = ParseExpression();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000978 if (Res.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000979 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +0000980 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000981 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000982 Comps.back().U.E = Res.release();
Chris Lattner4b009652007-07-25 00:24:17 +0000983
Chris Lattner69638b12007-08-30 15:51:11 +0000984 Comps.back().LocEnd =
985 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000986 } else if (Tok.is(tok::r_paren)) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000987 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, Ty,
988 &Comps[0], Comps.size(),
989 ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +0000990 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000991 } else {
Chris Lattner69638b12007-08-30 15:51:11 +0000992 // Error occurred.
Sebastian Redla6817a02008-12-11 22:33:27 +0000993 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000994 }
995 }
996 break;
Chris Lattner69638b12007-08-30 15:51:11 +0000997 }
Steve Naroff93c53012007-08-03 21:21:27 +0000998 case tok::kw___builtin_choose_expr: {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000999 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001000 if (Cond.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001001 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001002 return move(Cond);
Steve Naroff93c53012007-08-03 21:21:27 +00001003 }
Chris Lattner4b009652007-07-25 00:24:17 +00001004 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001005 return ExprError();
1006
Sebastian Redl14ca7412008-12-11 21:36:32 +00001007 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001008 if (Expr1.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001009 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001010 return move(Expr1);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001011 }
Chris Lattner4b009652007-07-25 00:24:17 +00001012 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001013 return ExprError();
1014
Sebastian Redl14ca7412008-12-11 21:36:32 +00001015 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001016 if (Expr2.isInvalid()) {
Steve Naroff93c53012007-08-03 21:21:27 +00001017 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001018 return move(Expr2);
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001019 }
Chris Lattner4d7d2342007-10-09 17:41:39 +00001020 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +00001021 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001022 return ExprError();
Steve Naroff93c53012007-08-03 21:21:27 +00001023 }
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001024 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
1025 Expr2.release(), ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001026 break;
Steve Naroff93c53012007-08-03 21:21:27 +00001027 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001028 case tok::kw___builtin_overload: {
Sebastian Redl6008ac32008-11-25 22:21:31 +00001029 ExprVector ArgExprs(Actions);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001030 llvm::SmallVector<SourceLocation, 8> CommaLocs;
1031
1032 // For each iteration through the loop look for assign-expr followed by a
1033 // comma. If there is no comma, break and attempt to match r-paren.
1034 if (Tok.isNot(tok::r_paren)) {
1035 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001036 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001037 if (ArgExpr.isInvalid()) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001038 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001039 return ExprError();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001040 } else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001041 ArgExprs.push_back(ArgExpr.release());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001042
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001043 if (Tok.isNot(tok::comma))
1044 break;
1045 // Move to the next argument, remember where the comma was.
1046 CommaLocs.push_back(ConsumeToken());
1047 }
1048 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001049
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001050 // Attempt to consume the r-paren
1051 if (Tok.isNot(tok::r_paren)) {
1052 Diag(Tok, diag::err_expected_rparen);
1053 SkipUntil(tok::r_paren);
Sebastian Redla6817a02008-12-11 22:33:27 +00001054 return ExprError();
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001055 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001056 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001057 &CommaLocs[0], StartLoc, ConsumeParen());
1058 break;
1059 }
Chris Lattner4b009652007-07-25 00:24:17 +00001060 case tok::kw___builtin_types_compatible_p:
Steve Naroff5b528922007-08-01 23:45:51 +00001061 TypeTy *Ty1 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001062
Chris Lattner4b009652007-07-25 00:24:17 +00001063 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Sebastian Redla6817a02008-12-11 22:33:27 +00001064 return ExprError();
1065
Steve Naroff5b528922007-08-01 23:45:51 +00001066 TypeTy *Ty2 = ParseTypeName();
Sebastian Redla6817a02008-12-11 22:33:27 +00001067
Chris Lattner4d7d2342007-10-09 17:41:39 +00001068 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +00001069 Diag(Tok, diag::err_expected_rparen);
Sebastian Redla6817a02008-12-11 22:33:27 +00001070 return ExprError();
Steve Naroff5b528922007-08-01 23:45:51 +00001071 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001072 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +00001073 break;
Sebastian Redl14ca7412008-12-11 21:36:32 +00001074 }
1075
Chris Lattner4b009652007-07-25 00:24:17 +00001076 // These can be followed by postfix-expr pieces because they are
1077 // primary-expressions.
Sebastian Redla6817a02008-12-11 22:33:27 +00001078 return ParsePostfixExpressionSuffix(move(Res));
Chris Lattner4b009652007-07-25 00:24:17 +00001079}
1080
1081/// ParseParenExpression - This parses the unit that starts with a '(' token,
1082/// based on what is allowed by ExprType. The actual thing parsed is returned
1083/// in ExprType.
1084///
1085/// primary-expression: [C99 6.5.1]
1086/// '(' expression ')'
1087/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1088/// postfix-expression: [C99 6.5.2]
1089/// '(' type-name ')' '{' initializer-list '}'
1090/// '(' type-name ')' '{' initializer-list ',' '}'
1091/// cast-expression: [C99 6.5.4]
1092/// '(' type-name ')' cast-expression
1093///
Sebastian Redla6817a02008-12-11 22:33:27 +00001094Parser::OwningExprResult
1095Parser::ParseParenExpression(ParenParseOption &ExprType,
1096 TypeTy *&CastTy, SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001097 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner4b009652007-07-25 00:24:17 +00001098 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl62261042008-12-09 20:22:58 +00001099 OwningExprResult Result(Actions, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001100 CastTy = 0;
Sebastian Redla6817a02008-12-11 22:33:27 +00001101
Chris Lattner4d7d2342007-10-09 17:41:39 +00001102 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001103 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl10c32952008-12-11 19:30:53 +00001104 OwningStmtResult Stmt(ParseCompoundStatement(true));
Chris Lattner4b009652007-07-25 00:24:17 +00001105 ExprType = CompoundStmt;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001106
Chris Lattner4b009652007-07-25 00:24:17 +00001107 // If the substmt parsed correctly, build the AST node.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001108 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1109 Result = Actions.ActOnStmtExpr(
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001110 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001111
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001112 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001113 // Otherwise, this is a compound literal expression or cast expression.
1114 TypeTy *Ty = ParseTypeName();
1115
1116 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001117 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001118 RParenLoc = ConsumeParen();
1119 else
1120 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001121
Chris Lattner4d7d2342007-10-09 17:41:39 +00001122 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001123 if (!getLang().C99) // Compound literals don't exist in C90.
1124 Diag(OpenLoc, diag::ext_c99_compound_literal);
1125 Result = ParseInitializer();
1126 ExprType = CompoundLiteral;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001127 if (!Result.isInvalid())
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001128 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
Sebastian Redl81db6682009-02-05 15:02:23 +00001129 move(Result));
Chris Lattnercde12fd2008-12-12 06:00:12 +00001130 return move(Result);
1131 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001132
Chris Lattnercde12fd2008-12-12 06:00:12 +00001133 if (ExprType == CastExpr) {
Chris Lattner4b009652007-07-25 00:24:17 +00001134 // Note that this doesn't parse the subsequence cast-expression, it just
1135 // returns the parsed type to the callee.
1136 ExprType = CastExpr;
1137 CastTy = Ty;
Sebastian Redla6817a02008-12-11 22:33:27 +00001138 return OwningExprResult(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +00001139 }
Sebastian Redl5457c5e2009-01-19 22:31:54 +00001140
Chris Lattnercde12fd2008-12-12 06:00:12 +00001141 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1142 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001143 } else {
1144 Result = ParseExpression();
1145 ExprType = SimpleExpr;
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001146 if (!Result.isInvalid() && Tok.is(tok::r_paren))
Sebastian Redl81db6682009-02-05 15:02:23 +00001147 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), move(Result));
Chris Lattner4b009652007-07-25 00:24:17 +00001148 }
Sebastian Redla6817a02008-12-11 22:33:27 +00001149
Chris Lattner4b009652007-07-25 00:24:17 +00001150 // Match the ')'.
Chris Lattnercde12fd2008-12-12 06:00:12 +00001151 if (Result.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001152 SkipUntil(tok::r_paren);
Chris Lattnercde12fd2008-12-12 06:00:12 +00001153 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +00001154 }
Chris Lattnercde12fd2008-12-12 06:00:12 +00001155
1156 if (Tok.is(tok::r_paren))
1157 RParenLoc = ConsumeParen();
1158 else
1159 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redla6817a02008-12-11 22:33:27 +00001160
1161 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001162}
1163
1164/// ParseStringLiteralExpression - This handles the various token types that
1165/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1166/// translation phase #6].
1167///
1168/// primary-expression: [C99 6.5.1]
1169/// string-literal
Sebastian Redl39d4f022008-12-11 22:51:44 +00001170Parser::OwningExprResult Parser::ParseStringLiteralExpression() {
Chris Lattner4b009652007-07-25 00:24:17 +00001171 assert(isTokenStringLiteral() && "Not a string literal!");
Sebastian Redl39d4f022008-12-11 22:51:44 +00001172
Chris Lattner4b009652007-07-25 00:24:17 +00001173 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1174 // considered to be strings for concatenation purposes.
1175 llvm::SmallVector<Token, 4> StringToks;
Sebastian Redl39d4f022008-12-11 22:51:44 +00001176
Chris Lattner4b009652007-07-25 00:24:17 +00001177 do {
1178 StringToks.push_back(Tok);
1179 ConsumeStringToken();
1180 } while (isTokenStringLiteral());
1181
1182 // Pass the set of string tokens, ready for concatenation, to the actions.
Sebastian Redlcd883f72009-01-18 18:53:16 +00001183 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001184}
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001185
1186/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1187///
1188/// argument-expression-list:
1189/// assignment-expression
1190/// argument-expression-list , assignment-expression
1191///
1192/// [C++] expression-list:
1193/// [C++] assignment-expression
1194/// [C++] expression-list , assignment-expression
1195///
1196bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1197 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +00001198 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001199 if (Expr.isInvalid())
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001200 return true;
Argiris Kirtzidisd37a4182008-08-18 22:49:40 +00001201
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001202 Exprs.push_back(Expr.release());
Argiris Kirtzidis83dce112008-08-16 20:03:01 +00001203
1204 if (Tok.isNot(tok::comma))
1205 return false;
1206 // Move to the next argument, remember where the comma was.
1207 CommaLocs.push_back(ConsumeToken());
1208 }
1209}
Steve Narofffd5b19d2008-08-28 19:20:44 +00001210
Mike Stumpc1fddff2009-02-04 22:31:32 +00001211/// ParseBlockId - Parse a block-id, which roughly looks like int (int x).
1212///
1213/// [clang] block-id:
1214/// [clang] specifier-qualifier-list block-declarator
1215///
1216void Parser::ParseBlockId() {
1217 // Parse the specifier-qualifier-list piece.
1218 DeclSpec DS;
1219 ParseSpecifierQualifierList(DS);
1220
1221 // Parse the block-declarator.
1222 Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext);
1223 ParseDeclarator(DeclaratorInfo);
1224 // Inform sema that we are starting a block.
1225 Actions.ActOnBlockArguments(DeclaratorInfo, CurScope);
1226}
1227
Steve Narofffd5b19d2008-08-28 19:20:44 +00001228/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroffa095a752008-09-16 23:11:46 +00001229/// like ^(int x){ return x+1; }
Steve Narofffd5b19d2008-08-28 19:20:44 +00001230///
1231/// block-literal:
1232/// [clang] '^' block-args[opt] compound-statement
Mike Stumpc1fddff2009-02-04 22:31:32 +00001233/// [clang] '^' block-id compound-statement
Steve Narofffd5b19d2008-08-28 19:20:44 +00001234/// [clang] block-args:
1235/// [clang] '(' parameter-list ')'
1236///
Sebastian Redla2deb432008-12-13 15:32:12 +00001237Parser::OwningExprResult Parser::ParseBlockLiteralExpression() {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001238 assert(Tok.is(tok::caret) && "block literal starts with ^");
1239 SourceLocation CaretLoc = ConsumeToken();
Sebastian Redla2deb432008-12-13 15:32:12 +00001240
Steve Narofffd5b19d2008-08-28 19:20:44 +00001241 // Enter a scope to hold everything within the block. This includes the
1242 // argument decls, decls within the compound expression, etc. This also
1243 // allows determining whether a variable reference inside the block is
1244 // within or outside of the block.
Douglas Gregor95d40792008-12-10 06:34:36 +00001245 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1246 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff52059382008-10-10 01:28:17 +00001247
1248 // Inform sema that we are starting a block.
1249 Actions.ActOnBlockStart(CaretLoc, CurScope);
Sebastian Redla2deb432008-12-13 15:32:12 +00001250
Steve Narofffd5b19d2008-08-28 19:20:44 +00001251 // Parse the return type if present.
1252 DeclSpec DS;
Mike Stumpc1fddff2009-02-04 22:31:32 +00001253 Declarator ParamInfo(DS, Declarator::BlockLiteralContext);
Sebastian Redla2deb432008-12-13 15:32:12 +00001254
Steve Narofffd5b19d2008-08-28 19:20:44 +00001255 // If this block has arguments, parse them. There is no ambiguity here with
1256 // the expression case, because the expression case requires a parameter list.
1257 if (Tok.is(tok::l_paren)) {
1258 ParseParenDeclarator(ParamInfo);
1259 // Parse the pieces after the identifier as if we had "int(...)".
1260 ParamInfo.SetIdentifier(0, CaretLoc);
1261 if (ParamInfo.getInvalidType()) {
Mike Stumpc1fddff2009-02-04 22:31:32 +00001262 // If there was an error parsing the arguments, they may have
1263 // tried to use ^(x+y) which requires an argument list. Just
1264 // skip the whole block literal.
Sebastian Redla2deb432008-12-13 15:32:12 +00001265 return ExprError();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001266 }
Mike Stumpc1fddff2009-02-04 22:31:32 +00001267 // Inform sema that we are starting a block.
1268 Actions.ActOnBlockArguments(ParamInfo, CurScope);
1269 } else if (! Tok.is(tok::l_brace)) {
1270 ParseBlockId();
Steve Narofffd5b19d2008-08-28 19:20:44 +00001271 } else {
1272 // Otherwise, pretend we saw (void).
1273 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Chris Lattnerdefaf412009-01-20 19:11:22 +00001274 0, 0, 0, CaretLoc,
1275 ParamInfo));
Mike Stumpc1fddff2009-02-04 22:31:32 +00001276 // Inform sema that we are starting a block.
1277 Actions.ActOnBlockArguments(ParamInfo, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001278 }
1279
Sebastian Redla2deb432008-12-13 15:32:12 +00001280
Sebastian Redl62261042008-12-09 20:22:58 +00001281 OwningExprResult Result(Actions, true);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001282 if (Tok.is(tok::l_brace)) {
Sebastian Redl10c32952008-12-11 19:30:53 +00001283 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001284 if (!Stmt.isInvalid()) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001285 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001286 } else {
1287 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001288 }
Mike Stump677b3f42009-02-02 23:46:21 +00001289 } else {
Fariborz Jahanian476bba72009-01-14 19:39:53 +00001290 // Saw something like: ^expr
1291 Diag(Tok, diag::err_expected_expression);
1292 return ExprError();
1293 }
Sebastian Redla2deb432008-12-13 15:32:12 +00001294 return move(Result);
Steve Narofffd5b19d2008-08-28 19:20:44 +00001295}
1296