1193326Sed//===--- ParseExpr.cpp - Expression Parsing -------------------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9239462Sdim/// 10239462Sdim/// \file 11239462Sdim/// \brief Provides the Expression parsing implementation. 12239462Sdim/// 13239462Sdim/// Expressions in C99 basically consist of a bunch of binary operators with 14239462Sdim/// unary operators and other random stuff at the leaves. 15239462Sdim/// 16239462Sdim/// In the C99 grammar, these unary operators bind tightest and are represented 17239462Sdim/// as the 'cast-expression' production. Everything else is either a binary 18239462Sdim/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are 19239462Sdim/// handled by ParseCastExpression, the higher level pieces are handled by 20239462Sdim/// ParseBinaryExpression. 21239462Sdim/// 22193326Sed//===----------------------------------------------------------------------===// 23193326Sed 24193326Sed#include "clang/Parse/Parser.h" 25249423Sdim#include "RAIIObjectsForParser.h" 26249423Sdim#include "clang/Basic/PrettyStackTrace.h" 27212904Sdim#include "clang/Sema/DeclSpec.h" 28249423Sdim#include "clang/Sema/ParsedTemplate.h" 29212904Sdim#include "clang/Sema/Scope.h" 30234353Sdim#include "clang/Sema/TypoCorrection.h" 31249423Sdim#include "llvm/ADT/SmallString.h" 32193326Sed#include "llvm/ADT/SmallVector.h" 33193326Sedusing namespace clang; 34193326Sed 35239462Sdim/// \brief Simple precedence-based parser for binary/ternary operators. 36193326Sed/// 37193326Sed/// Note: we diverge from the C99 grammar when parsing the assignment-expression 38193326Sed/// production. C99 specifies that the LHS of an assignment operator should be 39193326Sed/// parsed as a unary-expression, but consistency dictates that it be a 40193326Sed/// conditional-expession. In practice, the important thing here is that the 41193326Sed/// LHS of an assignment has to be an l-value, which productions between 42193326Sed/// unary-expression and conditional-expression don't produce. Because we want 43193326Sed/// consistency, we parse the LHS as a conditional-expression, then check for 44193326Sed/// l-value-ness in semantic analysis stages. 45193326Sed/// 46239462Sdim/// \verbatim 47193326Sed/// pm-expression: [C++ 5.5] 48193326Sed/// cast-expression 49193326Sed/// pm-expression '.*' cast-expression 50193326Sed/// pm-expression '->*' cast-expression 51193326Sed/// 52193326Sed/// multiplicative-expression: [C99 6.5.5] 53193326Sed/// Note: in C++, apply pm-expression instead of cast-expression 54193326Sed/// cast-expression 55193326Sed/// multiplicative-expression '*' cast-expression 56193326Sed/// multiplicative-expression '/' cast-expression 57193326Sed/// multiplicative-expression '%' cast-expression 58193326Sed/// 59193326Sed/// additive-expression: [C99 6.5.6] 60193326Sed/// multiplicative-expression 61193326Sed/// additive-expression '+' multiplicative-expression 62193326Sed/// additive-expression '-' multiplicative-expression 63193326Sed/// 64193326Sed/// shift-expression: [C99 6.5.7] 65193326Sed/// additive-expression 66193326Sed/// shift-expression '<<' additive-expression 67193326Sed/// shift-expression '>>' additive-expression 68193326Sed/// 69193326Sed/// relational-expression: [C99 6.5.8] 70193326Sed/// shift-expression 71193326Sed/// relational-expression '<' shift-expression 72193326Sed/// relational-expression '>' shift-expression 73193326Sed/// relational-expression '<=' shift-expression 74193326Sed/// relational-expression '>=' shift-expression 75193326Sed/// 76193326Sed/// equality-expression: [C99 6.5.9] 77193326Sed/// relational-expression 78193326Sed/// equality-expression '==' relational-expression 79193326Sed/// equality-expression '!=' relational-expression 80193326Sed/// 81193326Sed/// AND-expression: [C99 6.5.10] 82193326Sed/// equality-expression 83193326Sed/// AND-expression '&' equality-expression 84193326Sed/// 85193326Sed/// exclusive-OR-expression: [C99 6.5.11] 86193326Sed/// AND-expression 87193326Sed/// exclusive-OR-expression '^' AND-expression 88193326Sed/// 89193326Sed/// inclusive-OR-expression: [C99 6.5.12] 90193326Sed/// exclusive-OR-expression 91193326Sed/// inclusive-OR-expression '|' exclusive-OR-expression 92193326Sed/// 93193326Sed/// logical-AND-expression: [C99 6.5.13] 94193326Sed/// inclusive-OR-expression 95193326Sed/// logical-AND-expression '&&' inclusive-OR-expression 96193326Sed/// 97193326Sed/// logical-OR-expression: [C99 6.5.14] 98193326Sed/// logical-AND-expression 99193326Sed/// logical-OR-expression '||' logical-AND-expression 100193326Sed/// 101193326Sed/// conditional-expression: [C99 6.5.15] 102193326Sed/// logical-OR-expression 103193326Sed/// logical-OR-expression '?' expression ':' conditional-expression 104193326Sed/// [GNU] logical-OR-expression '?' ':' conditional-expression 105193326Sed/// [C++] the third operand is an assignment-expression 106193326Sed/// 107193326Sed/// assignment-expression: [C99 6.5.16] 108193326Sed/// conditional-expression 109193326Sed/// unary-expression assignment-operator assignment-expression 110193326Sed/// [C++] throw-expression [C++ 15] 111193326Sed/// 112193326Sed/// assignment-operator: one of 113193326Sed/// = *= /= %= += -= <<= >>= &= ^= |= 114193326Sed/// 115193326Sed/// expression: [C99 6.5.17] 116218893Sdim/// assignment-expression ...[opt] 117218893Sdim/// expression ',' assignment-expression ...[opt] 118239462Sdim/// \endverbatim 119234353SdimExprResult Parser::ParseExpression(TypeCastState isTypeCast) { 120234353Sdim ExprResult LHS(ParseAssignmentExpression(isTypeCast)); 121243830Sdim return ParseRHSOfBinaryExpression(LHS, prec::Comma); 122193326Sed} 123193326Sed 124198092Srdivacky/// This routine is called when the '@' is seen and consumed. 125193326Sed/// Current token is an Identifier and is not a 'try'. This 126239462Sdim/// routine is necessary to disambiguate \@try-statement from, 127239462Sdim/// for example, \@encode-expression. 128193326Sed/// 129212904SdimExprResult 130193326SedParser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { 131212904Sdim ExprResult LHS(ParseObjCAtExpression(AtLoc)); 132243830Sdim return ParseRHSOfBinaryExpression(LHS, prec::Comma); 133193326Sed} 134193326Sed 135193326Sed/// This routine is called when a leading '__extension__' is seen and 136193326Sed/// consumed. This is necessary because the token gets consumed in the 137193326Sed/// process of disambiguating between an expression and a declaration. 138212904SdimExprResult 139193326SedParser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) { 140212904Sdim ExprResult LHS(true); 141193326Sed { 142193326Sed // Silence extension warnings in the sub-expression 143193326Sed ExtensionRAIIObject O(Diags); 144193326Sed 145193326Sed LHS = ParseCastExpression(false); 146193326Sed } 147193326Sed 148218893Sdim if (!LHS.isInvalid()) 149218893Sdim LHS = Actions.ActOnUnaryOp(getCurScope(), ExtLoc, tok::kw___extension__, 150218893Sdim LHS.take()); 151193326Sed 152243830Sdim return ParseRHSOfBinaryExpression(LHS, prec::Comma); 153193326Sed} 154193326Sed 155239462Sdim/// \brief Parse an expr that doesn't include (top-level) commas. 156234353SdimExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) { 157202379Srdivacky if (Tok.is(tok::code_completion)) { 158212904Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 159226633Sdim cutOffParsing(); 160226633Sdim return ExprError(); 161202379Srdivacky } 162202379Srdivacky 163193326Sed if (Tok.is(tok::kw_throw)) 164193326Sed return ParseThrowExpression(); 165193326Sed 166234353Sdim ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false, 167234353Sdim /*isAddressOfOperand=*/false, 168234353Sdim isTypeCast); 169243830Sdim return ParseRHSOfBinaryExpression(LHS, prec::Assignment); 170193326Sed} 171193326Sed 172239462Sdim/// \brief Parse an assignment expression where part of an Objective-C message 173239462Sdim/// send has already been parsed. 174193326Sed/// 175239462Sdim/// In this case \p LBracLoc indicates the location of the '[' of the message 176239462Sdim/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating 177239462Sdim/// the receiver of the message. 178239462Sdim/// 179193326Sed/// Since this handles full assignment-expression's, it handles postfix 180193326Sed/// expressions and other binary operators for these expressions as well. 181212904SdimExprResult 182193326SedParser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, 183207619Srdivacky SourceLocation SuperLoc, 184212904Sdim ParsedType ReceiverType, 185212904Sdim Expr *ReceiverExpr) { 186212904Sdim ExprResult R 187212904Sdim = ParseObjCMessageExpressionBody(LBracLoc, SuperLoc, 188212904Sdim ReceiverType, ReceiverExpr); 189218893Sdim R = ParsePostfixExpressionSuffix(R); 190218893Sdim return ParseRHSOfBinaryExpression(R, prec::Assignment); 191193326Sed} 192193326Sed 193193326Sed 194234353SdimExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) { 195234353Sdim // C++03 [basic.def.odr]p2: 196198092Srdivacky // An expression is potentially evaluated unless it appears where an 197194613Sed // integral constant expression is required (see 5.19) [...]. 198234353Sdim // C++98 and C++11 have no such rule, but this is only a defect in C++98. 199194711Sed EnterExpressionEvaluationContext Unevaluated(Actions, 200234353Sdim Sema::ConstantEvaluated); 201198092Srdivacky 202234353Sdim ExprResult LHS(ParseCastExpression(false, false, isTypeCast)); 203234353Sdim ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); 204234353Sdim return Actions.ActOnConstantExpression(Res); 205193326Sed} 206193326Sed 207243830Sdimbool Parser::isNotExpressionStart() { 208243830Sdim tok::TokenKind K = Tok.getKind(); 209243830Sdim if (K == tok::l_brace || K == tok::r_brace || 210243830Sdim K == tok::kw_for || K == tok::kw_while || 211243830Sdim K == tok::kw_if || K == tok::kw_else || 212243830Sdim K == tok::kw_goto || K == tok::kw_try) 213243830Sdim return true; 214243830Sdim // If this is a decl-specifier, we can't be at the start of an expression. 215243830Sdim return isKnownToBeDeclarationSpecifier(); 216243830Sdim} 217243830Sdim 218239462Sdim/// \brief Parse a binary expression that starts with \p LHS and has a 219239462Sdim/// precedence of at least \p MinPrec. 220212904SdimExprResult 221212904SdimParser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) { 222207619Srdivacky prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(), 223207619Srdivacky GreaterThanIsOperator, 224249423Sdim getLangOpts().CPlusPlus11); 225193326Sed SourceLocation ColonLoc; 226193326Sed 227193326Sed while (1) { 228193326Sed // If this token has a lower precedence than we are allowed to parse (e.g. 229193326Sed // because we are called recursively, or because the token is not a binop), 230193326Sed // then we are done! 231193326Sed if (NextTokPrec < MinPrec) 232243830Sdim return LHS; 233193326Sed 234193326Sed // Consume the operator, saving the operator token for error reporting. 235193326Sed Token OpToken = Tok; 236193326Sed ConsumeToken(); 237193326Sed 238243830Sdim // Bail out when encountering a comma followed by a token which can't 239243830Sdim // possibly be the start of an expression. For instance: 240243830Sdim // int f() { return 1, } 241243830Sdim // We can't do this before consuming the comma, because 242243830Sdim // isNotExpressionStart() looks at the token stream. 243243830Sdim if (OpToken.is(tok::comma) && isNotExpressionStart()) { 244243830Sdim PP.EnterToken(Tok); 245243830Sdim Tok = OpToken; 246243830Sdim return LHS; 247243830Sdim } 248243830Sdim 249193326Sed // Special case handling for the ternary operator. 250212904Sdim ExprResult TernaryMiddle(true); 251193326Sed if (NextTokPrec == prec::Conditional) { 252193326Sed if (Tok.isNot(tok::colon)) { 253200583Srdivacky // Don't parse FOO:BAR as if it were a typo for FOO::BAR. 254200583Srdivacky ColonProtectionRAIIObject X(*this); 255200583Srdivacky 256193326Sed // Handle this production specially: 257193326Sed // logical-OR-expression '?' expression ':' conditional-expression 258193326Sed // In particular, the RHS of the '?' is 'expression', not 259193326Sed // 'logical-OR-expression' as we might expect. 260193326Sed TernaryMiddle = ParseExpression(); 261218893Sdim if (TernaryMiddle.isInvalid()) { 262218893Sdim LHS = ExprError(); 263218893Sdim TernaryMiddle = 0; 264218893Sdim } 265193326Sed } else { 266193326Sed // Special case handling of "X ? Y : Z" where Y is empty: 267193326Sed // logical-OR-expression '?' ':' conditional-expression [GNU] 268193326Sed TernaryMiddle = 0; 269193326Sed Diag(Tok, diag::ext_gnu_conditional_expr); 270193326Sed } 271193326Sed 272207619Srdivacky if (Tok.is(tok::colon)) { 273207619Srdivacky // Eat the colon. 274207619Srdivacky ColonLoc = ConsumeToken(); 275207619Srdivacky } else { 276226633Sdim // Otherwise, we're missing a ':'. Assume that this was a typo that 277226633Sdim // the user forgot. If we're not in a macro expansion, we can suggest 278226633Sdim // a fixit hint. If there were two spaces before the current token, 279208600Srdivacky // suggest inserting the colon in between them, otherwise insert ": ". 280208600Srdivacky SourceLocation FILoc = Tok.getLocation(); 281208600Srdivacky const char *FIText = ": "; 282224145Sdim const SourceManager &SM = PP.getSourceManager(); 283234353Sdim if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) { 284234353Sdim assert(FILoc.isFileID()); 285208600Srdivacky bool IsInvalid = false; 286208600Srdivacky const char *SourcePtr = 287226633Sdim SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid); 288208600Srdivacky if (!IsInvalid && *SourcePtr == ' ') { 289208600Srdivacky SourcePtr = 290226633Sdim SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid); 291208600Srdivacky if (!IsInvalid && *SourcePtr == ' ') { 292226633Sdim FILoc = FILoc.getLocWithOffset(-1); 293208600Srdivacky FIText = ":"; 294208600Srdivacky } 295208600Srdivacky } 296208600Srdivacky } 297208600Srdivacky 298207619Srdivacky Diag(Tok, diag::err_expected_colon) 299208600Srdivacky << FixItHint::CreateInsertion(FILoc, FIText); 300193326Sed Diag(OpToken, diag::note_matching) << "?"; 301207619Srdivacky ColonLoc = Tok.getLocation(); 302193326Sed } 303193326Sed } 304198893Srdivacky 305210299Sed // Code completion for the right-hand side of an assignment expression 306210299Sed // goes through a special hook that takes the left-hand side into account. 307210299Sed if (Tok.is(tok::code_completion) && NextTokPrec == prec::Assignment) { 308210299Sed Actions.CodeCompleteAssignmentRHS(getCurScope(), LHS.get()); 309226633Sdim cutOffParsing(); 310210299Sed return ExprError(); 311210299Sed } 312210299Sed 313193326Sed // Parse another leaf here for the RHS of the operator. 314193326Sed // ParseCastExpression works here because all RHS expressions in C have it 315193326Sed // as a prefix, at least. However, in C++, an assignment-expression could 316193326Sed // be a throw-expression, which is not a valid cast-expression. 317193326Sed // Therefore we need some special-casing here. 318193326Sed // Also note that the third operand of the conditional operator is 319234353Sdim // an assignment-expression in C++, and in C++11, we can have a 320234353Sdim // braced-init-list on the RHS of an assignment. For better diagnostics, 321234353Sdim // parse as if we were allowed braced-init-lists everywhere, and check that 322234353Sdim // they only appear on the RHS of assignments later. 323212904Sdim ExprResult RHS; 324234353Sdim bool RHSIsInitList = false; 325249423Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 326234353Sdim RHS = ParseBraceInitializer(); 327234353Sdim RHSIsInitList = true; 328234353Sdim } else if (getLangOpts().CPlusPlus && NextTokPrec <= prec::Conditional) 329193326Sed RHS = ParseAssignmentExpression(); 330193326Sed else 331193326Sed RHS = ParseCastExpression(false); 332218893Sdim 333193326Sed if (RHS.isInvalid()) 334218893Sdim LHS = ExprError(); 335218893Sdim 336193326Sed // Remember the precedence of this operator and get the precedence of the 337193326Sed // operator immediately to the right of the RHS. 338207619Srdivacky prec::Level ThisPrec = NextTokPrec; 339193326Sed NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, 340249423Sdim getLangOpts().CPlusPlus11); 341193326Sed 342193326Sed // Assignment and conditional expressions are right-associative. 343193326Sed bool isRightAssoc = ThisPrec == prec::Conditional || 344193326Sed ThisPrec == prec::Assignment; 345193326Sed 346193326Sed // Get the precedence of the operator to the right of the RHS. If it binds 347193326Sed // more tightly with RHS than we do, evaluate it completely first. 348193326Sed if (ThisPrec < NextTokPrec || 349193326Sed (ThisPrec == NextTokPrec && isRightAssoc)) { 350234353Sdim if (!RHS.isInvalid() && RHSIsInitList) { 351234353Sdim Diag(Tok, diag::err_init_list_bin_op) 352234353Sdim << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get()); 353234353Sdim RHS = ExprError(); 354234353Sdim } 355193326Sed // If this is left-associative, only parse things on the RHS that bind 356193326Sed // more tightly than the current operator. If it is left-associative, it 357193326Sed // is okay, to bind exactly as tightly. For example, compile A=B=C=D as 358193326Sed // A=(B=(C=D)), where each paren is a level of recursion here. 359193326Sed // The function takes ownership of the RHS. 360218893Sdim RHS = ParseRHSOfBinaryExpression(RHS, 361207619Srdivacky static_cast<prec::Level>(ThisPrec + !isRightAssoc)); 362234353Sdim RHSIsInitList = false; 363218893Sdim 364193326Sed if (RHS.isInvalid()) 365218893Sdim LHS = ExprError(); 366193326Sed 367193326Sed NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, 368249423Sdim getLangOpts().CPlusPlus11); 369193326Sed } 370193326Sed assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); 371193326Sed 372234353Sdim if (!RHS.isInvalid() && RHSIsInitList) { 373234353Sdim if (ThisPrec == prec::Assignment) { 374234353Sdim Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists) 375234353Sdim << Actions.getExprRange(RHS.get()); 376234353Sdim } else { 377234353Sdim Diag(OpToken, diag::err_init_list_bin_op) 378234353Sdim << /*RHS*/1 << PP.getSpelling(OpToken) 379234353Sdim << Actions.getExprRange(RHS.get()); 380234353Sdim LHS = ExprError(); 381234353Sdim } 382234353Sdim } 383234353Sdim 384193326Sed if (!LHS.isInvalid()) { 385193326Sed // Combine the LHS and RHS into the LHS (e.g. build AST). 386193326Sed if (TernaryMiddle.isInvalid()) { 387193326Sed // If we're using '>>' as an operator within a template 388193326Sed // argument list (in C++98), suggest the addition of 389193326Sed // parentheses so that the code remains well-formed in C++0x. 390193326Sed if (!GreaterThanIsOperator && OpToken.is(tok::greatergreater)) 391193326Sed SuggestParentheses(OpToken.getLocation(), 392193326Sed diag::warn_cxx0x_right_shift_in_template_arg, 393193326Sed SourceRange(Actions.getExprRange(LHS.get()).getBegin(), 394193326Sed Actions.getExprRange(RHS.get()).getEnd())); 395193326Sed 396210299Sed LHS = Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(), 397212904Sdim OpToken.getKind(), LHS.take(), RHS.take()); 398193326Sed } else 399193326Sed LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, 400212904Sdim LHS.take(), TernaryMiddle.take(), 401212904Sdim RHS.take()); 402193326Sed } 403193326Sed } 404193326Sed} 405193326Sed 406239462Sdim/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true, 407239462Sdim/// parse a unary-expression. 408193326Sed/// 409239462Sdim/// \p isAddressOfOperand exists because an id-expression that is the 410239462Sdim/// operand of address-of gets special treatment due to member pointers. 411239462Sdim/// 412212904SdimExprResult Parser::ParseCastExpression(bool isUnaryExpression, 413221345Sdim bool isAddressOfOperand, 414234353Sdim TypeCastState isTypeCast) { 415193326Sed bool NotCastExpr; 416212904Sdim ExprResult Res = ParseCastExpression(isUnaryExpression, 417218893Sdim isAddressOfOperand, 418218893Sdim NotCastExpr, 419224145Sdim isTypeCast); 420193326Sed if (NotCastExpr) 421193326Sed Diag(Tok, diag::err_expected_expression); 422243830Sdim return Res; 423193326Sed} 424193326Sed 425234353Sdimnamespace { 426234353Sdimclass CastExpressionIdValidator : public CorrectionCandidateCallback { 427234353Sdim public: 428234353Sdim CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes) 429234353Sdim : AllowNonTypes(AllowNonTypes) { 430234353Sdim WantTypeSpecifiers = AllowTypes; 431234353Sdim } 432234353Sdim 433234353Sdim virtual bool ValidateCandidate(const TypoCorrection &candidate) { 434234353Sdim NamedDecl *ND = candidate.getCorrectionDecl(); 435234353Sdim if (!ND) 436234353Sdim return candidate.isKeyword(); 437234353Sdim 438234353Sdim if (isa<TypeDecl>(ND)) 439234353Sdim return WantTypeSpecifiers; 440234353Sdim return AllowNonTypes; 441234353Sdim } 442234353Sdim 443234353Sdim private: 444234353Sdim bool AllowNonTypes; 445234353Sdim}; 446234353Sdim} 447234353Sdim 448239462Sdim/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse 449239462Sdim/// a unary-expression. 450193326Sed/// 451239462Sdim/// \p isAddressOfOperand exists because an id-expression that is the operand 452239462Sdim/// of address-of gets special treatment due to member pointers. NotCastExpr 453239462Sdim/// is set to true if the token is not the start of a cast-expression, and no 454239462Sdim/// diagnostic is emitted in this case. 455239462Sdim/// 456239462Sdim/// \verbatim 457193326Sed/// cast-expression: [C99 6.5.4] 458193326Sed/// unary-expression 459193326Sed/// '(' type-name ')' cast-expression 460193326Sed/// 461193326Sed/// unary-expression: [C99 6.5.3] 462193326Sed/// postfix-expression 463193326Sed/// '++' unary-expression 464193326Sed/// '--' unary-expression 465193326Sed/// unary-operator cast-expression 466193326Sed/// 'sizeof' unary-expression 467193326Sed/// 'sizeof' '(' type-name ')' 468234353Sdim/// [C++11] 'sizeof' '...' '(' identifier ')' 469193326Sed/// [GNU] '__alignof' unary-expression 470193326Sed/// [GNU] '__alignof' '(' type-name ')' 471239462Sdim/// [C11] '_Alignof' '(' type-name ')' 472234353Sdim/// [C++11] 'alignof' '(' type-id ')' 473193326Sed/// [GNU] '&&' identifier 474234353Sdim/// [C++11] 'noexcept' '(' expression ')' [C++11 5.3.7] 475193326Sed/// [C++] new-expression 476193326Sed/// [C++] delete-expression 477193326Sed/// 478193326Sed/// unary-operator: one of 479193326Sed/// '&' '*' '+' '-' '~' '!' 480193326Sed/// [GNU] '__extension__' '__real' '__imag' 481193326Sed/// 482193326Sed/// primary-expression: [C99 6.5.1] 483193326Sed/// [C99] identifier 484193326Sed/// [C++] id-expression 485193326Sed/// constant 486193326Sed/// string-literal 487193326Sed/// [C++] boolean-literal [C++ 2.13.5] 488234353Sdim/// [C++11] 'nullptr' [C++11 2.14.7] 489234353Sdim/// [C++11] user-defined-literal 490193326Sed/// '(' expression ')' 491234353Sdim/// [C11] generic-selection 492193326Sed/// '__func__' [C99 6.4.2.2] 493193326Sed/// [GNU] '__FUNCTION__' 494193326Sed/// [GNU] '__PRETTY_FUNCTION__' 495193326Sed/// [GNU] '(' compound-statement ')' 496193326Sed/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 497193326Sed/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 498193326Sed/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 499193326Sed/// assign-expr ')' 500193326Sed/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 501193326Sed/// [GNU] '__null' 502198092Srdivacky/// [OBJC] '[' objc-message-expr ']' 503239462Sdim/// [OBJC] '\@selector' '(' objc-selector-arg ')' 504239462Sdim/// [OBJC] '\@protocol' '(' identifier ')' 505239462Sdim/// [OBJC] '\@encode' '(' type-name ')' 506193326Sed/// [OBJC] objc-string-literal 507193326Sed/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 508234353Sdim/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3] 509207619Srdivacky/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 510234353Sdim/// [C++11] typename-specifier braced-init-list [C++11 5.2.3] 511193326Sed/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 512193326Sed/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 513193326Sed/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 514193326Sed/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 515193326Sed/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] 516193326Sed/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] 517193326Sed/// [C++] 'this' [C++ 9.3.2] 518193326Sed/// [G++] unary-type-trait '(' type-id ')' 519193326Sed/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO] 520221345Sdim/// [EMBT] array-type-trait '(' type-id ',' integer ')' 521193326Sed/// [clang] '^' block-literal 522193326Sed/// 523193326Sed/// constant: [C99 6.4.4] 524193326Sed/// integer-constant 525193326Sed/// floating-constant 526193326Sed/// enumeration-constant -> identifier 527193326Sed/// character-constant 528193326Sed/// 529193326Sed/// id-expression: [C++ 5.1] 530193326Sed/// unqualified-id 531207619Srdivacky/// qualified-id 532193326Sed/// 533193326Sed/// unqualified-id: [C++ 5.1] 534193326Sed/// identifier 535193326Sed/// operator-function-id 536207619Srdivacky/// conversion-function-id 537207619Srdivacky/// '~' class-name 538207619Srdivacky/// template-id 539193326Sed/// 540193326Sed/// new-expression: [C++ 5.3.4] 541193326Sed/// '::'[opt] 'new' new-placement[opt] new-type-id 542193326Sed/// new-initializer[opt] 543193326Sed/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 544193326Sed/// new-initializer[opt] 545193326Sed/// 546193326Sed/// delete-expression: [C++ 5.3.5] 547193326Sed/// '::'[opt] 'delete' cast-expression 548193326Sed/// '::'[opt] 'delete' '[' ']' cast-expression 549193326Sed/// 550221345Sdim/// [GNU/Embarcadero] unary-type-trait: 551221345Sdim/// '__is_arithmetic' 552221345Sdim/// '__is_floating_point' 553221345Sdim/// '__is_integral' 554221345Sdim/// '__is_lvalue_expr' 555221345Sdim/// '__is_rvalue_expr' 556221345Sdim/// '__is_complete_type' 557221345Sdim/// '__is_void' 558221345Sdim/// '__is_array' 559221345Sdim/// '__is_function' 560221345Sdim/// '__is_reference' 561221345Sdim/// '__is_lvalue_reference' 562221345Sdim/// '__is_rvalue_reference' 563221345Sdim/// '__is_fundamental' 564221345Sdim/// '__is_object' 565221345Sdim/// '__is_scalar' 566221345Sdim/// '__is_compound' 567221345Sdim/// '__is_pointer' 568221345Sdim/// '__is_member_object_pointer' 569221345Sdim/// '__is_member_function_pointer' 570221345Sdim/// '__is_member_pointer' 571221345Sdim/// '__is_const' 572221345Sdim/// '__is_volatile' 573221345Sdim/// '__is_trivial' 574221345Sdim/// '__is_standard_layout' 575221345Sdim/// '__is_signed' 576221345Sdim/// '__is_unsigned' 577221345Sdim/// 578193326Sed/// [GNU] unary-type-trait: 579212904Sdim/// '__has_nothrow_assign' 580212904Sdim/// '__has_nothrow_copy' 581212904Sdim/// '__has_nothrow_constructor' 582193326Sed/// '__has_trivial_assign' [TODO] 583193326Sed/// '__has_trivial_copy' [TODO] 584193326Sed/// '__has_trivial_constructor' 585193326Sed/// '__has_trivial_destructor' 586212904Sdim/// '__has_virtual_destructor' 587193326Sed/// '__is_abstract' [TODO] 588193326Sed/// '__is_class' 589193326Sed/// '__is_empty' [TODO] 590193326Sed/// '__is_enum' 591234353Sdim/// '__is_final' 592193326Sed/// '__is_pod' 593193326Sed/// '__is_polymorphic' 594221345Sdim/// '__is_trivial' 595193326Sed/// '__is_union' 596193326Sed/// 597223017Sdim/// [Clang] unary-type-trait: 598223017Sdim/// '__trivially_copyable' 599223017Sdim/// 600218893Sdim/// binary-type-trait: 601218893Sdim/// [GNU] '__is_base_of' 602218893Sdim/// [MS] '__is_convertible_to' 603221345Sdim/// '__is_convertible' 604221345Sdim/// '__is_same' 605193326Sed/// 606221345Sdim/// [Embarcadero] array-type-trait: 607221345Sdim/// '__array_rank' 608221345Sdim/// '__array_extent' 609221345Sdim/// 610221345Sdim/// [Embarcadero] expression-trait: 611221345Sdim/// '__is_lvalue_expr' 612221345Sdim/// '__is_rvalue_expr' 613239462Sdim/// \endverbatim 614221345Sdim/// 615212904SdimExprResult Parser::ParseCastExpression(bool isUnaryExpression, 616218893Sdim bool isAddressOfOperand, 617218893Sdim bool &NotCastExpr, 618234353Sdim TypeCastState isTypeCast) { 619212904Sdim ExprResult Res; 620193326Sed tok::TokenKind SavedKind = Tok.getKind(); 621193326Sed NotCastExpr = false; 622198092Srdivacky 623193326Sed // This handles all of cast-expression, unary-expression, postfix-expression, 624193326Sed // and primary-expression. We handle them together like this for efficiency 625193326Sed // and to simplify handling of an expression starting with a '(' token: which 626193326Sed // may be one of a parenthesized expression, cast-expression, compound literal 627193326Sed // expression, or statement expression. 628193326Sed // 629193326Sed // If the parsed tokens consist of a primary-expression, the cases below 630212904Sdim // break out of the switch; at the end we call ParsePostfixExpressionSuffix 631212904Sdim // to handle the postfix expression suffixes. Cases that cannot be followed 632212904Sdim // by postfix exprs should return without invoking 633212904Sdim // ParsePostfixExpressionSuffix. 634193326Sed switch (SavedKind) { 635193326Sed case tok::l_paren: { 636193326Sed // If this expression is limited to being a unary-expression, the parent can 637193326Sed // not start a cast expression. 638193326Sed ParenParseOption ParenExprType = 639234353Sdim (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr; 640212904Sdim ParsedType CastTy; 641193326Sed SourceLocation RParenLoc; 642200583Srdivacky 643200583Srdivacky { 644218893Sdim // The inside of the parens don't need to be a colon protected scope, and 645218893Sdim // isn't immediately a message send. 646200583Srdivacky ColonProtectionRAIIObject X(*this, false); 647218893Sdim 648200583Srdivacky Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, 649234353Sdim isTypeCast == IsTypeCast, CastTy, RParenLoc); 650200583Srdivacky } 651198092Srdivacky 652193326Sed switch (ParenExprType) { 653193326Sed case SimpleExpr: break; // Nothing else to do. 654193326Sed case CompoundStmt: break; // Nothing else to do. 655193326Sed case CompoundLiteral: 656193326Sed // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of 657193326Sed // postfix-expression exist, parse them now. 658193326Sed break; 659193326Sed case CastExpr: 660193326Sed // We have parsed the cast-expression and no postfix-expr pieces are 661193326Sed // following. 662243830Sdim return Res; 663193326Sed } 664193326Sed 665212904Sdim break; 666193326Sed } 667193326Sed 668193326Sed // primary-expression 669193326Sed case tok::numeric_constant: 670193326Sed // constant: integer-constant 671193326Sed // constant: floating-constant 672193326Sed 673234353Sdim Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope()); 674193326Sed ConsumeToken(); 675212904Sdim break; 676193326Sed 677193326Sed case tok::kw_true: 678193326Sed case tok::kw_false: 679193326Sed return ParseCXXBoolLiteral(); 680234353Sdim 681234353Sdim case tok::kw___objc_yes: 682234353Sdim case tok::kw___objc_no: 683234353Sdim return ParseObjCBoolLiteral(); 684193326Sed 685193326Sed case tok::kw_nullptr: 686234353Sdim Diag(Tok, diag::warn_cxx98_compat_nullptr); 687193326Sed return Actions.ActOnCXXNullPtrLiteral(ConsumeToken()); 688193326Sed 689221345Sdim case tok::annot_primary_expr: 690221345Sdim assert(Res.get() == 0 && "Stray primary-expression annotation?"); 691221345Sdim Res = getExprAnnotation(Tok); 692221345Sdim ConsumeToken(); 693221345Sdim break; 694251662Sdim 695251662Sdim case tok::kw_decltype: 696251662Sdim // Annotate the token and tail recurse. 697251662Sdim if (TryAnnotateTypeOrScopeToken()) 698251662Sdim return ExprError(); 699251662Sdim assert(Tok.isNot(tok::kw_decltype)); 700251662Sdim return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 701221345Sdim 702193326Sed case tok::identifier: { // primary-expression: identifier 703193326Sed // unqualified-id: identifier 704193326Sed // constant: enumeration-constant 705193326Sed // Turn a potentially qualified name into a annot_typename or 706193326Sed // annot_cxxscope if it would be valid. This handles things like x::y, etc. 707234353Sdim if (getLangOpts().CPlusPlus) { 708202379Srdivacky // Avoid the unnecessary parse-time lookup in the common case 709202379Srdivacky // where the syntax forbids a type. 710202379Srdivacky const Token &Next = NextToken(); 711243830Sdim 712243830Sdim // If this identifier was reverted from a token ID, and the next token 713243830Sdim // is a parenthesis, this is likely to be a use of a type trait. Check 714243830Sdim // those tokens. 715243830Sdim if (Next.is(tok::l_paren) && 716243830Sdim Tok.is(tok::identifier) && 717243830Sdim Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) { 718243830Sdim IdentifierInfo *II = Tok.getIdentifierInfo(); 719243830Sdim // Build up the mapping of revertable type traits, for future use. 720243830Sdim if (RevertableTypeTraits.empty()) { 721243830Sdim#define RTT_JOIN(X,Y) X##Y 722243830Sdim#define REVERTABLE_TYPE_TRAIT(Name) \ 723243830Sdim RevertableTypeTraits[PP.getIdentifierInfo(#Name)] \ 724243830Sdim = RTT_JOIN(tok::kw_,Name) 725243830Sdim 726243830Sdim REVERTABLE_TYPE_TRAIT(__is_arithmetic); 727243830Sdim REVERTABLE_TYPE_TRAIT(__is_convertible); 728243830Sdim REVERTABLE_TYPE_TRAIT(__is_empty); 729243830Sdim REVERTABLE_TYPE_TRAIT(__is_floating_point); 730243830Sdim REVERTABLE_TYPE_TRAIT(__is_function); 731243830Sdim REVERTABLE_TYPE_TRAIT(__is_fundamental); 732243830Sdim REVERTABLE_TYPE_TRAIT(__is_integral); 733243830Sdim REVERTABLE_TYPE_TRAIT(__is_member_function_pointer); 734243830Sdim REVERTABLE_TYPE_TRAIT(__is_member_pointer); 735243830Sdim REVERTABLE_TYPE_TRAIT(__is_pod); 736243830Sdim REVERTABLE_TYPE_TRAIT(__is_pointer); 737243830Sdim REVERTABLE_TYPE_TRAIT(__is_same); 738243830Sdim REVERTABLE_TYPE_TRAIT(__is_scalar); 739243830Sdim REVERTABLE_TYPE_TRAIT(__is_signed); 740243830Sdim REVERTABLE_TYPE_TRAIT(__is_unsigned); 741243830Sdim REVERTABLE_TYPE_TRAIT(__is_void); 742243830Sdim#undef REVERTABLE_TYPE_TRAIT 743243830Sdim#undef RTT_JOIN 744243830Sdim } 745243830Sdim 746243830Sdim // If we find that this is in fact the name of a type trait, 747243830Sdim // update the token kind in place and parse again to treat it as 748243830Sdim // the appropriate kind of type trait. 749243830Sdim llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known 750243830Sdim = RevertableTypeTraits.find(II); 751243830Sdim if (Known != RevertableTypeTraits.end()) { 752243830Sdim Tok.setKind(Known->second); 753243830Sdim return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 754243830Sdim NotCastExpr, isTypeCast); 755243830Sdim } 756243830Sdim } 757243830Sdim 758202379Srdivacky if (Next.is(tok::coloncolon) || 759202379Srdivacky (!ColonIsSacred && Next.is(tok::colon)) || 760202379Srdivacky Next.is(tok::less) || 761234353Sdim Next.is(tok::l_paren) || 762234353Sdim Next.is(tok::l_brace)) { 763202379Srdivacky // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 764202379Srdivacky if (TryAnnotateTypeOrScopeToken()) 765204643Srdivacky return ExprError(); 766204643Srdivacky if (!Tok.is(tok::identifier)) 767202379Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 768202379Srdivacky } 769193326Sed } 770193326Sed 771198893Srdivacky // Consume the identifier so that we can see if it is followed by a '(' or 772198893Srdivacky // '.'. 773198893Srdivacky IdentifierInfo &II = *Tok.getIdentifierInfo(); 774198893Srdivacky SourceLocation ILoc = ConsumeToken(); 775243830Sdim 776207619Srdivacky // Support 'Class.property' and 'super.property' notation. 777234353Sdim if (getLangOpts().ObjC1 && Tok.is(tok::period) && 778210299Sed (Actions.getTypeName(II, ILoc, getCurScope()) || 779207619Srdivacky // Allow the base to be 'super' if in an objc-method. 780210299Sed (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) { 781218893Sdim ConsumeToken(); 782198893Srdivacky 783234353Sdim // Allow either an identifier or the keyword 'class' (in C++). 784234353Sdim if (Tok.isNot(tok::identifier) && 785234353Sdim !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) { 786198893Srdivacky Diag(Tok, diag::err_expected_property_name); 787193326Sed return ExprError(); 788193326Sed } 789193326Sed IdentifierInfo &PropertyName = *Tok.getIdentifierInfo(); 790193326Sed SourceLocation PropertyLoc = ConsumeToken(); 791198893Srdivacky 792198893Srdivacky Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName, 793198893Srdivacky ILoc, PropertyLoc); 794212904Sdim break; 795193326Sed } 796212904Sdim 797218893Sdim // In an Objective-C method, if we have "super" followed by an identifier, 798218893Sdim // the token sequence is ill-formed. However, if there's a ':' or ']' after 799218893Sdim // that identifier, this is probably a message send with a missing open 800218893Sdim // bracket. Treat it as such. 801234353Sdim if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression && 802218893Sdim getCurScope()->isInObjcMethodScope() && 803218893Sdim ((Tok.is(tok::identifier) && 804218893Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) || 805218893Sdim Tok.is(tok::code_completion))) { 806218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(), 807218893Sdim 0); 808218893Sdim break; 809218893Sdim } 810218893Sdim 811218893Sdim // If we have an Objective-C class name followed by an identifier 812218893Sdim // and either ':' or ']', this is an Objective-C class message 813218893Sdim // send that's missing the opening '['. Recovery 814218893Sdim // appropriately. Also take this path if we're performing code 815218893Sdim // completion after an Objective-C class name. 816234353Sdim if (getLangOpts().ObjC1 && 817218893Sdim ((Tok.is(tok::identifier) && !InMessageExpression) || 818218893Sdim Tok.is(tok::code_completion))) { 819218893Sdim const Token& Next = NextToken(); 820218893Sdim if (Tok.is(tok::code_completion) || 821218893Sdim Next.is(tok::colon) || Next.is(tok::r_square)) 822218893Sdim if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope())) 823218893Sdim if (Typ.get()->isObjCObjectOrInterfaceType()) { 824218893Sdim // Fake up a Declarator to use with ActOnTypeName. 825221345Sdim DeclSpec DS(AttrFactory); 826218893Sdim DS.SetRangeStart(ILoc); 827218893Sdim DS.SetRangeEnd(ILoc); 828218893Sdim const char *PrevSpec = 0; 829218893Sdim unsigned DiagID; 830218893Sdim DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ); 831218893Sdim 832218893Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 833218893Sdim TypeResult Ty = Actions.ActOnTypeName(getCurScope(), 834218893Sdim DeclaratorInfo); 835218893Sdim if (Ty.isInvalid()) 836218893Sdim break; 837218893Sdim 838218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), 839218893Sdim SourceLocation(), 840218893Sdim Ty.get(), 0); 841218893Sdim break; 842218893Sdim } 843218893Sdim } 844218893Sdim 845212904Sdim // Make sure to pass down the right value for isAddressOfOperand. 846212904Sdim if (isAddressOfOperand && isPostfixExpressionSuffixStart()) 847212904Sdim isAddressOfOperand = false; 848198893Srdivacky 849193326Sed // Function designators are allowed to be undeclared (C99 6.5.1p2), so we 850193326Sed // need to know whether or not this identifier is a function designator or 851193326Sed // not. 852198893Srdivacky UnqualifiedId Name; 853198893Srdivacky CXXScopeSpec ScopeSpec; 854234353Sdim SourceLocation TemplateKWLoc; 855234353Sdim CastExpressionIdValidator Validator(isTypeCast != NotTypeCast, 856234353Sdim isTypeCast != IsTypeCast); 857198893Srdivacky Name.setIdentifier(&II, ILoc); 858234353Sdim Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc, 859234353Sdim Name, Tok.is(tok::l_paren), 860234353Sdim isAddressOfOperand, &Validator); 861212904Sdim break; 862193326Sed } 863193326Sed case tok::char_constant: // constant: character-constant 864226633Sdim case tok::wide_char_constant: 865226633Sdim case tok::utf16_char_constant: 866226633Sdim case tok::utf32_char_constant: 867234353Sdim Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope()); 868193326Sed ConsumeToken(); 869212904Sdim break; 870193326Sed case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] 871193326Sed case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] 872239462Sdim case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS] 873193326Sed case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] 874193326Sed Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); 875193326Sed ConsumeToken(); 876212904Sdim break; 877193326Sed case tok::string_literal: // primary-expression: string-literal 878193326Sed case tok::wide_string_literal: 879226633Sdim case tok::utf8_string_literal: 880226633Sdim case tok::utf16_string_literal: 881226633Sdim case tok::utf32_string_literal: 882234353Sdim Res = ParseStringLiteralExpression(true); 883212904Sdim break; 884234353Sdim case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1] 885221345Sdim Res = ParseGenericSelectionExpression(); 886221345Sdim break; 887193326Sed case tok::kw___builtin_va_arg: 888193326Sed case tok::kw___builtin_offsetof: 889193326Sed case tok::kw___builtin_choose_expr: 890223017Sdim case tok::kw___builtin_astype: // primary-expression: [OCL] as_type() 891193326Sed return ParseBuiltinPrimaryExpression(); 892193326Sed case tok::kw___null: 893193326Sed return Actions.ActOnGNUNullExpr(ConsumeToken()); 894224145Sdim 895212904Sdim case tok::plusplus: // unary-expression: '++' unary-expression [C99] 896212904Sdim case tok::minusminus: { // unary-expression: '--' unary-expression [C99] 897212904Sdim // C++ [expr.unary] has: 898212904Sdim // unary-expression: 899212904Sdim // ++ cast-expression 900212904Sdim // -- cast-expression 901193326Sed SourceLocation SavedLoc = ConsumeToken(); 902234353Sdim Res = ParseCastExpression(!getLangOpts().CPlusPlus); 903193326Sed if (!Res.isInvalid()) 904212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 905243830Sdim return Res; 906193326Sed } 907193326Sed case tok::amp: { // unary-expression: '&' cast-expression 908193326Sed // Special treatment because of member pointers 909193326Sed SourceLocation SavedLoc = ConsumeToken(); 910193326Sed Res = ParseCastExpression(false, true); 911193326Sed if (!Res.isInvalid()) 912212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 913243830Sdim return Res; 914193326Sed } 915193326Sed 916193326Sed case tok::star: // unary-expression: '*' cast-expression 917193326Sed case tok::plus: // unary-expression: '+' cast-expression 918193326Sed case tok::minus: // unary-expression: '-' cast-expression 919193326Sed case tok::tilde: // unary-expression: '~' cast-expression 920193326Sed case tok::exclaim: // unary-expression: '!' cast-expression 921193326Sed case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] 922193326Sed case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] 923193326Sed SourceLocation SavedLoc = ConsumeToken(); 924193326Sed Res = ParseCastExpression(false); 925193326Sed if (!Res.isInvalid()) 926212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 927243830Sdim return Res; 928193326Sed } 929193326Sed 930193326Sed case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] 931193326Sed // __extension__ silences extension warnings in the subexpression. 932193326Sed ExtensionRAIIObject O(Diags); // Use RAII to do this. 933193326Sed SourceLocation SavedLoc = ConsumeToken(); 934193326Sed Res = ParseCastExpression(false); 935193326Sed if (!Res.isInvalid()) 936212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 937243830Sdim return Res; 938193326Sed } 939239462Sdim case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')' 940239462Sdim if (!getLangOpts().C11) 941239462Sdim Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); 942239462Sdim // fallthrough 943239462Sdim case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')' 944239462Sdim case tok::kw___alignof: // unary-expression: '__alignof' unary-expression 945239462Sdim // unary-expression: '__alignof' '(' type-name ')' 946193326Sed case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression 947193326Sed // unary-expression: 'sizeof' '(' type-name ')' 948221345Sdim case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression 949221345Sdim return ParseUnaryExprOrTypeTraitExpression(); 950193326Sed case tok::ampamp: { // unary-expression: '&&' identifier 951193326Sed SourceLocation AmpAmpLoc = ConsumeToken(); 952193326Sed if (Tok.isNot(tok::identifier)) 953193326Sed return ExprError(Diag(Tok, diag::err_expected_ident)); 954193326Sed 955218893Sdim if (getCurScope()->getFnParent() == 0) 956218893Sdim return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn)); 957218893Sdim 958193326Sed Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); 959218893Sdim LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), 960218893Sdim Tok.getLocation()); 961218893Sdim Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD); 962193326Sed ConsumeToken(); 963243830Sdim return Res; 964193326Sed } 965193326Sed case tok::kw_const_cast: 966193326Sed case tok::kw_dynamic_cast: 967193326Sed case tok::kw_reinterpret_cast: 968193326Sed case tok::kw_static_cast: 969193326Sed Res = ParseCXXCasts(); 970212904Sdim break; 971193326Sed case tok::kw_typeid: 972193326Sed Res = ParseCXXTypeid(); 973212904Sdim break; 974218893Sdim case tok::kw___uuidof: 975218893Sdim Res = ParseCXXUuidof(); 976218893Sdim break; 977193326Sed case tok::kw_this: 978193326Sed Res = ParseCXXThis(); 979212904Sdim break; 980193326Sed 981218893Sdim case tok::annot_typename: 982218893Sdim if (isStartOfObjCClassMessageMissingOpenBracket()) { 983218893Sdim ParsedType Type = getTypeAnnotation(Tok); 984249423Sdim 985218893Sdim // Fake up a Declarator to use with ActOnTypeName. 986221345Sdim DeclSpec DS(AttrFactory); 987218893Sdim DS.SetRangeStart(Tok.getLocation()); 988218893Sdim DS.SetRangeEnd(Tok.getLastLoc()); 989218893Sdim 990218893Sdim const char *PrevSpec = 0; 991218893Sdim unsigned DiagID; 992218893Sdim DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(), 993218893Sdim PrevSpec, DiagID, Type); 994249423Sdim 995218893Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 996218893Sdim TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 997218893Sdim if (Ty.isInvalid()) 998218893Sdim break; 999218893Sdim 1000218893Sdim ConsumeToken(); 1001218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), 1002218893Sdim Ty.get(), 0); 1003218893Sdim break; 1004218893Sdim } 1005218893Sdim // Fall through 1006249423Sdim 1007234353Sdim case tok::annot_decltype: 1008193326Sed case tok::kw_char: 1009193326Sed case tok::kw_wchar_t: 1010198092Srdivacky case tok::kw_char16_t: 1011198092Srdivacky case tok::kw_char32_t: 1012193326Sed case tok::kw_bool: 1013193326Sed case tok::kw_short: 1014193326Sed case tok::kw_int: 1015193326Sed case tok::kw_long: 1016221345Sdim case tok::kw___int64: 1017234353Sdim case tok::kw___int128: 1018193326Sed case tok::kw_signed: 1019193326Sed case tok::kw_unsigned: 1020226633Sdim case tok::kw_half: 1021193326Sed case tok::kw_float: 1022193326Sed case tok::kw_double: 1023193326Sed case tok::kw_void: 1024193326Sed case tok::kw_typename: 1025193326Sed case tok::kw_typeof: 1026249423Sdim case tok::kw___vector: 1027249423Sdim case tok::kw_image1d_t: 1028249423Sdim case tok::kw_image1d_array_t: 1029249423Sdim case tok::kw_image1d_buffer_t: 1030249423Sdim case tok::kw_image2d_t: 1031249423Sdim case tok::kw_image2d_array_t: 1032249423Sdim case tok::kw_image3d_t: 1033249423Sdim case tok::kw_sampler_t: 1034249423Sdim case tok::kw_event_t: { 1035234353Sdim if (!getLangOpts().CPlusPlus) { 1036193326Sed Diag(Tok, diag::err_expected_expression); 1037193326Sed return ExprError(); 1038193326Sed } 1039194179Sed 1040194179Sed if (SavedKind == tok::kw_typename) { 1041194179Sed // postfix-expression: typename-specifier '(' expression-list[opt] ')' 1042223017Sdim // typename-specifier braced-init-list 1043204643Srdivacky if (TryAnnotateTypeOrScopeToken()) 1044194179Sed return ExprError(); 1045194179Sed } 1046194179Sed 1047193326Sed // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' 1048223017Sdim // simple-type-specifier braced-init-list 1049193326Sed // 1050221345Sdim DeclSpec DS(AttrFactory); 1051193326Sed ParseCXXSimpleTypeSpecifier(DS); 1052223017Sdim if (Tok.isNot(tok::l_paren) && 1053249423Sdim (!getLangOpts().CPlusPlus11 || Tok.isNot(tok::l_brace))) 1054193326Sed return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) 1055193326Sed << DS.getSourceRange()); 1056193326Sed 1057234353Sdim if (Tok.is(tok::l_brace)) 1058234353Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 1059234353Sdim 1060193326Sed Res = ParseCXXTypeConstructExpression(DS); 1061212904Sdim break; 1062193326Sed } 1063193326Sed 1064203955Srdivacky case tok::annot_cxxscope: { // [C++] id-expression: qualified-id 1065207619Srdivacky // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 1066207619Srdivacky // (We can end up in this situation after tentative parsing.) 1067207619Srdivacky if (TryAnnotateTypeOrScopeToken()) 1068207619Srdivacky return ExprError(); 1069207619Srdivacky if (!Tok.is(tok::annot_cxxscope)) 1070207619Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1071224145Sdim NotCastExpr, isTypeCast); 1072207619Srdivacky 1073203955Srdivacky Token Next = NextToken(); 1074203955Srdivacky if (Next.is(tok::annot_template_id)) { 1075224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); 1076203955Srdivacky if (TemplateId->Kind == TNK_Type_template) { 1077203955Srdivacky // We have a qualified template-id that we know refers to a 1078203955Srdivacky // type, translate it into a type and continue parsing as a 1079203955Srdivacky // cast expression. 1080203955Srdivacky CXXScopeSpec SS; 1081234353Sdim ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 1082234353Sdim /*EnteringContext=*/false); 1083221345Sdim AnnotateTemplateIdTokenAsType(); 1084203955Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1085224145Sdim NotCastExpr, isTypeCast); 1086203955Srdivacky } 1087203955Srdivacky } 1088203955Srdivacky 1089203955Srdivacky // Parse as an id-expression. 1090203955Srdivacky Res = ParseCXXIdExpression(isAddressOfOperand); 1091212904Sdim break; 1092203955Srdivacky } 1093203955Srdivacky 1094203955Srdivacky case tok::annot_template_id: { // [C++] template-id 1095224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1096203955Srdivacky if (TemplateId->Kind == TNK_Type_template) { 1097203955Srdivacky // We have a template-id that we know refers to a type, 1098203955Srdivacky // translate it into a type and continue parsing as a cast 1099203955Srdivacky // expression. 1100203955Srdivacky AnnotateTemplateIdTokenAsType(); 1101203955Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1102224145Sdim NotCastExpr, isTypeCast); 1103203955Srdivacky } 1104203955Srdivacky 1105203955Srdivacky // Fall through to treat the template-id as an id-expression. 1106203955Srdivacky } 1107203955Srdivacky 1108193326Sed case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id 1109193326Sed Res = ParseCXXIdExpression(isAddressOfOperand); 1110212904Sdim break; 1111193326Sed 1112193326Sed case tok::coloncolon: { 1113193326Sed // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken 1114193326Sed // annotates the token, tail recurse. 1115193326Sed if (TryAnnotateTypeOrScopeToken()) 1116204643Srdivacky return ExprError(); 1117204643Srdivacky if (!Tok.is(tok::coloncolon)) 1118193326Sed return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 1119193326Sed 1120193326Sed // ::new -> [C++] new-expression 1121193326Sed // ::delete -> [C++] delete-expression 1122193326Sed SourceLocation CCLoc = ConsumeToken(); 1123193326Sed if (Tok.is(tok::kw_new)) 1124193326Sed return ParseCXXNewExpression(true, CCLoc); 1125193326Sed if (Tok.is(tok::kw_delete)) 1126193326Sed return ParseCXXDeleteExpression(true, CCLoc); 1127198092Srdivacky 1128193326Sed // This is not a type name or scope specifier, it is an invalid expression. 1129193326Sed Diag(CCLoc, diag::err_expected_expression); 1130193326Sed return ExprError(); 1131193326Sed } 1132193326Sed 1133193326Sed case tok::kw_new: // [C++] new-expression 1134193326Sed return ParseCXXNewExpression(false, Tok.getLocation()); 1135193326Sed 1136193326Sed case tok::kw_delete: // [C++] delete-expression 1137193326Sed return ParseCXXDeleteExpression(false, Tok.getLocation()); 1138193326Sed 1139218893Sdim case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')' 1140234353Sdim Diag(Tok, diag::warn_cxx98_compat_noexcept_expr); 1141218893Sdim SourceLocation KeyLoc = ConsumeToken(); 1142226633Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 1143226633Sdim 1144226633Sdim if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept")) 1145218893Sdim return ExprError(); 1146234353Sdim // C++11 [expr.unary.noexcept]p1: 1147218893Sdim // The noexcept operator determines whether the evaluation of its operand, 1148218893Sdim // which is an unevaluated operand, can throw an exception. 1149218893Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 1150218893Sdim ExprResult Result = ParseExpression(); 1151226633Sdim 1152226633Sdim T.consumeClose(); 1153226633Sdim 1154218893Sdim if (!Result.isInvalid()) 1155226633Sdim Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(), 1156226633Sdim Result.take(), T.getCloseLocation()); 1157243830Sdim return Result; 1158218893Sdim } 1159218893Sdim 1160221345Sdim case tok::kw___is_abstract: // [GNU] unary-type-trait 1161193326Sed case tok::kw___is_class: 1162221345Sdim case tok::kw___is_empty: 1163193326Sed case tok::kw___is_enum: 1164243830Sdim case tok::kw___is_interface_class: 1165221345Sdim case tok::kw___is_literal: 1166221345Sdim case tok::kw___is_arithmetic: 1167221345Sdim case tok::kw___is_integral: 1168221345Sdim case tok::kw___is_floating_point: 1169221345Sdim case tok::kw___is_complete_type: 1170221345Sdim case tok::kw___is_void: 1171221345Sdim case tok::kw___is_array: 1172221345Sdim case tok::kw___is_function: 1173221345Sdim case tok::kw___is_reference: 1174221345Sdim case tok::kw___is_lvalue_reference: 1175221345Sdim case tok::kw___is_rvalue_reference: 1176221345Sdim case tok::kw___is_fundamental: 1177221345Sdim case tok::kw___is_object: 1178221345Sdim case tok::kw___is_scalar: 1179221345Sdim case tok::kw___is_compound: 1180221345Sdim case tok::kw___is_pointer: 1181221345Sdim case tok::kw___is_member_object_pointer: 1182221345Sdim case tok::kw___is_member_function_pointer: 1183221345Sdim case tok::kw___is_member_pointer: 1184221345Sdim case tok::kw___is_const: 1185221345Sdim case tok::kw___is_volatile: 1186221345Sdim case tok::kw___is_standard_layout: 1187221345Sdim case tok::kw___is_signed: 1188221345Sdim case tok::kw___is_unsigned: 1189221345Sdim case tok::kw___is_literal_type: 1190221345Sdim case tok::kw___is_pod: 1191221345Sdim case tok::kw___is_polymorphic: 1192221345Sdim case tok::kw___is_trivial: 1193223017Sdim case tok::kw___is_trivially_copyable: 1194193326Sed case tok::kw___is_union: 1195234353Sdim case tok::kw___is_final: 1196193326Sed case tok::kw___has_trivial_constructor: 1197249423Sdim case tok::kw___has_trivial_move_constructor: 1198198092Srdivacky case tok::kw___has_trivial_copy: 1199198092Srdivacky case tok::kw___has_trivial_assign: 1200249423Sdim case tok::kw___has_trivial_move_assign: 1201193326Sed case tok::kw___has_trivial_destructor: 1202212904Sdim case tok::kw___has_nothrow_assign: 1203249423Sdim case tok::kw___has_nothrow_move_assign: 1204212904Sdim case tok::kw___has_nothrow_copy: 1205212904Sdim case tok::kw___has_nothrow_constructor: 1206212904Sdim case tok::kw___has_virtual_destructor: 1207193326Sed return ParseUnaryTypeTrait(); 1208193326Sed 1209218893Sdim case tok::kw___builtin_types_compatible_p: 1210218893Sdim case tok::kw___is_base_of: 1211221345Sdim case tok::kw___is_same: 1212221345Sdim case tok::kw___is_convertible: 1213218893Sdim case tok::kw___is_convertible_to: 1214234353Sdim case tok::kw___is_trivially_assignable: 1215218893Sdim return ParseBinaryTypeTrait(); 1216218893Sdim 1217234353Sdim case tok::kw___is_trivially_constructible: 1218234353Sdim return ParseTypeTrait(); 1219234353Sdim 1220221345Sdim case tok::kw___array_rank: 1221221345Sdim case tok::kw___array_extent: 1222221345Sdim return ParseArrayTypeTrait(); 1223221345Sdim 1224221345Sdim case tok::kw___is_lvalue_expr: 1225221345Sdim case tok::kw___is_rvalue_expr: 1226221345Sdim return ParseExpressionTrait(); 1227221345Sdim 1228193326Sed case tok::at: { 1229193326Sed SourceLocation AtLoc = ConsumeToken(); 1230193326Sed return ParseObjCAtExpression(AtLoc); 1231193326Sed } 1232193326Sed case tok::caret: 1233224145Sdim Res = ParseBlockLiteralExpression(); 1234224145Sdim break; 1235224145Sdim case tok::code_completion: { 1236212904Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 1237226633Sdim cutOffParsing(); 1238226633Sdim return ExprError(); 1239224145Sdim } 1240193326Sed case tok::l_square: 1241249423Sdim if (getLangOpts().CPlusPlus11) { 1242234353Sdim if (getLangOpts().ObjC1) { 1243234353Sdim // C++11 lambda expressions and Objective-C message sends both start with a 1244234353Sdim // square bracket. There are three possibilities here: 1245234353Sdim // we have a valid lambda expression, we have an invalid lambda 1246234353Sdim // expression, or we have something that doesn't appear to be a lambda. 1247234353Sdim // If we're in the last case, we fall back to ParseObjCMessageExpression. 1248226633Sdim Res = TryParseLambdaExpression(); 1249234353Sdim if (!Res.isInvalid() && !Res.get()) 1250226633Sdim Res = ParseObjCMessageExpression(); 1251226633Sdim break; 1252226633Sdim } 1253226633Sdim Res = ParseLambdaExpression(); 1254226633Sdim break; 1255226633Sdim } 1256234353Sdim if (getLangOpts().ObjC1) { 1257224145Sdim Res = ParseObjCMessageExpression(); 1258224145Sdim break; 1259224145Sdim } 1260224145Sdim // FALL THROUGH. 1261193326Sed default: 1262193326Sed NotCastExpr = true; 1263193326Sed return ExprError(); 1264193326Sed } 1265193326Sed 1266212904Sdim // These can be followed by postfix-expr pieces. 1267218893Sdim return ParsePostfixExpressionSuffix(Res); 1268193326Sed} 1269193326Sed 1270239462Sdim/// \brief Once the leading part of a postfix-expression is parsed, this 1271239462Sdim/// method parses any suffixes that apply. 1272193326Sed/// 1273239462Sdim/// \verbatim 1274193326Sed/// postfix-expression: [C99 6.5.2] 1275193326Sed/// primary-expression 1276193326Sed/// postfix-expression '[' expression ']' 1277223017Sdim/// postfix-expression '[' braced-init-list ']' 1278193326Sed/// postfix-expression '(' argument-expression-list[opt] ')' 1279193326Sed/// postfix-expression '.' identifier 1280193326Sed/// postfix-expression '->' identifier 1281193326Sed/// postfix-expression '++' 1282193326Sed/// postfix-expression '--' 1283193326Sed/// '(' type-name ')' '{' initializer-list '}' 1284193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 1285193326Sed/// 1286193326Sed/// argument-expression-list: [C99 6.5.2] 1287218893Sdim/// argument-expression ...[opt] 1288218893Sdim/// argument-expression-list ',' assignment-expression ...[opt] 1289239462Sdim/// \endverbatim 1290212904SdimExprResult 1291212904SdimParser::ParsePostfixExpressionSuffix(ExprResult LHS) { 1292193326Sed // Now that the primary-expression piece of the postfix-expression has been 1293193326Sed // parsed, see if there are any postfix-expression pieces here. 1294193326Sed SourceLocation Loc; 1295193326Sed while (1) { 1296193326Sed switch (Tok.getKind()) { 1297218893Sdim case tok::code_completion: 1298218893Sdim if (InMessageExpression) 1299243830Sdim return LHS; 1300218893Sdim 1301218893Sdim Actions.CodeCompletePostfixExpression(getCurScope(), LHS); 1302226633Sdim cutOffParsing(); 1303226633Sdim return ExprError(); 1304218893Sdim 1305218893Sdim case tok::identifier: 1306218893Sdim // If we see identifier: after an expression, and we're not already in a 1307218893Sdim // message send, then this is probably a message send with a missing 1308218893Sdim // opening bracket '['. 1309234353Sdim if (getLangOpts().ObjC1 && !InMessageExpression && 1310218893Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { 1311218893Sdim LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), 1312218893Sdim ParsedType(), LHS.get()); 1313218893Sdim break; 1314218893Sdim } 1315218893Sdim 1316218893Sdim // Fall through; this isn't a message send. 1317218893Sdim 1318193326Sed default: // Not a postfix-expression suffix. 1319243830Sdim return LHS; 1320193326Sed case tok::l_square: { // postfix-expression: p-e '[' expression ']' 1321210299Sed // If we have a array postfix expression that starts on a new line and 1322210299Sed // Objective-C is enabled, it is highly likely that the user forgot a 1323210299Sed // semicolon after the base expression and that the array postfix-expr is 1324210299Sed // actually another message send. In this case, do some look-ahead to see 1325210299Sed // if the contents of the square brackets are obviously not a valid 1326210299Sed // expression and recover by pretending there is no suffix. 1327234353Sdim if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() && 1328210299Sed isSimpleObjCMessageExpression()) 1329243830Sdim return LHS; 1330234353Sdim 1331234353Sdim // Reject array indices starting with a lambda-expression. '[[' is 1332234353Sdim // reserved for attributes. 1333234353Sdim if (CheckProhibitedCXX11Attribute()) 1334234353Sdim return ExprError(); 1335234353Sdim 1336226633Sdim BalancedDelimiterTracker T(*this, tok::l_square); 1337226633Sdim T.consumeOpen(); 1338226633Sdim Loc = T.getOpenLocation(); 1339223017Sdim ExprResult Idx; 1340249423Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 1341234353Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 1342223017Sdim Idx = ParseBraceInitializer(); 1343234353Sdim } else 1344223017Sdim Idx = ParseExpression(); 1345193326Sed 1346193326Sed SourceLocation RLoc = Tok.getLocation(); 1347193326Sed 1348193326Sed if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { 1349212904Sdim LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc, 1350212904Sdim Idx.take(), RLoc); 1351193326Sed } else 1352193326Sed LHS = ExprError(); 1353193326Sed 1354193326Sed // Match the ']'. 1355226633Sdim T.consumeClose(); 1356193326Sed break; 1357193326Sed } 1358193326Sed 1359218893Sdim case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')' 1360218893Sdim case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>' 1361218893Sdim // '(' argument-expression-list[opt] ')' 1362218893Sdim tok::TokenKind OpKind = Tok.getKind(); 1363218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 1364218893Sdim 1365218893Sdim Expr *ExecConfig = 0; 1366193326Sed 1367226633Sdim BalancedDelimiterTracker PT(*this, tok::l_paren); 1368226633Sdim 1369218893Sdim if (OpKind == tok::lesslessless) { 1370243830Sdim ExprVector ExecConfigExprs; 1371218893Sdim CommaLocsTy ExecConfigCommaLocs; 1372234353Sdim SourceLocation OpenLoc = ConsumeToken(); 1373193326Sed 1374218893Sdim if (ParseExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) { 1375218893Sdim LHS = ExprError(); 1376218893Sdim } 1377218893Sdim 1378234353Sdim SourceLocation CloseLoc = Tok.getLocation(); 1379234353Sdim if (Tok.is(tok::greatergreatergreater)) { 1380234353Sdim ConsumeToken(); 1381234353Sdim } else if (LHS.isInvalid()) { 1382218893Sdim SkipUntil(tok::greatergreatergreater); 1383234353Sdim } else { 1384226633Sdim // There was an error closing the brackets 1385234353Sdim Diag(Tok, diag::err_expected_ggg); 1386234353Sdim Diag(OpenLoc, diag::note_matching) << "<<<"; 1387234353Sdim SkipUntil(tok::greatergreatergreater); 1388218893Sdim LHS = ExprError(); 1389218893Sdim } 1390218893Sdim 1391218893Sdim if (!LHS.isInvalid()) { 1392218893Sdim if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, "")) 1393218893Sdim LHS = ExprError(); 1394218893Sdim else 1395218893Sdim Loc = PrevTokLocation; 1396218893Sdim } 1397218893Sdim 1398218893Sdim if (!LHS.isInvalid()) { 1399218893Sdim ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(), 1400234353Sdim OpenLoc, 1401243830Sdim ExecConfigExprs, 1402234353Sdim CloseLoc); 1403218893Sdim if (ECResult.isInvalid()) 1404218893Sdim LHS = ExprError(); 1405218893Sdim else 1406218893Sdim ExecConfig = ECResult.get(); 1407218893Sdim } 1408218893Sdim } else { 1409226633Sdim PT.consumeOpen(); 1410226633Sdim Loc = PT.getOpenLocation(); 1411210299Sed } 1412210299Sed 1413243830Sdim ExprVector ArgExprs; 1414218893Sdim CommaLocsTy CommaLocs; 1415218893Sdim 1416198092Srdivacky if (Tok.is(tok::code_completion)) { 1417251662Sdim Actions.CodeCompleteCall(getCurScope(), LHS.get(), None); 1418226633Sdim cutOffParsing(); 1419226633Sdim return ExprError(); 1420198092Srdivacky } 1421218893Sdim 1422218893Sdim if (OpKind == tok::l_paren || !LHS.isInvalid()) { 1423218893Sdim if (Tok.isNot(tok::r_paren)) { 1424218893Sdim if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall, 1425218893Sdim LHS.get())) { 1426218893Sdim LHS = ExprError(); 1427218893Sdim } 1428193326Sed } 1429193326Sed } 1430193326Sed 1431193326Sed // Match the ')'. 1432218893Sdim if (LHS.isInvalid()) { 1433218893Sdim SkipUntil(tok::r_paren); 1434218893Sdim } else if (Tok.isNot(tok::r_paren)) { 1435226633Sdim PT.consumeClose(); 1436218893Sdim LHS = ExprError(); 1437218893Sdim } else { 1438218893Sdim assert((ArgExprs.size() == 0 || 1439218893Sdim ArgExprs.size()-1 == CommaLocs.size())&& 1440193326Sed "Unexpected number of commas!"); 1441212904Sdim LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc, 1442243830Sdim ArgExprs, Tok.getLocation(), 1443218893Sdim ExecConfig); 1444226633Sdim PT.consumeClose(); 1445193326Sed } 1446198092Srdivacky 1447193326Sed break; 1448193326Sed } 1449198092Srdivacky case tok::arrow: 1450198092Srdivacky case tok::period: { 1451198092Srdivacky // postfix-expression: p-e '->' template[opt] id-expression 1452198092Srdivacky // postfix-expression: p-e '.' template[opt] id-expression 1453193326Sed tok::TokenKind OpKind = Tok.getKind(); 1454193326Sed SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. 1455193326Sed 1456198092Srdivacky CXXScopeSpec SS; 1457212904Sdim ParsedType ObjectType; 1458204643Srdivacky bool MayBePseudoDestructor = false; 1459234353Sdim if (getLangOpts().CPlusPlus && !LHS.isInvalid()) { 1460212904Sdim LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), LHS.take(), 1461204643Srdivacky OpLoc, OpKind, ObjectType, 1462204643Srdivacky MayBePseudoDestructor); 1463198092Srdivacky if (LHS.isInvalid()) 1464198092Srdivacky break; 1465204643Srdivacky 1466234353Sdim ParseOptionalCXXScopeSpecifier(SS, ObjectType, 1467234353Sdim /*EnteringContext=*/false, 1468204643Srdivacky &MayBePseudoDestructor); 1469210299Sed if (SS.isNotEmpty()) 1470212904Sdim ObjectType = ParsedType(); 1471198092Srdivacky } 1472198092Srdivacky 1473198092Srdivacky if (Tok.is(tok::code_completion)) { 1474198092Srdivacky // Code completion for a member access expression. 1475210299Sed Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(), 1476198092Srdivacky OpLoc, OpKind == tok::arrow); 1477198092Srdivacky 1478226633Sdim cutOffParsing(); 1479226633Sdim return ExprError(); 1480198092Srdivacky } 1481198092Srdivacky 1482212904Sdim if (MayBePseudoDestructor && !LHS.isInvalid()) { 1483212904Sdim LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS, 1484204643Srdivacky ObjectType); 1485204643Srdivacky break; 1486204643Srdivacky } 1487204643Srdivacky 1488204643Srdivacky // Either the action has told is that this cannot be a 1489204643Srdivacky // pseudo-destructor expression (based on the type of base 1490204643Srdivacky // expression), or we didn't see a '~' in the right place. We 1491204643Srdivacky // can still parse a destructor name here, but in that case it 1492204643Srdivacky // names a real destructor. 1493218893Sdim // Allow explicit constructor calls in Microsoft mode. 1494218893Sdim // FIXME: Add support for explicit call of template constructor. 1495234353Sdim SourceLocation TemplateKWLoc; 1496198893Srdivacky UnqualifiedId Name; 1497234353Sdim if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) { 1498234353Sdim // Objective-C++: 1499234353Sdim // After a '.' in a member access expression, treat the keyword 1500234353Sdim // 'class' as if it were an identifier. 1501234353Sdim // 1502234353Sdim // This hack allows property access to the 'class' method because it is 1503234353Sdim // such a common method name. For other C++ keywords that are 1504234353Sdim // Objective-C method names, one must use the message send syntax. 1505234353Sdim IdentifierInfo *Id = Tok.getIdentifierInfo(); 1506234353Sdim SourceLocation Loc = ConsumeToken(); 1507234353Sdim Name.setIdentifier(Id, Loc); 1508234353Sdim } else if (ParseUnqualifiedId(SS, 1509234353Sdim /*EnteringContext=*/false, 1510234353Sdim /*AllowDestructorName=*/true, 1511234353Sdim /*AllowConstructorName=*/ 1512234353Sdim getLangOpts().MicrosoftExt, 1513234353Sdim ObjectType, TemplateKWLoc, Name)) 1514218893Sdim LHS = ExprError(); 1515198893Srdivacky 1516198893Srdivacky if (!LHS.isInvalid()) 1517212904Sdim LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc, 1518234353Sdim OpKind, SS, TemplateKWLoc, Name, 1519234353Sdim CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0, 1520198893Srdivacky Tok.is(tok::l_paren)); 1521193326Sed break; 1522193326Sed } 1523193326Sed case tok::plusplus: // postfix-expression: postfix-expression '++' 1524193326Sed case tok::minusminus: // postfix-expression: postfix-expression '--' 1525193326Sed if (!LHS.isInvalid()) { 1526210299Sed LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(), 1527212904Sdim Tok.getKind(), LHS.take()); 1528193326Sed } 1529193326Sed ConsumeToken(); 1530193326Sed break; 1531193326Sed } 1532193326Sed } 1533193326Sed} 1534193326Sed 1535221345Sdim/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/ 1536221345Sdim/// vec_step and we are at the start of an expression or a parenthesized 1537221345Sdim/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the 1538221345Sdim/// expression (isCastExpr == false) or the type (isCastExpr == true). 1539193326Sed/// 1540239462Sdim/// \verbatim 1541193326Sed/// unary-expression: [C99 6.5.3] 1542193326Sed/// 'sizeof' unary-expression 1543193326Sed/// 'sizeof' '(' type-name ')' 1544193326Sed/// [GNU] '__alignof' unary-expression 1545193326Sed/// [GNU] '__alignof' '(' type-name ')' 1546239462Sdim/// [C11] '_Alignof' '(' type-name ')' 1547193326Sed/// [C++0x] 'alignof' '(' type-id ')' 1548193326Sed/// 1549193326Sed/// [GNU] typeof-specifier: 1550193326Sed/// typeof ( expressions ) 1551193326Sed/// typeof ( type-name ) 1552193326Sed/// [GNU/C++] typeof unary-expression 1553193326Sed/// 1554221345Sdim/// [OpenCL 1.1 6.11.12] vec_step built-in function: 1555221345Sdim/// vec_step ( expressions ) 1556221345Sdim/// vec_step ( type-name ) 1557239462Sdim/// \endverbatim 1558212904SdimExprResult 1559221345SdimParser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok, 1560221345Sdim bool &isCastExpr, 1561221345Sdim ParsedType &CastTy, 1562221345Sdim SourceRange &CastRange) { 1563198092Srdivacky 1564198092Srdivacky assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) || 1565221345Sdim OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) || 1566239462Sdim OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) && 1567221345Sdim "Not a typeof/sizeof/alignof/vec_step expression!"); 1568193326Sed 1569212904Sdim ExprResult Operand; 1570198092Srdivacky 1571193326Sed // If the operand doesn't start with an '(', it must be an expression. 1572193326Sed if (Tok.isNot(tok::l_paren)) { 1573193326Sed isCastExpr = false; 1574234353Sdim if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) { 1575193326Sed Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo(); 1576193326Sed return ExprError(); 1577193326Sed } 1578198092Srdivacky 1579193326Sed Operand = ParseCastExpression(true/*isUnaryExpression*/); 1580193326Sed } else { 1581193326Sed // If it starts with a '(', we know that it is either a parenthesized 1582193326Sed // type-name, or it is a unary-expression that starts with a compound 1583193326Sed // literal, or starts with a primary-expression that is a parenthesized 1584193326Sed // expression. 1585193326Sed ParenParseOption ExprType = CastExpr; 1586193326Sed SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 1587198092Srdivacky 1588199990Srdivacky Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, 1589224145Sdim false, CastTy, RParenLoc); 1590193326Sed CastRange = SourceRange(LParenLoc, RParenLoc); 1591193326Sed 1592193326Sed // If ParseParenExpression parsed a '(typename)' sequence only, then this is 1593193326Sed // a type. 1594193326Sed if (ExprType == CastExpr) { 1595193326Sed isCastExpr = true; 1596193326Sed return ExprEmpty(); 1597193326Sed } 1598193326Sed 1599234353Sdim if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) { 1600212904Sdim // GNU typeof in C requires the expression to be parenthesized. Not so for 1601212904Sdim // sizeof/alignof or in C++. Therefore, the parenthesized expression is 1602212904Sdim // the start of a unary-expression, but doesn't include any postfix 1603212904Sdim // pieces. Parse these now if present. 1604212904Sdim if (!Operand.isInvalid()) 1605212904Sdim Operand = ParsePostfixExpressionSuffix(Operand.get()); 1606212904Sdim } 1607193326Sed } 1608193326Sed 1609193326Sed // If we get here, the operand to the typeof/sizeof/alignof was an expresion. 1610193326Sed isCastExpr = false; 1611243830Sdim return Operand; 1612193326Sed} 1613193326Sed 1614193326Sed 1615239462Sdim/// \brief Parse a sizeof or alignof expression. 1616239462Sdim/// 1617239462Sdim/// \verbatim 1618193326Sed/// unary-expression: [C99 6.5.3] 1619193326Sed/// 'sizeof' unary-expression 1620193326Sed/// 'sizeof' '(' type-name ')' 1621249423Sdim/// [C++11] 'sizeof' '...' '(' identifier ')' 1622193326Sed/// [GNU] '__alignof' unary-expression 1623193326Sed/// [GNU] '__alignof' '(' type-name ')' 1624239462Sdim/// [C11] '_Alignof' '(' type-name ')' 1625249423Sdim/// [C++11] 'alignof' '(' type-id ')' 1626239462Sdim/// \endverbatim 1627221345SdimExprResult Parser::ParseUnaryExprOrTypeTraitExpression() { 1628239462Sdim assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) || 1629239462Sdim Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) || 1630239462Sdim Tok.is(tok::kw_vec_step)) && 1631221345Sdim "Not a sizeof/alignof/vec_step expression!"); 1632193326Sed Token OpTok = Tok; 1633193326Sed ConsumeToken(); 1634198092Srdivacky 1635249423Sdim // [C++11] 'sizeof' '...' '(' identifier ')' 1636218893Sdim if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) { 1637218893Sdim SourceLocation EllipsisLoc = ConsumeToken(); 1638218893Sdim SourceLocation LParenLoc, RParenLoc; 1639218893Sdim IdentifierInfo *Name = 0; 1640218893Sdim SourceLocation NameLoc; 1641218893Sdim if (Tok.is(tok::l_paren)) { 1642226633Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 1643226633Sdim T.consumeOpen(); 1644226633Sdim LParenLoc = T.getOpenLocation(); 1645218893Sdim if (Tok.is(tok::identifier)) { 1646218893Sdim Name = Tok.getIdentifierInfo(); 1647218893Sdim NameLoc = ConsumeToken(); 1648226633Sdim T.consumeClose(); 1649226633Sdim RParenLoc = T.getCloseLocation(); 1650218893Sdim if (RParenLoc.isInvalid()) 1651218893Sdim RParenLoc = PP.getLocForEndOfToken(NameLoc); 1652218893Sdim } else { 1653218893Sdim Diag(Tok, diag::err_expected_parameter_pack); 1654218893Sdim SkipUntil(tok::r_paren); 1655218893Sdim } 1656218893Sdim } else if (Tok.is(tok::identifier)) { 1657218893Sdim Name = Tok.getIdentifierInfo(); 1658218893Sdim NameLoc = ConsumeToken(); 1659218893Sdim LParenLoc = PP.getLocForEndOfToken(EllipsisLoc); 1660218893Sdim RParenLoc = PP.getLocForEndOfToken(NameLoc); 1661218893Sdim Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack) 1662218893Sdim << Name 1663218893Sdim << FixItHint::CreateInsertion(LParenLoc, "(") 1664218893Sdim << FixItHint::CreateInsertion(RParenLoc, ")"); 1665218893Sdim } else { 1666218893Sdim Diag(Tok, diag::err_sizeof_parameter_pack); 1667218893Sdim } 1668218893Sdim 1669218893Sdim if (!Name) 1670218893Sdim return ExprError(); 1671218893Sdim 1672218893Sdim return Actions.ActOnSizeofParameterPackExpr(getCurScope(), 1673218893Sdim OpTok.getLocation(), 1674218893Sdim *Name, NameLoc, 1675218893Sdim RParenLoc); 1676218893Sdim } 1677234353Sdim 1678239462Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) 1679234353Sdim Diag(OpTok, diag::warn_cxx98_compat_alignof); 1680234353Sdim 1681243830Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, 1682243830Sdim Sema::ReuseLambdaContextDecl); 1683234353Sdim 1684193326Sed bool isCastExpr; 1685212904Sdim ParsedType CastTy; 1686193326Sed SourceRange CastRange; 1687221345Sdim ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, 1688221345Sdim isCastExpr, 1689221345Sdim CastTy, 1690221345Sdim CastRange); 1691193326Sed 1692221345Sdim UnaryExprOrTypeTrait ExprKind = UETT_SizeOf; 1693239462Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) || 1694239462Sdim OpTok.is(tok::kw__Alignof)) 1695221345Sdim ExprKind = UETT_AlignOf; 1696221345Sdim else if (OpTok.is(tok::kw_vec_step)) 1697221345Sdim ExprKind = UETT_VecStep; 1698221345Sdim 1699193326Sed if (isCastExpr) 1700221345Sdim return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), 1701221345Sdim ExprKind, 1702221345Sdim /*isType=*/true, 1703221345Sdim CastTy.getAsOpaquePtr(), 1704221345Sdim CastRange); 1705193326Sed 1706249423Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) 1707249423Sdim Diag(OpTok, diag::ext_alignof_expr) << OpTok.getIdentifierInfo(); 1708249423Sdim 1709193326Sed // If we get here, the operand to the sizeof/alignof was an expresion. 1710193326Sed if (!Operand.isInvalid()) 1711221345Sdim Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), 1712221345Sdim ExprKind, 1713221345Sdim /*isType=*/false, 1714221345Sdim Operand.release(), 1715221345Sdim CastRange); 1716243830Sdim return Operand; 1717193326Sed} 1718193326Sed 1719193326Sed/// ParseBuiltinPrimaryExpression 1720193326Sed/// 1721239462Sdim/// \verbatim 1722193326Sed/// primary-expression: [C99 6.5.1] 1723193326Sed/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 1724193326Sed/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 1725193326Sed/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 1726193326Sed/// assign-expr ')' 1727193326Sed/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 1728234353Sdim/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')' 1729198092Srdivacky/// 1730193326Sed/// [GNU] offsetof-member-designator: 1731193326Sed/// [GNU] identifier 1732193326Sed/// [GNU] offsetof-member-designator '.' identifier 1733193326Sed/// [GNU] offsetof-member-designator '[' expression ']' 1734239462Sdim/// \endverbatim 1735212904SdimExprResult Parser::ParseBuiltinPrimaryExpression() { 1736212904Sdim ExprResult Res; 1737193326Sed const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); 1738193326Sed 1739193326Sed tok::TokenKind T = Tok.getKind(); 1740193326Sed SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. 1741193326Sed 1742193326Sed // All of these start with an open paren. 1743193326Sed if (Tok.isNot(tok::l_paren)) 1744193326Sed return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) 1745193326Sed << BuiltinII); 1746193326Sed 1747226633Sdim BalancedDelimiterTracker PT(*this, tok::l_paren); 1748226633Sdim PT.consumeOpen(); 1749226633Sdim 1750193326Sed // TODO: Build AST. 1751193326Sed 1752193326Sed switch (T) { 1753226633Sdim default: llvm_unreachable("Not a builtin primary expression!"); 1754193326Sed case tok::kw___builtin_va_arg: { 1755212904Sdim ExprResult Expr(ParseAssignmentExpression()); 1756193326Sed 1757193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1758218893Sdim Expr = ExprError(); 1759193326Sed 1760193326Sed TypeResult Ty = ParseTypeName(); 1761193326Sed 1762193326Sed if (Tok.isNot(tok::r_paren)) { 1763193326Sed Diag(Tok, diag::err_expected_rparen); 1764218893Sdim Expr = ExprError(); 1765193326Sed } 1766218893Sdim 1767218893Sdim if (Expr.isInvalid() || Ty.isInvalid()) 1768193326Sed Res = ExprError(); 1769193326Sed else 1770212904Sdim Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen()); 1771193326Sed break; 1772193326Sed } 1773193326Sed case tok::kw___builtin_offsetof: { 1774193326Sed SourceLocation TypeLoc = Tok.getLocation(); 1775193326Sed TypeResult Ty = ParseTypeName(); 1776193326Sed if (Ty.isInvalid()) { 1777193326Sed SkipUntil(tok::r_paren); 1778193326Sed return ExprError(); 1779193326Sed } 1780198092Srdivacky 1781193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1782193326Sed return ExprError(); 1783193326Sed 1784193326Sed // We must have at least one identifier here. 1785193326Sed if (Tok.isNot(tok::identifier)) { 1786193326Sed Diag(Tok, diag::err_expected_ident); 1787193326Sed SkipUntil(tok::r_paren); 1788193326Sed return ExprError(); 1789193326Sed } 1790193326Sed 1791193326Sed // Keep track of the various subcomponents we see. 1792226633Sdim SmallVector<Sema::OffsetOfComponent, 4> Comps; 1793193326Sed 1794212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1795193326Sed Comps.back().isBrackets = false; 1796193326Sed Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 1797193326Sed Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); 1798193326Sed 1799193326Sed // FIXME: This loop leaks the index expressions on error. 1800193326Sed while (1) { 1801193326Sed if (Tok.is(tok::period)) { 1802193326Sed // offsetof-member-designator: offsetof-member-designator '.' identifier 1803212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1804193326Sed Comps.back().isBrackets = false; 1805193326Sed Comps.back().LocStart = ConsumeToken(); 1806193326Sed 1807193326Sed if (Tok.isNot(tok::identifier)) { 1808193326Sed Diag(Tok, diag::err_expected_ident); 1809193326Sed SkipUntil(tok::r_paren); 1810193326Sed return ExprError(); 1811193326Sed } 1812193326Sed Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 1813193326Sed Comps.back().LocEnd = ConsumeToken(); 1814193326Sed 1815193326Sed } else if (Tok.is(tok::l_square)) { 1816234353Sdim if (CheckProhibitedCXX11Attribute()) 1817234353Sdim return ExprError(); 1818234353Sdim 1819193326Sed // offsetof-member-designator: offsetof-member-design '[' expression ']' 1820212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1821193326Sed Comps.back().isBrackets = true; 1822226633Sdim BalancedDelimiterTracker ST(*this, tok::l_square); 1823226633Sdim ST.consumeOpen(); 1824226633Sdim Comps.back().LocStart = ST.getOpenLocation(); 1825193326Sed Res = ParseExpression(); 1826193326Sed if (Res.isInvalid()) { 1827193326Sed SkipUntil(tok::r_paren); 1828243830Sdim return Res; 1829193326Sed } 1830193326Sed Comps.back().U.E = Res.release(); 1831193326Sed 1832226633Sdim ST.consumeClose(); 1833226633Sdim Comps.back().LocEnd = ST.getCloseLocation(); 1834195341Sed } else { 1835195341Sed if (Tok.isNot(tok::r_paren)) { 1836226633Sdim PT.consumeClose(); 1837193326Sed Res = ExprError(); 1838195341Sed } else if (Ty.isInvalid()) { 1839195341Sed Res = ExprError(); 1840195341Sed } else { 1841226633Sdim PT.consumeClose(); 1842210299Sed Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc, 1843226633Sdim Ty.get(), &Comps[0], Comps.size(), 1844226633Sdim PT.getCloseLocation()); 1845195341Sed } 1846193326Sed break; 1847193326Sed } 1848193326Sed } 1849193326Sed break; 1850193326Sed } 1851193326Sed case tok::kw___builtin_choose_expr: { 1852212904Sdim ExprResult Cond(ParseAssignmentExpression()); 1853193326Sed if (Cond.isInvalid()) { 1854193326Sed SkipUntil(tok::r_paren); 1855243830Sdim return Cond; 1856193326Sed } 1857193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1858193326Sed return ExprError(); 1859193326Sed 1860212904Sdim ExprResult Expr1(ParseAssignmentExpression()); 1861193326Sed if (Expr1.isInvalid()) { 1862193326Sed SkipUntil(tok::r_paren); 1863243830Sdim return Expr1; 1864193326Sed } 1865193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1866193326Sed return ExprError(); 1867193326Sed 1868212904Sdim ExprResult Expr2(ParseAssignmentExpression()); 1869193326Sed if (Expr2.isInvalid()) { 1870193326Sed SkipUntil(tok::r_paren); 1871243830Sdim return Expr2; 1872193326Sed } 1873193326Sed if (Tok.isNot(tok::r_paren)) { 1874193326Sed Diag(Tok, diag::err_expected_rparen); 1875193326Sed return ExprError(); 1876193326Sed } 1877212904Sdim Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(), 1878212904Sdim Expr2.take(), ConsumeParen()); 1879193326Sed break; 1880193326Sed } 1881223017Sdim case tok::kw___builtin_astype: { 1882223017Sdim // The first argument is an expression to be converted, followed by a comma. 1883223017Sdim ExprResult Expr(ParseAssignmentExpression()); 1884223017Sdim if (Expr.isInvalid()) { 1885223017Sdim SkipUntil(tok::r_paren); 1886223017Sdim return ExprError(); 1887223017Sdim } 1888223017Sdim 1889223017Sdim if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", 1890223017Sdim tok::r_paren)) 1891223017Sdim return ExprError(); 1892223017Sdim 1893223017Sdim // Second argument is the type to bitcast to. 1894223017Sdim TypeResult DestTy = ParseTypeName(); 1895223017Sdim if (DestTy.isInvalid()) 1896223017Sdim return ExprError(); 1897223017Sdim 1898223017Sdim // Attempt to consume the r-paren. 1899223017Sdim if (Tok.isNot(tok::r_paren)) { 1900223017Sdim Diag(Tok, diag::err_expected_rparen); 1901223017Sdim SkipUntil(tok::r_paren); 1902223017Sdim return ExprError(); 1903223017Sdim } 1904223017Sdim 1905223017Sdim Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc, 1906223017Sdim ConsumeParen()); 1907223017Sdim break; 1908193326Sed } 1909224145Sdim } 1910193326Sed 1911212904Sdim if (Res.isInvalid()) 1912212904Sdim return ExprError(); 1913212904Sdim 1914193326Sed // These can be followed by postfix-expr pieces because they are 1915193326Sed // primary-expressions. 1916212904Sdim return ParsePostfixExpressionSuffix(Res.take()); 1917193326Sed} 1918193326Sed 1919193326Sed/// ParseParenExpression - This parses the unit that starts with a '(' token, 1920193326Sed/// based on what is allowed by ExprType. The actual thing parsed is returned 1921193326Sed/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type, 1922193326Sed/// not the parsed cast-expression. 1923193326Sed/// 1924239462Sdim/// \verbatim 1925193326Sed/// primary-expression: [C99 6.5.1] 1926193326Sed/// '(' expression ')' 1927193326Sed/// [GNU] '(' compound-statement ')' (if !ParenExprOnly) 1928193326Sed/// postfix-expression: [C99 6.5.2] 1929193326Sed/// '(' type-name ')' '{' initializer-list '}' 1930193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 1931193326Sed/// cast-expression: [C99 6.5.4] 1932193326Sed/// '(' type-name ')' cast-expression 1933224145Sdim/// [ARC] bridged-cast-expression 1934224145Sdim/// 1935224145Sdim/// [ARC] bridged-cast-expression: 1936224145Sdim/// (__bridge type-name) cast-expression 1937224145Sdim/// (__bridge_transfer type-name) cast-expression 1938224145Sdim/// (__bridge_retained type-name) cast-expression 1939239462Sdim/// \endverbatim 1940212904SdimExprResult 1941193326SedParser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, 1942224145Sdim bool isTypeCast, ParsedType &CastTy, 1943198092Srdivacky SourceLocation &RParenLoc) { 1944193326Sed assert(Tok.is(tok::l_paren) && "Not a paren expr!"); 1945226633Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 1946226633Sdim if (T.consumeOpen()) 1947226633Sdim return ExprError(); 1948226633Sdim SourceLocation OpenLoc = T.getOpenLocation(); 1949226633Sdim 1950212904Sdim ExprResult Result(true); 1951193326Sed bool isAmbiguousTypeId; 1952212904Sdim CastTy = ParsedType(); 1953193326Sed 1954218893Sdim if (Tok.is(tok::code_completion)) { 1955218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), 1956218893Sdim ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression 1957218893Sdim : Sema::PCC_Expression); 1958226633Sdim cutOffParsing(); 1959218893Sdim return ExprError(); 1960218893Sdim } 1961221345Sdim 1962234353Sdim // Diagnose use of bridge casts in non-arc mode. 1963234353Sdim bool BridgeCast = (getLangOpts().ObjC2 && 1964234353Sdim (Tok.is(tok::kw___bridge) || 1965234353Sdim Tok.is(tok::kw___bridge_transfer) || 1966234353Sdim Tok.is(tok::kw___bridge_retained) || 1967234353Sdim Tok.is(tok::kw___bridge_retain))); 1968234353Sdim if (BridgeCast && !getLangOpts().ObjCAutoRefCount) { 1969249423Sdim if (Tok.isNot(tok::kw___bridge)) { 1970249423Sdim StringRef BridgeCastName = Tok.getName(); 1971249423Sdim SourceLocation BridgeKeywordLoc = ConsumeToken(); 1972249423Sdim if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) 1973249423Sdim Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc) 1974249423Sdim << BridgeCastName 1975249423Sdim << FixItHint::CreateReplacement(BridgeKeywordLoc, ""); 1976249423Sdim } 1977249423Sdim else 1978249423Sdim ConsumeToken(); // consume __bridge 1979234353Sdim BridgeCast = false; 1980234353Sdim } 1981234353Sdim 1982221345Sdim // None of these cases should fall through with an invalid Result 1983221345Sdim // unless they've already reported an error. 1984193326Sed if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { 1985193326Sed Diag(Tok, diag::ext_gnu_statement_expr); 1986234353Sdim Actions.ActOnStartStmtExpr(); 1987234353Sdim 1988234982Sdim StmtResult Stmt(ParseCompoundStatement(true)); 1989193326Sed ExprType = CompoundStmt; 1990193326Sed 1991193326Sed // If the substmt parsed correctly, build the AST node. 1992234353Sdim if (!Stmt.isInvalid()) { 1993212904Sdim Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation()); 1994234353Sdim } else { 1995234353Sdim Actions.ActOnStmtExprError(); 1996234353Sdim } 1997234353Sdim } else if (ExprType >= CompoundLiteral && BridgeCast) { 1998224145Sdim tok::TokenKind tokenKind = Tok.getKind(); 1999224145Sdim SourceLocation BridgeKeywordLoc = ConsumeToken(); 2000193326Sed 2001224145Sdim // Parse an Objective-C ARC ownership cast expression. 2002224145Sdim ObjCBridgeCastKind Kind; 2003224145Sdim if (tokenKind == tok::kw___bridge) 2004224145Sdim Kind = OBC_Bridge; 2005224145Sdim else if (tokenKind == tok::kw___bridge_transfer) 2006224145Sdim Kind = OBC_BridgeTransfer; 2007224145Sdim else if (tokenKind == tok::kw___bridge_retained) 2008224145Sdim Kind = OBC_BridgeRetained; 2009224145Sdim else { 2010224145Sdim // As a hopefully temporary workaround, allow __bridge_retain as 2011224145Sdim // a synonym for __bridge_retained, but only in system headers. 2012224145Sdim assert(tokenKind == tok::kw___bridge_retain); 2013224145Sdim Kind = OBC_BridgeRetained; 2014224145Sdim if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) 2015224145Sdim Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain) 2016224145Sdim << FixItHint::CreateReplacement(BridgeKeywordLoc, 2017224145Sdim "__bridge_retained"); 2018224145Sdim } 2019224145Sdim 2020224145Sdim TypeResult Ty = ParseTypeName(); 2021226633Sdim T.consumeClose(); 2022226633Sdim RParenLoc = T.getCloseLocation(); 2023224145Sdim ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false); 2024224145Sdim 2025224145Sdim if (Ty.isInvalid() || SubExpr.isInvalid()) 2026224145Sdim return ExprError(); 2027224145Sdim 2028224145Sdim return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind, 2029224145Sdim BridgeKeywordLoc, Ty.get(), 2030224145Sdim RParenLoc, SubExpr.get()); 2031193326Sed } else if (ExprType >= CompoundLiteral && 2032193326Sed isTypeIdInParens(isAmbiguousTypeId)) { 2033198092Srdivacky 2034193326Sed // Otherwise, this is a compound literal expression or cast expression. 2035198092Srdivacky 2036193326Sed // In C++, if the type-id is ambiguous we disambiguate based on context. 2037193326Sed // If stopIfCastExpr is true the context is a typeof/sizeof/alignof 2038193326Sed // in which case we should treat it as type-id. 2039193326Sed // if stopIfCastExpr is false, we need to determine the context past the 2040193326Sed // parens, so we defer to ParseCXXAmbiguousParenExpression for that. 2041226633Sdim if (isAmbiguousTypeId && !stopIfCastExpr) { 2042226633Sdim ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T); 2043226633Sdim RParenLoc = T.getCloseLocation(); 2044226633Sdim return res; 2045226633Sdim } 2046198092Srdivacky 2047224145Sdim // Parse the type declarator. 2048224145Sdim DeclSpec DS(AttrFactory); 2049224145Sdim ParseSpecifierQualifierList(DS); 2050224145Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 2051224145Sdim ParseDeclarator(DeclaratorInfo); 2052218893Sdim 2053218893Sdim // If our type is followed by an identifier and either ':' or ']', then 2054218893Sdim // this is probably an Objective-C message send where the leading '[' is 2055218893Sdim // missing. Recover as if that were the case. 2056224145Sdim if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) && 2057234353Sdim !InMessageExpression && getLangOpts().ObjC1 && 2058224145Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { 2059224145Sdim TypeResult Ty; 2060224145Sdim { 2061224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2062224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2063224145Sdim } 2064218893Sdim Result = ParseObjCMessageExpressionBody(SourceLocation(), 2065218893Sdim SourceLocation(), 2066218893Sdim Ty.get(), 0); 2067218893Sdim } else { 2068218893Sdim // Match the ')'. 2069226633Sdim T.consumeClose(); 2070226633Sdim RParenLoc = T.getCloseLocation(); 2071218893Sdim if (Tok.is(tok::l_brace)) { 2072218893Sdim ExprType = CompoundLiteral; 2073224145Sdim TypeResult Ty; 2074224145Sdim { 2075224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2076224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2077224145Sdim } 2078218893Sdim return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc); 2079218893Sdim } 2080193326Sed 2081218893Sdim if (ExprType == CastExpr) { 2082218893Sdim // We parsed '(' type-name ')' and the thing after it wasn't a '{'. 2083193326Sed 2084224145Sdim if (DeclaratorInfo.isInvalidType()) 2085218893Sdim return ExprError(); 2086193326Sed 2087218893Sdim // Note that this doesn't parse the subsequent cast-expression, it just 2088218893Sdim // returns the parsed type to the callee. 2089224145Sdim if (stopIfCastExpr) { 2090224145Sdim TypeResult Ty; 2091224145Sdim { 2092224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2093224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2094224145Sdim } 2095224145Sdim CastTy = Ty.get(); 2096218893Sdim return ExprResult(); 2097224145Sdim } 2098218893Sdim 2099218893Sdim // Reject the cast of super idiom in ObjC. 2100234353Sdim if (Tok.is(tok::identifier) && getLangOpts().ObjC1 && 2101218893Sdim Tok.getIdentifierInfo() == Ident_super && 2102218893Sdim getCurScope()->isInObjcMethodScope() && 2103218893Sdim GetLookAheadToken(1).isNot(tok::period)) { 2104218893Sdim Diag(Tok.getLocation(), diag::err_illegal_super_cast) 2105218893Sdim << SourceRange(OpenLoc, RParenLoc); 2106218893Sdim return ExprError(); 2107218893Sdim } 2108193326Sed 2109218893Sdim // Parse the cast-expression that follows it next. 2110218893Sdim // TODO: For cast expression with CastTy. 2111224145Sdim Result = ParseCastExpression(/*isUnaryExpression=*/false, 2112224145Sdim /*isAddressOfOperand=*/false, 2113234353Sdim /*isTypeCast=*/IsTypeCast); 2114224145Sdim if (!Result.isInvalid()) { 2115224145Sdim Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc, 2116224145Sdim DeclaratorInfo, CastTy, 2117218893Sdim RParenLoc, Result.take()); 2118224145Sdim } 2119243830Sdim return Result; 2120193326Sed } 2121193326Sed 2122218893Sdim Diag(Tok, diag::err_expected_lbrace_in_compound_literal); 2123218893Sdim return ExprError(); 2124193326Sed } 2125224145Sdim } else if (isTypeCast) { 2126198092Srdivacky // Parse the expression-list. 2127218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2128218893Sdim 2129243830Sdim ExprVector ArgExprs; 2130198092Srdivacky CommaLocsTy CommaLocs; 2131198092Srdivacky 2132198092Srdivacky if (!ParseExpressionList(ArgExprs, CommaLocs)) { 2133198092Srdivacky ExprType = SimpleExpr; 2134234353Sdim Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(), 2135243830Sdim ArgExprs); 2136198092Srdivacky } 2137193326Sed } else { 2138218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2139218893Sdim 2140234353Sdim Result = ParseExpression(MaybeTypeCast); 2141193326Sed ExprType = SimpleExpr; 2142221345Sdim 2143221345Sdim // Don't build a paren expression unless we actually match a ')'. 2144193326Sed if (!Result.isInvalid() && Tok.is(tok::r_paren)) 2145212904Sdim Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take()); 2146193326Sed } 2147193326Sed 2148193326Sed // Match the ')'. 2149193326Sed if (Result.isInvalid()) { 2150193326Sed SkipUntil(tok::r_paren); 2151193326Sed return ExprError(); 2152193326Sed } 2153198092Srdivacky 2154226633Sdim T.consumeClose(); 2155226633Sdim RParenLoc = T.getCloseLocation(); 2156243830Sdim return Result; 2157193326Sed} 2158193326Sed 2159193326Sed/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name 2160193326Sed/// and we are at the left brace. 2161193326Sed/// 2162239462Sdim/// \verbatim 2163193326Sed/// postfix-expression: [C99 6.5.2] 2164193326Sed/// '(' type-name ')' '{' initializer-list '}' 2165193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 2166239462Sdim/// \endverbatim 2167212904SdimExprResult 2168212904SdimParser::ParseCompoundLiteralExpression(ParsedType Ty, 2169193326Sed SourceLocation LParenLoc, 2170193326Sed SourceLocation RParenLoc) { 2171193326Sed assert(Tok.is(tok::l_brace) && "Not a compound literal!"); 2172234353Sdim if (!getLangOpts().C99) // Compound literals don't exist in C90. 2173193326Sed Diag(LParenLoc, diag::ext_c99_compound_literal); 2174212904Sdim ExprResult Result = ParseInitializer(); 2175193326Sed if (!Result.isInvalid() && Ty) 2176212904Sdim return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take()); 2177243830Sdim return Result; 2178193326Sed} 2179193326Sed 2180193326Sed/// ParseStringLiteralExpression - This handles the various token types that 2181193326Sed/// form string literals, and also handles string concatenation [C99 5.1.1.2, 2182193326Sed/// translation phase #6]. 2183193326Sed/// 2184239462Sdim/// \verbatim 2185193326Sed/// primary-expression: [C99 6.5.1] 2186193326Sed/// string-literal 2187239462Sdim/// \verbatim 2188234353SdimExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) { 2189193326Sed assert(isTokenStringLiteral() && "Not a string literal!"); 2190193326Sed 2191193326Sed // String concat. Note that keywords like __func__ and __FUNCTION__ are not 2192193326Sed // considered to be strings for concatenation purposes. 2193226633Sdim SmallVector<Token, 4> StringToks; 2194193326Sed 2195193326Sed do { 2196193326Sed StringToks.push_back(Tok); 2197193326Sed ConsumeStringToken(); 2198193326Sed } while (isTokenStringLiteral()); 2199193326Sed 2200193326Sed // Pass the set of string tokens, ready for concatenation, to the actions. 2201234353Sdim return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(), 2202234353Sdim AllowUserDefinedLiteral ? getCurScope() : 0); 2203193326Sed} 2204193326Sed 2205234353Sdim/// ParseGenericSelectionExpression - Parse a C11 generic-selection 2206234353Sdim/// [C11 6.5.1.1]. 2207221345Sdim/// 2208239462Sdim/// \verbatim 2209221345Sdim/// generic-selection: 2210221345Sdim/// _Generic ( assignment-expression , generic-assoc-list ) 2211221345Sdim/// generic-assoc-list: 2212221345Sdim/// generic-association 2213221345Sdim/// generic-assoc-list , generic-association 2214221345Sdim/// generic-association: 2215221345Sdim/// type-name : assignment-expression 2216221345Sdim/// default : assignment-expression 2217239462Sdim/// \endverbatim 2218221345SdimExprResult Parser::ParseGenericSelectionExpression() { 2219221345Sdim assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected"); 2220221345Sdim SourceLocation KeyLoc = ConsumeToken(); 2221221345Sdim 2222234353Sdim if (!getLangOpts().C11) 2223234353Sdim Diag(KeyLoc, diag::ext_c11_generic_selection); 2224221345Sdim 2225226633Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 2226226633Sdim if (T.expectAndConsume(diag::err_expected_lparen)) 2227221345Sdim return ExprError(); 2228221345Sdim 2229221345Sdim ExprResult ControllingExpr; 2230221345Sdim { 2231234353Sdim // C11 6.5.1.1p3 "The controlling expression of a generic selection is 2232221345Sdim // not evaluated." 2233221345Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 2234221345Sdim ControllingExpr = ParseAssignmentExpression(); 2235221345Sdim if (ControllingExpr.isInvalid()) { 2236221345Sdim SkipUntil(tok::r_paren); 2237221345Sdim return ExprError(); 2238221345Sdim } 2239221345Sdim } 2240221345Sdim 2241221345Sdim if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) { 2242221345Sdim SkipUntil(tok::r_paren); 2243221345Sdim return ExprError(); 2244221345Sdim } 2245221345Sdim 2246221345Sdim SourceLocation DefaultLoc; 2247243830Sdim TypeVector Types; 2248243830Sdim ExprVector Exprs; 2249221345Sdim while (1) { 2250221345Sdim ParsedType Ty; 2251221345Sdim if (Tok.is(tok::kw_default)) { 2252234353Sdim // C11 6.5.1.1p2 "A generic selection shall have no more than one default 2253221345Sdim // generic association." 2254221345Sdim if (!DefaultLoc.isInvalid()) { 2255221345Sdim Diag(Tok, diag::err_duplicate_default_assoc); 2256221345Sdim Diag(DefaultLoc, diag::note_previous_default_assoc); 2257221345Sdim SkipUntil(tok::r_paren); 2258221345Sdim return ExprError(); 2259221345Sdim } 2260221345Sdim DefaultLoc = ConsumeToken(); 2261221345Sdim Ty = ParsedType(); 2262221345Sdim } else { 2263221345Sdim ColonProtectionRAIIObject X(*this); 2264221345Sdim TypeResult TR = ParseTypeName(); 2265221345Sdim if (TR.isInvalid()) { 2266221345Sdim SkipUntil(tok::r_paren); 2267221345Sdim return ExprError(); 2268221345Sdim } 2269221345Sdim Ty = TR.release(); 2270221345Sdim } 2271221345Sdim Types.push_back(Ty); 2272221345Sdim 2273221345Sdim if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) { 2274221345Sdim SkipUntil(tok::r_paren); 2275221345Sdim return ExprError(); 2276221345Sdim } 2277221345Sdim 2278221345Sdim // FIXME: These expressions should be parsed in a potentially potentially 2279221345Sdim // evaluated context. 2280221345Sdim ExprResult ER(ParseAssignmentExpression()); 2281221345Sdim if (ER.isInvalid()) { 2282221345Sdim SkipUntil(tok::r_paren); 2283221345Sdim return ExprError(); 2284221345Sdim } 2285221345Sdim Exprs.push_back(ER.release()); 2286221345Sdim 2287221345Sdim if (Tok.isNot(tok::comma)) 2288221345Sdim break; 2289221345Sdim ConsumeToken(); 2290221345Sdim } 2291221345Sdim 2292226633Sdim T.consumeClose(); 2293226633Sdim if (T.getCloseLocation().isInvalid()) 2294221345Sdim return ExprError(); 2295221345Sdim 2296226633Sdim return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc, 2297226633Sdim T.getCloseLocation(), 2298221345Sdim ControllingExpr.release(), 2299243830Sdim Types, Exprs); 2300221345Sdim} 2301221345Sdim 2302193326Sed/// ParseExpressionList - Used for C/C++ (argument-)expression-list. 2303193326Sed/// 2304239462Sdim/// \verbatim 2305193326Sed/// argument-expression-list: 2306193326Sed/// assignment-expression 2307193326Sed/// argument-expression-list , assignment-expression 2308193326Sed/// 2309193326Sed/// [C++] expression-list: 2310223017Sdim/// [C++] assignment-expression 2311223017Sdim/// [C++] expression-list , assignment-expression 2312193326Sed/// 2313223017Sdim/// [C++0x] expression-list: 2314223017Sdim/// [C++0x] initializer-list 2315223017Sdim/// 2316223017Sdim/// [C++0x] initializer-list 2317223017Sdim/// [C++0x] initializer-clause ...[opt] 2318223017Sdim/// [C++0x] initializer-list , initializer-clause ...[opt] 2319223017Sdim/// 2320223017Sdim/// [C++0x] initializer-clause: 2321223017Sdim/// [C++0x] assignment-expression 2322223017Sdim/// [C++0x] braced-init-list 2323239462Sdim/// \endverbatim 2324226633Sdimbool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs, 2325249423Sdim SmallVectorImpl<SourceLocation> &CommaLocs, 2326249423Sdim void (Sema::*Completer)(Scope *S, 2327249423Sdim Expr *Data, 2328249423Sdim ArrayRef<Expr *> Args), 2329212904Sdim Expr *Data) { 2330193326Sed while (1) { 2331198092Srdivacky if (Tok.is(tok::code_completion)) { 2332198092Srdivacky if (Completer) 2333234353Sdim (Actions.*Completer)(getCurScope(), Data, Exprs); 2334218893Sdim else 2335218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 2336226633Sdim cutOffParsing(); 2337226633Sdim return true; 2338198092Srdivacky } 2339223017Sdim 2340223017Sdim ExprResult Expr; 2341249423Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 2342234353Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 2343223017Sdim Expr = ParseBraceInitializer(); 2344234353Sdim } else 2345223017Sdim Expr = ParseAssignmentExpression(); 2346223017Sdim 2347218893Sdim if (Tok.is(tok::ellipsis)) 2348218893Sdim Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken()); 2349193326Sed if (Expr.isInvalid()) 2350193326Sed return true; 2351193326Sed 2352193326Sed Exprs.push_back(Expr.release()); 2353193326Sed 2354193326Sed if (Tok.isNot(tok::comma)) 2355193326Sed return false; 2356193326Sed // Move to the next argument, remember where the comma was. 2357193326Sed CommaLocs.push_back(ConsumeToken()); 2358193326Sed } 2359193326Sed} 2360193326Sed 2361193326Sed/// ParseBlockId - Parse a block-id, which roughly looks like int (int x). 2362193326Sed/// 2363239462Sdim/// \verbatim 2364193326Sed/// [clang] block-id: 2365193326Sed/// [clang] specifier-qualifier-list block-declarator 2366239462Sdim/// \endverbatim 2367239462Sdimvoid Parser::ParseBlockId(SourceLocation CaretLoc) { 2368218893Sdim if (Tok.is(tok::code_completion)) { 2369218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type); 2370226633Sdim return cutOffParsing(); 2371218893Sdim } 2372218893Sdim 2373193326Sed // Parse the specifier-qualifier-list piece. 2374221345Sdim DeclSpec DS(AttrFactory); 2375193326Sed ParseSpecifierQualifierList(DS); 2376193326Sed 2377193326Sed // Parse the block-declarator. 2378193326Sed Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); 2379193326Sed ParseDeclarator(DeclaratorInfo); 2380193326Sed 2381193326Sed // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes. 2382221345Sdim DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation()); 2383193326Sed 2384218893Sdim MaybeParseGNUAttributes(DeclaratorInfo); 2385193326Sed 2386193326Sed // Inform sema that we are starting a block. 2387239462Sdim Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope()); 2388193326Sed} 2389193326Sed 2390193326Sed/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks 2391193326Sed/// like ^(int x){ return x+1; } 2392193326Sed/// 2393239462Sdim/// \verbatim 2394193326Sed/// block-literal: 2395193326Sed/// [clang] '^' block-args[opt] compound-statement 2396193326Sed/// [clang] '^' block-id compound-statement 2397193326Sed/// [clang] block-args: 2398193326Sed/// [clang] '(' parameter-list ')' 2399239462Sdim/// \endverbatim 2400212904SdimExprResult Parser::ParseBlockLiteralExpression() { 2401193326Sed assert(Tok.is(tok::caret) && "block literal starts with ^"); 2402193326Sed SourceLocation CaretLoc = ConsumeToken(); 2403193326Sed 2404193326Sed PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, 2405193326Sed "block literal parsing"); 2406193326Sed 2407198092Srdivacky // Enter a scope to hold everything within the block. This includes the 2408193326Sed // argument decls, decls within the compound expression, etc. This also 2409193326Sed // allows determining whether a variable reference inside the block is 2410193326Sed // within or outside of the block. 2411193326Sed ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | 2412193326Sed Scope::DeclScope); 2413193326Sed 2414193326Sed // Inform sema that we are starting a block. 2415210299Sed Actions.ActOnBlockStart(CaretLoc, getCurScope()); 2416198092Srdivacky 2417193326Sed // Parse the return type if present. 2418221345Sdim DeclSpec DS(AttrFactory); 2419193326Sed Declarator ParamInfo(DS, Declarator::BlockLiteralContext); 2420193326Sed // FIXME: Since the return type isn't actually parsed, it can't be used to 2421193326Sed // fill ParamInfo with an initial valid range, so do it manually. 2422193326Sed ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); 2423193326Sed 2424193326Sed // If this block has arguments, parse them. There is no ambiguity here with 2425193326Sed // the expression case, because the expression case requires a parameter list. 2426193326Sed if (Tok.is(tok::l_paren)) { 2427193326Sed ParseParenDeclarator(ParamInfo); 2428193326Sed // Parse the pieces after the identifier as if we had "int(...)". 2429193326Sed // SetIdentifier sets the source range end, but in this case we're past 2430193326Sed // that location. 2431193326Sed SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); 2432193326Sed ParamInfo.SetIdentifier(0, CaretLoc); 2433193326Sed ParamInfo.SetRangeEnd(Tmp); 2434193326Sed if (ParamInfo.isInvalidType()) { 2435193326Sed // If there was an error parsing the arguments, they may have 2436193326Sed // tried to use ^(x+y) which requires an argument list. Just 2437193326Sed // skip the whole block literal. 2438210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2439193326Sed return ExprError(); 2440193326Sed } 2441193326Sed 2442218893Sdim MaybeParseGNUAttributes(ParamInfo); 2443193326Sed 2444193326Sed // Inform sema that we are starting a block. 2445239462Sdim Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope()); 2446193326Sed } else if (!Tok.is(tok::l_brace)) { 2447239462Sdim ParseBlockId(CaretLoc); 2448193326Sed } else { 2449193326Sed // Otherwise, pretend we saw (void). 2450221345Sdim ParsedAttributes attrs(AttrFactory); 2451243830Sdim SourceLocation NoLoc; 2452243830Sdim ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/true, 2453243830Sdim /*IsAmbiguous=*/false, 2454243830Sdim /*RParenLoc=*/NoLoc, 2455243830Sdim /*ArgInfo=*/0, 2456243830Sdim /*NumArgs=*/0, 2457243830Sdim /*EllipsisLoc=*/NoLoc, 2458243830Sdim /*RParenLoc=*/NoLoc, 2459243830Sdim /*TypeQuals=*/0, 2460243830Sdim /*RefQualifierIsLvalueRef=*/true, 2461243830Sdim /*RefQualifierLoc=*/NoLoc, 2462243830Sdim /*ConstQualifierLoc=*/NoLoc, 2463243830Sdim /*VolatileQualifierLoc=*/NoLoc, 2464243830Sdim /*MutableLoc=*/NoLoc, 2465243830Sdim EST_None, 2466243830Sdim /*ESpecLoc=*/NoLoc, 2467243830Sdim /*Exceptions=*/0, 2468243830Sdim /*ExceptionRanges=*/0, 2469243830Sdim /*NumExceptions=*/0, 2470243830Sdim /*NoexceptExpr=*/0, 2471243830Sdim CaretLoc, CaretLoc, 2472243830Sdim ParamInfo), 2473221345Sdim attrs, CaretLoc); 2474193326Sed 2475218893Sdim MaybeParseGNUAttributes(ParamInfo); 2476193326Sed 2477193326Sed // Inform sema that we are starting a block. 2478239462Sdim Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope()); 2479193326Sed } 2480193326Sed 2481193326Sed 2482212904Sdim ExprResult Result(true); 2483193326Sed if (!Tok.is(tok::l_brace)) { 2484193326Sed // Saw something like: ^expr 2485193326Sed Diag(Tok, diag::err_expected_expression); 2486210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2487193326Sed return ExprError(); 2488193326Sed } 2489198092Srdivacky 2490212904Sdim StmtResult Stmt(ParseCompoundStatementBody()); 2491221345Sdim BlockScope.Exit(); 2492193326Sed if (!Stmt.isInvalid()) 2493212904Sdim Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope()); 2494193326Sed else 2495210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2496243830Sdim return Result; 2497193326Sed} 2498234353Sdim 2499234353Sdim/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals. 2500234353Sdim/// 2501234353Sdim/// '__objc_yes' 2502234353Sdim/// '__objc_no' 2503234353SdimExprResult Parser::ParseObjCBoolLiteral() { 2504234353Sdim tok::TokenKind Kind = Tok.getKind(); 2505234353Sdim return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind); 2506234353Sdim} 2507