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