blob: 8a27f1db0e1037c7728ba90d05b9a1ddc9cf3f10 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000023#include "clang/Parse/DeclSpec.h"
Steve Naroff296e8d52008-08-28 19:20:44 +000024#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000025#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000026#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/SmallString.h"
29using namespace clang;
30
31/// PrecedenceLevels - These are precedences for the binary/ternary operators in
32/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
36 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
45 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13 // *, /, %
50 };
51}
52
53
54/// getBinOpPrecedence - Return the precedence of the specified binary operator
55/// token. This returns:
56///
57static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
58 switch (Kind) {
59 default: return prec::Unknown;
60 case tok::comma: return prec::Comma;
61 case tok::equal:
62 case tok::starequal:
63 case tok::slashequal:
64 case tok::percentequal:
65 case tok::plusequal:
66 case tok::minusequal:
67 case tok::lesslessequal:
68 case tok::greatergreaterequal:
69 case tok::ampequal:
70 case tok::caretequal:
71 case tok::pipeequal: return prec::Assignment;
72 case tok::question: return prec::Conditional;
73 case tok::pipepipe: return prec::LogicalOr;
74 case tok::ampamp: return prec::LogicalAnd;
75 case tok::pipe: return prec::InclusiveOr;
76 case tok::caret: return prec::ExclusiveOr;
77 case tok::amp: return prec::And;
78 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
82 case tok::greaterequal:
83 case tok::greater: return prec::Relational;
84 case tok::lessless:
85 case tok::greatergreater: return prec::Shift;
86 case tok::plus:
87 case tok::minus: return prec::Additive;
88 case tok::percent:
89 case tok::slash:
90 case tok::star: return prec::Multiplicative;
91 }
92}
93
94
95/// ParseExpression - Simple precedence-based parser for binary/ternary
96/// operators.
97///
98/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
107/// multiplicative-expression: [C99 6.5.5]
108/// cast-expression
109/// multiplicative-expression '*' cast-expression
110/// multiplicative-expression '/' cast-expression
111/// multiplicative-expression '%' cast-expression
112///
113/// additive-expression: [C99 6.5.6]
114/// multiplicative-expression
115/// additive-expression '+' multiplicative-expression
116/// additive-expression '-' multiplicative-expression
117///
118/// shift-expression: [C99 6.5.7]
119/// additive-expression
120/// shift-expression '<<' additive-expression
121/// shift-expression '>>' additive-expression
122///
123/// relational-expression: [C99 6.5.8]
124/// shift-expression
125/// relational-expression '<' shift-expression
126/// relational-expression '>' shift-expression
127/// relational-expression '<=' shift-expression
128/// relational-expression '>=' shift-expression
129///
130/// equality-expression: [C99 6.5.9]
131/// relational-expression
132/// equality-expression '==' relational-expression
133/// equality-expression '!=' relational-expression
134///
135/// AND-expression: [C99 6.5.10]
136/// equality-expression
137/// AND-expression '&' equality-expression
138///
139/// exclusive-OR-expression: [C99 6.5.11]
140/// AND-expression
141/// exclusive-OR-expression '^' AND-expression
142///
143/// inclusive-OR-expression: [C99 6.5.12]
144/// exclusive-OR-expression
145/// inclusive-OR-expression '|' exclusive-OR-expression
146///
147/// logical-AND-expression: [C99 6.5.13]
148/// inclusive-OR-expression
149/// logical-AND-expression '&&' inclusive-OR-expression
150///
151/// logical-OR-expression: [C99 6.5.14]
152/// logical-AND-expression
153/// logical-OR-expression '||' logical-AND-expression
154///
155/// conditional-expression: [C99 6.5.15]
156/// logical-OR-expression
157/// logical-OR-expression '?' expression ':' conditional-expression
158/// [GNU] logical-OR-expression '?' ':' conditional-expression
159///
160/// assignment-expression: [C99 6.5.16]
161/// conditional-expression
162/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000163/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000164///
165/// assignment-operator: one of
166/// = *= /= %= += -= <<= >>= &= ^= |=
167///
168/// expression: [C99 6.5.17]
169/// assignment-expression
170/// expression ',' assignment-expression
171///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000172Parser::OwningExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000173 if (Tok.is(tok::kw_throw))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000174 return Owned(ParseThrowExpression());
Chris Lattner50dd2892008-02-26 00:51:44 +0000175
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000176 OwningExprResult LHS(ParseCastExpression(false));
177 if (LHS.isInvalid()) return move(LHS);
178
179 return Owned(ParseRHSOfBinaryExpression(LHS.result(), prec::Comma));
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000182/// This routine is called when the '@' is seen and consumed.
183/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000184/// routine is necessary to disambiguate @try-statement from,
185/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000186///
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000187Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000188 OwningExprResult LHS(Actions, ParseObjCAtExpression(AtLoc));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000189 if (LHS.isInvalid()) return LHS.result();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000190
Sebastian Redleffa8d12008-12-10 00:02:53 +0000191 return ParseRHSOfBinaryExpression(LHS.result(), prec::Comma);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000192}
193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
195///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000196Parser::OwningExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000197 if (Tok.is(tok::kw_throw))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000198 return Owned(ParseThrowExpression());
Chris Lattner50dd2892008-02-26 00:51:44 +0000199
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000200 OwningExprResult LHS(ParseCastExpression(false));
201 if (LHS.isInvalid()) return move(LHS);
202
203 return Owned(ParseRHSOfBinaryExpression(LHS.result(), prec::Assignment));
Reid Spencer5f016e22007-07-11 17:01:13 +0000204}
205
Chris Lattnerb93fb492008-06-02 21:31:07 +0000206/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
207/// where part of an objc message send has already been parsed. In this case
208/// LBracLoc indicates the location of the '[' of the message send, and either
209/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
210/// message.
211///
212/// Since this handles full assignment-expression's, it handles postfix
213/// expressions and other binary operators for these expressions as well.
214Parser::ExprResult
215Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000216 SourceLocation NameLoc,
Chris Lattnerb93fb492008-06-02 21:31:07 +0000217 IdentifierInfo *ReceiverName,
218 ExprTy *ReceiverExpr) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000219 OwningExprResult R(Actions, ParseObjCMessageExpressionBody(LBracLoc, NameLoc,
220 ReceiverName,
221 ReceiverExpr));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000222 if (R.isInvalid()) return R.result();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000223 R = ParsePostfixExpressionSuffix(move(R));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000224 if (R.isInvalid()) return R.result();
225 return ParseRHSOfBinaryExpression(R.result(), 2);
Chris Lattnerb93fb492008-06-02 21:31:07 +0000226}
227
228
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000229Parser::OwningExprResult Parser::ParseConstantExpression() {
230 OwningExprResult LHS(ParseCastExpression(false));
231 if (LHS.isInvalid()) return move(LHS);
232
233 return Owned(ParseRHSOfBinaryExpression(LHS.result(), prec::Conditional));
Reid Spencer5f016e22007-07-11 17:01:13 +0000234}
235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
237/// LHS and has a precedence of at least MinPrec.
238Parser::ExprResult
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000239Parser::ParseRHSOfBinaryExpression(ExprResult LHSArg, unsigned MinPrec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
241 SourceLocation ColonLoc;
242
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000243 OwningExprResult LHS(Actions, LHSArg);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 while (1) {
245 // If this token has a lower precedence than we are allowed to parse (e.g.
246 // because we are called recursively, or because the token is not a binop),
247 // then we are done!
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000248 if (NextTokPrec < MinPrec)
Sebastian Redleffa8d12008-12-10 00:02:53 +0000249 return LHS.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
251 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000252 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 ConsumeToken();
254
255 // Special case handling for the ternary operator.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000256 OwningExprResult TernaryMiddle(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000258 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 // Handle this production specially:
260 // logical-OR-expression '?' expression ':' conditional-expression
261 // In particular, the RHS of the '?' is 'expression', not
262 // 'logical-OR-expression' as we might expect.
263 TernaryMiddle = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000264 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000265 return TernaryMiddle.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 } else {
267 // Special case handling of "X ? Y : Z" where Y is empty:
268 // logical-OR-expression '?' ':' conditional-expression [GNU]
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000269 TernaryMiddle = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 Diag(Tok, diag::ext_gnu_conditional_expr);
271 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000272
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000273 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 Diag(Tok, diag::err_expected_colon);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000275 Diag(OpToken, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 return ExprResult(true);
277 }
278
279 // Eat the colon.
280 ColonLoc = ConsumeToken();
281 }
282
283 // Parse another leaf here for the RHS of the operator.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000284 OwningExprResult RHS(ParseCastExpression(false));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000285 if (RHS.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000286 return RHS.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
288 // Remember the precedence of this operator and get the precedence of the
289 // operator immediately to the right of the RHS.
290 unsigned ThisPrec = NextTokPrec;
291 NextTokPrec = getBinOpPrecedence(Tok.getKind());
292
293 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000294 bool isRightAssoc = ThisPrec == prec::Conditional ||
295 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000296
297 // Get the precedence of the operator to the right of the RHS. If it binds
298 // more tightly with RHS than we do, evaluate it completely first.
299 if (ThisPrec < NextTokPrec ||
300 (ThisPrec == NextTokPrec && isRightAssoc)) {
301 // If this is left-associative, only parse things on the RHS that bind
302 // more tightly than the current operator. If it is left-associative, it
303 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
304 // A=(B=(C=D)), where each paren is a level of recursion here.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000305 // The function takes ownership of the RHS.
Sebastian Redleffa8d12008-12-10 00:02:53 +0000306 RHS = ParseRHSOfBinaryExpression(RHS.result(), ThisPrec + !isRightAssoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000307 if (RHS.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000308 return RHS.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000309
310 NextTokPrec = getBinOpPrecedence(Tok.getKind());
311 }
312 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
Sebastian Redla55e52c2008-11-25 22:21:31 +0000313
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000314 if (!LHS.isInvalid()) {
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000315 // Combine the LHS and RHS into the LHS (e.g. build AST).
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000316 if (TernaryMiddle.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000317 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(),
318 OpToken.getKind(), LHS.release(),
319 RHS.release());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000320 else
Steve Narofff69936d2007-09-16 03:34:24 +0000321 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +0000322 LHS.release(), TernaryMiddle.release(),
323 RHS.release());
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
326}
327
328/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
329/// true, parse a unary-expression.
330///
331/// cast-expression: [C99 6.5.4]
332/// unary-expression
333/// '(' type-name ')' cast-expression
334///
335/// unary-expression: [C99 6.5.3]
336/// postfix-expression
337/// '++' unary-expression
338/// '--' unary-expression
339/// unary-operator cast-expression
340/// 'sizeof' unary-expression
341/// 'sizeof' '(' type-name ')'
342/// [GNU] '__alignof' unary-expression
343/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000344/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000345/// [GNU] '&&' identifier
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000346/// [C++] new-expression
347/// [C++] delete-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000348///
349/// unary-operator: one of
350/// '&' '*' '+' '-' '~' '!'
351/// [GNU] '__extension__' '__real' '__imag'
352///
353/// primary-expression: [C99 6.5.1]
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000354/// [C99] identifier
Sebastian Redlc42e1182008-11-11 11:37:55 +0000355/// [C++] id-expression
Reid Spencer5f016e22007-07-11 17:01:13 +0000356/// constant
357/// string-literal
358/// [C++] boolean-literal [C++ 2.13.5]
359/// '(' expression ')'
360/// '__func__' [C99 6.4.2.2]
361/// [GNU] '__FUNCTION__'
362/// [GNU] '__PRETTY_FUNCTION__'
363/// [GNU] '(' compound-statement ')'
364/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
365/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
366/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
367/// assign-expr ')'
368/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000369/// [GNU] '__null'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000370/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000371/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000372/// [OBJC] '@protocol' '(' identifier ')'
373/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000374/// [OBJC] objc-string-literal
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000375/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3]
376/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
378/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
379/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
380/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Sebastian Redlc42e1182008-11-11 11:37:55 +0000381/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1]
382/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000383/// [C++] 'this' [C++ 9.3.2]
Steve Naroff296e8d52008-08-28 19:20:44 +0000384/// [clang] '^' block-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000385///
386/// constant: [C99 6.4.4]
387/// integer-constant
388/// floating-constant
389/// enumeration-constant -> identifier
390/// character-constant
391///
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000392/// id-expression: [C++ 5.1]
393/// unqualified-id
394/// qualified-id [TODO]
395///
396/// unqualified-id: [C++ 5.1]
397/// identifier
398/// operator-function-id
399/// conversion-function-id [TODO]
400/// '~' class-name [TODO]
401/// template-id [TODO]
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000402///
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000403/// new-expression: [C++ 5.3.4]
404/// '::'[opt] 'new' new-placement[opt] new-type-id
405/// new-initializer[opt]
406/// '::'[opt] 'new' new-placement[opt] '(' type-id ')'
407/// new-initializer[opt]
408///
409/// delete-expression: [C++ 5.3.5]
410/// '::'[opt] 'delete' cast-expression
411/// '::'[opt] 'delete' '[' ']' cast-expression
412///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000413Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000414 if (getLang().CPlusPlus) {
415 // Annotate typenames and C++ scope specifiers.
Argyrios Kyrtzidis44802cc2008-11-26 21:51:07 +0000416 // Used only in C++, where the typename can be considered as a functional
417 // style cast ("int(1)").
418 // In C we don't expect identifiers to be treated as typenames; if it's a
419 // typedef name, let it be handled as an identifier and
420 // Actions.ActOnIdentifierExpr will emit the proper diagnostic.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000421 TryAnnotateTypeOrScopeToken();
422 }
423
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000424 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 tok::TokenKind SavedKind = Tok.getKind();
426
427 // This handles all of cast-expression, unary-expression, postfix-expression,
428 // and primary-expression. We handle them together like this for efficiency
429 // and to simplify handling of an expression starting with a '(' token: which
430 // may be one of a parenthesized expression, cast-expression, compound literal
431 // expression, or statement expression.
432 //
433 // If the parsed tokens consist of a primary-expression, the cases below
434 // call ParsePostfixExpressionSuffix to handle the postfix expression
435 // suffixes. Cases that cannot be followed by postfix exprs should
436 // return without invoking ParsePostfixExpressionSuffix.
437 switch (SavedKind) {
438 case tok::l_paren: {
439 // If this expression is limited to being a unary-expression, the parent can
440 // not start a cast expression.
441 ParenParseOption ParenExprType =
442 isUnaryExpression ? CompoundLiteral : CastExpr;
443 TypeTy *CastTy;
444 SourceLocation LParenLoc = Tok.getLocation();
445 SourceLocation RParenLoc;
446 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000447 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
449 switch (ParenExprType) {
450 case SimpleExpr: break; // Nothing else to do.
451 case CompoundStmt: break; // Nothing else to do.
452 case CompoundLiteral:
453 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
454 // postfix-expression exist, parse them now.
455 break;
456 case CastExpr:
457 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
458 // the cast-expression that follows it next.
459 // TODO: For cast expression with CastTy.
460 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000461 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000462 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,
463 Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000464 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000468 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // primary-expression
472 case tok::numeric_constant:
473 // constant: integer-constant
474 // constant: floating-constant
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000475
Steve Narofff69936d2007-09-16 03:34:24 +0000476 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000480 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000481
482 case tok::kw_true:
483 case tok::kw_false:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000484 return Owned(ParseCXXBoolLiteral());
Reid Spencer5f016e22007-07-11 17:01:13 +0000485
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000486 case tok::identifier: { // primary-expression: identifier
487 // unqualified-id: identifier
488 // constant: enumeration-constant
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000489
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 // Consume the identifier so that we can see if it is followed by a '('.
491 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
492 // need to know whether or not this identifier is a function designator or
493 // not.
494 IdentifierInfo &II = *Tok.getIdentifierInfo();
495 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000496 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000498 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 }
500 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000501 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 ConsumeToken();
503 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000504 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
506 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
507 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000508 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 ConsumeToken();
510 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000511 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 case tok::string_literal: // primary-expression: string-literal
513 case tok::wide_string_literal:
514 Res = ParseStringLiteralExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000515 if (Res.isInvalid()) return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000517 return ParsePostfixExpressionSuffix(move(Res));
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 case tok::kw___builtin_va_arg:
519 case tok::kw___builtin_offsetof:
520 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000521 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 case tok::kw___builtin_types_compatible_p:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000523 return Owned(ParseBuiltinPrimaryExpression());
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000524 case tok::kw___null:
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000525 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken()));
Douglas Gregor2d8b2732008-11-29 04:51:27 +0000526 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 case tok::plusplus: // unary-expression: '++' unary-expression
528 case tok::minusminus: { // unary-expression: '--' unary-expression
529 SourceLocation SavedLoc = ConsumeToken();
530 Res = ParseCastExpression(true);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000531 if (!Res.isInvalid())
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000532 Res = Owned(Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind,
533 Res.release()));
534 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 }
536 case tok::amp: // unary-expression: '&' cast-expression
537 case tok::star: // unary-expression: '*' cast-expression
538 case tok::plus: // unary-expression: '+' cast-expression
539 case tok::minus: // unary-expression: '-' cast-expression
540 case tok::tilde: // unary-expression: '~' cast-expression
541 case tok::exclaim: // unary-expression: '!' cast-expression
542 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000543 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 SourceLocation SavedLoc = ConsumeToken();
545 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000546 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000547 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000548 return move(Res);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000549 }
550
Chris Lattner35080842008-02-02 20:20:10 +0000551 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
552 // __extension__ silences extension warnings in the subexpression.
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000553 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner35080842008-02-02 20:20:10 +0000554 SourceLocation SavedLoc = ConsumeToken();
555 Res = ParseCastExpression(false);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000556 if (!Res.isInvalid())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000557 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release());
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000558 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
560 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
561 // unary-expression: 'sizeof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000562 case tok::kw_alignof:
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
564 // unary-expression: '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000565 // unary-expression: 'alignof' '(' type-id ')'
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000566 return Owned(ParseSizeofAlignofExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 case tok::ampamp: { // unary-expression: '&&' identifier
568 SourceLocation AmpAmpLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000569 if (Tok.isNot(tok::identifier))
570 return ExprError(Diag(Tok, diag::err_expected_ident));
Sebastian Redleffa8d12008-12-10 00:02:53 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000573 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Tok.getIdentifierInfo());
575 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000576 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 }
578 case tok::kw_const_cast:
579 case tok::kw_dynamic_cast:
580 case tok::kw_reinterpret_cast:
581 case tok::kw_static_cast:
Argyrios Kyrtzidisb348b812008-08-16 19:45:32 +0000582 Res = ParseCXXCasts();
583 // These can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000584 return ParsePostfixExpressionSuffix(move(Res));
Sebastian Redlc42e1182008-11-11 11:37:55 +0000585 case tok::kw_typeid:
586 Res = ParseCXXTypeid();
587 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000588 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000589 case tok::kw_this:
Argyrios Kyrtzidis289d7732008-08-16 19:34:46 +0000590 Res = ParseCXXThis();
591 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000592 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000593
594 case tok::kw_char:
595 case tok::kw_wchar_t:
596 case tok::kw_bool:
597 case tok::kw_short:
598 case tok::kw_int:
599 case tok::kw_long:
600 case tok::kw_signed:
601 case tok::kw_unsigned:
602 case tok::kw_float:
603 case tok::kw_double:
604 case tok::kw_void:
605 case tok::kw_typeof: {
606 if (!getLang().CPlusPlus)
607 goto UnhandledToken;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000608 case tok::annot_qualtypename:
609 assert(getLang().CPlusPlus && "Expected C++");
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000610 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
611 //
612 DeclSpec DS;
613 ParseCXXSimpleTypeSpecifier(DS);
614 if (Tok.isNot(tok::l_paren))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000615 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type)
616 << DS.getSourceRange());
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000617
618 Res = ParseCXXTypeConstructExpression(DS);
619 // This can be followed by postfix-expr pieces.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000620 return ParsePostfixExpressionSuffix(move(Res));
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000621 }
622
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000623 case tok::annot_cxxscope: // [C++] id-expression: qualified-id
624 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id
625 // template-id
626 Res = ParseCXXIdExpression();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000627 return ParsePostfixExpressionSuffix(move(Res));
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +0000628
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000629 case tok::coloncolon: // [C++] new-expression or [C++] delete-expression
Sebastian Redlbcf293b2008-12-02 17:10:24 +0000630 // If the next token is neither 'new' nor 'delete', the :: would have been
631 // parsed as a scope specifier already.
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000632 if (NextToken().is(tok::kw_new))
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000633 return Owned(ParseCXXNewExpression());
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000634 else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000635 return Owned(ParseCXXDeleteExpression());
Sebastian Redlfb4ccd72008-12-02 16:35:44 +0000636
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000637 case tok::kw_new: // [C++] new-expression
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000638 return Owned(ParseCXXNewExpression());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000639
640 case tok::kw_delete: // [C++] delete-expression
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000641 return Owned(ParseCXXDeleteExpression());
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000642
Chris Lattnerc97c2042007-10-03 22:03:06 +0000643 case tok::at: {
644 SourceLocation AtLoc = ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000645 return Owned(ParseObjCAtExpression(AtLoc));
Chris Lattnerc97c2042007-10-03 22:03:06 +0000646 }
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000647 case tok::l_square:
Steve Naroffa642beb2007-10-15 20:55:58 +0000648 // These can be followed by postfix-expr pieces.
Chris Lattner039a6422008-05-09 05:28:21 +0000649 if (getLang().ObjC1)
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000650 return ParsePostfixExpressionSuffix(Owned(ParseObjCMessageExpression()));
Chris Lattner039a6422008-05-09 05:28:21 +0000651 // FALL THROUGH.
Steve Naroff296e8d52008-08-28 19:20:44 +0000652 case tok::caret:
653 if (getLang().Blocks)
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000654 return ParsePostfixExpressionSuffix(Owned(ParseBlockLiteralExpression()));
Steve Naroff296e8d52008-08-28 19:20:44 +0000655 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000656 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 default:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000658 UnhandledToken:
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 Diag(Tok, diag::err_expected_expression);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000660 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // unreachable.
664 abort();
665}
666
667/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
668/// is parsed, this method parses any suffixes that apply.
669///
670/// postfix-expression: [C99 6.5.2]
671/// primary-expression
672/// postfix-expression '[' expression ']'
673/// postfix-expression '(' argument-expression-list[opt] ')'
674/// postfix-expression '.' identifier
675/// postfix-expression '->' identifier
676/// postfix-expression '++'
677/// postfix-expression '--'
678/// '(' type-name ')' '{' initializer-list '}'
679/// '(' type-name ')' '{' initializer-list ',' '}'
680///
681/// argument-expression-list: [C99 6.5.2]
682/// argument-expression
683/// argument-expression-list ',' assignment-expression
684///
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000685Parser::OwningExprResult
686Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Now that the primary-expression piece of the postfix-expression has been
688 // parsed, see if there are any postfix-expression pieces here.
689 SourceLocation Loc;
690 while (1) {
691 switch (Tok.getKind()) {
692 default: // Not a postfix-expression suffix.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000693 return move(LHS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
695 Loc = ConsumeBracket();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000696 OwningExprResult Idx(ParseExpression());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000697
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 SourceLocation RLoc = Tok.getLocation();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000699
700 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000701 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.release(), Loc,
702 Idx.release(), RLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000703 } else
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000704 LHS = ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000705
706 // Match the ']'.
707 MatchRHSPunctuation(tok::r_square, Loc);
708 break;
709 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000710
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
Sebastian Redla55e52c2008-11-25 22:21:31 +0000712 ExprVector ArgExprs(Actions);
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000713 CommaLocsTy CommaLocs;
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 Loc = ConsumeParen();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000716
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000717 if (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +0000718 if (ParseExpressionList(ArgExprs, CommaLocs)) {
719 SkipUntil(tok::r_paren);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000720 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 }
722 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000725 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
727 "Unexpected number of commas!");
Sebastian Redleffa8d12008-12-10 00:02:53 +0000728 LHS = Actions.ActOnCallExpr(CurScope, LHS.release(), Loc,
Douglas Gregor5c37de72008-12-06 00:22:45 +0000729 ArgExprs.take(),
Sebastian Redla55e52c2008-11-25 22:21:31 +0000730 ArgExprs.size(), &CommaLocs[0],
731 Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000733
Chris Lattner2ff54262007-07-21 05:18:12 +0000734 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 break;
736 }
737 case tok::arrow: // postfix-expression: p-e '->' identifier
738 case tok::period: { // postfix-expression: p-e '.' identifier
739 tok::TokenKind OpKind = Tok.getKind();
740 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000741
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000742 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 Diag(Tok, diag::err_expected_ident);
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000744 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 }
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000746
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000747 if (!LHS.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000748 LHS = Actions.ActOnMemberReferenceExpr(LHS.release(), OpLoc, OpKind,
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 Tok.getLocation(),
750 *Tok.getIdentifierInfo());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000751 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 ConsumeToken();
753 break;
754 }
755 case tok::plusplus: // postfix-expression: postfix-expression '++'
756 case tok::minusminus: // postfix-expression: postfix-expression '--'
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000757 if (!LHS.isInvalid()) {
Douglas Gregor74253732008-11-19 15:42:04 +0000758 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000759 Tok.getKind(), LHS.release());
Sebastian Redla55e52c2008-11-25 22:21:31 +0000760 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 ConsumeToken();
762 break;
763 }
764 }
765}
766
767
768/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
769/// unary-expression: [C99 6.5.3]
770/// 'sizeof' unary-expression
771/// 'sizeof' '(' type-name ')'
772/// [GNU] '__alignof' unary-expression
773/// [GNU] '__alignof' '(' type-name ')'
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000774/// [C++0x] 'alignof' '(' type-id ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000775Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Douglas Gregor85bb3da2008-11-06 15:17:27 +0000776 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)
777 || Tok.is(tok::kw_alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000779 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 ConsumeToken();
781
782 // If the operand doesn't start with an '(', it must be an expression.
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000783 OwningExprResult Operand(Actions);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000784 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 Operand = ParseCastExpression(true);
786 } else {
787 // If it starts with a '(', we know that it is either a parenthesized
788 // type-name, or it is a unary-expression that starts with a compound
789 // literal, or starts with a primary-expression that is a parenthesized
790 // expression.
791 ParenParseOption ExprType = CastExpr;
792 TypeTy *CastTy;
793 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
794 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
795
796 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
797 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000798 if (ExprType == CastExpr)
Sebastian Redl05189992008-11-11 17:56:53 +0000799 return Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
800 OpTok.is(tok::kw_sizeof),
801 /*isType=*/true, CastTy,
802 SourceRange(LParenLoc, RParenLoc));
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000803
804 // If this is a parenthesized expression, it is the start of a
805 // unary-expression, but doesn't include any postfix pieces. Parse these
806 // now if present.
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000807 Operand = ParsePostfixExpressionSuffix(move(Operand));
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 }
809
810 // If we get here, the operand to the sizeof/alignof was an expresion.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000811 if (!Operand.isInvalid())
Sebastian Redl05189992008-11-11 17:56:53 +0000812 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(),
813 OpTok.is(tok::kw_sizeof),
Sebastian Redleffa8d12008-12-10 00:02:53 +0000814 /*isType=*/false,
815 Operand.release(), SourceRange());
816 return Operand.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000817}
818
819/// ParseBuiltinPrimaryExpression
820///
821/// primary-expression: [C99 6.5.1]
822/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
823/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
824/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
825/// assign-expr ')'
826/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000827/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000828///
829/// [GNU] offsetof-member-designator:
830/// [GNU] identifier
831/// [GNU] offsetof-member-designator '.' identifier
832/// [GNU] offsetof-member-designator '[' expression ']'
833///
834Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000835 OwningExprResult Res(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
837
838 tok::TokenKind T = Tok.getKind();
839 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
840
841 // All of these start with an open paren.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000842 if (Tok.isNot(tok::l_paren)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000843 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 return ExprResult(true);
845 }
846
847 SourceLocation LParenLoc = ConsumeParen();
848 // TODO: Build AST.
849
850 switch (T) {
851 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000852 case tok::kw___builtin_va_arg: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000853 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000854 if (Expr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 SkipUntil(tok::r_paren);
Eli Friedman09762782008-08-20 22:07:34 +0000856 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 }
858
859 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
860 return ExprResult(true);
861
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000862 TypeTy *Ty = ParseTypeName();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000863
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000864 if (Tok.isNot(tok::r_paren)) {
865 Diag(Tok, diag::err_expected_rparen);
866 return ExprResult(true);
867 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000868 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000870 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000871 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000872 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000873 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000874
875 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
876 return ExprResult(true);
877
878 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000879 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000880 Diag(Tok, diag::err_expected_ident);
881 SkipUntil(tok::r_paren);
882 return true;
883 }
884
885 // Keep track of the various subcomponents we see.
886 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
887
888 Comps.push_back(Action::OffsetOfComponent());
889 Comps.back().isBrackets = false;
890 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
891 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000892
Sebastian Redla55e52c2008-11-25 22:21:31 +0000893 // FIXME: This loop leaks the index expressions on error.
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000895 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000897 Comps.push_back(Action::OffsetOfComponent());
898 Comps.back().isBrackets = false;
899 Comps.back().LocStart = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000900
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000901 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000902 Diag(Tok, diag::err_expected_ident);
903 SkipUntil(tok::r_paren);
904 return true;
905 }
906 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
907 Comps.back().LocEnd = ConsumeToken();
908
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000909 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000911 Comps.push_back(Action::OffsetOfComponent());
912 Comps.back().isBrackets = true;
913 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000915 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 SkipUntil(tok::r_paren);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000917 return Res.result();
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000919 Comps.back().U.E = Res.release();
Reid Spencer5f016e22007-07-11 17:01:13 +0000920
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000921 Comps.back().LocEnd =
922 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000923 } else if (Tok.is(tok::r_paren)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000924 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner6eb21092007-08-30 15:52:49 +0000925 Comps.size(), ConsumeParen());
926 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000928 // Error occurred.
929 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 }
931 }
932 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000933 }
Steve Naroffd04fdd52007-08-03 21:21:27 +0000934 case tok::kw___builtin_choose_expr: {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000935 OwningExprResult Cond(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000936 if (Cond.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000937 SkipUntil(tok::r_paren);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000938 return Cond.result();
Steve Naroffd04fdd52007-08-03 21:21:27 +0000939 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
941 return ExprResult(true);
942
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000943 OwningExprResult Expr1(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000944 if (Expr1.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000945 SkipUntil(tok::r_paren);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000946 return Expr1.result();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000947 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
949 return ExprResult(true);
950
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000951 OwningExprResult Expr2(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000952 if (Expr2.isInvalid()) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000953 SkipUntil(tok::r_paren);
Sebastian Redleffa8d12008-12-10 00:02:53 +0000954 return Expr2.result();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000955 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000956 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000957 Diag(Tok, diag::err_expected_rparen);
958 return ExprResult(true);
959 }
Sebastian Redleffa8d12008-12-10 00:02:53 +0000960 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(),
961 Expr2.release(), ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +0000962 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +0000963 }
Nate Begemane2ce1d92008-01-17 17:46:27 +0000964 case tok::kw___builtin_overload: {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000965 ExprVector ArgExprs(Actions);
Nate Begemane2ce1d92008-01-17 17:46:27 +0000966 llvm::SmallVector<SourceLocation, 8> CommaLocs;
967
968 // For each iteration through the loop look for assign-expr followed by a
969 // comma. If there is no comma, break and attempt to match r-paren.
970 if (Tok.isNot(tok::r_paren)) {
971 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000972 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000973 if (ArgExpr.isInvalid()) {
Nate Begemane2ce1d92008-01-17 17:46:27 +0000974 SkipUntil(tok::r_paren);
975 return ExprResult(true);
976 } else
Sebastian Redleffa8d12008-12-10 00:02:53 +0000977 ArgExprs.push_back(ArgExpr.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000978
Nate Begemane2ce1d92008-01-17 17:46:27 +0000979 if (Tok.isNot(tok::comma))
980 break;
981 // Move to the next argument, remember where the comma was.
982 CommaLocs.push_back(ConsumeToken());
983 }
984 }
985
986 // Attempt to consume the r-paren
987 if (Tok.isNot(tok::r_paren)) {
988 Diag(Tok, diag::err_expected_rparen);
989 SkipUntil(tok::r_paren);
990 return ExprResult(true);
991 }
Sebastian Redla55e52c2008-11-25 22:21:31 +0000992 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(),
Nate Begemane2ce1d92008-01-17 17:46:27 +0000993 &CommaLocs[0], StartLoc, ConsumeParen());
994 break;
995 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +0000997 TypeTy *Ty1 = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000998
999 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
1000 return ExprResult(true);
1001
Steve Naroff363bcff2007-08-01 23:45:51 +00001002 TypeTy *Ty2 = ParseTypeName();
1003
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001004 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +00001005 Diag(Tok, diag::err_expected_rparen);
1006 return ExprResult(true);
1007 }
Steve Naroff1b273c42007-09-16 14:56:35 +00001008 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +00001009 break;
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001010 }
1011
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 // These can be followed by postfix-expr pieces because they are
1013 // primary-expressions.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001014 return ParsePostfixExpressionSuffix(move(Res)).result();
Reid Spencer5f016e22007-07-11 17:01:13 +00001015}
1016
1017/// ParseParenExpression - This parses the unit that starts with a '(' token,
1018/// based on what is allowed by ExprType. The actual thing parsed is returned
1019/// in ExprType.
1020///
1021/// primary-expression: [C99 6.5.1]
1022/// '(' expression ')'
1023/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
1024/// postfix-expression: [C99 6.5.2]
1025/// '(' type-name ')' '{' initializer-list '}'
1026/// '(' type-name ')' '{' initializer-list ',' '}'
1027/// cast-expression: [C99 6.5.4]
1028/// '(' type-name ')' cast-expression
1029///
1030Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
1031 TypeTy *&CastTy,
1032 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001033 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 SourceLocation OpenLoc = ConsumeParen();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001035 OwningExprResult Result(Actions, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 CastTy = 0;
1037
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001038 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 Diag(Tok, diag::ext_gnu_statement_expr);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001040 OwningStmtResult Stmt(ParseCompoundStatement(true));
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 ExprType = CompoundStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001042
Chris Lattnerab18c4c2007-07-24 16:58:17 +00001043 // If the substmt parsed correctly, build the AST node.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001044 if (!Stmt.isInvalid() && Tok.is(tok::r_paren))
1045 Result = Actions.ActOnStmtExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001046 OpenLoc, Stmt.release(), Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001047
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001048 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // Otherwise, this is a compound literal expression or cast expression.
1050 TypeTy *Ty = ParseTypeName();
1051
1052 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001053 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 RParenLoc = ConsumeParen();
1055 else
1056 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1057
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001058 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 if (!getLang().C99) // Compound literals don't exist in C90.
1060 Diag(OpenLoc, diag::ext_c99_compound_literal);
1061 Result = ParseInitializer();
1062 ExprType = CompoundLiteral;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001063 if (!Result.isInvalid())
1064 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001065 Result.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 } else if (ExprType == CastExpr) {
1067 // Note that this doesn't parse the subsequence cast-expression, it just
1068 // returns the parsed type to the callee.
1069 ExprType = CastExpr;
1070 CastTy = Ty;
1071 return ExprResult(false);
1072 } else {
1073 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1074 return ExprResult(true);
1075 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001076 return Result.result();
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 } else {
1078 Result = ParseExpression();
1079 ExprType = SimpleExpr;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001080 if (!Result.isInvalid() && Tok.is(tok::r_paren))
1081 Result = Actions.ActOnParenExpr(
Sebastian Redleffa8d12008-12-10 00:02:53 +00001082 OpenLoc, Tok.getLocation(), Result.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 }
1084
1085 // Match the ')'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001086 if (Result.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 SkipUntil(tok::r_paren);
1088 else {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001089 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 RParenLoc = ConsumeParen();
1091 else
1092 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1093 }
1094
Sebastian Redleffa8d12008-12-10 00:02:53 +00001095 return Result.result();
Reid Spencer5f016e22007-07-11 17:01:13 +00001096}
1097
1098/// ParseStringLiteralExpression - This handles the various token types that
1099/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1100/// translation phase #6].
1101///
1102/// primary-expression: [C99 6.5.1]
1103/// string-literal
1104Parser::ExprResult Parser::ParseStringLiteralExpression() {
1105 assert(isTokenStringLiteral() && "Not a string literal!");
1106
1107 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1108 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001109 llvm::SmallVector<Token, 4> StringToks;
Reid Spencer5f016e22007-07-11 17:01:13 +00001110
1111 do {
1112 StringToks.push_back(Tok);
1113 ConsumeStringToken();
1114 } while (isTokenStringLiteral());
1115
1116 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Narofff69936d2007-09-16 03:34:24 +00001117 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001118}
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001119
1120/// ParseExpressionList - Used for C/C++ (argument-)expression-list.
1121///
1122/// argument-expression-list:
1123/// assignment-expression
1124/// argument-expression-list , assignment-expression
1125///
1126/// [C++] expression-list:
1127/// [C++] assignment-expression
1128/// [C++] expression-list , assignment-expression
1129///
1130bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) {
1131 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001132 OwningExprResult Expr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001133 if (Expr.isInvalid())
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001134 return true;
Argyrios Kyrtzidis4fdc1ca2008-08-18 22:49:40 +00001135
Sebastian Redleffa8d12008-12-10 00:02:53 +00001136 Exprs.push_back(Expr.release());
Argyrios Kyrtzidis0cd5b422008-08-16 20:03:01 +00001137
1138 if (Tok.isNot(tok::comma))
1139 return false;
1140 // Move to the next argument, remember where the comma was.
1141 CommaLocs.push_back(ConsumeToken());
1142 }
1143}
Steve Naroff296e8d52008-08-28 19:20:44 +00001144
1145/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
Steve Naroff17dab4f2008-09-16 23:11:46 +00001146/// like ^(int x){ return x+1; }
Steve Naroff296e8d52008-08-28 19:20:44 +00001147///
1148/// block-literal:
1149/// [clang] '^' block-args[opt] compound-statement
Steve Naroff296e8d52008-08-28 19:20:44 +00001150/// [clang] block-args:
1151/// [clang] '(' parameter-list ')'
1152///
1153Parser::ExprResult Parser::ParseBlockLiteralExpression() {
1154 assert(Tok.is(tok::caret) && "block literal starts with ^");
1155 SourceLocation CaretLoc = ConsumeToken();
1156
1157 // Enter a scope to hold everything within the block. This includes the
1158 // argument decls, decls within the compound expression, etc. This also
1159 // allows determining whether a variable reference inside the block is
1160 // within or outside of the block.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001161 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope|
1162 Scope::ContinueScope|Scope::DeclScope);
Steve Naroff090276f2008-10-10 01:28:17 +00001163
1164 // Inform sema that we are starting a block.
1165 Actions.ActOnBlockStart(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001166
1167 // Parse the return type if present.
1168 DeclSpec DS;
1169 Declarator ParamInfo(DS, Declarator::PrototypeContext);
1170
1171 // If this block has arguments, parse them. There is no ambiguity here with
1172 // the expression case, because the expression case requires a parameter list.
1173 if (Tok.is(tok::l_paren)) {
1174 ParseParenDeclarator(ParamInfo);
1175 // Parse the pieces after the identifier as if we had "int(...)".
1176 ParamInfo.SetIdentifier(0, CaretLoc);
1177 if (ParamInfo.getInvalidType()) {
1178 // If there was an error parsing the arguments, they may have tried to use
1179 // ^(x+y) which requires an argument list. Just skip the whole block
1180 // literal.
Steve Naroff296e8d52008-08-28 19:20:44 +00001181 return true;
1182 }
1183 } else {
1184 // Otherwise, pretend we saw (void).
1185 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001186 0, 0, 0, CaretLoc));
Steve Naroff296e8d52008-08-28 19:20:44 +00001187 }
1188
1189 // Inform sema that we are starting a block.
Steve Naroff090276f2008-10-10 01:28:17 +00001190 Actions.ActOnBlockArguments(ParamInfo);
Steve Naroff296e8d52008-08-28 19:20:44 +00001191
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001192 OwningExprResult Result(Actions, true);
Steve Naroff296e8d52008-08-28 19:20:44 +00001193 if (Tok.is(tok::l_brace)) {
Sebastian Redl61364dd2008-12-11 19:30:53 +00001194 OwningStmtResult Stmt(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001195 if (!Stmt.isInvalid()) {
Sebastian Redleffa8d12008-12-10 00:02:53 +00001196 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001197 } else {
1198 Actions.ActOnBlockError(CaretLoc, CurScope);
Steve Naroff296e8d52008-08-28 19:20:44 +00001199 }
1200 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001201 return Result.result();
Steve Naroff296e8d52008-08-28 19:20:44 +00001202}
1203