ParseExpr.cpp revision 59232d35f5820e334b6c8b007ae8006f4390055d
1//===--- ParseExpr.cpp - Expression Parsing -------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 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/Parse/DeclSpec.h" 24#include "clang/Parse/Scope.h" 25#include "ExtensionRAIIObject.h" 26#include "AstGuard.h" 27#include "llvm/ADT/SmallVector.h" 28#include "llvm/ADT/SmallString.h" 29using namespace clang; 30 31/// PrecedenceLevels - These are precedences for the binary/ternary operators in 32/// the C99 grammar. These have been named to relate with the C99 grammar 33/// productions. Low precedences numbers bind more weakly than high numbers. 34namespace prec { 35 enum Level { 36 Unknown = 0, // Not binary operator. 37 Comma = 1, // , 38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= 39 Conditional = 3, // ? 40 LogicalOr = 4, // || 41 LogicalAnd = 5, // && 42 InclusiveOr = 6, // | 43 ExclusiveOr = 7, // ^ 44 And = 8, // & 45 Equality = 9, // ==, != 46 Relational = 10, // >=, <=, >, < 47 Shift = 11, // <<, >> 48 Additive = 12, // -, + 49 Multiplicative = 13 // *, /, % 50 }; 51} 52 53 54/// getBinOpPrecedence - Return the precedence of the specified binary operator 55/// token. This returns: 56/// 57static prec::Level getBinOpPrecedence(tok::TokenKind Kind) { 58 switch (Kind) { 59 default: return prec::Unknown; 60 case tok::comma: return prec::Comma; 61 case tok::equal: 62 case tok::starequal: 63 case tok::slashequal: 64 case tok::percentequal: 65 case tok::plusequal: 66 case tok::minusequal: 67 case tok::lesslessequal: 68 case tok::greatergreaterequal: 69 case tok::ampequal: 70 case tok::caretequal: 71 case tok::pipeequal: return prec::Assignment; 72 case tok::question: return prec::Conditional; 73 case tok::pipepipe: return prec::LogicalOr; 74 case tok::ampamp: return prec::LogicalAnd; 75 case tok::pipe: return prec::InclusiveOr; 76 case tok::caret: return prec::ExclusiveOr; 77 case tok::amp: return prec::And; 78 case tok::exclaimequal: 79 case tok::equalequal: return prec::Equality; 80 case tok::lessequal: 81 case tok::less: 82 case tok::greaterequal: 83 case tok::greater: return prec::Relational; 84 case tok::lessless: 85 case tok::greatergreater: return prec::Shift; 86 case tok::plus: 87 case tok::minus: return prec::Additive; 88 case tok::percent: 89 case tok::slash: 90 case tok::star: return prec::Multiplicative; 91 } 92} 93 94 95/// ParseExpression - Simple precedence-based parser for binary/ternary 96/// operators. 97/// 98/// Note: we diverge from the C99 grammar when parsing the assignment-expression 99/// production. C99 specifies that the LHS of an assignment operator should be 100/// parsed as a unary-expression, but consistency dictates that it be a 101/// conditional-expession. In practice, the important thing here is that the 102/// LHS of an assignment has to be an l-value, which productions between 103/// unary-expression and conditional-expression don't produce. Because we want 104/// consistency, we parse the LHS as a conditional-expression, then check for 105/// l-value-ness in semantic analysis stages. 106/// 107/// multiplicative-expression: [C99 6.5.5] 108/// cast-expression 109/// multiplicative-expression '*' cast-expression 110/// multiplicative-expression '/' cast-expression 111/// multiplicative-expression '%' cast-expression 112/// 113/// additive-expression: [C99 6.5.6] 114/// multiplicative-expression 115/// additive-expression '+' multiplicative-expression 116/// additive-expression '-' multiplicative-expression 117/// 118/// shift-expression: [C99 6.5.7] 119/// additive-expression 120/// shift-expression '<<' additive-expression 121/// shift-expression '>>' additive-expression 122/// 123/// relational-expression: [C99 6.5.8] 124/// shift-expression 125/// relational-expression '<' shift-expression 126/// relational-expression '>' shift-expression 127/// relational-expression '<=' shift-expression 128/// relational-expression '>=' shift-expression 129/// 130/// equality-expression: [C99 6.5.9] 131/// relational-expression 132/// equality-expression '==' relational-expression 133/// equality-expression '!=' relational-expression 134/// 135/// AND-expression: [C99 6.5.10] 136/// equality-expression 137/// AND-expression '&' equality-expression 138/// 139/// exclusive-OR-expression: [C99 6.5.11] 140/// AND-expression 141/// exclusive-OR-expression '^' AND-expression 142/// 143/// inclusive-OR-expression: [C99 6.5.12] 144/// exclusive-OR-expression 145/// inclusive-OR-expression '|' exclusive-OR-expression 146/// 147/// logical-AND-expression: [C99 6.5.13] 148/// inclusive-OR-expression 149/// logical-AND-expression '&&' inclusive-OR-expression 150/// 151/// logical-OR-expression: [C99 6.5.14] 152/// logical-AND-expression 153/// logical-OR-expression '||' logical-AND-expression 154/// 155/// conditional-expression: [C99 6.5.15] 156/// logical-OR-expression 157/// logical-OR-expression '?' expression ':' conditional-expression 158/// [GNU] logical-OR-expression '?' ':' conditional-expression 159/// 160/// assignment-expression: [C99 6.5.16] 161/// conditional-expression 162/// unary-expression assignment-operator assignment-expression 163/// [C++] throw-expression [C++ 15] 164/// 165/// assignment-operator: one of 166/// = *= /= %= += -= <<= >>= &= ^= |= 167/// 168/// expression: [C99 6.5.17] 169/// assignment-expression 170/// expression ',' assignment-expression 171/// 172Parser::OwningExprResult Parser::ParseExpression() { 173 if (Tok.is(tok::kw_throw)) 174 return ParseThrowExpression(); 175 176 OwningExprResult LHS(ParseCastExpression(false)); 177 if (LHS.isInvalid()) return move(LHS); 178 179 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); 180} 181 182/// This routine is called when the '@' is seen and consumed. 183/// Current token is an Identifier and is not a 'try'. This 184/// routine is necessary to disambiguate @try-statement from, 185/// for example, @encode-expression. 186/// 187Parser::OwningExprResult 188Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { 189 OwningExprResult LHS(ParseObjCAtExpression(AtLoc)); 190 if (LHS.isInvalid()) return move(LHS); 191 192 return ParseRHSOfBinaryExpression(move(LHS), prec::Comma); 193} 194 195/// ParseAssignmentExpression - Parse an expr that doesn't include commas. 196/// 197Parser::OwningExprResult Parser::ParseAssignmentExpression() { 198 if (Tok.is(tok::kw_throw)) 199 return ParseThrowExpression(); 200 201 OwningExprResult LHS(ParseCastExpression(false)); 202 if (LHS.isInvalid()) return move(LHS); 203 204 return ParseRHSOfBinaryExpression(move(LHS), prec::Assignment); 205} 206 207/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression 208/// where part of an objc message send has already been parsed. In this case 209/// LBracLoc indicates the location of the '[' of the message send, and either 210/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the 211/// message. 212/// 213/// Since this handles full assignment-expression's, it handles postfix 214/// expressions and other binary operators for these expressions as well. 215Parser::OwningExprResult 216Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, 217 SourceLocation NameLoc, 218 IdentifierInfo *ReceiverName, 219 ExprArg ReceiverExpr) { 220 OwningExprResult R(ParseObjCMessageExpressionBody(LBracLoc, NameLoc, 221 ReceiverName, 222 move(ReceiverExpr))); 223 if (R.isInvalid()) return move(R); 224 R = ParsePostfixExpressionSuffix(move(R)); 225 if (R.isInvalid()) return move(R); 226 return ParseRHSOfBinaryExpression(move(R), prec::Assignment); 227} 228 229 230Parser::OwningExprResult Parser::ParseConstantExpression() { 231 OwningExprResult LHS(ParseCastExpression(false)); 232 if (LHS.isInvalid()) return move(LHS); 233 234 return ParseRHSOfBinaryExpression(move(LHS), prec::Conditional); 235} 236 237/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with 238/// LHS and has a precedence of at least MinPrec. 239Parser::OwningExprResult 240Parser::ParseRHSOfBinaryExpression(OwningExprResult LHS, unsigned MinPrec) { 241 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); 242 SourceLocation ColonLoc; 243 244 while (1) { 245 // If this token has a lower precedence than we are allowed to parse (e.g. 246 // because we are called recursively, or because the token is not a binop), 247 // then we are done! 248 if (NextTokPrec < MinPrec) 249 return move(LHS); 250 251 // Consume the operator, saving the operator token for error reporting. 252 Token OpToken = Tok; 253 ConsumeToken(); 254 255 // Special case handling for the ternary operator. 256 OwningExprResult TernaryMiddle(Actions, true); 257 if (NextTokPrec == prec::Conditional) { 258 if (Tok.isNot(tok::colon)) { 259 // Handle this production specially: 260 // logical-OR-expression '?' expression ':' conditional-expression 261 // In particular, the RHS of the '?' is 'expression', not 262 // 'logical-OR-expression' as we might expect. 263 TernaryMiddle = ParseExpression(); 264 if (TernaryMiddle.isInvalid()) 265 return move(TernaryMiddle); 266 } else { 267 // Special case handling of "X ? Y : Z" where Y is empty: 268 // logical-OR-expression '?' ':' conditional-expression [GNU] 269 TernaryMiddle = 0; 270 Diag(Tok, diag::ext_gnu_conditional_expr); 271 } 272 273 if (Tok.isNot(tok::colon)) { 274 Diag(Tok, diag::err_expected_colon); 275 Diag(OpToken, diag::note_matching) << "?"; 276 return ExprError(); 277 } 278 279 // Eat the colon. 280 ColonLoc = ConsumeToken(); 281 } 282 283 // Parse another leaf here for the RHS of the operator. 284 OwningExprResult RHS(ParseCastExpression(false)); 285 if (RHS.isInvalid()) 286 return move(RHS); 287 288 // Remember the precedence of this operator and get the precedence of the 289 // operator immediately to the right of the RHS. 290 unsigned ThisPrec = NextTokPrec; 291 NextTokPrec = getBinOpPrecedence(Tok.getKind()); 292 293 // Assignment and conditional expressions are right-associative. 294 bool isRightAssoc = ThisPrec == prec::Conditional || 295 ThisPrec == prec::Assignment; 296 297 // Get the precedence of the operator to the right of the RHS. If it binds 298 // more tightly with RHS than we do, evaluate it completely first. 299 if (ThisPrec < NextTokPrec || 300 (ThisPrec == NextTokPrec && isRightAssoc)) { 301 // If this is left-associative, only parse things on the RHS that bind 302 // more tightly than the current operator. If it is left-associative, it 303 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as 304 // A=(B=(C=D)), where each paren is a level of recursion here. 305 // The function takes ownership of the RHS. 306 RHS = ParseRHSOfBinaryExpression(move(RHS), ThisPrec + !isRightAssoc); 307 if (RHS.isInvalid()) 308 return move(RHS); 309 310 NextTokPrec = getBinOpPrecedence(Tok.getKind()); 311 } 312 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); 313 314 if (!LHS.isInvalid()) { 315 // Combine the LHS and RHS into the LHS (e.g. build AST). 316 if (TernaryMiddle.isInvalid()) 317 LHS = Actions.ActOnBinOp(CurScope, OpToken.getLocation(), 318 OpToken.getKind(), LHS.release(), 319 RHS.release()); 320 else 321 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, 322 LHS.release(), TernaryMiddle.release(), 323 RHS.release()); 324 } 325 } 326} 327 328/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is 329/// true, parse a unary-expression. 330/// 331/// cast-expression: [C99 6.5.4] 332/// unary-expression 333/// '(' type-name ')' cast-expression 334/// 335/// unary-expression: [C99 6.5.3] 336/// postfix-expression 337/// '++' unary-expression 338/// '--' unary-expression 339/// unary-operator cast-expression 340/// 'sizeof' unary-expression 341/// 'sizeof' '(' type-name ')' 342/// [GNU] '__alignof' unary-expression 343/// [GNU] '__alignof' '(' type-name ')' 344/// [C++0x] 'alignof' '(' type-id ')' 345/// [GNU] '&&' identifier 346/// [C++] new-expression 347/// [C++] delete-expression 348/// 349/// unary-operator: one of 350/// '&' '*' '+' '-' '~' '!' 351/// [GNU] '__extension__' '__real' '__imag' 352/// 353/// primary-expression: [C99 6.5.1] 354/// [C99] identifier 355/// [C++] id-expression 356/// constant 357/// string-literal 358/// [C++] boolean-literal [C++ 2.13.5] 359/// '(' expression ')' 360/// '__func__' [C99 6.4.2.2] 361/// [GNU] '__FUNCTION__' 362/// [GNU] '__PRETTY_FUNCTION__' 363/// [GNU] '(' compound-statement ')' 364/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 365/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 366/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 367/// assign-expr ')' 368/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 369/// [GNU] '__null' 370/// [OBJC] '[' objc-message-expr ']' 371/// [OBJC] '@selector' '(' objc-selector-arg ')' 372/// [OBJC] '@protocol' '(' identifier ')' 373/// [OBJC] '@encode' '(' type-name ')' 374/// [OBJC] objc-string-literal 375/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 376/// [C++] typename-specifier '(' expression-list[opt] ')' [TODO] 377/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 378/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 379/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 380/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 381/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] 382/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] 383/// [C++] 'this' [C++ 9.3.2] 384/// [clang] '^' block-literal 385/// 386/// constant: [C99 6.4.4] 387/// integer-constant 388/// floating-constant 389/// enumeration-constant -> identifier 390/// character-constant 391/// 392/// id-expression: [C++ 5.1] 393/// unqualified-id 394/// qualified-id [TODO] 395/// 396/// unqualified-id: [C++ 5.1] 397/// identifier 398/// operator-function-id 399/// conversion-function-id [TODO] 400/// '~' class-name [TODO] 401/// template-id [TODO] 402/// 403/// new-expression: [C++ 5.3.4] 404/// '::'[opt] 'new' new-placement[opt] new-type-id 405/// new-initializer[opt] 406/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 407/// new-initializer[opt] 408/// 409/// delete-expression: [C++ 5.3.5] 410/// '::'[opt] 'delete' cast-expression 411/// '::'[opt] 'delete' '[' ']' cast-expression 412/// 413Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression) { 414 if (getLang().CPlusPlus) { 415 // Annotate typenames and C++ scope specifiers. 416 // Used only in C++, where the typename can be considered as a functional 417 // style cast ("int(1)"). 418 // In C we don't expect identifiers to be treated as typenames; if it's a 419 // typedef name, let it be handled as an identifier and 420 // Actions.ActOnIdentifierExpr will emit the proper diagnostic. 421 TryAnnotateTypeOrScopeToken(); 422 } 423 424 OwningExprResult Res(Actions); 425 tok::TokenKind SavedKind = Tok.getKind(); 426 427 // This handles all of cast-expression, unary-expression, postfix-expression, 428 // and primary-expression. We handle them together like this for efficiency 429 // and to simplify handling of an expression starting with a '(' token: which 430 // may be one of a parenthesized expression, cast-expression, compound literal 431 // expression, or statement expression. 432 // 433 // If the parsed tokens consist of a primary-expression, the cases below 434 // call ParsePostfixExpressionSuffix to handle the postfix expression 435 // suffixes. Cases that cannot be followed by postfix exprs should 436 // return without invoking ParsePostfixExpressionSuffix. 437 switch (SavedKind) { 438 case tok::l_paren: { 439 // If this expression is limited to being a unary-expression, the parent can 440 // not start a cast expression. 441 ParenParseOption ParenExprType = 442 isUnaryExpression ? CompoundLiteral : CastExpr; 443 TypeTy *CastTy; 444 SourceLocation LParenLoc = Tok.getLocation(); 445 SourceLocation RParenLoc; 446 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); 447 if (Res.isInvalid()) return move(Res); 448 449 switch (ParenExprType) { 450 case SimpleExpr: break; // Nothing else to do. 451 case CompoundStmt: break; // Nothing else to do. 452 case CompoundLiteral: 453 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of 454 // postfix-expression exist, parse them now. 455 break; 456 case CastExpr: 457 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse 458 // the cast-expression that follows it next. 459 // TODO: For cast expression with CastTy. 460 Res = ParseCastExpression(false); 461 if (!Res.isInvalid()) 462 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, 463 Res.release()); 464 return move(Res); 465 } 466 467 // These can be followed by postfix-expr pieces. 468 return ParsePostfixExpressionSuffix(move(Res)); 469 } 470 471 // primary-expression 472 case tok::numeric_constant: 473 // constant: integer-constant 474 // constant: floating-constant 475 476 Res = Actions.ActOnNumericConstant(Tok); 477 ConsumeToken(); 478 479 // These can be followed by postfix-expr pieces. 480 return ParsePostfixExpressionSuffix(move(Res)); 481 482 case tok::kw_true: 483 case tok::kw_false: 484 return ParseCXXBoolLiteral(); 485 486 case tok::identifier: { // primary-expression: identifier 487 // unqualified-id: identifier 488 // constant: enumeration-constant 489 490 // Consume the identifier so that we can see if it is followed by a '('. 491 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we 492 // need to know whether or not this identifier is a function designator or 493 // not. 494 IdentifierInfo &II = *Tok.getIdentifierInfo(); 495 SourceLocation L = ConsumeToken(); 496 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); 497 // These can be followed by postfix-expr pieces. 498 return ParsePostfixExpressionSuffix(move(Res)); 499 } 500 case tok::char_constant: // constant: character-constant 501 Res = Actions.ActOnCharacterConstant(Tok); 502 ConsumeToken(); 503 // These can be followed by postfix-expr pieces. 504 return ParsePostfixExpressionSuffix(move(Res)); 505 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] 506 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] 507 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] 508 Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); 509 ConsumeToken(); 510 // These can be followed by postfix-expr pieces. 511 return ParsePostfixExpressionSuffix(move(Res)); 512 case tok::string_literal: // primary-expression: string-literal 513 case tok::wide_string_literal: 514 Res = ParseStringLiteralExpression(); 515 if (Res.isInvalid()) return move(Res); 516 // This can be followed by postfix-expr pieces (e.g. "foo"[1]). 517 return ParsePostfixExpressionSuffix(move(Res)); 518 case tok::kw___builtin_va_arg: 519 case tok::kw___builtin_offsetof: 520 case tok::kw___builtin_choose_expr: 521 case tok::kw___builtin_overload: 522 case tok::kw___builtin_types_compatible_p: 523 return ParseBuiltinPrimaryExpression(); 524 case tok::kw___null: 525 return Owned(Actions.ActOnGNUNullExpr(ConsumeToken())); 526 break; 527 case tok::plusplus: // unary-expression: '++' unary-expression 528 case tok::minusminus: { // unary-expression: '--' unary-expression 529 SourceLocation SavedLoc = ConsumeToken(); 530 Res = ParseCastExpression(true); 531 if (!Res.isInvalid()) 532 Res = Owned(Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, 533 Res.release())); 534 return move(Res); 535 } 536 case tok::amp: // unary-expression: '&' cast-expression 537 case tok::star: // unary-expression: '*' cast-expression 538 case tok::plus: // unary-expression: '+' cast-expression 539 case tok::minus: // unary-expression: '-' cast-expression 540 case tok::tilde: // unary-expression: '~' cast-expression 541 case tok::exclaim: // unary-expression: '!' cast-expression 542 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] 543 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] 544 SourceLocation SavedLoc = ConsumeToken(); 545 Res = ParseCastExpression(false); 546 if (!Res.isInvalid()) 547 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release()); 548 return move(Res); 549 } 550 551 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] 552 // __extension__ silences extension warnings in the subexpression. 553 ExtensionRAIIObject O(Diags); // Use RAII to do this. 554 SourceLocation SavedLoc = ConsumeToken(); 555 Res = ParseCastExpression(false); 556 if (!Res.isInvalid()) 557 Res = Actions.ActOnUnaryOp(CurScope, SavedLoc, SavedKind, Res.release()); 558 return move(Res); 559 } 560 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression 561 // unary-expression: 'sizeof' '(' type-name ')' 562 case tok::kw_alignof: 563 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression 564 // unary-expression: '__alignof' '(' type-name ')' 565 // unary-expression: 'alignof' '(' type-id ')' 566 return ParseSizeofAlignofExpression(); 567 case tok::ampamp: { // unary-expression: '&&' identifier 568 SourceLocation AmpAmpLoc = ConsumeToken(); 569 if (Tok.isNot(tok::identifier)) 570 return ExprError(Diag(Tok, diag::err_expected_ident)); 571 572 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); 573 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), 574 Tok.getIdentifierInfo()); 575 ConsumeToken(); 576 return move(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 Res = ParseCXXCasts(); 583 // These can be followed by postfix-expr pieces. 584 return ParsePostfixExpressionSuffix(move(Res)); 585 case tok::kw_typeid: 586 Res = ParseCXXTypeid(); 587 // This can be followed by postfix-expr pieces. 588 return ParsePostfixExpressionSuffix(move(Res)); 589 case tok::kw_this: 590 Res = ParseCXXThis(); 591 // This can be followed by postfix-expr pieces. 592 return ParsePostfixExpressionSuffix(move(Res)); 593 594 case tok::kw_char: 595 case tok::kw_wchar_t: 596 case tok::kw_bool: 597 case tok::kw_short: 598 case tok::kw_int: 599 case tok::kw_long: 600 case tok::kw_signed: 601 case tok::kw_unsigned: 602 case tok::kw_float: 603 case tok::kw_double: 604 case tok::kw_void: 605 case tok::kw_typeof: { 606 if (!getLang().CPlusPlus) 607 goto UnhandledToken; 608 case tok::annot_qualtypename: 609 assert(getLang().CPlusPlus && "Expected C++"); 610 // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' 611 // 612 DeclSpec DS; 613 ParseCXXSimpleTypeSpecifier(DS); 614 if (Tok.isNot(tok::l_paren)) 615 return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) 616 << DS.getSourceRange()); 617 618 Res = ParseCXXTypeConstructExpression(DS); 619 // This can be followed by postfix-expr pieces. 620 return ParsePostfixExpressionSuffix(move(Res)); 621 } 622 623 case tok::annot_cxxscope: // [C++] id-expression: qualified-id 624 case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id 625 // template-id 626 Res = ParseCXXIdExpression(); 627 return ParsePostfixExpressionSuffix(move(Res)); 628 629 case tok::coloncolon: { // [C++] new-expression or [C++] delete-expression 630 SourceLocation ScopeLoc = ConsumeToken(); 631 if (Tok.is(tok::kw_new)) 632 return ParseCXXNewExpression(true, ScopeLoc); 633 else { 634 // If the next token is neither 'new' nor 'delete', the :: would have been 635 // parsed as a scope specifier already. 636 assert(Tok.is(tok::kw_delete)); 637 return ParseCXXDeleteExpression(true, ScopeLoc); 638 } 639 } 640 641 case tok::kw_new: // [C++] new-expression 642 return ParseCXXNewExpression(false, Tok.getLocation()); 643 644 case tok::kw_delete: // [C++] delete-expression 645 return ParseCXXDeleteExpression(false, Tok.getLocation()); 646 647 case tok::at: { 648 SourceLocation AtLoc = ConsumeToken(); 649 return ParseObjCAtExpression(AtLoc); 650 } 651 case tok::caret: 652 if (getLang().Blocks) 653 return ParsePostfixExpressionSuffix(ParseBlockLiteralExpression()); 654 Diag(Tok, diag::err_expected_expression); 655 return ExprError(); 656 case tok::l_square: 657 // These can be followed by postfix-expr pieces. 658 if (getLang().ObjC1) 659 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); 660 // FALL THROUGH. 661 default: 662 UnhandledToken: 663 Diag(Tok, diag::err_expected_expression); 664 return ExprError(); 665 } 666 667 // unreachable. 668 abort(); 669} 670 671/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression 672/// is parsed, this method parses any suffixes that apply. 673/// 674/// postfix-expression: [C99 6.5.2] 675/// primary-expression 676/// postfix-expression '[' expression ']' 677/// postfix-expression '(' argument-expression-list[opt] ')' 678/// postfix-expression '.' identifier 679/// postfix-expression '->' identifier 680/// postfix-expression '++' 681/// postfix-expression '--' 682/// '(' type-name ')' '{' initializer-list '}' 683/// '(' type-name ')' '{' initializer-list ',' '}' 684/// 685/// argument-expression-list: [C99 6.5.2] 686/// argument-expression 687/// argument-expression-list ',' assignment-expression 688/// 689Parser::OwningExprResult 690Parser::ParsePostfixExpressionSuffix(OwningExprResult LHS) { 691 // Now that the primary-expression piece of the postfix-expression has been 692 // parsed, see if there are any postfix-expression pieces here. 693 SourceLocation Loc; 694 while (1) { 695 switch (Tok.getKind()) { 696 default: // Not a postfix-expression suffix. 697 return move(LHS); 698 case tok::l_square: { // postfix-expression: p-e '[' expression ']' 699 Loc = ConsumeBracket(); 700 OwningExprResult Idx(ParseExpression()); 701 702 SourceLocation RLoc = Tok.getLocation(); 703 704 if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { 705 LHS = Actions.ActOnArraySubscriptExpr(CurScope, LHS.release(), Loc, 706 Idx.release(), RLoc); 707 } else 708 LHS = ExprError(); 709 710 // Match the ']'. 711 MatchRHSPunctuation(tok::r_square, Loc); 712 break; 713 } 714 715 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' 716 ExprVector ArgExprs(Actions); 717 CommaLocsTy CommaLocs; 718 719 Loc = ConsumeParen(); 720 721 if (Tok.isNot(tok::r_paren)) { 722 if (ParseExpressionList(ArgExprs, CommaLocs)) { 723 SkipUntil(tok::r_paren); 724 return ExprError(); 725 } 726 } 727 728 // Match the ')'. 729 if (!LHS.isInvalid() && Tok.is(tok::r_paren)) { 730 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& 731 "Unexpected number of commas!"); 732 LHS = Actions.ActOnCallExpr(CurScope, LHS.release(), Loc, 733 ArgExprs.take(), 734 ArgExprs.size(), &CommaLocs[0], 735 Tok.getLocation()); 736 } 737 738 MatchRHSPunctuation(tok::r_paren, Loc); 739 break; 740 } 741 case tok::arrow: // postfix-expression: p-e '->' identifier 742 case tok::period: { // postfix-expression: p-e '.' identifier 743 tok::TokenKind OpKind = Tok.getKind(); 744 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. 745 746 if (Tok.isNot(tok::identifier)) { 747 Diag(Tok, diag::err_expected_ident); 748 return ExprError(); 749 } 750 751 if (!LHS.isInvalid()) { 752 LHS = Actions.ActOnMemberReferenceExpr(CurScope, LHS.release(), OpLoc, 753 OpKind, Tok.getLocation(), 754 *Tok.getIdentifierInfo()); 755 } 756 ConsumeToken(); 757 break; 758 } 759 case tok::plusplus: // postfix-expression: postfix-expression '++' 760 case tok::minusminus: // postfix-expression: postfix-expression '--' 761 if (!LHS.isInvalid()) { 762 LHS = Actions.ActOnPostfixUnaryOp(CurScope, Tok.getLocation(), 763 Tok.getKind(), LHS.release()); 764 } 765 ConsumeToken(); 766 break; 767 } 768 } 769} 770 771 772/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. 773/// unary-expression: [C99 6.5.3] 774/// 'sizeof' unary-expression 775/// 'sizeof' '(' type-name ')' 776/// [GNU] '__alignof' unary-expression 777/// [GNU] '__alignof' '(' type-name ')' 778/// [C++0x] 'alignof' '(' type-id ')' 779Parser::OwningExprResult Parser::ParseSizeofAlignofExpression() { 780 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) 781 || Tok.is(tok::kw_alignof)) && 782 "Not a sizeof/alignof expression!"); 783 Token OpTok = Tok; 784 ConsumeToken(); 785 786 // If the operand doesn't start with an '(', it must be an expression. 787 OwningExprResult Operand(Actions); 788 if (Tok.isNot(tok::l_paren)) { 789 Operand = ParseCastExpression(true); 790 } else { 791 // If it starts with a '(', we know that it is either a parenthesized 792 // type-name, or it is a unary-expression that starts with a compound 793 // literal, or starts with a primary-expression that is a parenthesized 794 // expression. 795 ParenParseOption ExprType = CastExpr; 796 TypeTy *CastTy; 797 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 798 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); 799 800 // If ParseParenExpression parsed a '(typename)' sequence only, the this is 801 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. 802 if (ExprType == CastExpr) 803 return Owned(Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), 804 OpTok.is(tok::kw_sizeof), 805 /*isType=*/true, CastTy, 806 SourceRange(LParenLoc, RParenLoc))); 807 808 // If this is a parenthesized expression, it is the start of a 809 // unary-expression, but doesn't include any postfix pieces. Parse these 810 // now if present. 811 Operand = ParsePostfixExpressionSuffix(move(Operand)); 812 } 813 814 // If we get here, the operand to the sizeof/alignof was an expresion. 815 if (!Operand.isInvalid()) 816 Operand = Actions.ActOnSizeOfAlignOfExpr(OpTok.getLocation(), 817 OpTok.is(tok::kw_sizeof), 818 /*isType=*/false, 819 Operand.release(), SourceRange()); 820 return move(Operand); 821} 822 823/// ParseBuiltinPrimaryExpression 824/// 825/// primary-expression: [C99 6.5.1] 826/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 827/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 828/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 829/// assign-expr ')' 830/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 831/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')' 832/// 833/// [GNU] offsetof-member-designator: 834/// [GNU] identifier 835/// [GNU] offsetof-member-designator '.' identifier 836/// [GNU] offsetof-member-designator '[' expression ']' 837/// 838Parser::OwningExprResult Parser::ParseBuiltinPrimaryExpression() { 839 OwningExprResult Res(Actions); 840 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); 841 842 tok::TokenKind T = Tok.getKind(); 843 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. 844 845 // All of these start with an open paren. 846 if (Tok.isNot(tok::l_paren)) 847 return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) 848 << BuiltinII); 849 850 SourceLocation LParenLoc = ConsumeParen(); 851 // TODO: Build AST. 852 853 switch (T) { 854 default: assert(0 && "Not a builtin primary expression!"); 855 case tok::kw___builtin_va_arg: { 856 OwningExprResult Expr(ParseAssignmentExpression()); 857 if (Expr.isInvalid()) { 858 SkipUntil(tok::r_paren); 859 return ExprError(); 860 } 861 862 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 863 return ExprError(); 864 865 TypeTy *Ty = ParseTypeName(); 866 867 if (Tok.isNot(tok::r_paren)) { 868 Diag(Tok, diag::err_expected_rparen); 869 return ExprError(); 870 } 871 Res = Actions.ActOnVAArg(StartLoc, Expr.release(), Ty, ConsumeParen()); 872 break; 873 } 874 case tok::kw___builtin_offsetof: { 875 SourceLocation TypeLoc = Tok.getLocation(); 876 TypeTy *Ty = ParseTypeName(); 877 878 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 879 return ExprError(); 880 881 // We must have at least one identifier here. 882 if (Tok.isNot(tok::identifier)) { 883 Diag(Tok, diag::err_expected_ident); 884 SkipUntil(tok::r_paren); 885 return ExprError(); 886 } 887 888 // Keep track of the various subcomponents we see. 889 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; 890 891 Comps.push_back(Action::OffsetOfComponent()); 892 Comps.back().isBrackets = false; 893 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 894 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); 895 896 // FIXME: This loop leaks the index expressions on error. 897 while (1) { 898 if (Tok.is(tok::period)) { 899 // offsetof-member-designator: offsetof-member-designator '.' identifier 900 Comps.push_back(Action::OffsetOfComponent()); 901 Comps.back().isBrackets = false; 902 Comps.back().LocStart = ConsumeToken(); 903 904 if (Tok.isNot(tok::identifier)) { 905 Diag(Tok, diag::err_expected_ident); 906 SkipUntil(tok::r_paren); 907 return ExprError(); 908 } 909 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 910 Comps.back().LocEnd = ConsumeToken(); 911 912 } else if (Tok.is(tok::l_square)) { 913 // offsetof-member-designator: offsetof-member-design '[' expression ']' 914 Comps.push_back(Action::OffsetOfComponent()); 915 Comps.back().isBrackets = true; 916 Comps.back().LocStart = ConsumeBracket(); 917 Res = ParseExpression(); 918 if (Res.isInvalid()) { 919 SkipUntil(tok::r_paren); 920 return move(Res); 921 } 922 Comps.back().U.E = Res.release(); 923 924 Comps.back().LocEnd = 925 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); 926 } else if (Tok.is(tok::r_paren)) { 927 Res = Actions.ActOnBuiltinOffsetOf(CurScope, StartLoc, TypeLoc, Ty, 928 &Comps[0], Comps.size(), 929 ConsumeParen()); 930 break; 931 } else { 932 // Error occurred. 933 return ExprError(); 934 } 935 } 936 break; 937 } 938 case tok::kw___builtin_choose_expr: { 939 OwningExprResult Cond(ParseAssignmentExpression()); 940 if (Cond.isInvalid()) { 941 SkipUntil(tok::r_paren); 942 return move(Cond); 943 } 944 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 945 return ExprError(); 946 947 OwningExprResult Expr1(ParseAssignmentExpression()); 948 if (Expr1.isInvalid()) { 949 SkipUntil(tok::r_paren); 950 return move(Expr1); 951 } 952 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 953 return ExprError(); 954 955 OwningExprResult Expr2(ParseAssignmentExpression()); 956 if (Expr2.isInvalid()) { 957 SkipUntil(tok::r_paren); 958 return move(Expr2); 959 } 960 if (Tok.isNot(tok::r_paren)) { 961 Diag(Tok, diag::err_expected_rparen); 962 return ExprError(); 963 } 964 Res = Actions.ActOnChooseExpr(StartLoc, Cond.release(), Expr1.release(), 965 Expr2.release(), ConsumeParen()); 966 break; 967 } 968 case tok::kw___builtin_overload: { 969 ExprVector ArgExprs(Actions); 970 llvm::SmallVector<SourceLocation, 8> CommaLocs; 971 972 // For each iteration through the loop look for assign-expr followed by a 973 // comma. If there is no comma, break and attempt to match r-paren. 974 if (Tok.isNot(tok::r_paren)) { 975 while (1) { 976 OwningExprResult ArgExpr(ParseAssignmentExpression()); 977 if (ArgExpr.isInvalid()) { 978 SkipUntil(tok::r_paren); 979 return ExprError(); 980 } else 981 ArgExprs.push_back(ArgExpr.release()); 982 983 if (Tok.isNot(tok::comma)) 984 break; 985 // Move to the next argument, remember where the comma was. 986 CommaLocs.push_back(ConsumeToken()); 987 } 988 } 989 990 // Attempt to consume the r-paren 991 if (Tok.isNot(tok::r_paren)) { 992 Diag(Tok, diag::err_expected_rparen); 993 SkipUntil(tok::r_paren); 994 return ExprError(); 995 } 996 Res = Actions.ActOnOverloadExpr(ArgExprs.take(), ArgExprs.size(), 997 &CommaLocs[0], StartLoc, ConsumeParen()); 998 break; 999 } 1000 case tok::kw___builtin_types_compatible_p: 1001 TypeTy *Ty1 = ParseTypeName(); 1002 1003 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1004 return ExprError(); 1005 1006 TypeTy *Ty2 = ParseTypeName(); 1007 1008 if (Tok.isNot(tok::r_paren)) { 1009 Diag(Tok, diag::err_expected_rparen); 1010 return ExprError(); 1011 } 1012 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen()); 1013 break; 1014 } 1015 1016 // These can be followed by postfix-expr pieces because they are 1017 // primary-expressions. 1018 return ParsePostfixExpressionSuffix(move(Res)); 1019} 1020 1021/// ParseParenExpression - This parses the unit that starts with a '(' token, 1022/// based on what is allowed by ExprType. The actual thing parsed is returned 1023/// in ExprType. 1024/// 1025/// primary-expression: [C99 6.5.1] 1026/// '(' expression ')' 1027/// [GNU] '(' compound-statement ')' (if !ParenExprOnly) 1028/// postfix-expression: [C99 6.5.2] 1029/// '(' type-name ')' '{' initializer-list '}' 1030/// '(' type-name ')' '{' initializer-list ',' '}' 1031/// cast-expression: [C99 6.5.4] 1032/// '(' type-name ')' cast-expression 1033/// 1034Parser::OwningExprResult 1035Parser::ParseParenExpression(ParenParseOption &ExprType, 1036 TypeTy *&CastTy, SourceLocation &RParenLoc) { 1037 assert(Tok.is(tok::l_paren) && "Not a paren expr!"); 1038 SourceLocation OpenLoc = ConsumeParen(); 1039 OwningExprResult Result(Actions, true); 1040 CastTy = 0; 1041 1042 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { 1043 Diag(Tok, diag::ext_gnu_statement_expr); 1044 OwningStmtResult Stmt(ParseCompoundStatement(true)); 1045 ExprType = CompoundStmt; 1046 1047 // If the substmt parsed correctly, build the AST node. 1048 if (!Stmt.isInvalid() && Tok.is(tok::r_paren)) 1049 Result = Actions.ActOnStmtExpr( 1050 OpenLoc, Stmt.release(), Tok.getLocation()); 1051 1052 } else if (ExprType >= CompoundLiteral && isTypeIdInParens()) { 1053 // Otherwise, this is a compound literal expression or cast expression. 1054 TypeTy *Ty = ParseTypeName(); 1055 1056 // Match the ')'. 1057 if (Tok.is(tok::r_paren)) 1058 RParenLoc = ConsumeParen(); 1059 else 1060 MatchRHSPunctuation(tok::r_paren, OpenLoc); 1061 1062 if (Tok.is(tok::l_brace)) { 1063 if (!getLang().C99) // Compound literals don't exist in C90. 1064 Diag(OpenLoc, diag::ext_c99_compound_literal); 1065 Result = ParseInitializer(); 1066 ExprType = CompoundLiteral; 1067 if (!Result.isInvalid()) 1068 return Owned(Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, 1069 Result.release())); 1070 return move(Result); 1071 } 1072 1073 if (ExprType == CastExpr) { 1074 // Note that this doesn't parse the subsequence cast-expression, it just 1075 // returns the parsed type to the callee. 1076 ExprType = CastExpr; 1077 CastTy = Ty; 1078 return OwningExprResult(Actions); 1079 } 1080 1081 Diag(Tok, diag::err_expected_lbrace_in_compound_literal); 1082 return ExprError(); 1083 } else { 1084 Result = ParseExpression(); 1085 ExprType = SimpleExpr; 1086 if (!Result.isInvalid() && Tok.is(tok::r_paren)) 1087 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), 1088 Result.release()); 1089 } 1090 1091 // Match the ')'. 1092 if (Result.isInvalid()) { 1093 SkipUntil(tok::r_paren); 1094 return ExprError(); 1095 } 1096 1097 if (Tok.is(tok::r_paren)) 1098 RParenLoc = ConsumeParen(); 1099 else 1100 MatchRHSPunctuation(tok::r_paren, OpenLoc); 1101 1102 return move(Result); 1103} 1104 1105/// ParseStringLiteralExpression - This handles the various token types that 1106/// form string literals, and also handles string concatenation [C99 5.1.1.2, 1107/// translation phase #6]. 1108/// 1109/// primary-expression: [C99 6.5.1] 1110/// string-literal 1111Parser::OwningExprResult Parser::ParseStringLiteralExpression() { 1112 assert(isTokenStringLiteral() && "Not a string literal!"); 1113 1114 // String concat. Note that keywords like __func__ and __FUNCTION__ are not 1115 // considered to be strings for concatenation purposes. 1116 llvm::SmallVector<Token, 4> StringToks; 1117 1118 do { 1119 StringToks.push_back(Tok); 1120 ConsumeStringToken(); 1121 } while (isTokenStringLiteral()); 1122 1123 // Pass the set of string tokens, ready for concatenation, to the actions. 1124 return Owned(Actions.ActOnStringLiteral(&StringToks[0], StringToks.size())); 1125} 1126 1127/// ParseExpressionList - Used for C/C++ (argument-)expression-list. 1128/// 1129/// argument-expression-list: 1130/// assignment-expression 1131/// argument-expression-list , assignment-expression 1132/// 1133/// [C++] expression-list: 1134/// [C++] assignment-expression 1135/// [C++] expression-list , assignment-expression 1136/// 1137bool Parser::ParseExpressionList(ExprListTy &Exprs, CommaLocsTy &CommaLocs) { 1138 while (1) { 1139 OwningExprResult Expr(ParseAssignmentExpression()); 1140 if (Expr.isInvalid()) 1141 return true; 1142 1143 Exprs.push_back(Expr.release()); 1144 1145 if (Tok.isNot(tok::comma)) 1146 return false; 1147 // Move to the next argument, remember where the comma was. 1148 CommaLocs.push_back(ConsumeToken()); 1149 } 1150} 1151 1152/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks 1153/// like ^(int x){ return x+1; } 1154/// 1155/// block-literal: 1156/// [clang] '^' block-args[opt] compound-statement 1157/// [clang] block-args: 1158/// [clang] '(' parameter-list ')' 1159/// 1160Parser::OwningExprResult Parser::ParseBlockLiteralExpression() { 1161 assert(Tok.is(tok::caret) && "block literal starts with ^"); 1162 SourceLocation CaretLoc = ConsumeToken(); 1163 1164 // Enter a scope to hold everything within the block. This includes the 1165 // argument decls, decls within the compound expression, etc. This also 1166 // allows determining whether a variable reference inside the block is 1167 // within or outside of the block. 1168 ParseScope BlockScope(this, Scope::BlockScope|Scope::FnScope|Scope::BreakScope| 1169 Scope::ContinueScope|Scope::DeclScope); 1170 1171 // Inform sema that we are starting a block. 1172 Actions.ActOnBlockStart(CaretLoc, CurScope); 1173 1174 // Parse the return type if present. 1175 DeclSpec DS; 1176 Declarator ParamInfo(DS, Declarator::PrototypeContext); 1177 1178 // If this block has arguments, parse them. There is no ambiguity here with 1179 // the expression case, because the expression case requires a parameter list. 1180 if (Tok.is(tok::l_paren)) { 1181 ParseParenDeclarator(ParamInfo); 1182 // Parse the pieces after the identifier as if we had "int(...)". 1183 ParamInfo.SetIdentifier(0, CaretLoc); 1184 if (ParamInfo.getInvalidType()) { 1185 // If there was an error parsing the arguments, they may have tried to use 1186 // ^(x+y) which requires an argument list. Just skip the whole block 1187 // literal. 1188 return ExprError(); 1189 } 1190 } else { 1191 // Otherwise, pretend we saw (void). 1192 ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(true, false, 1193 0, 0, 0, CaretLoc)); 1194 } 1195 1196 // Inform sema that we are starting a block. 1197 Actions.ActOnBlockArguments(ParamInfo); 1198 1199 OwningExprResult Result(Actions, true); 1200 if (Tok.is(tok::l_brace)) { 1201 OwningStmtResult Stmt(ParseCompoundStatementBody()); 1202 if (!Stmt.isInvalid()) { 1203 Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope); 1204 } else { 1205 Actions.ActOnBlockError(CaretLoc, CurScope); 1206 } 1207 } 1208 return move(Result); 1209} 1210 1211