blob: 2ecca33cf53a7265a4e3841e7639199f88bffb85 [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"
23#include "clang/Basic/Diagnostic.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/SmallString.h"
26using namespace clang;
27
28/// PrecedenceLevels - These are precedences for the binary/ternary operators in
29/// the C99 grammar. These have been named to relate with the C99 grammar
30/// productions. Low precedences numbers bind more weakly than high numbers.
31namespace prec {
32 enum Level {
33 Unknown = 0, // Not binary operator.
34 Comma = 1, // ,
35 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
36 Conditional = 3, // ?
37 LogicalOr = 4, // ||
38 LogicalAnd = 5, // &&
39 InclusiveOr = 6, // |
40 ExclusiveOr = 7, // ^
41 And = 8, // &
42 Equality = 9, // ==, !=
43 Relational = 10, // >=, <=, >, <
44 Shift = 11, // <<, >>
45 Additive = 12, // -, +
46 Multiplicative = 13 // *, /, %
47 };
48}
49
50
51/// getBinOpPrecedence - Return the precedence of the specified binary operator
52/// token. This returns:
53///
54static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
55 switch (Kind) {
56 default: return prec::Unknown;
57 case tok::comma: return prec::Comma;
58 case tok::equal:
59 case tok::starequal:
60 case tok::slashequal:
61 case tok::percentequal:
62 case tok::plusequal:
63 case tok::minusequal:
64 case tok::lesslessequal:
65 case tok::greatergreaterequal:
66 case tok::ampequal:
67 case tok::caretequal:
68 case tok::pipeequal: return prec::Assignment;
69 case tok::question: return prec::Conditional;
70 case tok::pipepipe: return prec::LogicalOr;
71 case tok::ampamp: return prec::LogicalAnd;
72 case tok::pipe: return prec::InclusiveOr;
73 case tok::caret: return prec::ExclusiveOr;
74 case tok::amp: return prec::And;
75 case tok::exclaimequal:
76 case tok::equalequal: return prec::Equality;
77 case tok::lessequal:
78 case tok::less:
79 case tok::greaterequal:
80 case tok::greater: return prec::Relational;
81 case tok::lessless:
82 case tok::greatergreater: return prec::Shift;
83 case tok::plus:
84 case tok::minus: return prec::Additive;
85 case tok::percent:
86 case tok::slash:
87 case tok::star: return prec::Multiplicative;
88 }
89}
90
91
92/// ParseExpression - Simple precedence-based parser for binary/ternary
93/// operators.
94///
95/// Note: we diverge from the C99 grammar when parsing the assignment-expression
96/// production. C99 specifies that the LHS of an assignment operator should be
97/// parsed as a unary-expression, but consistency dictates that it be a
98/// conditional-expession. In practice, the important thing here is that the
99/// LHS of an assignment has to be an l-value, which productions between
100/// unary-expression and conditional-expression don't produce. Because we want
101/// consistency, we parse the LHS as a conditional-expression, then check for
102/// l-value-ness in semantic analysis stages.
103///
104/// multiplicative-expression: [C99 6.5.5]
105/// cast-expression
106/// multiplicative-expression '*' cast-expression
107/// multiplicative-expression '/' cast-expression
108/// multiplicative-expression '%' cast-expression
109///
110/// additive-expression: [C99 6.5.6]
111/// multiplicative-expression
112/// additive-expression '+' multiplicative-expression
113/// additive-expression '-' multiplicative-expression
114///
115/// shift-expression: [C99 6.5.7]
116/// additive-expression
117/// shift-expression '<<' additive-expression
118/// shift-expression '>>' additive-expression
119///
120/// relational-expression: [C99 6.5.8]
121/// shift-expression
122/// relational-expression '<' shift-expression
123/// relational-expression '>' shift-expression
124/// relational-expression '<=' shift-expression
125/// relational-expression '>=' shift-expression
126///
127/// equality-expression: [C99 6.5.9]
128/// relational-expression
129/// equality-expression '==' relational-expression
130/// equality-expression '!=' relational-expression
131///
132/// AND-expression: [C99 6.5.10]
133/// equality-expression
134/// AND-expression '&' equality-expression
135///
136/// exclusive-OR-expression: [C99 6.5.11]
137/// AND-expression
138/// exclusive-OR-expression '^' AND-expression
139///
140/// inclusive-OR-expression: [C99 6.5.12]
141/// exclusive-OR-expression
142/// inclusive-OR-expression '|' exclusive-OR-expression
143///
144/// logical-AND-expression: [C99 6.5.13]
145/// inclusive-OR-expression
146/// logical-AND-expression '&&' inclusive-OR-expression
147///
148/// logical-OR-expression: [C99 6.5.14]
149/// logical-AND-expression
150/// logical-OR-expression '||' logical-AND-expression
151///
152/// conditional-expression: [C99 6.5.15]
153/// logical-OR-expression
154/// logical-OR-expression '?' expression ':' conditional-expression
155/// [GNU] logical-OR-expression '?' ':' conditional-expression
156///
157/// assignment-expression: [C99 6.5.16]
158/// conditional-expression
159/// unary-expression assignment-operator assignment-expression
Chris Lattner50dd2892008-02-26 00:51:44 +0000160/// [C++] throw-expression [C++ 15]
Reid Spencer5f016e22007-07-11 17:01:13 +0000161///
162/// assignment-operator: one of
163/// = *= /= %= += -= <<= >>= &= ^= |=
164///
165/// expression: [C99 6.5.17]
166/// assignment-expression
167/// expression ',' assignment-expression
168///
169Parser::ExprResult Parser::ParseExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000170 if (Tok.is(tok::kw_throw))
171 return ParseThrowExpression();
172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 ExprResult LHS = ParseCastExpression(false);
174 if (LHS.isInvalid) return LHS;
175
176 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
177}
178
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000179/// This routine is called when the '@' is seen and consumed.
180/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerc97c2042007-10-03 22:03:06 +0000181/// routine is necessary to disambiguate @try-statement from,
182/// for example, @encode-expression.
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000183///
Fariborz Jahanianb384d322007-10-04 20:19:06 +0000184Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Steve Naroffa642beb2007-10-15 20:55:58 +0000185 ExprResult LHS = ParseObjCAtExpression(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000186 if (LHS.isInvalid) return LHS;
187
188 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
189}
190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
192///
193Parser::ExprResult Parser::ParseAssignmentExpression() {
Chris Lattner50dd2892008-02-26 00:51:44 +0000194 if (Tok.is(tok::kw_throw))
195 return ParseThrowExpression();
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 ExprResult LHS = ParseCastExpression(false);
198 if (LHS.isInvalid) return LHS;
199
200 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
201}
202
Chris Lattnerb93fb492008-06-02 21:31:07 +0000203/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression
204/// where part of an objc message send has already been parsed. In this case
205/// LBracLoc indicates the location of the '[' of the message send, and either
206/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the
207/// message.
208///
209/// Since this handles full assignment-expression's, it handles postfix
210/// expressions and other binary operators for these expressions as well.
211Parser::ExprResult
212Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc,
213 IdentifierInfo *ReceiverName,
214 ExprTy *ReceiverExpr) {
215 ExprResult R = ParseObjCMessageExpressionBody(LBracLoc, ReceiverName,
216 ReceiverExpr);
217 if (R.isInvalid) return R;
218 R = ParsePostfixExpressionSuffix(R);
219 if (R.isInvalid) return R;
220 return ParseRHSOfBinaryExpression(R, 2);
221}
222
223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224Parser::ExprResult Parser::ParseConstantExpression() {
225 ExprResult LHS = ParseCastExpression(false);
226 if (LHS.isInvalid) return LHS;
227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
229}
230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
232/// LHS and has a precedence of at least MinPrec.
233Parser::ExprResult
234Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
235 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
236 SourceLocation ColonLoc;
237
238 while (1) {
239 // If this token has a lower precedence than we are allowed to parse (e.g.
240 // because we are called recursively, or because the token is not a binop),
241 // then we are done!
242 if (NextTokPrec < MinPrec)
243 return LHS;
244
245 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000246 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 ConsumeToken();
248
249 // Special case handling for the ternary operator.
250 ExprResult TernaryMiddle(true);
251 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000252 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // Handle this production specially:
254 // logical-OR-expression '?' expression ':' conditional-expression
255 // In particular, the RHS of the '?' is 'expression', not
256 // 'logical-OR-expression' as we might expect.
257 TernaryMiddle = ParseExpression();
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000258 if (TernaryMiddle.isInvalid) {
259 Actions.DeleteExpr(LHS.Val);
260 return TernaryMiddle;
261 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 } else {
263 // Special case handling of "X ? Y : Z" where Y is empty:
264 // logical-OR-expression '?' ':' conditional-expression [GNU]
265 TernaryMiddle = ExprResult(false);
266 Diag(Tok, diag::ext_gnu_conditional_expr);
267 }
268
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000269 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 Diag(Tok, diag::err_expected_colon);
271 Diag(OpToken, diag::err_matching, "?");
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000272 Actions.DeleteExpr(LHS.Val);
273 Actions.DeleteExpr(TernaryMiddle.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 return ExprResult(true);
275 }
276
277 // Eat the colon.
278 ColonLoc = ConsumeToken();
279 }
280
281 // Parse another leaf here for the RHS of the operator.
282 ExprResult RHS = ParseCastExpression(false);
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000283 if (RHS.isInvalid) {
284 Actions.DeleteExpr(LHS.Val);
285 Actions.DeleteExpr(TernaryMiddle.Val);
286 return RHS;
287 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000288
289 // Remember the precedence of this operator and get the precedence of the
290 // operator immediately to the right of the RHS.
291 unsigned ThisPrec = NextTokPrec;
292 NextTokPrec = getBinOpPrecedence(Tok.getKind());
293
294 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000295 bool isRightAssoc = ThisPrec == prec::Conditional ||
296 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000297
298 // Get the precedence of the operator to the right of the RHS. If it binds
299 // more tightly with RHS than we do, evaluate it completely first.
300 if (ThisPrec < NextTokPrec ||
301 (ThisPrec == NextTokPrec && isRightAssoc)) {
302 // If this is left-associative, only parse things on the RHS that bind
303 // more tightly than the current operator. If it is left-associative, it
304 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
305 // A=(B=(C=D)), where each paren is a level of recursion here.
306 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000307 if (RHS.isInvalid) {
308 Actions.DeleteExpr(LHS.Val);
309 Actions.DeleteExpr(TernaryMiddle.Val);
310 return RHS;
311 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000312
313 NextTokPrec = getBinOpPrecedence(Tok.getKind());
314 }
315 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
316
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000317 if (!LHS.isInvalid) {
318 // Combine the LHS and RHS into the LHS (e.g. build AST).
319 if (TernaryMiddle.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000320 LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(),
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000321 LHS.Val, RHS.Val);
322 else
Steve Narofff69936d2007-09-16 03:34:24 +0000323 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000324 LHS.Val, TernaryMiddle.Val, RHS.Val);
325 } else {
326 // We had a semantic error on the LHS. Just free the RHS and continue.
327 Actions.DeleteExpr(TernaryMiddle.Val);
328 Actions.DeleteExpr(RHS.Val);
329 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 }
331}
332
333/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
334/// true, parse a unary-expression.
335///
336/// cast-expression: [C99 6.5.4]
337/// unary-expression
338/// '(' type-name ')' cast-expression
339///
340/// unary-expression: [C99 6.5.3]
341/// postfix-expression
342/// '++' unary-expression
343/// '--' unary-expression
344/// unary-operator cast-expression
345/// 'sizeof' unary-expression
346/// 'sizeof' '(' type-name ')'
347/// [GNU] '__alignof' unary-expression
348/// [GNU] '__alignof' '(' type-name ')'
349/// [GNU] '&&' identifier
350///
351/// unary-operator: one of
352/// '&' '*' '+' '-' '~' '!'
353/// [GNU] '__extension__' '__real' '__imag'
354///
355/// primary-expression: [C99 6.5.1]
356/// identifier
357/// constant
358/// string-literal
359/// [C++] boolean-literal [C++ 2.13.5]
360/// '(' expression ')'
361/// '__func__' [C99 6.4.2.2]
362/// [GNU] '__FUNCTION__'
363/// [GNU] '__PRETTY_FUNCTION__'
364/// [GNU] '(' compound-statement ')'
365/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
366/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
367/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
368/// assign-expr ')'
369/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
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
Reid Spencer5f016e22007-07-11 17:01:13 +0000375/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
376/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
377/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
378/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
Argyrios Kyrtzidisd7464be2008-07-16 07:23:27 +0000379/// [C++] 'this' [C++ 9.3.2]
Reid Spencer5f016e22007-07-11 17:01:13 +0000380///
381/// constant: [C99 6.4.4]
382/// integer-constant
383/// floating-constant
384/// enumeration-constant -> identifier
385/// character-constant
386///
387Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
388 ExprResult Res;
389 tok::TokenKind SavedKind = Tok.getKind();
390
391 // This handles all of cast-expression, unary-expression, postfix-expression,
392 // and primary-expression. We handle them together like this for efficiency
393 // and to simplify handling of an expression starting with a '(' token: which
394 // may be one of a parenthesized expression, cast-expression, compound literal
395 // expression, or statement expression.
396 //
397 // If the parsed tokens consist of a primary-expression, the cases below
398 // call ParsePostfixExpressionSuffix to handle the postfix expression
399 // suffixes. Cases that cannot be followed by postfix exprs should
400 // return without invoking ParsePostfixExpressionSuffix.
401 switch (SavedKind) {
402 case tok::l_paren: {
403 // If this expression is limited to being a unary-expression, the parent can
404 // not start a cast expression.
405 ParenParseOption ParenExprType =
406 isUnaryExpression ? CompoundLiteral : CastExpr;
407 TypeTy *CastTy;
408 SourceLocation LParenLoc = Tok.getLocation();
409 SourceLocation RParenLoc;
410 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
411 if (Res.isInvalid) return Res;
412
413 switch (ParenExprType) {
414 case SimpleExpr: break; // Nothing else to do.
415 case CompoundStmt: break; // Nothing else to do.
416 case CompoundLiteral:
417 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
418 // postfix-expression exist, parse them now.
419 break;
420 case CastExpr:
421 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
422 // the cast-expression that follows it next.
423 // TODO: For cast expression with CastTy.
424 Res = ParseCastExpression(false);
425 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000426 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 return Res;
428 }
429
430 // These can be followed by postfix-expr pieces.
431 return ParsePostfixExpressionSuffix(Res);
432 }
433
434 // primary-expression
435 case tok::numeric_constant:
436 // constant: integer-constant
437 // constant: floating-constant
438
Steve Narofff69936d2007-09-16 03:34:24 +0000439 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 ConsumeToken();
441
442 // These can be followed by postfix-expr pieces.
443 return ParsePostfixExpressionSuffix(Res);
444
445 case tok::kw_true:
446 case tok::kw_false:
447 return ParseCXXBoolLiteral();
448
449 case tok::identifier: { // primary-expression: identifier
450 // constant: enumeration-constant
451 // Consume the identifier so that we can see if it is followed by a '('.
452 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
453 // need to know whether or not this identifier is a function designator or
454 // not.
455 IdentifierInfo &II = *Tok.getIdentifierInfo();
456 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000457 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 // These can be followed by postfix-expr pieces.
459 return ParsePostfixExpressionSuffix(Res);
460 }
461 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000462 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 ConsumeToken();
464 // These can be followed by postfix-expr pieces.
465 return ParsePostfixExpressionSuffix(Res);
466 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
467 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
468 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerd9f69102008-08-10 01:53:14 +0000469 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 ConsumeToken();
471 // These can be followed by postfix-expr pieces.
472 return ParsePostfixExpressionSuffix(Res);
473 case tok::string_literal: // primary-expression: string-literal
474 case tok::wide_string_literal:
475 Res = ParseStringLiteralExpression();
476 if (Res.isInvalid) return Res;
477 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
478 return ParsePostfixExpressionSuffix(Res);
479 case tok::kw___builtin_va_arg:
480 case tok::kw___builtin_offsetof:
481 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000482 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 case tok::kw___builtin_types_compatible_p:
484 return ParseBuiltinPrimaryExpression();
485 case tok::plusplus: // unary-expression: '++' unary-expression
486 case tok::minusminus: { // unary-expression: '--' unary-expression
487 SourceLocation SavedLoc = ConsumeToken();
488 Res = ParseCastExpression(true);
489 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000490 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return Res;
492 }
493 case tok::amp: // unary-expression: '&' cast-expression
494 case tok::star: // unary-expression: '*' cast-expression
495 case tok::plus: // unary-expression: '+' cast-expression
496 case tok::minus: // unary-expression: '-' cast-expression
497 case tok::tilde: // unary-expression: '~' cast-expression
498 case tok::exclaim: // unary-expression: '!' cast-expression
499 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000500 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 SourceLocation SavedLoc = ConsumeToken();
502 Res = ParseCastExpression(false);
503 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000504 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 return Res;
Chris Lattner35080842008-02-02 20:20:10 +0000506 }
507
508 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
509 // __extension__ silences extension warnings in the subexpression.
510 bool SavedExtWarn = Diags.getWarnOnExtensions();
511 Diags.setWarnOnExtensions(false);
512 SourceLocation SavedLoc = ConsumeToken();
513 Res = ParseCastExpression(false);
514 if (!Res.isInvalid)
515 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
516 Diags.setWarnOnExtensions(SavedExtWarn);
517 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 }
519 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
520 // unary-expression: 'sizeof' '(' type-name ')'
521 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
522 // unary-expression: '__alignof' '(' type-name ')'
523 return ParseSizeofAlignofExpression();
524 case tok::ampamp: { // unary-expression: '&&' identifier
525 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000526 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 Diag(Tok, diag::err_expected_ident);
528 return ExprResult(true);
529 }
530
531 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000532 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 Tok.getIdentifierInfo());
534 ConsumeToken();
535 return Res;
536 }
537 case tok::kw_const_cast:
538 case tok::kw_dynamic_cast:
539 case tok::kw_reinterpret_cast:
540 case tok::kw_static_cast:
541 return ParseCXXCasts();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000542 case tok::kw_this:
543 return ParseCXXThis();
Chris Lattnerc97c2042007-10-03 22:03:06 +0000544 case tok::at: {
545 SourceLocation AtLoc = ConsumeToken();
Steve Naroffa642beb2007-10-15 20:55:58 +0000546 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000547 }
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000548 case tok::l_square:
Steve Naroffa642beb2007-10-15 20:55:58 +0000549 // These can be followed by postfix-expr pieces.
Chris Lattner039a6422008-05-09 05:28:21 +0000550 if (getLang().ObjC1)
551 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
552 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 default:
554 Diag(Tok, diag::err_expected_expression);
555 return ExprResult(true);
556 }
557
558 // unreachable.
559 abort();
560}
561
562/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
563/// is parsed, this method parses any suffixes that apply.
564///
565/// postfix-expression: [C99 6.5.2]
566/// primary-expression
567/// postfix-expression '[' expression ']'
568/// postfix-expression '(' argument-expression-list[opt] ')'
569/// postfix-expression '.' identifier
570/// postfix-expression '->' identifier
571/// postfix-expression '++'
572/// postfix-expression '--'
573/// '(' type-name ')' '{' initializer-list '}'
574/// '(' type-name ')' '{' initializer-list ',' '}'
575///
576/// argument-expression-list: [C99 6.5.2]
577/// argument-expression
578/// argument-expression-list ',' assignment-expression
579///
580Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
581
582 // Now that the primary-expression piece of the postfix-expression has been
583 // parsed, see if there are any postfix-expression pieces here.
584 SourceLocation Loc;
585 while (1) {
586 switch (Tok.getKind()) {
587 default: // Not a postfix-expression suffix.
588 return LHS;
589 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
590 Loc = ConsumeBracket();
591 ExprResult Idx = ParseExpression();
592
593 SourceLocation RLoc = Tok.getLocation();
594
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000595 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square))
Steve Narofff69936d2007-09-16 03:34:24 +0000596 LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 else
598 LHS = ExprResult(true);
599
600 // Match the ']'.
601 MatchRHSPunctuation(tok::r_square, Loc);
602 break;
603 }
604
605 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
606 llvm::SmallVector<ExprTy*, 8> ArgExprs;
607 llvm::SmallVector<SourceLocation, 8> CommaLocs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608
609 Loc = ConsumeParen();
610
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000611 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 while (1) {
613 ExprResult ArgExpr = ParseAssignmentExpression();
614 if (ArgExpr.isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 SkipUntil(tok::r_paren);
Chris Lattner2ff54262007-07-21 05:18:12 +0000616 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 } else
618 ArgExprs.push_back(ArgExpr.Val);
619
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000620 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 break;
622 // Move to the next argument, remember where the comma was.
623 CommaLocs.push_back(ConsumeToken());
624 }
625 }
626
627 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000628 if (!LHS.isInvalid && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
630 "Unexpected number of commas!");
Steve Narofff69936d2007-09-16 03:34:24 +0000631 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 &CommaLocs[0], Tok.getLocation());
633 }
634
Chris Lattner2ff54262007-07-21 05:18:12 +0000635 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 break;
637 }
638 case tok::arrow: // postfix-expression: p-e '->' identifier
639 case tok::period: { // postfix-expression: p-e '.' identifier
640 tok::TokenKind OpKind = Tok.getKind();
641 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
642
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000643 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 Diag(Tok, diag::err_expected_ident);
645 return ExprResult(true);
646 }
647
648 if (!LHS.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000649 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 Tok.getLocation(),
651 *Tok.getIdentifierInfo());
652 ConsumeToken();
653 break;
654 }
655 case tok::plusplus: // postfix-expression: postfix-expression '++'
656 case tok::minusminus: // postfix-expression: postfix-expression '--'
657 if (!LHS.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000658 LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 LHS.Val);
660 ConsumeToken();
661 break;
662 }
663 }
664}
665
666
667/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
668/// unary-expression: [C99 6.5.3]
669/// 'sizeof' unary-expression
670/// 'sizeof' '(' type-name ')'
671/// [GNU] '__alignof' unary-expression
672/// [GNU] '__alignof' '(' type-name ')'
673Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000674 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000676 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 ConsumeToken();
678
679 // If the operand doesn't start with an '(', it must be an expression.
680 ExprResult Operand;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000681 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 Operand = ParseCastExpression(true);
683 } else {
684 // If it starts with a '(', we know that it is either a parenthesized
685 // type-name, or it is a unary-expression that starts with a compound
686 // literal, or starts with a primary-expression that is a parenthesized
687 // expression.
688 ParenParseOption ExprType = CastExpr;
689 TypeTy *CastTy;
690 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
691 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
692
693 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
694 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000695 if (ExprType == CastExpr)
Steve Narofff69936d2007-09-16 03:34:24 +0000696 return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(),
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000697 OpTok.is(tok::kw_sizeof),
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 LParenLoc, CastTy, RParenLoc);
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000699
700 // If this is a parenthesized expression, it is the start of a
701 // unary-expression, but doesn't include any postfix pieces. Parse these
702 // now if present.
703 Operand = ParsePostfixExpressionSuffix(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 }
705
706 // If we get here, the operand to the sizeof/alignof was an expresion.
707 if (!Operand.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000708 Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 Operand.Val);
710 return Operand;
711}
712
713/// ParseBuiltinPrimaryExpression
714///
715/// primary-expression: [C99 6.5.1]
716/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
717/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
718/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
719/// assign-expr ')'
720/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000721/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000722///
723/// [GNU] offsetof-member-designator:
724/// [GNU] identifier
725/// [GNU] offsetof-member-designator '.' identifier
726/// [GNU] offsetof-member-designator '[' expression ']'
727///
728Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
729 ExprResult Res(false);
730 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
731
732 tok::TokenKind T = Tok.getKind();
733 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
734
735 // All of these start with an open paren.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000736 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
738 return ExprResult(true);
739 }
740
741 SourceLocation LParenLoc = ConsumeParen();
742 // TODO: Build AST.
743
744 switch (T) {
745 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000746 case tok::kw___builtin_va_arg: {
747 ExprResult Expr = ParseAssignmentExpression();
748 if (Expr.isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 SkipUntil(tok::r_paren);
750 return Res;
751 }
752
753 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
754 return ExprResult(true);
755
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000756 TypeTy *Ty = ParseTypeName();
Chris Lattner6eb21092007-08-30 15:52:49 +0000757
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000758 if (Tok.isNot(tok::r_paren)) {
759 Diag(Tok, diag::err_expected_rparen);
760 return ExprResult(true);
761 }
762 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000764 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000765 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000766 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000767 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000768
769 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
770 return ExprResult(true);
771
772 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000773 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000774 Diag(Tok, diag::err_expected_ident);
775 SkipUntil(tok::r_paren);
776 return true;
777 }
778
779 // Keep track of the various subcomponents we see.
780 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
781
782 Comps.push_back(Action::OffsetOfComponent());
783 Comps.back().isBrackets = false;
784 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
785 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
787 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000788 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000790 Comps.push_back(Action::OffsetOfComponent());
791 Comps.back().isBrackets = false;
792 Comps.back().LocStart = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000793
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000794 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000795 Diag(Tok, diag::err_expected_ident);
796 SkipUntil(tok::r_paren);
797 return true;
798 }
799 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
800 Comps.back().LocEnd = ConsumeToken();
801
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000802 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000804 Comps.push_back(Action::OffsetOfComponent());
805 Comps.back().isBrackets = true;
806 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 Res = ParseExpression();
808 if (Res.isInvalid) {
809 SkipUntil(tok::r_paren);
810 return Res;
811 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000812 Comps.back().U.E = Res.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000814 Comps.back().LocEnd =
815 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000816 } else if (Tok.is(tok::r_paren)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000817 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner6eb21092007-08-30 15:52:49 +0000818 Comps.size(), ConsumeParen());
819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000821 // Error occurred.
822 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000823 }
824 }
825 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000826 }
Steve Naroffd04fdd52007-08-03 21:21:27 +0000827 case tok::kw___builtin_choose_expr: {
828 ExprResult Cond = ParseAssignmentExpression();
829 if (Cond.isInvalid) {
830 SkipUntil(tok::r_paren);
831 return Cond;
832 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
834 return ExprResult(true);
835
Steve Naroffd04fdd52007-08-03 21:21:27 +0000836 ExprResult Expr1 = ParseAssignmentExpression();
837 if (Expr1.isInvalid) {
838 SkipUntil(tok::r_paren);
839 return Expr1;
840 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
842 return ExprResult(true);
843
Steve Naroffd04fdd52007-08-03 21:21:27 +0000844 ExprResult Expr2 = ParseAssignmentExpression();
845 if (Expr2.isInvalid) {
846 SkipUntil(tok::r_paren);
847 return Expr2;
848 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000849 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000850 Diag(Tok, diag::err_expected_rparen);
851 return ExprResult(true);
852 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000853 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
Chris Lattner6eb21092007-08-30 15:52:49 +0000854 ConsumeParen());
855 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +0000856 }
Nate Begemane2ce1d92008-01-17 17:46:27 +0000857 case tok::kw___builtin_overload: {
858 llvm::SmallVector<ExprTy*, 8> ArgExprs;
859 llvm::SmallVector<SourceLocation, 8> CommaLocs;
860
861 // For each iteration through the loop look for assign-expr followed by a
862 // comma. If there is no comma, break and attempt to match r-paren.
863 if (Tok.isNot(tok::r_paren)) {
864 while (1) {
865 ExprResult ArgExpr = ParseAssignmentExpression();
866 if (ArgExpr.isInvalid) {
867 SkipUntil(tok::r_paren);
868 return ExprResult(true);
869 } else
870 ArgExprs.push_back(ArgExpr.Val);
871
872 if (Tok.isNot(tok::comma))
873 break;
874 // Move to the next argument, remember where the comma was.
875 CommaLocs.push_back(ConsumeToken());
876 }
877 }
878
879 // Attempt to consume the r-paren
880 if (Tok.isNot(tok::r_paren)) {
881 Diag(Tok, diag::err_expected_rparen);
882 SkipUntil(tok::r_paren);
883 return ExprResult(true);
884 }
Nate Begemane2ce1d92008-01-17 17:46:27 +0000885 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(),
886 &CommaLocs[0], StartLoc, ConsumeParen());
887 break;
888 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +0000890 TypeTy *Ty1 = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000891
892 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
893 return ExprResult(true);
894
Steve Naroff363bcff2007-08-01 23:45:51 +0000895 TypeTy *Ty2 = ParseTypeName();
896
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000897 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +0000898 Diag(Tok, diag::err_expected_rparen);
899 return ExprResult(true);
900 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000901 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +0000902 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 }
904
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 // These can be followed by postfix-expr pieces because they are
906 // primary-expressions.
907 return ParsePostfixExpressionSuffix(Res);
908}
909
910/// ParseParenExpression - This parses the unit that starts with a '(' token,
911/// based on what is allowed by ExprType. The actual thing parsed is returned
912/// in ExprType.
913///
914/// primary-expression: [C99 6.5.1]
915/// '(' expression ')'
916/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
917/// postfix-expression: [C99 6.5.2]
918/// '(' type-name ')' '{' initializer-list '}'
919/// '(' type-name ')' '{' initializer-list ',' '}'
920/// cast-expression: [C99 6.5.4]
921/// '(' type-name ')' cast-expression
922///
923Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
924 TypeTy *&CastTy,
925 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000926 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 SourceLocation OpenLoc = ConsumeParen();
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000928 ExprResult Result(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 CastTy = 0;
930
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000931 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattner98414c12007-08-31 21:49:55 +0000933 Parser::StmtResult Stmt = ParseCompoundStatement(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 ExprType = CompoundStmt;
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000935
936 // If the substmt parsed correctly, build the AST node.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000937 if (!Stmt.isInvalid && Tok.is(tok::r_paren))
Steve Naroff1b273c42007-09-16 14:56:35 +0000938 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
941 // Otherwise, this is a compound literal expression or cast expression.
942 TypeTy *Ty = ParseTypeName();
943
944 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000945 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 RParenLoc = ConsumeParen();
947 else
948 MatchRHSPunctuation(tok::r_paren, OpenLoc);
949
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000950 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 if (!getLang().C99) // Compound literals don't exist in C90.
952 Diag(OpenLoc, diag::ext_c99_compound_literal);
953 Result = ParseInitializer();
954 ExprType = CompoundLiteral;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000955 if (!Result.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000956 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 } else if (ExprType == CastExpr) {
958 // Note that this doesn't parse the subsequence cast-expression, it just
959 // returns the parsed type to the callee.
960 ExprType = CastExpr;
961 CastTy = Ty;
962 return ExprResult(false);
963 } else {
964 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
965 return ExprResult(true);
966 }
967 return Result;
968 } else {
969 Result = ParseExpression();
970 ExprType = SimpleExpr;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000971 if (!Result.isInvalid && Tok.is(tok::r_paren))
Steve Narofff69936d2007-09-16 03:34:24 +0000972 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 }
974
975 // Match the ')'.
976 if (Result.isInvalid)
977 SkipUntil(tok::r_paren);
978 else {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000979 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 RParenLoc = ConsumeParen();
981 else
982 MatchRHSPunctuation(tok::r_paren, OpenLoc);
983 }
984
985 return Result;
986}
987
988/// ParseStringLiteralExpression - This handles the various token types that
989/// form string literals, and also handles string concatenation [C99 5.1.1.2,
990/// translation phase #6].
991///
992/// primary-expression: [C99 6.5.1]
993/// string-literal
994Parser::ExprResult Parser::ParseStringLiteralExpression() {
995 assert(isTokenStringLiteral() && "Not a string literal!");
996
997 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
998 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +0000999 llvm::SmallVector<Token, 4> StringToks;
Reid Spencer5f016e22007-07-11 17:01:13 +00001000
1001 do {
1002 StringToks.push_back(Tok);
1003 ConsumeStringToken();
1004 } while (isTokenStringLiteral());
1005
1006 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Narofff69936d2007-09-16 03:34:24 +00001007 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001008}