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