blob: 523f6473417b3cbcff35337093a911ab362ccb78 [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
203Parser::ExprResult Parser::ParseConstantExpression() {
204 ExprResult LHS = ParseCastExpression(false);
205 if (LHS.isInvalid) return LHS;
206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
208}
209
210/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
211/// in contexts where we have already consumed an identifier (which we saved in
212/// 'IdTok'), then discovered that the identifier was really the leading token
213/// of part of an expression. For example, in "A[1]+B", we consumed "A" (which
214/// is now in 'IdTok') and the current token is "[".
215Parser::ExprResult Parser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000216ParseExpressionWithLeadingIdentifier(const Token &IdTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // We know that 'IdTok' must correspond to this production:
218 // primary-expression: identifier
219
220 // Let the actions module handle the identifier.
Steve Naroff08d92e42007-09-15 18:49:24 +0000221 ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 *IdTok.getIdentifierInfo(),
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000223 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000224
225 // Because we have to parse an entire cast-expression before starting the
226 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
227 // need to handle the 'postfix-expression' rules. We do this by invoking
228 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
229 Res = ParsePostfixExpressionSuffix(Res);
230 if (Res.isInvalid) return Res;
231
232 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
233 // done, we know we don't have to do anything for cast-expression, because the
234 // only non-postfix-expression production starts with a '(' token, and we know
235 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
236 // to consume any trailing operators (e.g. "+" in this example) and connected
237 // chunks of the expression.
238 return ParseRHSOfBinaryExpression(Res, prec::Comma);
239}
240
241/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
242/// in contexts where we have already consumed an identifier (which we saved in
243/// 'IdTok'), then discovered that the identifier was really the leading token
244/// of part of an assignment-expression. For example, in "A[1]+B", we consumed
245/// "A" (which is now in 'IdTok') and the current token is "[".
246Parser::ExprResult Parser::
Chris Lattnerd2177732007-07-20 16:59:19 +0000247ParseAssignmentExprWithLeadingIdentifier(const Token &IdTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // We know that 'IdTok' must correspond to this production:
249 // primary-expression: identifier
250
251 // Let the actions module handle the identifier.
Steve Naroff08d92e42007-09-15 18:49:24 +0000252 ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 *IdTok.getIdentifierInfo(),
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000254 Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000255
256 // Because we have to parse an entire cast-expression before starting the
257 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
258 // need to handle the 'postfix-expression' rules. We do this by invoking
259 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
260 Res = ParsePostfixExpressionSuffix(Res);
261 if (Res.isInvalid) return Res;
262
263 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
264 // done, we know we don't have to do anything for cast-expression, because the
265 // only non-postfix-expression production starts with a '(' token, and we know
266 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
267 // to consume any trailing operators (e.g. "+" in this example) and connected
268 // chunks of the expression.
269 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
270}
271
272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
274/// LHS and has a precedence of at least MinPrec.
275Parser::ExprResult
276Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
277 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
278 SourceLocation ColonLoc;
279
280 while (1) {
281 // If this token has a lower precedence than we are allowed to parse (e.g.
282 // because we are called recursively, or because the token is not a binop),
283 // then we are done!
284 if (NextTokPrec < MinPrec)
285 return LHS;
286
287 // Consume the operator, saving the operator token for error reporting.
Chris Lattnerd2177732007-07-20 16:59:19 +0000288 Token OpToken = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ConsumeToken();
290
291 // Special case handling for the ternary operator.
292 ExprResult TernaryMiddle(true);
293 if (NextTokPrec == prec::Conditional) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000294 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Handle this production specially:
296 // logical-OR-expression '?' expression ':' conditional-expression
297 // In particular, the RHS of the '?' is 'expression', not
298 // 'logical-OR-expression' as we might expect.
299 TernaryMiddle = ParseExpression();
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000300 if (TernaryMiddle.isInvalid) {
301 Actions.DeleteExpr(LHS.Val);
302 return TernaryMiddle;
303 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 } else {
305 // Special case handling of "X ? Y : Z" where Y is empty:
306 // logical-OR-expression '?' ':' conditional-expression [GNU]
307 TernaryMiddle = ExprResult(false);
308 Diag(Tok, diag::ext_gnu_conditional_expr);
309 }
310
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000311 if (Tok.isNot(tok::colon)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 Diag(Tok, diag::err_expected_colon);
313 Diag(OpToken, diag::err_matching, "?");
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000314 Actions.DeleteExpr(LHS.Val);
315 Actions.DeleteExpr(TernaryMiddle.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 return ExprResult(true);
317 }
318
319 // Eat the colon.
320 ColonLoc = ConsumeToken();
321 }
322
323 // Parse another leaf here for the RHS of the operator.
324 ExprResult RHS = ParseCastExpression(false);
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000325 if (RHS.isInvalid) {
326 Actions.DeleteExpr(LHS.Val);
327 Actions.DeleteExpr(TernaryMiddle.Val);
328 return RHS;
329 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000330
331 // Remember the precedence of this operator and get the precedence of the
332 // operator immediately to the right of the RHS.
333 unsigned ThisPrec = NextTokPrec;
334 NextTokPrec = getBinOpPrecedence(Tok.getKind());
335
336 // Assignment and conditional expressions are right-associative.
Chris Lattnerd7d860d2007-12-18 06:06:23 +0000337 bool isRightAssoc = ThisPrec == prec::Conditional ||
338 ThisPrec == prec::Assignment;
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 // Get the precedence of the operator to the right of the RHS. If it binds
341 // more tightly with RHS than we do, evaluate it completely first.
342 if (ThisPrec < NextTokPrec ||
343 (ThisPrec == NextTokPrec && isRightAssoc)) {
344 // If this is left-associative, only parse things on the RHS that bind
345 // more tightly than the current operator. If it is left-associative, it
346 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
347 // A=(B=(C=D)), where each paren is a level of recursion here.
348 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnerdbd583c2007-08-31 04:58:34 +0000349 if (RHS.isInvalid) {
350 Actions.DeleteExpr(LHS.Val);
351 Actions.DeleteExpr(TernaryMiddle.Val);
352 return RHS;
353 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000354
355 NextTokPrec = getBinOpPrecedence(Tok.getKind());
356 }
357 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
358
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000359 if (!LHS.isInvalid) {
360 // Combine the LHS and RHS into the LHS (e.g. build AST).
361 if (TernaryMiddle.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000362 LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(),
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000363 LHS.Val, RHS.Val);
364 else
Steve Narofff69936d2007-09-16 03:34:24 +0000365 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc,
Chris Lattnerd56d6b62007-08-31 05:01:50 +0000366 LHS.Val, TernaryMiddle.Val, RHS.Val);
367 } else {
368 // We had a semantic error on the LHS. Just free the RHS and continue.
369 Actions.DeleteExpr(TernaryMiddle.Val);
370 Actions.DeleteExpr(RHS.Val);
371 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 }
373}
374
375/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
376/// true, parse a unary-expression.
377///
378/// cast-expression: [C99 6.5.4]
379/// unary-expression
380/// '(' type-name ')' cast-expression
381///
382/// unary-expression: [C99 6.5.3]
383/// postfix-expression
384/// '++' unary-expression
385/// '--' unary-expression
386/// unary-operator cast-expression
387/// 'sizeof' unary-expression
388/// 'sizeof' '(' type-name ')'
389/// [GNU] '__alignof' unary-expression
390/// [GNU] '__alignof' '(' type-name ')'
391/// [GNU] '&&' identifier
392///
393/// unary-operator: one of
394/// '&' '*' '+' '-' '~' '!'
395/// [GNU] '__extension__' '__real' '__imag'
396///
397/// primary-expression: [C99 6.5.1]
398/// identifier
399/// constant
400/// string-literal
401/// [C++] boolean-literal [C++ 2.13.5]
402/// '(' expression ')'
403/// '__func__' [C99 6.4.2.2]
404/// [GNU] '__FUNCTION__'
405/// [GNU] '__PRETTY_FUNCTION__'
406/// [GNU] '(' compound-statement ')'
407/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
408/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
409/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
410/// assign-expr ')'
411/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000412/// [OBJC] '[' objc-message-expr ']'
Chris Lattner5ac87ed2008-01-25 18:58:06 +0000413/// [OBJC] '@selector' '(' objc-selector-arg ')'
Fariborz Jahanian095ffca2007-09-26 17:03:44 +0000414/// [OBJC] '@protocol' '(' identifier ')'
415/// [OBJC] '@encode' '(' type-name ')'
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000416/// [OBJC] objc-string-literal
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
418/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
419/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
420/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
421///
422/// constant: [C99 6.4.4]
423/// integer-constant
424/// floating-constant
425/// enumeration-constant -> identifier
426/// character-constant
427///
428Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
429 ExprResult Res;
430 tok::TokenKind SavedKind = Tok.getKind();
431
432 // This handles all of cast-expression, unary-expression, postfix-expression,
433 // and primary-expression. We handle them together like this for efficiency
434 // and to simplify handling of an expression starting with a '(' token: which
435 // may be one of a parenthesized expression, cast-expression, compound literal
436 // expression, or statement expression.
437 //
438 // If the parsed tokens consist of a primary-expression, the cases below
439 // call ParsePostfixExpressionSuffix to handle the postfix expression
440 // suffixes. Cases that cannot be followed by postfix exprs should
441 // return without invoking ParsePostfixExpressionSuffix.
442 switch (SavedKind) {
443 case tok::l_paren: {
444 // If this expression is limited to being a unary-expression, the parent can
445 // not start a cast expression.
446 ParenParseOption ParenExprType =
447 isUnaryExpression ? CompoundLiteral : CastExpr;
448 TypeTy *CastTy;
449 SourceLocation LParenLoc = Tok.getLocation();
450 SourceLocation RParenLoc;
451 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
452 if (Res.isInvalid) return Res;
453
454 switch (ParenExprType) {
455 case SimpleExpr: break; // Nothing else to do.
456 case CompoundStmt: break; // Nothing else to do.
457 case CompoundLiteral:
458 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
459 // postfix-expression exist, parse them now.
460 break;
461 case CastExpr:
462 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
463 // the cast-expression that follows it next.
464 // TODO: For cast expression with CastTy.
465 Res = ParseCastExpression(false);
466 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000467 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 return Res;
469 }
470
471 // These can be followed by postfix-expr pieces.
472 return ParsePostfixExpressionSuffix(Res);
473 }
474
475 // primary-expression
476 case tok::numeric_constant:
477 // constant: integer-constant
478 // constant: floating-constant
479
Steve Narofff69936d2007-09-16 03:34:24 +0000480 Res = Actions.ActOnNumericConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 ConsumeToken();
482
483 // These can be followed by postfix-expr pieces.
484 return ParsePostfixExpressionSuffix(Res);
485
486 case tok::kw_true:
487 case tok::kw_false:
488 return ParseCXXBoolLiteral();
489
490 case tok::identifier: { // primary-expression: identifier
491 // constant: enumeration-constant
492 // Consume the identifier so that we can see if it is followed by a '('.
493 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
494 // need to know whether or not this identifier is a function designator or
495 // not.
496 IdentifierInfo &II = *Tok.getIdentifierInfo();
497 SourceLocation L = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000498 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren));
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 // These can be followed by postfix-expr pieces.
500 return ParsePostfixExpressionSuffix(Res);
501 }
502 case tok::char_constant: // constant: character-constant
Steve Narofff69936d2007-09-16 03:34:24 +0000503 Res = Actions.ActOnCharacterConstant(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 ConsumeToken();
505 // These can be followed by postfix-expr pieces.
506 return ParsePostfixExpressionSuffix(Res);
507 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
508 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
509 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Steve Narofff69936d2007-09-16 03:34:24 +0000510 Res = Actions.ActOnPreDefinedExpr(Tok.getLocation(), SavedKind);
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 ConsumeToken();
512 // These can be followed by postfix-expr pieces.
513 return ParsePostfixExpressionSuffix(Res);
514 case tok::string_literal: // primary-expression: string-literal
515 case tok::wide_string_literal:
516 Res = ParseStringLiteralExpression();
517 if (Res.isInvalid) return Res;
518 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
519 return ParsePostfixExpressionSuffix(Res);
520 case tok::kw___builtin_va_arg:
521 case tok::kw___builtin_offsetof:
522 case tok::kw___builtin_choose_expr:
Nate Begemane2ce1d92008-01-17 17:46:27 +0000523 case tok::kw___builtin_overload:
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 case tok::kw___builtin_types_compatible_p:
525 return ParseBuiltinPrimaryExpression();
526 case tok::plusplus: // unary-expression: '++' unary-expression
527 case tok::minusminus: { // unary-expression: '--' unary-expression
528 SourceLocation SavedLoc = ConsumeToken();
529 Res = ParseCastExpression(true);
530 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000531 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 return Res;
533 }
534 case tok::amp: // unary-expression: '&' cast-expression
535 case tok::star: // unary-expression: '*' cast-expression
536 case tok::plus: // unary-expression: '+' cast-expression
537 case tok::minus: // unary-expression: '-' cast-expression
538 case tok::tilde: // unary-expression: '~' cast-expression
539 case tok::exclaim: // unary-expression: '!' cast-expression
540 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner35080842008-02-02 20:20:10 +0000541 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU]
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 SourceLocation SavedLoc = ConsumeToken();
543 Res = ParseCastExpression(false);
544 if (!Res.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000545 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 return Res;
Chris Lattner35080842008-02-02 20:20:10 +0000547 }
548
549 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
550 // __extension__ silences extension warnings in the subexpression.
551 bool SavedExtWarn = Diags.getWarnOnExtensions();
552 Diags.setWarnOnExtensions(false);
553 SourceLocation SavedLoc = ConsumeToken();
554 Res = ParseCastExpression(false);
555 if (!Res.isInvalid)
556 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val);
557 Diags.setWarnOnExtensions(SavedExtWarn);
558 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
560 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
561 // unary-expression: 'sizeof' '(' type-name ')'
562 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
563 // unary-expression: '__alignof' '(' type-name ')'
564 return ParseSizeofAlignofExpression();
565 case tok::ampamp: { // unary-expression: '&&' identifier
566 SourceLocation AmpAmpLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000567 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 Diag(Tok, diag::err_expected_ident);
569 return ExprResult(true);
570 }
571
572 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
Steve Naroff1b273c42007-09-16 14:56:35 +0000573 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 Tok.getIdentifierInfo());
575 ConsumeToken();
576 return Res;
577 }
578 case tok::kw_const_cast:
579 case tok::kw_dynamic_cast:
580 case tok::kw_reinterpret_cast:
581 case tok::kw_static_cast:
582 return ParseCXXCasts();
Chris Lattnerc97c2042007-10-03 22:03:06 +0000583 case tok::at: {
584 SourceLocation AtLoc = ConsumeToken();
Steve Naroffa642beb2007-10-15 20:55:58 +0000585 return ParseObjCAtExpression(AtLoc);
Chris Lattnerc97c2042007-10-03 22:03:06 +0000586 }
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +0000587 case tok::l_square:
Steve Naroffa642beb2007-10-15 20:55:58 +0000588 // These can be followed by postfix-expr pieces.
Chris Lattner039a6422008-05-09 05:28:21 +0000589 if (getLang().ObjC1)
590 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
591 // FALL THROUGH.
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 default:
593 Diag(Tok, diag::err_expected_expression);
594 return ExprResult(true);
595 }
596
597 // unreachable.
598 abort();
599}
600
601/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
602/// is parsed, this method parses any suffixes that apply.
603///
604/// postfix-expression: [C99 6.5.2]
605/// primary-expression
606/// postfix-expression '[' expression ']'
607/// postfix-expression '(' argument-expression-list[opt] ')'
608/// postfix-expression '.' identifier
609/// postfix-expression '->' identifier
610/// postfix-expression '++'
611/// postfix-expression '--'
612/// '(' type-name ')' '{' initializer-list '}'
613/// '(' type-name ')' '{' initializer-list ',' '}'
614///
615/// argument-expression-list: [C99 6.5.2]
616/// argument-expression
617/// argument-expression-list ',' assignment-expression
618///
619Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
620
621 // Now that the primary-expression piece of the postfix-expression has been
622 // parsed, see if there are any postfix-expression pieces here.
623 SourceLocation Loc;
624 while (1) {
625 switch (Tok.getKind()) {
626 default: // Not a postfix-expression suffix.
627 return LHS;
628 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
629 Loc = ConsumeBracket();
630 ExprResult Idx = ParseExpression();
631
632 SourceLocation RLoc = Tok.getLocation();
633
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000634 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square))
Steve Narofff69936d2007-09-16 03:34:24 +0000635 LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 else
637 LHS = ExprResult(true);
638
639 // Match the ']'.
640 MatchRHSPunctuation(tok::r_square, Loc);
641 break;
642 }
643
644 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
645 llvm::SmallVector<ExprTy*, 8> ArgExprs;
646 llvm::SmallVector<SourceLocation, 8> CommaLocs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647
648 Loc = ConsumeParen();
649
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000650 if (Tok.isNot(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 while (1) {
652 ExprResult ArgExpr = ParseAssignmentExpression();
653 if (ArgExpr.isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 SkipUntil(tok::r_paren);
Chris Lattner2ff54262007-07-21 05:18:12 +0000655 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 } else
657 ArgExprs.push_back(ArgExpr.Val);
658
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000659 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 break;
661 // Move to the next argument, remember where the comma was.
662 CommaLocs.push_back(ConsumeToken());
663 }
664 }
665
666 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000667 if (!LHS.isInvalid && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
669 "Unexpected number of commas!");
Steve Narofff69936d2007-09-16 03:34:24 +0000670 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 &CommaLocs[0], Tok.getLocation());
672 }
673
Chris Lattner2ff54262007-07-21 05:18:12 +0000674 MatchRHSPunctuation(tok::r_paren, Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 break;
676 }
677 case tok::arrow: // postfix-expression: p-e '->' identifier
678 case tok::period: { // postfix-expression: p-e '.' identifier
679 tok::TokenKind OpKind = Tok.getKind();
680 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
681
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000682 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 Diag(Tok, diag::err_expected_ident);
684 return ExprResult(true);
685 }
686
687 if (!LHS.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000688 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 Tok.getLocation(),
690 *Tok.getIdentifierInfo());
691 ConsumeToken();
692 break;
693 }
694 case tok::plusplus: // postfix-expression: postfix-expression '++'
695 case tok::minusminus: // postfix-expression: postfix-expression '--'
696 if (!LHS.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000697 LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 LHS.Val);
699 ConsumeToken();
700 break;
701 }
702 }
703}
704
705
706/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
707/// unary-expression: [C99 6.5.3]
708/// 'sizeof' unary-expression
709/// 'sizeof' '(' type-name ')'
710/// [GNU] '__alignof' unary-expression
711/// [GNU] '__alignof' '(' type-name ')'
712Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000713 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)) &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 "Not a sizeof/alignof expression!");
Chris Lattnerd2177732007-07-20 16:59:19 +0000715 Token OpTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 ConsumeToken();
717
718 // If the operand doesn't start with an '(', it must be an expression.
719 ExprResult Operand;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000720 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 Operand = ParseCastExpression(true);
722 } else {
723 // If it starts with a '(', we know that it is either a parenthesized
724 // type-name, or it is a unary-expression that starts with a compound
725 // literal, or starts with a primary-expression that is a parenthesized
726 // expression.
727 ParenParseOption ExprType = CastExpr;
728 TypeTy *CastTy;
729 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
730 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
731
732 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
733 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000734 if (ExprType == CastExpr)
Steve Narofff69936d2007-09-16 03:34:24 +0000735 return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(),
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000736 OpTok.is(tok::kw_sizeof),
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 LParenLoc, CastTy, RParenLoc);
Chris Lattner4c1a2a92007-11-13 20:50:37 +0000738
739 // If this is a parenthesized expression, it is the start of a
740 // unary-expression, but doesn't include any postfix pieces. Parse these
741 // now if present.
742 Operand = ParsePostfixExpressionSuffix(Operand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 }
744
745 // If we get here, the operand to the sizeof/alignof was an expresion.
746 if (!Operand.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000747 Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 Operand.Val);
749 return Operand;
750}
751
752/// ParseBuiltinPrimaryExpression
753///
754/// primary-expression: [C99 6.5.1]
755/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
756/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
757/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
758/// assign-expr ')'
759/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
Nate Begemane2ce1d92008-01-17 17:46:27 +0000760/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000761///
762/// [GNU] offsetof-member-designator:
763/// [GNU] identifier
764/// [GNU] offsetof-member-designator '.' identifier
765/// [GNU] offsetof-member-designator '[' expression ']'
766///
767Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
768 ExprResult Res(false);
769 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
770
771 tok::TokenKind T = Tok.getKind();
772 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
773
774 // All of these start with an open paren.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000775 if (Tok.isNot(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
777 return ExprResult(true);
778 }
779
780 SourceLocation LParenLoc = ConsumeParen();
781 // TODO: Build AST.
782
783 switch (T) {
784 default: assert(0 && "Not a builtin primary expression!");
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000785 case tok::kw___builtin_va_arg: {
786 ExprResult Expr = ParseAssignmentExpression();
787 if (Expr.isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 SkipUntil(tok::r_paren);
789 return Res;
790 }
791
792 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
793 return ExprResult(true);
794
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000795 TypeTy *Ty = ParseTypeName();
Chris Lattner6eb21092007-08-30 15:52:49 +0000796
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000797 if (Tok.isNot(tok::r_paren)) {
798 Diag(Tok, diag::err_expected_rparen);
799 return ExprResult(true);
800 }
801 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen());
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 break;
Anders Carlsson7c50aca2007-10-15 20:28:48 +0000803 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000804 case tok::kw___builtin_offsetof: {
Chris Lattner9fddf0a2007-08-30 17:08:45 +0000805 SourceLocation TypeLoc = Tok.getLocation();
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000806 TypeTy *Ty = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807
808 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
809 return ExprResult(true);
810
811 // We must have at least one identifier here.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000812 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000813 Diag(Tok, diag::err_expected_ident);
814 SkipUntil(tok::r_paren);
815 return true;
816 }
817
818 // Keep track of the various subcomponents we see.
819 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps;
820
821 Comps.push_back(Action::OffsetOfComponent());
822 Comps.back().isBrackets = false;
823 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
824 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000825
826 while (1) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000827 if (Tok.is(tok::period)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 // offsetof-member-designator: offsetof-member-designator '.' identifier
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000829 Comps.push_back(Action::OffsetOfComponent());
830 Comps.back().isBrackets = false;
831 Comps.back().LocStart = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +0000832
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000833 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000834 Diag(Tok, diag::err_expected_ident);
835 SkipUntil(tok::r_paren);
836 return true;
837 }
838 Comps.back().U.IdentInfo = Tok.getIdentifierInfo();
839 Comps.back().LocEnd = ConsumeToken();
840
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000841 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000843 Comps.push_back(Action::OffsetOfComponent());
844 Comps.back().isBrackets = true;
845 Comps.back().LocStart = ConsumeBracket();
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 Res = ParseExpression();
847 if (Res.isInvalid) {
848 SkipUntil(tok::r_paren);
849 return Res;
850 }
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000851 Comps.back().U.E = Res.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000852
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000853 Comps.back().LocEnd =
854 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart);
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000855 } else if (Tok.is(tok::r_paren)) {
Steve Naroff1b273c42007-09-16 14:56:35 +0000856 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0],
Chris Lattner6eb21092007-08-30 15:52:49 +0000857 Comps.size(), ConsumeParen());
858 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 } else {
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000860 // Error occurred.
861 return ExprResult(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 }
863 }
864 break;
Chris Lattnerf9aa3cb2007-08-30 15:51:11 +0000865 }
Steve Naroffd04fdd52007-08-03 21:21:27 +0000866 case tok::kw___builtin_choose_expr: {
867 ExprResult Cond = ParseAssignmentExpression();
868 if (Cond.isInvalid) {
869 SkipUntil(tok::r_paren);
870 return Cond;
871 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
873 return ExprResult(true);
874
Steve Naroffd04fdd52007-08-03 21:21:27 +0000875 ExprResult Expr1 = ParseAssignmentExpression();
876 if (Expr1.isInvalid) {
877 SkipUntil(tok::r_paren);
878 return Expr1;
879 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
881 return ExprResult(true);
882
Steve Naroffd04fdd52007-08-03 21:21:27 +0000883 ExprResult Expr2 = ParseAssignmentExpression();
884 if (Expr2.isInvalid) {
885 SkipUntil(tok::r_paren);
886 return Expr2;
887 }
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000888 if (Tok.isNot(tok::r_paren)) {
Steve Naroffd04fdd52007-08-03 21:21:27 +0000889 Diag(Tok, diag::err_expected_rparen);
890 return ExprResult(true);
891 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000892 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val,
Chris Lattner6eb21092007-08-30 15:52:49 +0000893 ConsumeParen());
894 break;
Steve Naroffd04fdd52007-08-03 21:21:27 +0000895 }
Nate Begemane2ce1d92008-01-17 17:46:27 +0000896 case tok::kw___builtin_overload: {
897 llvm::SmallVector<ExprTy*, 8> ArgExprs;
898 llvm::SmallVector<SourceLocation, 8> CommaLocs;
899
900 // For each iteration through the loop look for assign-expr followed by a
901 // comma. If there is no comma, break and attempt to match r-paren.
902 if (Tok.isNot(tok::r_paren)) {
903 while (1) {
904 ExprResult ArgExpr = ParseAssignmentExpression();
905 if (ArgExpr.isInvalid) {
906 SkipUntil(tok::r_paren);
907 return ExprResult(true);
908 } else
909 ArgExprs.push_back(ArgExpr.Val);
910
911 if (Tok.isNot(tok::comma))
912 break;
913 // Move to the next argument, remember where the comma was.
914 CommaLocs.push_back(ConsumeToken());
915 }
916 }
917
918 // Attempt to consume the r-paren
919 if (Tok.isNot(tok::r_paren)) {
920 Diag(Tok, diag::err_expected_rparen);
921 SkipUntil(tok::r_paren);
922 return ExprResult(true);
923 }
Nate Begemane2ce1d92008-01-17 17:46:27 +0000924 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(),
925 &CommaLocs[0], StartLoc, ConsumeParen());
926 break;
927 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 case tok::kw___builtin_types_compatible_p:
Steve Naroff363bcff2007-08-01 23:45:51 +0000929 TypeTy *Ty1 = ParseTypeName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
931 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
932 return ExprResult(true);
933
Steve Naroff363bcff2007-08-01 23:45:51 +0000934 TypeTy *Ty2 = ParseTypeName();
935
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000936 if (Tok.isNot(tok::r_paren)) {
Steve Naroff363bcff2007-08-01 23:45:51 +0000937 Diag(Tok, diag::err_expected_rparen);
938 return ExprResult(true);
939 }
Steve Naroff1b273c42007-09-16 14:56:35 +0000940 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen());
Chris Lattner6eb21092007-08-30 15:52:49 +0000941 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 }
943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // These can be followed by postfix-expr pieces because they are
945 // primary-expressions.
946 return ParsePostfixExpressionSuffix(Res);
947}
948
949/// ParseParenExpression - This parses the unit that starts with a '(' token,
950/// based on what is allowed by ExprType. The actual thing parsed is returned
951/// in ExprType.
952///
953/// primary-expression: [C99 6.5.1]
954/// '(' expression ')'
955/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
956/// postfix-expression: [C99 6.5.2]
957/// '(' type-name ')' '{' initializer-list '}'
958/// '(' type-name ')' '{' initializer-list ',' '}'
959/// cast-expression: [C99 6.5.4]
960/// '(' type-name ')' cast-expression
961///
962Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
963 TypeTy *&CastTy,
964 SourceLocation &RParenLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000965 assert(Tok.is(tok::l_paren) && "Not a paren expr!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 SourceLocation OpenLoc = ConsumeParen();
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000967 ExprResult Result(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 CastTy = 0;
969
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000970 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 Diag(Tok, diag::ext_gnu_statement_expr);
Chris Lattner98414c12007-08-31 21:49:55 +0000972 Parser::StmtResult Stmt = ParseCompoundStatement(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 ExprType = CompoundStmt;
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000974
975 // If the substmt parsed correctly, build the AST node.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000976 if (!Stmt.isInvalid && Tok.is(tok::r_paren))
Steve Naroff1b273c42007-09-16 14:56:35 +0000977 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation());
Chris Lattnerab18c4c2007-07-24 16:58:17 +0000978
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
980 // Otherwise, this is a compound literal expression or cast expression.
981 TypeTy *Ty = ParseTypeName();
982
983 // Match the ')'.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000984 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 RParenLoc = ConsumeParen();
986 else
987 MatchRHSPunctuation(tok::r_paren, OpenLoc);
988
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000989 if (Tok.is(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 if (!getLang().C99) // Compound literals don't exist in C90.
991 Diag(OpenLoc, diag::ext_c99_compound_literal);
992 Result = ParseInitializer();
993 ExprType = CompoundLiteral;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000994 if (!Result.isInvalid)
Steve Narofff69936d2007-09-16 03:34:24 +0000995 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 } else if (ExprType == CastExpr) {
997 // Note that this doesn't parse the subsequence cast-expression, it just
998 // returns the parsed type to the callee.
999 ExprType = CastExpr;
1000 CastTy = Ty;
1001 return ExprResult(false);
1002 } else {
1003 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
1004 return ExprResult(true);
1005 }
1006 return Result;
1007 } else {
1008 Result = ParseExpression();
1009 ExprType = SimpleExpr;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001010 if (!Result.isInvalid && Tok.is(tok::r_paren))
Steve Narofff69936d2007-09-16 03:34:24 +00001011 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 }
1013
1014 // Match the ')'.
1015 if (Result.isInvalid)
1016 SkipUntil(tok::r_paren);
1017 else {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001018 if (Tok.is(tok::r_paren))
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 RParenLoc = ConsumeParen();
1020 else
1021 MatchRHSPunctuation(tok::r_paren, OpenLoc);
1022 }
1023
1024 return Result;
1025}
1026
1027/// ParseStringLiteralExpression - This handles the various token types that
1028/// form string literals, and also handles string concatenation [C99 5.1.1.2,
1029/// translation phase #6].
1030///
1031/// primary-expression: [C99 6.5.1]
1032/// string-literal
1033Parser::ExprResult Parser::ParseStringLiteralExpression() {
1034 assert(isTokenStringLiteral() && "Not a string literal!");
1035
1036 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
1037 // considered to be strings for concatenation purposes.
Chris Lattnerd2177732007-07-20 16:59:19 +00001038 llvm::SmallVector<Token, 4> StringToks;
Reid Spencer5f016e22007-07-11 17:01:13 +00001039
1040 do {
1041 StringToks.push_back(Tok);
1042 ConsumeStringToken();
1043 } while (isTokenStringLiteral());
1044
1045 // Pass the set of string tokens, ready for concatenation, to the actions.
Steve Narofff69936d2007-09-16 03:34:24 +00001046 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001047}