blob: 67ce5f1a545bdec9fc416772137ae29870bf5678 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation. Expressions in
11// C99 basically consist of a bunch of binary operators with unary operators and
12// other random stuff at the leaves.
13//
14// In the C99 grammar, these unary operators bind tightest and are represented
15// as the 'cast-expression' production. Everything else is either a binary
16// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
17// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
19//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
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
160///
161/// assignment-operator: one of
162/// = *= /= %= += -= <<= >>= &= ^= |=
163///
164/// expression: [C99 6.5.17]
165/// assignment-expression
166/// expression ',' assignment-expression
167///
168Parser::ExprResult Parser::ParseExpression() {
169 ExprResult LHS = ParseCastExpression(false);
170 if (LHS.isInvalid) return LHS;
171
172 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
173}
174
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000175/// This routine is called when the '@' is seen and consumed.
176/// Current token is an Identifier and is not a 'try'. This
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000177/// routine is necessary to disambiguate @try-statement from,
178/// for example, @encode-expression.
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000179///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +0000180Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
Steve Narofffb9dd752007-10-15 20:55:58 +0000181 ExprResult LHS = ParseObjCAtExpression(AtLoc);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000182 if (LHS.isInvalid) return LHS;
183
184 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
185}
186
Chris Lattner4b009652007-07-25 00:24:17 +0000187/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
188///
189Parser::ExprResult Parser::ParseAssignmentExpression() {
190 ExprResult LHS = ParseCastExpression(false);
191 if (LHS.isInvalid) return LHS;
192
193 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
194}
195
196Parser::ExprResult Parser::ParseConstantExpression() {
197 ExprResult LHS = ParseCastExpression(false);
198 if (LHS.isInvalid) return LHS;
199
Chris Lattner4b009652007-07-25 00:24:17 +0000200 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
201}
202
203/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
204/// in contexts where we have already consumed an identifier (which we saved in
205/// 'IdTok'), then discovered that the identifier was really the leading token
206/// of part of an expression. For example, in "A[1]+B", we consumed "A" (which
207/// is now in 'IdTok') and the current token is "[".
208Parser::ExprResult Parser::
209ParseExpressionWithLeadingIdentifier(const Token &IdTok) {
210 // We know that 'IdTok' must correspond to this production:
211 // primary-expression: identifier
212
213 // Let the actions module handle the identifier.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000214 ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000215 *IdTok.getIdentifierInfo(),
Chris Lattner4d7d2342007-10-09 17:41:39 +0000216 Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000217
218 // Because we have to parse an entire cast-expression before starting the
219 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
220 // need to handle the 'postfix-expression' rules. We do this by invoking
221 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
222 Res = ParsePostfixExpressionSuffix(Res);
223 if (Res.isInvalid) return Res;
224
225 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
226 // done, we know we don't have to do anything for cast-expression, because the
227 // only non-postfix-expression production starts with a '(' token, and we know
228 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
229 // to consume any trailing operators (e.g. "+" in this example) and connected
230 // chunks of the expression.
231 return ParseRHSOfBinaryExpression(Res, prec::Comma);
232}
233
234/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
235/// in contexts where we have already consumed an identifier (which we saved in
236/// 'IdTok'), then discovered that the identifier was really the leading token
237/// of part of an assignment-expression. For example, in "A[1]+B", we consumed
238/// "A" (which is now in 'IdTok') and the current token is "[".
239Parser::ExprResult Parser::
240ParseAssignmentExprWithLeadingIdentifier(const Token &IdTok) {
241 // We know that 'IdTok' must correspond to this production:
242 // primary-expression: identifier
243
244 // Let the actions module handle the identifier.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000245 ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000246 *IdTok.getIdentifierInfo(),
Chris Lattner4d7d2342007-10-09 17:41:39 +0000247 Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000248
249 // Because we have to parse an entire cast-expression before starting the
250 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
251 // need to handle the 'postfix-expression' rules. We do this by invoking
252 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
253 Res = ParsePostfixExpressionSuffix(Res);
254 if (Res.isInvalid) return Res;
255
256 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
257 // done, we know we don't have to do anything for cast-expression, because the
258 // only non-postfix-expression production starts with a '(' token, and we know
259 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
260 // to consume any trailing operators (e.g. "+" in this example) and connected
261 // chunks of the expression.
262 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
263}
264
265
266/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
267/// used in contexts where we have already consumed a '*' (which we saved in
268/// 'StarTok'), then discovered that the '*' was really the leading token of an
269/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
270/// now in 'StarTok') and the current token is "(".
271Parser::ExprResult Parser::
272ParseAssignmentExpressionWithLeadingStar(const Token &StarTok) {
273 // We know that 'StarTok' must correspond to this production:
274 // unary-expression: unary-operator cast-expression
275 // where 'unary-operator' is '*'.
276
277 // Parse the cast-expression that follows the '*'. This will parse the
278 // "*(int*)P" part of "*(int*)P+B".
279 ExprResult Res = ParseCastExpression(false);
280 if (Res.isInvalid) return Res;
281
282 // Combine StarTok + Res to get the new AST for the combined expression..
Steve Naroff87d58b42007-09-16 03:34:24 +0000283 Res = Actions.ActOnUnaryOp(StarTok.getLocation(), tok::star, Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000284 if (Res.isInvalid) return Res;
285
286
287 // We have to parse an entire cast-expression before starting the
288 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
289 // we know that the only production above us is the cast-expression
290 // production, and because the only alternative productions start with a '('
291 // token (we know we had a '*'), there is no work to do to get a whole
292 // cast-expression.
293
294 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
295 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
296 // trailing operators (e.g. "+" in this example) and connected chunks of the
297 // assignment-expression.
298 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
299}
300
301
302/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
303/// LHS and has a precedence of at least MinPrec.
304Parser::ExprResult
305Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
306 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
307 SourceLocation ColonLoc;
308
309 while (1) {
310 // If this token has a lower precedence than we are allowed to parse (e.g.
311 // because we are called recursively, or because the token is not a binop),
312 // then we are done!
313 if (NextTokPrec < MinPrec)
314 return LHS;
315
316 // Consume the operator, saving the operator token for error reporting.
317 Token OpToken = Tok;
318 ConsumeToken();
319
320 // Special case handling for the ternary operator.
321 ExprResult TernaryMiddle(true);
322 if (NextTokPrec == prec::Conditional) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000323 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000324 // Handle this production specially:
325 // logical-OR-expression '?' expression ':' conditional-expression
326 // In particular, the RHS of the '?' is 'expression', not
327 // 'logical-OR-expression' as we might expect.
328 TernaryMiddle = ParseExpression();
Chris Lattner214cbaf2007-08-31 04:58:34 +0000329 if (TernaryMiddle.isInvalid) {
330 Actions.DeleteExpr(LHS.Val);
331 return TernaryMiddle;
332 }
Chris Lattner4b009652007-07-25 00:24:17 +0000333 } else {
334 // Special case handling of "X ? Y : Z" where Y is empty:
335 // logical-OR-expression '?' ':' conditional-expression [GNU]
336 TernaryMiddle = ExprResult(false);
337 Diag(Tok, diag::ext_gnu_conditional_expr);
338 }
339
Chris Lattner4d7d2342007-10-09 17:41:39 +0000340 if (Tok.isNot(tok::colon)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000341 Diag(Tok, diag::err_expected_colon);
342 Diag(OpToken, diag::err_matching, "?");
Chris Lattner214cbaf2007-08-31 04:58:34 +0000343 Actions.DeleteExpr(LHS.Val);
344 Actions.DeleteExpr(TernaryMiddle.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000345 return ExprResult(true);
346 }
347
348 // Eat the colon.
349 ColonLoc = ConsumeToken();
350 }
351
352 // Parse another leaf here for the RHS of the operator.
353 ExprResult RHS = ParseCastExpression(false);
Chris Lattner214cbaf2007-08-31 04:58:34 +0000354 if (RHS.isInvalid) {
355 Actions.DeleteExpr(LHS.Val);
356 Actions.DeleteExpr(TernaryMiddle.Val);
357 return RHS;
358 }
Chris Lattner4b009652007-07-25 00:24:17 +0000359
360 // Remember the precedence of this operator and get the precedence of the
361 // operator immediately to the right of the RHS.
362 unsigned ThisPrec = NextTokPrec;
363 NextTokPrec = getBinOpPrecedence(Tok.getKind());
364
365 // Assignment and conditional expressions are right-associative.
Chris Lattner464c7f62007-12-18 06:06:23 +0000366 bool isRightAssoc = ThisPrec == prec::Conditional ||
367 ThisPrec == prec::Assignment;
Chris Lattner4b009652007-07-25 00:24:17 +0000368
369 // Get the precedence of the operator to the right of the RHS. If it binds
370 // more tightly with RHS than we do, evaluate it completely first.
371 if (ThisPrec < NextTokPrec ||
372 (ThisPrec == NextTokPrec && isRightAssoc)) {
373 // If this is left-associative, only parse things on the RHS that bind
374 // more tightly than the current operator. If it is left-associative, it
375 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
376 // A=(B=(C=D)), where each paren is a level of recursion here.
377 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattner214cbaf2007-08-31 04:58:34 +0000378 if (RHS.isInvalid) {
379 Actions.DeleteExpr(LHS.Val);
380 Actions.DeleteExpr(TernaryMiddle.Val);
381 return RHS;
382 }
Chris Lattner4b009652007-07-25 00:24:17 +0000383
384 NextTokPrec = getBinOpPrecedence(Tok.getKind());
385 }
386 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
387
Chris Lattner4a149b62007-08-31 05:01:50 +0000388 if (!LHS.isInvalid) {
389 // Combine the LHS and RHS into the LHS (e.g. build AST).
390 if (TernaryMiddle.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000391 LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(),
Chris Lattner4a149b62007-08-31 05:01:50 +0000392 LHS.Val, RHS.Val);
393 else
Steve Naroff87d58b42007-09-16 03:34:24 +0000394 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Chris Lattner4a149b62007-08-31 05:01:50 +0000395 LHS.Val, TernaryMiddle.Val, RHS.Val);
396 } else {
397 // We had a semantic error on the LHS. Just free the RHS and continue.
398 Actions.DeleteExpr(TernaryMiddle.Val);
399 Actions.DeleteExpr(RHS.Val);
400 }
Chris Lattner4b009652007-07-25 00:24:17 +0000401 }
402}
403
404/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
405/// true, parse a unary-expression.
406///
407/// cast-expression: [C99 6.5.4]
408/// unary-expression
409/// '(' type-name ')' cast-expression
410///
411/// unary-expression: [C99 6.5.3]
412/// postfix-expression
413/// '++' unary-expression
414/// '--' unary-expression
415/// unary-operator cast-expression
416/// 'sizeof' unary-expression
417/// 'sizeof' '(' type-name ')'
418/// [GNU] '__alignof' unary-expression
419/// [GNU] '__alignof' '(' type-name ')'
420/// [GNU] '&&' identifier
421///
422/// unary-operator: one of
423/// '&' '*' '+' '-' '~' '!'
424/// [GNU] '__extension__' '__real' '__imag'
425///
426/// primary-expression: [C99 6.5.1]
427/// identifier
428/// constant
429/// string-literal
430/// [C++] boolean-literal [C++ 2.13.5]
431/// '(' expression ')'
432/// '__func__' [C99 6.4.2.2]
433/// [GNU] '__FUNCTION__'
434/// [GNU] '__PRETTY_FUNCTION__'
435/// [GNU] '(' compound-statement ')'
436/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
437/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
438/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
439/// assign-expr ')'
440/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000441/// [OBJC] '[' objc-message-expr ']'
Chris Lattner8d050af2008-01-25 18:58:06 +0000442/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian628abf12007-09-26 17:03:44 +0000443/// [OBJC] '@protocol' '(' identifier ')'
444/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000445/// [OBJC] objc-string-literal
Chris Lattner4b009652007-07-25 00:24:17 +0000446/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
447/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
448/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
449/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
450///
451/// constant: [C99 6.4.4]
452/// integer-constant
453/// floating-constant
454/// enumeration-constant -> identifier
455/// character-constant
456///
457Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
458 ExprResult Res;
459 tok::TokenKind SavedKind = Tok.getKind();
460
461 // This handles all of cast-expression, unary-expression, postfix-expression,
462 // and primary-expression. We handle them together like this for efficiency
463 // and to simplify handling of an expression starting with a '(' token: which
464 // may be one of a parenthesized expression, cast-expression, compound literal
465 // expression, or statement expression.
466 //
467 // If the parsed tokens consist of a primary-expression, the cases below
468 // call ParsePostfixExpressionSuffix to handle the postfix expression
469 // suffixes. Cases that cannot be followed by postfix exprs should
470 // return without invoking ParsePostfixExpressionSuffix.
471 switch (SavedKind) {
472 case tok::l_paren: {
473 // If this expression is limited to being a unary-expression, the parent can
474 // not start a cast expression.
475 ParenParseOption ParenExprType =
476 isUnaryExpression ? CompoundLiteral : CastExpr;
477 TypeTy *CastTy;
478 SourceLocation LParenLoc = Tok.getLocation();
479 SourceLocation RParenLoc;
480 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
481 if (Res.isInvalid) return Res;
482
483 switch (ParenExprType) {
484 case SimpleExpr: break; // Nothing else to do.
485 case CompoundStmt: break; // Nothing else to do.
486 case CompoundLiteral:
487 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
488 // postfix-expression exist, parse them now.
489 break;
490 case CastExpr:
491 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
492 // the cast-expression that follows it next.
493 // TODO: For cast expression with CastTy.
494 Res = ParseCastExpression(false);
495 if (!Res.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000496 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 return Res;
498 }
499
500 // These can be followed by postfix-expr pieces.
501 return ParsePostfixExpressionSuffix(Res);
502 }
503
504 // primary-expression
505 case tok::numeric_constant:
506 // constant: integer-constant
507 // constant: floating-constant
508
Steve Naroff87d58b42007-09-16 03:34:24 +0000509 Res = Actions.ActOnNumericConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000510 ConsumeToken();
511
512 // These can be followed by postfix-expr pieces.
513 return ParsePostfixExpressionSuffix(Res);
514
515 case tok::kw_true:
516 case tok::kw_false:
517 return ParseCXXBoolLiteral();
518
519 case tok::identifier: { // primary-expression: identifier
520 // constant: enumeration-constant
521 // Consume the identifier so that we can see if it is followed by a '('.
522 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
523 // need to know whether or not this identifier is a function designator or
524 // not.
525 IdentifierInfo &II = *Tok.getIdentifierInfo();
526 SourceLocation L = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000527 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Chris Lattner4b009652007-07-25 00:24:17 +0000528 // These can be followed by postfix-expr pieces.
529 return ParsePostfixExpressionSuffix(Res);
530 }
531 case tok::char_constant: // constant: character-constant
Steve Naroff87d58b42007-09-16 03:34:24 +0000532 Res = Actions.ActOnCharacterConstant(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000533 ConsumeToken();
534 // These can be followed by postfix-expr pieces.
535 return ParsePostfixExpressionSuffix(Res);
536 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
537 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
538 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Steve Naroff87d58b42007-09-16 03:34:24 +0000539 Res = Actions.ActOnPreDefinedExpr(Tok.getLocation(), SavedKind);
Chris Lattner4b009652007-07-25 00:24:17 +0000540 ConsumeToken();
541 // These can be followed by postfix-expr pieces.
542 return ParsePostfixExpressionSuffix(Res);
543 case tok::string_literal: // primary-expression: string-literal
544 case tok::wide_string_literal:
545 Res = ParseStringLiteralExpression();
546 if (Res.isInvalid) return Res;
547 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
548 return ParsePostfixExpressionSuffix(Res);
549 case tok::kw___builtin_va_arg:
550 case tok::kw___builtin_offsetof:
551 case tok::kw___builtin_choose_expr:
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000552 case tok::kw___builtin_overload:
Chris Lattner4b009652007-07-25 00:24:17 +0000553 case tok::kw___builtin_types_compatible_p:
554 return ParseBuiltinPrimaryExpression();
555 case tok::plusplus: // unary-expression: '++' unary-expression
556 case tok::minusminus: { // unary-expression: '--' unary-expression
557 SourceLocation SavedLoc = ConsumeToken();
558 Res = ParseCastExpression(true);
559 if (!Res.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000560 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000561 return Res;
562 }
563 case tok::amp: // unary-expression: '&' cast-expression
564 case tok::star: // unary-expression: '*' cast-expression
565 case tok::plus: // unary-expression: '+' cast-expression
566 case tok::minus: // unary-expression: '-' cast-expression
567 case tok::tilde: // unary-expression: '~' cast-expression
568 case tok::exclaim: // unary-expression: '!' cast-expression
569 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner6cf92942008-02-02 20:20:10 +0000570 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner4b009652007-07-25 00:24:17 +0000571 SourceLocation SavedLoc = ConsumeToken();
572 Res = ParseCastExpression(false);
573 if (!Res.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000574 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000575 return Res;
Chris Lattner6cf92942008-02-02 20:20:10 +0000576 }
577
578 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
579 // __extension__ silences extension warnings in the subexpression.
580 bool SavedExtWarn = Diags.getWarnOnExtensions();
581 Diags.setWarnOnExtensions(false);
582 SourceLocation SavedLoc = ConsumeToken();
583 Res = ParseCastExpression(false);
584 if (!Res.isInvalid)
585 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
586 Diags.setWarnOnExtensions(SavedExtWarn);
587 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000588 }
589 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
590 // unary-expression: 'sizeof' '(' type-name ')'
591 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
592 // unary-expression: '__alignof' '(' type-name ')'
593 return ParseSizeofAlignofExpression();
594 case tok::ampamp: { // unary-expression: '&&' identifier
595 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattner4d7d2342007-10-09 17:41:39 +0000596 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000597 Diag(Tok, diag::err_expected_ident);
598 return ExprResult(true);
599 }
600
601 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000602 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +0000603 Tok.getIdentifierInfo());
604 ConsumeToken();
605 return Res;
606 }
607 case tok::kw_const_cast:
608 case tok::kw_dynamic_cast:
609 case tok::kw_reinterpret_cast:
610 case tok::kw_static_cast:
611 return ParseCXXCasts();
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000612 case tok::at: {
613 SourceLocation AtLoc = ConsumeToken();
Steve Narofffb9dd752007-10-15 20:55:58 +0000614 return ParseObjCAtExpression(AtLoc);
Chris Lattnerb82d6ef2007-10-03 22:03:06 +0000615 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000616 case tok::l_square:
Steve Narofffb9dd752007-10-15 20:55:58 +0000617 // These can be followed by postfix-expr pieces.
618 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
Chris Lattner4b009652007-07-25 00:24:17 +0000619 default:
620 Diag(Tok, diag::err_expected_expression);
621 return ExprResult(true);
622 }
623
624 // unreachable.
625 abort();
626}
627
628/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
629/// is parsed, this method parses any suffixes that apply.
630///
631/// postfix-expression: [C99 6.5.2]
632/// primary-expression
633/// postfix-expression '[' expression ']'
634/// postfix-expression '(' argument-expression-list[opt] ')'
635/// postfix-expression '.' identifier
636/// postfix-expression '->' identifier
637/// postfix-expression '++'
638/// postfix-expression '--'
639/// '(' type-name ')' '{' initializer-list '}'
640/// '(' type-name ')' '{' initializer-list ',' '}'
641///
642/// argument-expression-list: [C99 6.5.2]
643/// argument-expression
644/// argument-expression-list ',' assignment-expression
645///
646Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
647
648 // Now that the primary-expression piece of the postfix-expression has been
649 // parsed, see if there are any postfix-expression pieces here.
650 SourceLocation Loc;
651 while (1) {
652 switch (Tok.getKind()) {
653 default: // Not a postfix-expression suffix.
654 return LHS;
655 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
656 Loc = ConsumeBracket();
657 ExprResult Idx = ParseExpression();
658
659 SourceLocation RLoc = Tok.getLocation();
660
Chris Lattner4d7d2342007-10-09 17:41:39 +0000661 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square))
Steve Naroff87d58b42007-09-16 03:34:24 +0000662 LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000663 else
664 LHS = ExprResult(true);
665
666 // Match the ']'.
667 MatchRHSPunctuation(tok::r_square, Loc);
668 break;
669 }
670
671 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
672 llvm::SmallVector<ExprTy*, 8> ArgExprs;
673 llvm::SmallVector<SourceLocation, 8> CommaLocs;
674
675 Loc = ConsumeParen();
676
Chris Lattner4d7d2342007-10-09 17:41:39 +0000677 if (Tok.isNot(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000678 while (1) {
679 ExprResult ArgExpr = ParseAssignmentExpression();
680 if (ArgExpr.isInvalid) {
681 SkipUntil(tok::r_paren);
682 return ExprResult(true);
683 } else
684 ArgExprs.push_back(ArgExpr.Val);
685
Chris Lattner4d7d2342007-10-09 17:41:39 +0000686 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000687 break;
688 // Move to the next argument, remember where the comma was.
689 CommaLocs.push_back(ConsumeToken());
690 }
691 }
692
693 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000694 if (!LHS.isInvalid && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000695 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
696 "Unexpected number of commas!");
Steve Naroff87d58b42007-09-16 03:34:24 +0000697 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000698 &CommaLocs[0], Tok.getLocation());
699 }
700
701 MatchRHSPunctuation(tok::r_paren, Loc);
702 break;
703 }
704 case tok::arrow: // postfix-expression: p-e '->' identifier
705 case tok::period: { // postfix-expression: p-e '.' identifier
706 tok::TokenKind OpKind = Tok.getKind();
707 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
708
Chris Lattner4d7d2342007-10-09 17:41:39 +0000709 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000710 Diag(Tok, diag::err_expected_ident);
711 return ExprResult(true);
712 }
713
714 if (!LHS.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000715 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
Chris Lattner4b009652007-07-25 00:24:17 +0000716 Tok.getLocation(),
717 *Tok.getIdentifierInfo());
718 ConsumeToken();
719 break;
720 }
721 case tok::plusplus: // postfix-expression: postfix-expression '++'
722 case tok::minusminus: // postfix-expression: postfix-expression '--'
723 if (!LHS.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000724 LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
Chris Lattner4b009652007-07-25 00:24:17 +0000725 LHS.Val);
726 ConsumeToken();
727 break;
728 }
729 }
730}
731
732
733/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
734/// unary-expression: [C99 6.5.3]
735/// 'sizeof' unary-expression
736/// 'sizeof' '(' type-name ')'
737/// [GNU] '__alignof' unary-expression
738/// [GNU] '__alignof' '(' type-name ')'
739Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000740 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000741 "Not a sizeof/alignof expression!");
742 Token OpTok = Tok;
743 ConsumeToken();
744
745 // If the operand doesn't start with an '(', it must be an expression.
746 ExprResult Operand;
Chris Lattner4d7d2342007-10-09 17:41:39 +0000747 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000748 Operand = ParseCastExpression(true);
749 } else {
750 // If it starts with a '(', we know that it is either a parenthesized
751 // type-name, or it is a unary-expression that starts with a compound
752 // literal, or starts with a primary-expression that is a parenthesized
753 // expression.
754 ParenParseOption ExprType = CastExpr;
755 TypeTy *CastTy;
756 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
757 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
758
759 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
760 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner48553562007-11-13 20:50:37 +0000761 if (ExprType == CastExpr)
Steve Naroff87d58b42007-09-16 03:34:24 +0000762 return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(),
Chris Lattner4d7d2342007-10-09 17:41:39 +0000763 OpTok.is(tok::kw_sizeof),
Chris Lattner4b009652007-07-25 00:24:17 +0000764 LParenLoc, CastTy, RParenLoc);
Chris Lattner48553562007-11-13 20:50:37 +0000765
766 // If this is a parenthesized expression, it is the start of a
767 // unary-expression, but doesn't include any postfix pieces. Parse these
768 // now if present.
769 Operand = ParsePostfixExpressionSuffix(Operand);
Chris Lattner4b009652007-07-25 00:24:17 +0000770 }
771
772 // If we get here, the operand to the sizeof/alignof was an expresion.
773 if (!Operand.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +0000774 Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(),
Chris Lattner4b009652007-07-25 00:24:17 +0000775 Operand.Val);
776 return Operand;
777}
778
779/// ParseBuiltinPrimaryExpression
780///
781/// primary-expression: [C99 6.5.1]
782/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
783/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
784/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
785/// assign-expr ')'
786/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000787/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000788///
789/// [GNU] offsetof-member-designator:
790/// [GNU] identifier
791/// [GNU] offsetof-member-designator '.' identifier
792/// [GNU] offsetof-member-designator '[' expression ']'
793///
794Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
795 ExprResult Res(false);
796 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
797
798 tok::TokenKind T = Tok.getKind();
799 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
800
801 // All of these start with an open paren.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000802 if (Tok.isNot(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000803 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
804 return ExprResult(true);
805 }
806
807 SourceLocation LParenLoc = ConsumeParen();
808 // TODO: Build AST.
809
810 switch (T) {
811 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson36760332007-10-15 20:28:48 +0000812 case tok::kw___builtin_va_arg: {
813 ExprResult Expr = ParseAssignmentExpression();
814 if (Expr.isInvalid) {
Chris Lattner4b009652007-07-25 00:24:17 +0000815 SkipUntil(tok::r_paren);
816 return Res;
817 }
818
819 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
820 return ExprResult(true);
821
Anders Carlsson36760332007-10-15 20:28:48 +0000822 TypeTy *Ty = ParseTypeName();
Chris Lattnercb8943a2007-08-30 15:52:49 +0000823
Anders Carlsson36760332007-10-15 20:28:48 +0000824 if (Tok.isNot(tok::r_paren)) {
825 Diag(Tok, diag::err_expected_rparen);
826 return ExprResult(true);
827 }
828 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen());
Chris Lattner4b009652007-07-25 00:24:17 +0000829 break;
Anders Carlsson36760332007-10-15 20:28:48 +0000830 }
Chris Lattner69638b12007-08-30 15:51:11 +0000831 case tok::kw___builtin_offsetof: {
Chris Lattner1b6b5be2007-08-30 17:08:45 +0000832 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattner69638b12007-08-30 15:51:11 +0000833 TypeTy *Ty = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000834
835 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
836 return ExprResult(true);
837
838 // We must have at least one identifier here.
Chris Lattner4d7d2342007-10-09 17:41:39 +0000839 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000840 Diag(Tok, diag::err_expected_ident);
841 SkipUntil(tok::r_paren);
842 return true;
843 }
844
845 // Keep track of the various subcomponents we see.
846 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
847
848 Comps.push_back(Action::OffsetOfComponent());
849 Comps.back().isBrackets = false;
850 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
851 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000852
853 while (1) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000854 if (Tok.is(tok::period)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000855 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattner69638b12007-08-30 15:51:11 +0000856 Comps.push_back(Action::OffsetOfComponent());
857 Comps.back().isBrackets = false;
858 Comps.back().LocStart = ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +0000859
Chris Lattner4d7d2342007-10-09 17:41:39 +0000860 if (Tok.isNot(tok::identifier)) {
Chris Lattner69638b12007-08-30 15:51:11 +0000861 Diag(Tok, diag::err_expected_ident);
862 SkipUntil(tok::r_paren);
863 return true;
864 }
865 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
866 Comps.back().LocEnd = ConsumeToken();
867
Chris Lattner4d7d2342007-10-09 17:41:39 +0000868 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000869 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner69638b12007-08-30 15:51:11 +0000870 Comps.push_back(Action::OffsetOfComponent());
871 Comps.back().isBrackets = true;
872 Comps.back().LocStart = ConsumeBracket();
Chris Lattner4b009652007-07-25 00:24:17 +0000873 Res = ParseExpression();
874 if (Res.isInvalid) {
875 SkipUntil(tok::r_paren);
876 return Res;
877 }
Chris Lattner69638b12007-08-30 15:51:11 +0000878 Comps.back().U.E = Res.Val;
Chris Lattner4b009652007-07-25 00:24:17 +0000879
Chris Lattner69638b12007-08-30 15:51:11 +0000880 Comps.back().LocEnd =
881 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4d7d2342007-10-09 17:41:39 +0000882 } else if (Tok.is(tok::r_paren)) {
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000883 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattnercb8943a2007-08-30 15:52:49 +0000884 Comps.size(), ConsumeParen());
885 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000886 } else {
Chris Lattner69638b12007-08-30 15:51:11 +0000887 // Error occurred.
888 return ExprResult(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000889 }
890 }
891 break;
Chris Lattner69638b12007-08-30 15:51:11 +0000892 }
Steve Naroff93c53012007-08-03 21:21:27 +0000893 case tok::kw___builtin_choose_expr: {
894 ExprResult Cond = ParseAssignmentExpression();
895 if (Cond.isInvalid) {
896 SkipUntil(tok::r_paren);
897 return Cond;
898 }
Chris Lattner4b009652007-07-25 00:24:17 +0000899 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
900 return ExprResult(true);
901
Steve Naroff93c53012007-08-03 21:21:27 +0000902 ExprResult Expr1 = ParseAssignmentExpression();
903 if (Expr1.isInvalid) {
904 SkipUntil(tok::r_paren);
905 return Expr1;
906 }
Chris Lattner4b009652007-07-25 00:24:17 +0000907 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
908 return ExprResult(true);
909
Steve Naroff93c53012007-08-03 21:21:27 +0000910 ExprResult Expr2 = ParseAssignmentExpression();
911 if (Expr2.isInvalid) {
912 SkipUntil(tok::r_paren);
913 return Expr2;
914 }
Chris Lattner4d7d2342007-10-09 17:41:39 +0000915 if (Tok.isNot(tok::r_paren)) {
Steve Naroff93c53012007-08-03 21:21:27 +0000916 Diag(Tok, diag::err_expected_rparen);
917 return ExprResult(true);
918 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000919 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
Chris Lattnercb8943a2007-08-30 15:52:49 +0000920 ConsumeParen());
921 break;
Steve Naroff93c53012007-08-03 21:21:27 +0000922 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000923 case tok::kw___builtin_overload: {
924 llvm::SmallVector<ExprTy*, 8> ArgExprs;
925 llvm::SmallVector<SourceLocation, 8> CommaLocs;
926
927 // For each iteration through the loop look for assign-expr followed by a
928 // comma. If there is no comma, break and attempt to match r-paren.
929 if (Tok.isNot(tok::r_paren)) {
930 while (1) {
931 ExprResult ArgExpr = ParseAssignmentExpression();
932 if (ArgExpr.isInvalid) {
933 SkipUntil(tok::r_paren);
934 return ExprResult(true);
935 } else
936 ArgExprs.push_back(ArgExpr.Val);
937
938 if (Tok.isNot(tok::comma))
939 break;
940 // Move to the next argument, remember where the comma was.
941 CommaLocs.push_back(ConsumeToken());
942 }
943 }
944
945 // Attempt to consume the r-paren
946 if (Tok.isNot(tok::r_paren)) {
947 Diag(Tok, diag::err_expected_rparen);
948 SkipUntil(tok::r_paren);
949 return ExprResult(true);
950 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000951 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(),
952 &CommaLocs[0], StartLoc, ConsumeParen());
953 break;
954 }
Chris Lattner4b009652007-07-25 00:24:17 +0000955 case tok::kw___builtin_types_compatible_p:
Steve Naroff5b528922007-08-01 23:45:51 +0000956 TypeTy *Ty1 = ParseTypeName();
Chris Lattner4b009652007-07-25 00:24:17 +0000957
958 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
959 return ExprResult(true);
960
Steve Naroff5b528922007-08-01 23:45:51 +0000961 TypeTy *Ty2 = ParseTypeName();
962
Chris Lattner4d7d2342007-10-09 17:41:39 +0000963 if (Tok.isNot(tok::r_paren)) {
Steve Naroff5b528922007-08-01 23:45:51 +0000964 Diag(Tok, diag::err_expected_rparen);
965 return ExprResult(true);
966 }
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000967 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattnercb8943a2007-08-30 15:52:49 +0000968 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000969 }
970
Chris Lattner4b009652007-07-25 00:24:17 +0000971 // These can be followed by postfix-expr pieces because they are
972 // primary-expressions.
973 return ParsePostfixExpressionSuffix(Res);
974}
975
976/// ParseParenExpression - This parses the unit that starts with a '(' token,
977/// based on what is allowed by ExprType. The actual thing parsed is returned
978/// in ExprType.
979///
980/// primary-expression: [C99 6.5.1]
981/// '(' expression ')'
982/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
983/// postfix-expression: [C99 6.5.2]
984/// '(' type-name ')' '{' initializer-list '}'
985/// '(' type-name ')' '{' initializer-list ',' '}'
986/// cast-expression: [C99 6.5.4]
987/// '(' type-name ')' cast-expression
988///
989Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
990 TypeTy *&CastTy,
991 SourceLocation &RParenLoc) {
Chris Lattner4d7d2342007-10-09 17:41:39 +0000992 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Chris Lattner4b009652007-07-25 00:24:17 +0000993 SourceLocation OpenLoc = ConsumeParen();
994 ExprResult Result(true);
995 CastTy = 0;
996
Chris Lattner4d7d2342007-10-09 17:41:39 +0000997 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000998 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattnerf2b07572007-08-31 21:49:55 +0000999 Parser::StmtResult Stmt = ParseCompoundStatement(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001000 ExprType = CompoundStmt;
1001
1002 // If the substmt parsed correctly, build the AST node.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001003 if (!Stmt.isInvalid && Tok.is(tok::r_paren))
Steve Naroff5cbb02f2007-09-16 14:56:35 +00001004 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001005
1006 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
1007 // Otherwise, this is a compound literal expression or cast expression.
1008 TypeTy *Ty = ParseTypeName();
1009
1010 // Match the ')'.
Chris Lattner4d7d2342007-10-09 17:41:39 +00001011 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001012 RParenLoc = ConsumeParen();
1013 else
1014 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1015
Chris Lattner4d7d2342007-10-09 17:41:39 +00001016 if (Tok.is(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001017 if (!getLang().C99) // Compound literals don't exist in C90.
1018 Diag(OpenLoc, diag::ext_c99_compound_literal);
1019 Result = ParseInitializer();
1020 ExprType = CompoundLiteral;
1021 if (!Result.isInvalid)
Steve Naroff87d58b42007-09-16 03:34:24 +00001022 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001023 } else if (ExprType == CastExpr) {
1024 // Note that this doesn't parse the subsequence cast-expression, it just
1025 // returns the parsed type to the callee.
1026 ExprType = CastExpr;
1027 CastTy = Ty;
1028 return ExprResult(false);
1029 } else {
1030 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1031 return ExprResult(true);
1032 }
1033 return Result;
1034 } else {
1035 Result = ParseExpression();
1036 ExprType = SimpleExpr;
Chris Lattner4d7d2342007-10-09 17:41:39 +00001037 if (!Result.isInvalid && Tok.is(tok::r_paren))
Steve Naroff87d58b42007-09-16 03:34:24 +00001038 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001039 }
1040
1041 // Match the ')'.
1042 if (Result.isInvalid)
1043 SkipUntil(tok::r_paren);
1044 else {
Chris Lattner4d7d2342007-10-09 17:41:39 +00001045 if (Tok.is(tok::r_paren))
Chris Lattner4b009652007-07-25 00:24:17 +00001046 RParenLoc = ConsumeParen();
1047 else
1048 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1049 }
1050
1051 return Result;
1052}
1053
1054/// ParseStringLiteralExpression - This handles the various token types that
1055/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1056/// translation phase #6].
1057///
1058/// primary-expression: [C99 6.5.1]
1059/// string-literal
1060Parser::ExprResult Parser::ParseStringLiteralExpression() {
1061 assert(isTokenStringLiteral() && "Not a string literal!");
1062
1063 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1064 // considered to be strings for concatenation purposes.
1065 llvm::SmallVector<Token, 4> StringToks;
1066
1067 do {
1068 StringToks.push_back(Tok);
1069 ConsumeStringToken();
1070 } while (isTokenStringLiteral());
1071
1072 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Naroff87d58b42007-09-16 03:34:24 +00001073 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001074}