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//===----------------------------------------------------------------------===// 9245431Sdim/// 10245431Sdim/// \file 11245431Sdim/// \brief Provides the Expression parsing implementation. 12245431Sdim/// 13245431Sdim/// Expressions in C99 basically consist of a bunch of binary operators with 14245431Sdim/// unary operators and other random stuff at the leaves. 15245431Sdim/// 16245431Sdim/// In the C99 grammar, these unary operators bind tightest and are represented 17245431Sdim/// as the 'cast-expression' production. Everything else is either a binary 18245431Sdim/// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are 19245431Sdim/// handled by ParseCastExpression, the higher level pieces are handled by 20245431Sdim/// ParseBinaryExpression. 21245431Sdim/// 22193326Sed//===----------------------------------------------------------------------===// 23193326Sed 24193326Sed#include "clang/Parse/Parser.h" 25252723Sdim#include "RAIIObjectsForParser.h" 26252723Sdim#include "clang/Basic/PrettyStackTrace.h" 27212904Sdim#include "clang/Sema/DeclSpec.h" 28252723Sdim#include "clang/Sema/ParsedTemplate.h" 29212904Sdim#include "clang/Sema/Scope.h" 30235633Sdim#include "clang/Sema/TypoCorrection.h" 31252723Sdim#include "llvm/ADT/SmallString.h" 32193326Sed#include "llvm/ADT/SmallVector.h" 33193326Sedusing namespace clang; 34193326Sed 35245431Sdim/// \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/// 46245431Sdim/// \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] 118245431Sdim/// \endverbatim 119235633SdimExprResult Parser::ParseExpression(TypeCastState isTypeCast) { 120235633Sdim ExprResult LHS(ParseAssignmentExpression(isTypeCast)); 121245431Sdim 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 126245431Sdim/// routine is necessary to disambiguate \@try-statement from, 127245431Sdim/// for example, \@encode-expression. 128193326Sed/// 129212904SdimExprResult 130193326SedParser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { 131212904Sdim ExprResult LHS(ParseObjCAtExpression(AtLoc)); 132245431Sdim 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 152245431Sdim return ParseRHSOfBinaryExpression(LHS, prec::Comma); 153193326Sed} 154193326Sed 155245431Sdim/// \brief Parse an expr that doesn't include (top-level) commas. 156235633SdimExprResult Parser::ParseAssignmentExpression(TypeCastState isTypeCast) { 157202379Srdivacky if (Tok.is(tok::code_completion)) { 158212904Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 159226890Sdim cutOffParsing(); 160226890Sdim return ExprError(); 161202379Srdivacky } 162202379Srdivacky 163193326Sed if (Tok.is(tok::kw_throw)) 164193326Sed return ParseThrowExpression(); 165193326Sed 166235633Sdim ExprResult LHS = ParseCastExpression(/*isUnaryExpression=*/false, 167235633Sdim /*isAddressOfOperand=*/false, 168235633Sdim isTypeCast); 169245431Sdim return ParseRHSOfBinaryExpression(LHS, prec::Assignment); 170193326Sed} 171193326Sed 172245431Sdim/// \brief Parse an assignment expression where part of an Objective-C message 173245431Sdim/// send has already been parsed. 174193326Sed/// 175245431Sdim/// In this case \p LBracLoc indicates the location of the '[' of the message 176245431Sdim/// send, and either \p ReceiverName or \p ReceiverExpr is non-null indicating 177245431Sdim/// the receiver of the message. 178245431Sdim/// 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 194235633SdimExprResult Parser::ParseConstantExpression(TypeCastState isTypeCast) { 195235633Sdim // 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) [...]. 198235633Sdim // C++98 and C++11 have no such rule, but this is only a defect in C++98. 199194711Sed EnterExpressionEvaluationContext Unevaluated(Actions, 200235633Sdim Sema::ConstantEvaluated); 201198092Srdivacky 202235633Sdim ExprResult LHS(ParseCastExpression(false, false, isTypeCast)); 203235633Sdim ExprResult Res(ParseRHSOfBinaryExpression(LHS, prec::Conditional)); 204235633Sdim return Actions.ActOnConstantExpression(Res); 205193326Sed} 206193326Sed 207245431Sdimbool Parser::isNotExpressionStart() { 208245431Sdim tok::TokenKind K = Tok.getKind(); 209245431Sdim if (K == tok::l_brace || K == tok::r_brace || 210245431Sdim K == tok::kw_for || K == tok::kw_while || 211245431Sdim K == tok::kw_if || K == tok::kw_else || 212245431Sdim K == tok::kw_goto || K == tok::kw_try) 213245431Sdim return true; 214245431Sdim // If this is a decl-specifier, we can't be at the start of an expression. 215245431Sdim return isKnownToBeDeclarationSpecifier(); 216245431Sdim} 217245431Sdim 218245431Sdim/// \brief Parse a binary expression that starts with \p LHS and has a 219245431Sdim/// precedence of at least \p MinPrec. 220212904SdimExprResult 221212904SdimParser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) { 222207619Srdivacky prec::Level NextTokPrec = getBinOpPrecedence(Tok.getKind(), 223207619Srdivacky GreaterThanIsOperator, 224252723Sdim 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) 232245431Sdim return LHS; 233193326Sed 234193326Sed // Consume the operator, saving the operator token for error reporting. 235193326Sed Token OpToken = Tok; 236193326Sed ConsumeToken(); 237193326Sed 238245431Sdim // Bail out when encountering a comma followed by a token which can't 239245431Sdim // possibly be the start of an expression. For instance: 240245431Sdim // int f() { return 1, } 241245431Sdim // We can't do this before consuming the comma, because 242245431Sdim // isNotExpressionStart() looks at the token stream. 243245431Sdim if (OpToken.is(tok::comma) && isNotExpressionStart()) { 244245431Sdim PP.EnterToken(Tok); 245245431Sdim Tok = OpToken; 246245431Sdim return LHS; 247245431Sdim } 248245431Sdim 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 { 276226890Sdim // Otherwise, we're missing a ':'. Assume that this was a typo that 277226890Sdim // the user forgot. If we're not in a macro expansion, we can suggest 278226890Sdim // 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(); 283235633Sdim if (FILoc.isFileID() || PP.isAtStartOfMacroExpansion(FILoc, &FILoc)) { 284235633Sdim assert(FILoc.isFileID()); 285208600Srdivacky bool IsInvalid = false; 286208600Srdivacky const char *SourcePtr = 287226890Sdim SM.getCharacterData(FILoc.getLocWithOffset(-1), &IsInvalid); 288208600Srdivacky if (!IsInvalid && *SourcePtr == ' ') { 289208600Srdivacky SourcePtr = 290226890Sdim SM.getCharacterData(FILoc.getLocWithOffset(-2), &IsInvalid); 291208600Srdivacky if (!IsInvalid && *SourcePtr == ' ') { 292226890Sdim 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()); 309226890Sdim 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 319235633Sdim // an assignment-expression in C++, and in C++11, we can have a 320235633Sdim // braced-init-list on the RHS of an assignment. For better diagnostics, 321235633Sdim // parse as if we were allowed braced-init-lists everywhere, and check that 322235633Sdim // they only appear on the RHS of assignments later. 323212904Sdim ExprResult RHS; 324235633Sdim bool RHSIsInitList = false; 325252723Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 326235633Sdim RHS = ParseBraceInitializer(); 327235633Sdim RHSIsInitList = true; 328235633Sdim } 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, 340252723Sdim 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)) { 350235633Sdim if (!RHS.isInvalid() && RHSIsInitList) { 351235633Sdim Diag(Tok, diag::err_init_list_bin_op) 352235633Sdim << /*LHS*/0 << PP.getSpelling(Tok) << Actions.getExprRange(RHS.get()); 353235633Sdim RHS = ExprError(); 354235633Sdim } 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)); 362235633Sdim RHSIsInitList = false; 363218893Sdim 364193326Sed if (RHS.isInvalid()) 365218893Sdim LHS = ExprError(); 366193326Sed 367193326Sed NextTokPrec = getBinOpPrecedence(Tok.getKind(), GreaterThanIsOperator, 368252723Sdim getLangOpts().CPlusPlus11); 369193326Sed } 370193326Sed assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); 371193326Sed 372235633Sdim if (!RHS.isInvalid() && RHSIsInitList) { 373235633Sdim if (ThisPrec == prec::Assignment) { 374235633Sdim Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists) 375235633Sdim << Actions.getExprRange(RHS.get()); 376235633Sdim } else { 377235633Sdim Diag(OpToken, diag::err_init_list_bin_op) 378235633Sdim << /*RHS*/1 << PP.getSpelling(OpToken) 379235633Sdim << Actions.getExprRange(RHS.get()); 380235633Sdim LHS = ExprError(); 381235633Sdim } 382235633Sdim } 383235633Sdim 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(), 392263509Sdim diag::warn_cxx11_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 406245431Sdim/// \brief Parse a cast-expression, or, if \p isUnaryExpression is true, 407245431Sdim/// parse a unary-expression. 408193326Sed/// 409245431Sdim/// \p isAddressOfOperand exists because an id-expression that is the 410245431Sdim/// operand of address-of gets special treatment due to member pointers. 411245431Sdim/// 412212904SdimExprResult Parser::ParseCastExpression(bool isUnaryExpression, 413221345Sdim bool isAddressOfOperand, 414235633Sdim 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); 422245431Sdim return Res; 423193326Sed} 424193326Sed 425235633Sdimnamespace { 426235633Sdimclass CastExpressionIdValidator : public CorrectionCandidateCallback { 427235633Sdim public: 428235633Sdim CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes) 429235633Sdim : AllowNonTypes(AllowNonTypes) { 430235633Sdim WantTypeSpecifiers = AllowTypes; 431235633Sdim } 432235633Sdim 433235633Sdim virtual bool ValidateCandidate(const TypoCorrection &candidate) { 434235633Sdim NamedDecl *ND = candidate.getCorrectionDecl(); 435235633Sdim if (!ND) 436235633Sdim return candidate.isKeyword(); 437235633Sdim 438235633Sdim if (isa<TypeDecl>(ND)) 439235633Sdim return WantTypeSpecifiers; 440235633Sdim return AllowNonTypes; 441235633Sdim } 442235633Sdim 443235633Sdim private: 444235633Sdim bool AllowNonTypes; 445235633Sdim}; 446235633Sdim} 447235633Sdim 448245431Sdim/// \brief Parse a cast-expression, or, if \pisUnaryExpression is true, parse 449245431Sdim/// a unary-expression. 450193326Sed/// 451245431Sdim/// \p isAddressOfOperand exists because an id-expression that is the operand 452245431Sdim/// of address-of gets special treatment due to member pointers. NotCastExpr 453245431Sdim/// is set to true if the token is not the start of a cast-expression, and no 454245431Sdim/// diagnostic is emitted in this case. 455245431Sdim/// 456245431Sdim/// \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 ')' 468235633Sdim/// [C++11] 'sizeof' '...' '(' identifier ')' 469193326Sed/// [GNU] '__alignof' unary-expression 470193326Sed/// [GNU] '__alignof' '(' type-name ')' 471245431Sdim/// [C11] '_Alignof' '(' type-name ')' 472235633Sdim/// [C++11] 'alignof' '(' type-id ')' 473193326Sed/// [GNU] '&&' identifier 474235633Sdim/// [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] 488235633Sdim/// [C++11] 'nullptr' [C++11 2.14.7] 489235633Sdim/// [C++11] user-defined-literal 490193326Sed/// '(' expression ')' 491235633Sdim/// [C11] generic-selection 492193326Sed/// '__func__' [C99 6.4.2.2] 493193326Sed/// [GNU] '__FUNCTION__' 494263509Sdim/// [MS] '__FUNCDNAME__' 495263509Sdim/// [MS] 'L__FUNCTION__' 496193326Sed/// [GNU] '__PRETTY_FUNCTION__' 497193326Sed/// [GNU] '(' compound-statement ')' 498193326Sed/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 499193326Sed/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 500193326Sed/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 501193326Sed/// assign-expr ')' 502193326Sed/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 503193326Sed/// [GNU] '__null' 504198092Srdivacky/// [OBJC] '[' objc-message-expr ']' 505245431Sdim/// [OBJC] '\@selector' '(' objc-selector-arg ')' 506245431Sdim/// [OBJC] '\@protocol' '(' identifier ')' 507245431Sdim/// [OBJC] '\@encode' '(' type-name ')' 508193326Sed/// [OBJC] objc-string-literal 509193326Sed/// [C++] simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 510235633Sdim/// [C++11] simple-type-specifier braced-init-list [C++11 5.2.3] 511207619Srdivacky/// [C++] typename-specifier '(' expression-list[opt] ')' [C++ 5.2.3] 512235633Sdim/// [C++11] typename-specifier braced-init-list [C++11 5.2.3] 513193326Sed/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 514193326Sed/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 515193326Sed/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 516193326Sed/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 517193326Sed/// [C++] 'typeid' '(' expression ')' [C++ 5.2p1] 518193326Sed/// [C++] 'typeid' '(' type-id ')' [C++ 5.2p1] 519193326Sed/// [C++] 'this' [C++ 9.3.2] 520193326Sed/// [G++] unary-type-trait '(' type-id ')' 521193326Sed/// [G++] binary-type-trait '(' type-id ',' type-id ')' [TODO] 522221345Sdim/// [EMBT] array-type-trait '(' type-id ',' integer ')' 523193326Sed/// [clang] '^' block-literal 524193326Sed/// 525193326Sed/// constant: [C99 6.4.4] 526193326Sed/// integer-constant 527193326Sed/// floating-constant 528193326Sed/// enumeration-constant -> identifier 529193326Sed/// character-constant 530193326Sed/// 531193326Sed/// id-expression: [C++ 5.1] 532193326Sed/// unqualified-id 533207619Srdivacky/// qualified-id 534193326Sed/// 535193326Sed/// unqualified-id: [C++ 5.1] 536193326Sed/// identifier 537193326Sed/// operator-function-id 538207619Srdivacky/// conversion-function-id 539207619Srdivacky/// '~' class-name 540207619Srdivacky/// template-id 541193326Sed/// 542193326Sed/// new-expression: [C++ 5.3.4] 543193326Sed/// '::'[opt] 'new' new-placement[opt] new-type-id 544193326Sed/// new-initializer[opt] 545193326Sed/// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 546193326Sed/// new-initializer[opt] 547193326Sed/// 548193326Sed/// delete-expression: [C++ 5.3.5] 549193326Sed/// '::'[opt] 'delete' cast-expression 550193326Sed/// '::'[opt] 'delete' '[' ']' cast-expression 551193326Sed/// 552221345Sdim/// [GNU/Embarcadero] unary-type-trait: 553221345Sdim/// '__is_arithmetic' 554221345Sdim/// '__is_floating_point' 555221345Sdim/// '__is_integral' 556221345Sdim/// '__is_lvalue_expr' 557221345Sdim/// '__is_rvalue_expr' 558221345Sdim/// '__is_complete_type' 559221345Sdim/// '__is_void' 560221345Sdim/// '__is_array' 561221345Sdim/// '__is_function' 562221345Sdim/// '__is_reference' 563221345Sdim/// '__is_lvalue_reference' 564221345Sdim/// '__is_rvalue_reference' 565221345Sdim/// '__is_fundamental' 566221345Sdim/// '__is_object' 567221345Sdim/// '__is_scalar' 568221345Sdim/// '__is_compound' 569221345Sdim/// '__is_pointer' 570221345Sdim/// '__is_member_object_pointer' 571221345Sdim/// '__is_member_function_pointer' 572221345Sdim/// '__is_member_pointer' 573221345Sdim/// '__is_const' 574221345Sdim/// '__is_volatile' 575221345Sdim/// '__is_trivial' 576221345Sdim/// '__is_standard_layout' 577221345Sdim/// '__is_signed' 578221345Sdim/// '__is_unsigned' 579221345Sdim/// 580193326Sed/// [GNU] unary-type-trait: 581212904Sdim/// '__has_nothrow_assign' 582212904Sdim/// '__has_nothrow_copy' 583212904Sdim/// '__has_nothrow_constructor' 584193326Sed/// '__has_trivial_assign' [TODO] 585193326Sed/// '__has_trivial_copy' [TODO] 586193326Sed/// '__has_trivial_constructor' 587193326Sed/// '__has_trivial_destructor' 588212904Sdim/// '__has_virtual_destructor' 589193326Sed/// '__is_abstract' [TODO] 590193326Sed/// '__is_class' 591193326Sed/// '__is_empty' [TODO] 592193326Sed/// '__is_enum' 593235633Sdim/// '__is_final' 594193326Sed/// '__is_pod' 595193326Sed/// '__is_polymorphic' 596263509Sdim/// '__is_sealed' [MS] 597221345Sdim/// '__is_trivial' 598193326Sed/// '__is_union' 599193326Sed/// 600223017Sdim/// [Clang] unary-type-trait: 601223017Sdim/// '__trivially_copyable' 602223017Sdim/// 603218893Sdim/// binary-type-trait: 604218893Sdim/// [GNU] '__is_base_of' 605218893Sdim/// [MS] '__is_convertible_to' 606221345Sdim/// '__is_convertible' 607221345Sdim/// '__is_same' 608193326Sed/// 609221345Sdim/// [Embarcadero] array-type-trait: 610221345Sdim/// '__array_rank' 611221345Sdim/// '__array_extent' 612221345Sdim/// 613221345Sdim/// [Embarcadero] expression-trait: 614221345Sdim/// '__is_lvalue_expr' 615221345Sdim/// '__is_rvalue_expr' 616245431Sdim/// \endverbatim 617221345Sdim/// 618212904SdimExprResult Parser::ParseCastExpression(bool isUnaryExpression, 619218893Sdim bool isAddressOfOperand, 620218893Sdim bool &NotCastExpr, 621235633Sdim TypeCastState isTypeCast) { 622212904Sdim ExprResult Res; 623193326Sed tok::TokenKind SavedKind = Tok.getKind(); 624193326Sed NotCastExpr = false; 625198092Srdivacky 626193326Sed // This handles all of cast-expression, unary-expression, postfix-expression, 627193326Sed // and primary-expression. We handle them together like this for efficiency 628193326Sed // and to simplify handling of an expression starting with a '(' token: which 629193326Sed // may be one of a parenthesized expression, cast-expression, compound literal 630193326Sed // expression, or statement expression. 631193326Sed // 632193326Sed // If the parsed tokens consist of a primary-expression, the cases below 633212904Sdim // break out of the switch; at the end we call ParsePostfixExpressionSuffix 634212904Sdim // to handle the postfix expression suffixes. Cases that cannot be followed 635212904Sdim // by postfix exprs should return without invoking 636212904Sdim // ParsePostfixExpressionSuffix. 637193326Sed switch (SavedKind) { 638193326Sed case tok::l_paren: { 639193326Sed // If this expression is limited to being a unary-expression, the parent can 640193326Sed // not start a cast expression. 641193326Sed ParenParseOption ParenExprType = 642235633Sdim (isUnaryExpression && !getLangOpts().CPlusPlus)? CompoundLiteral : CastExpr; 643212904Sdim ParsedType CastTy; 644193326Sed SourceLocation RParenLoc; 645200583Srdivacky 646200583Srdivacky { 647218893Sdim // The inside of the parens don't need to be a colon protected scope, and 648218893Sdim // isn't immediately a message send. 649200583Srdivacky ColonProtectionRAIIObject X(*this, false); 650218893Sdim 651200583Srdivacky Res = ParseParenExpression(ParenExprType, false/*stopIfCastExr*/, 652235633Sdim isTypeCast == IsTypeCast, CastTy, RParenLoc); 653200583Srdivacky } 654198092Srdivacky 655193326Sed switch (ParenExprType) { 656193326Sed case SimpleExpr: break; // Nothing else to do. 657193326Sed case CompoundStmt: break; // Nothing else to do. 658193326Sed case CompoundLiteral: 659193326Sed // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of 660193326Sed // postfix-expression exist, parse them now. 661193326Sed break; 662193326Sed case CastExpr: 663193326Sed // We have parsed the cast-expression and no postfix-expr pieces are 664193326Sed // following. 665245431Sdim return Res; 666193326Sed } 667193326Sed 668212904Sdim break; 669193326Sed } 670193326Sed 671193326Sed // primary-expression 672193326Sed case tok::numeric_constant: 673193326Sed // constant: integer-constant 674193326Sed // constant: floating-constant 675193326Sed 676235633Sdim Res = Actions.ActOnNumericConstant(Tok, /*UDLScope*/getCurScope()); 677193326Sed ConsumeToken(); 678212904Sdim break; 679193326Sed 680193326Sed case tok::kw_true: 681193326Sed case tok::kw_false: 682193326Sed return ParseCXXBoolLiteral(); 683235633Sdim 684235633Sdim case tok::kw___objc_yes: 685235633Sdim case tok::kw___objc_no: 686235633Sdim return ParseObjCBoolLiteral(); 687193326Sed 688193326Sed case tok::kw_nullptr: 689235633Sdim Diag(Tok, diag::warn_cxx98_compat_nullptr); 690193326Sed return Actions.ActOnCXXNullPtrLiteral(ConsumeToken()); 691193326Sed 692221345Sdim case tok::annot_primary_expr: 693221345Sdim assert(Res.get() == 0 && "Stray primary-expression annotation?"); 694221345Sdim Res = getExprAnnotation(Tok); 695221345Sdim ConsumeToken(); 696221345Sdim break; 697252723Sdim 698252723Sdim case tok::kw_decltype: 699252723Sdim // Annotate the token and tail recurse. 700252723Sdim if (TryAnnotateTypeOrScopeToken()) 701252723Sdim return ExprError(); 702252723Sdim assert(Tok.isNot(tok::kw_decltype)); 703252723Sdim return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 704221345Sdim 705193326Sed case tok::identifier: { // primary-expression: identifier 706193326Sed // unqualified-id: identifier 707193326Sed // constant: enumeration-constant 708193326Sed // Turn a potentially qualified name into a annot_typename or 709193326Sed // annot_cxxscope if it would be valid. This handles things like x::y, etc. 710235633Sdim if (getLangOpts().CPlusPlus) { 711202379Srdivacky // Avoid the unnecessary parse-time lookup in the common case 712202379Srdivacky // where the syntax forbids a type. 713202379Srdivacky const Token &Next = NextToken(); 714245431Sdim 715245431Sdim // If this identifier was reverted from a token ID, and the next token 716245431Sdim // is a parenthesis, this is likely to be a use of a type trait. Check 717245431Sdim // those tokens. 718245431Sdim if (Next.is(tok::l_paren) && 719245431Sdim Tok.is(tok::identifier) && 720245431Sdim Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) { 721245431Sdim IdentifierInfo *II = Tok.getIdentifierInfo(); 722245431Sdim // Build up the mapping of revertable type traits, for future use. 723245431Sdim if (RevertableTypeTraits.empty()) { 724245431Sdim#define RTT_JOIN(X,Y) X##Y 725245431Sdim#define REVERTABLE_TYPE_TRAIT(Name) \ 726245431Sdim RevertableTypeTraits[PP.getIdentifierInfo(#Name)] \ 727245431Sdim = RTT_JOIN(tok::kw_,Name) 728245431Sdim 729245431Sdim REVERTABLE_TYPE_TRAIT(__is_arithmetic); 730245431Sdim REVERTABLE_TYPE_TRAIT(__is_convertible); 731245431Sdim REVERTABLE_TYPE_TRAIT(__is_empty); 732245431Sdim REVERTABLE_TYPE_TRAIT(__is_floating_point); 733245431Sdim REVERTABLE_TYPE_TRAIT(__is_function); 734245431Sdim REVERTABLE_TYPE_TRAIT(__is_fundamental); 735245431Sdim REVERTABLE_TYPE_TRAIT(__is_integral); 736245431Sdim REVERTABLE_TYPE_TRAIT(__is_member_function_pointer); 737245431Sdim REVERTABLE_TYPE_TRAIT(__is_member_pointer); 738245431Sdim REVERTABLE_TYPE_TRAIT(__is_pod); 739245431Sdim REVERTABLE_TYPE_TRAIT(__is_pointer); 740245431Sdim REVERTABLE_TYPE_TRAIT(__is_same); 741245431Sdim REVERTABLE_TYPE_TRAIT(__is_scalar); 742245431Sdim REVERTABLE_TYPE_TRAIT(__is_signed); 743245431Sdim REVERTABLE_TYPE_TRAIT(__is_unsigned); 744245431Sdim REVERTABLE_TYPE_TRAIT(__is_void); 745245431Sdim#undef REVERTABLE_TYPE_TRAIT 746245431Sdim#undef RTT_JOIN 747263509Sdim } 748245431Sdim 749263509Sdim // If we find that this is in fact the name of a type trait, 750263509Sdim // update the token kind in place and parse again to treat it as 751263509Sdim // the appropriate kind of type trait. 752263509Sdim llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known 753263509Sdim = RevertableTypeTraits.find(II); 754263509Sdim if (Known != RevertableTypeTraits.end()) { 755263509Sdim Tok.setKind(Known->second); 756263509Sdim return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 757263509Sdim NotCastExpr, isTypeCast); 758245431Sdim } 759263509Sdim } 760245431Sdim 761202379Srdivacky if (Next.is(tok::coloncolon) || 762202379Srdivacky (!ColonIsSacred && Next.is(tok::colon)) || 763202379Srdivacky Next.is(tok::less) || 764235633Sdim Next.is(tok::l_paren) || 765235633Sdim Next.is(tok::l_brace)) { 766202379Srdivacky // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 767202379Srdivacky if (TryAnnotateTypeOrScopeToken()) 768204643Srdivacky return ExprError(); 769204643Srdivacky if (!Tok.is(tok::identifier)) 770202379Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 771202379Srdivacky } 772193326Sed } 773193326Sed 774198893Srdivacky // Consume the identifier so that we can see if it is followed by a '(' or 775198893Srdivacky // '.'. 776198893Srdivacky IdentifierInfo &II = *Tok.getIdentifierInfo(); 777198893Srdivacky SourceLocation ILoc = ConsumeToken(); 778245431Sdim 779207619Srdivacky // Support 'Class.property' and 'super.property' notation. 780235633Sdim if (getLangOpts().ObjC1 && Tok.is(tok::period) && 781210299Sed (Actions.getTypeName(II, ILoc, getCurScope()) || 782207619Srdivacky // Allow the base to be 'super' if in an objc-method. 783210299Sed (&II == Ident_super && getCurScope()->isInObjcMethodScope()))) { 784218893Sdim ConsumeToken(); 785198893Srdivacky 786235633Sdim // Allow either an identifier or the keyword 'class' (in C++). 787235633Sdim if (Tok.isNot(tok::identifier) && 788235633Sdim !(getLangOpts().CPlusPlus && Tok.is(tok::kw_class))) { 789198893Srdivacky Diag(Tok, diag::err_expected_property_name); 790193326Sed return ExprError(); 791193326Sed } 792193326Sed IdentifierInfo &PropertyName = *Tok.getIdentifierInfo(); 793193326Sed SourceLocation PropertyLoc = ConsumeToken(); 794198893Srdivacky 795198893Srdivacky Res = Actions.ActOnClassPropertyRefExpr(II, PropertyName, 796198893Srdivacky ILoc, PropertyLoc); 797212904Sdim break; 798193326Sed } 799212904Sdim 800218893Sdim // In an Objective-C method, if we have "super" followed by an identifier, 801218893Sdim // the token sequence is ill-formed. However, if there's a ':' or ']' after 802218893Sdim // that identifier, this is probably a message send with a missing open 803218893Sdim // bracket. Treat it as such. 804235633Sdim if (getLangOpts().ObjC1 && &II == Ident_super && !InMessageExpression && 805218893Sdim getCurScope()->isInObjcMethodScope() && 806218893Sdim ((Tok.is(tok::identifier) && 807218893Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) || 808218893Sdim Tok.is(tok::code_completion))) { 809218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), ILoc, ParsedType(), 810218893Sdim 0); 811218893Sdim break; 812218893Sdim } 813218893Sdim 814218893Sdim // If we have an Objective-C class name followed by an identifier 815218893Sdim // and either ':' or ']', this is an Objective-C class message 816218893Sdim // send that's missing the opening '['. Recovery 817218893Sdim // appropriately. Also take this path if we're performing code 818218893Sdim // completion after an Objective-C class name. 819235633Sdim if (getLangOpts().ObjC1 && 820218893Sdim ((Tok.is(tok::identifier) && !InMessageExpression) || 821218893Sdim Tok.is(tok::code_completion))) { 822218893Sdim const Token& Next = NextToken(); 823218893Sdim if (Tok.is(tok::code_completion) || 824218893Sdim Next.is(tok::colon) || Next.is(tok::r_square)) 825218893Sdim if (ParsedType Typ = Actions.getTypeName(II, ILoc, getCurScope())) 826218893Sdim if (Typ.get()->isObjCObjectOrInterfaceType()) { 827218893Sdim // Fake up a Declarator to use with ActOnTypeName. 828221345Sdim DeclSpec DS(AttrFactory); 829218893Sdim DS.SetRangeStart(ILoc); 830218893Sdim DS.SetRangeEnd(ILoc); 831218893Sdim const char *PrevSpec = 0; 832218893Sdim unsigned DiagID; 833218893Sdim DS.SetTypeSpecType(TST_typename, ILoc, PrevSpec, DiagID, Typ); 834218893Sdim 835218893Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 836218893Sdim TypeResult Ty = Actions.ActOnTypeName(getCurScope(), 837218893Sdim DeclaratorInfo); 838218893Sdim if (Ty.isInvalid()) 839218893Sdim break; 840218893Sdim 841218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), 842218893Sdim SourceLocation(), 843218893Sdim Ty.get(), 0); 844218893Sdim break; 845218893Sdim } 846218893Sdim } 847218893Sdim 848212904Sdim // Make sure to pass down the right value for isAddressOfOperand. 849212904Sdim if (isAddressOfOperand && isPostfixExpressionSuffixStart()) 850212904Sdim isAddressOfOperand = false; 851198893Srdivacky 852193326Sed // Function designators are allowed to be undeclared (C99 6.5.1p2), so we 853193326Sed // need to know whether or not this identifier is a function designator or 854193326Sed // not. 855198893Srdivacky UnqualifiedId Name; 856198893Srdivacky CXXScopeSpec ScopeSpec; 857235633Sdim SourceLocation TemplateKWLoc; 858235633Sdim CastExpressionIdValidator Validator(isTypeCast != NotTypeCast, 859235633Sdim isTypeCast != IsTypeCast); 860198893Srdivacky Name.setIdentifier(&II, ILoc); 861235633Sdim Res = Actions.ActOnIdExpression(getCurScope(), ScopeSpec, TemplateKWLoc, 862235633Sdim Name, Tok.is(tok::l_paren), 863235633Sdim isAddressOfOperand, &Validator); 864212904Sdim break; 865193326Sed } 866193326Sed case tok::char_constant: // constant: character-constant 867226890Sdim case tok::wide_char_constant: 868226890Sdim case tok::utf16_char_constant: 869226890Sdim case tok::utf32_char_constant: 870235633Sdim Res = Actions.ActOnCharacterConstant(Tok, /*UDLScope*/getCurScope()); 871193326Sed ConsumeToken(); 872212904Sdim break; 873193326Sed case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] 874193326Sed case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] 875263509Sdim case tok::kw___FUNCDNAME__: // primary-expression: __FUNCDNAME__ [MS] 876245431Sdim case tok::kw_L__FUNCTION__: // primary-expression: L__FUNCTION__ [MS] 877193326Sed case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] 878193326Sed Res = Actions.ActOnPredefinedExpr(Tok.getLocation(), SavedKind); 879193326Sed ConsumeToken(); 880212904Sdim break; 881193326Sed case tok::string_literal: // primary-expression: string-literal 882193326Sed case tok::wide_string_literal: 883226890Sdim case tok::utf8_string_literal: 884226890Sdim case tok::utf16_string_literal: 885226890Sdim case tok::utf32_string_literal: 886235633Sdim Res = ParseStringLiteralExpression(true); 887212904Sdim break; 888235633Sdim case tok::kw__Generic: // primary-expression: generic-selection [C11 6.5.1] 889221345Sdim Res = ParseGenericSelectionExpression(); 890221345Sdim break; 891193326Sed case tok::kw___builtin_va_arg: 892193326Sed case tok::kw___builtin_offsetof: 893193326Sed case tok::kw___builtin_choose_expr: 894223017Sdim case tok::kw___builtin_astype: // primary-expression: [OCL] as_type() 895263509Sdim case tok::kw___builtin_convertvector: 896193326Sed return ParseBuiltinPrimaryExpression(); 897193326Sed case tok::kw___null: 898193326Sed return Actions.ActOnGNUNullExpr(ConsumeToken()); 899224145Sdim 900212904Sdim case tok::plusplus: // unary-expression: '++' unary-expression [C99] 901212904Sdim case tok::minusminus: { // unary-expression: '--' unary-expression [C99] 902212904Sdim // C++ [expr.unary] has: 903212904Sdim // unary-expression: 904212904Sdim // ++ cast-expression 905212904Sdim // -- cast-expression 906193326Sed SourceLocation SavedLoc = ConsumeToken(); 907235633Sdim Res = ParseCastExpression(!getLangOpts().CPlusPlus); 908193326Sed if (!Res.isInvalid()) 909212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 910245431Sdim return Res; 911193326Sed } 912193326Sed case tok::amp: { // unary-expression: '&' cast-expression 913193326Sed // Special treatment because of member pointers 914193326Sed SourceLocation SavedLoc = ConsumeToken(); 915193326Sed Res = ParseCastExpression(false, true); 916193326Sed if (!Res.isInvalid()) 917212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 918245431Sdim return Res; 919193326Sed } 920193326Sed 921193326Sed case tok::star: // unary-expression: '*' cast-expression 922193326Sed case tok::plus: // unary-expression: '+' cast-expression 923193326Sed case tok::minus: // unary-expression: '-' cast-expression 924193326Sed case tok::tilde: // unary-expression: '~' cast-expression 925193326Sed case tok::exclaim: // unary-expression: '!' cast-expression 926193326Sed case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] 927193326Sed case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] 928193326Sed SourceLocation SavedLoc = ConsumeToken(); 929193326Sed Res = ParseCastExpression(false); 930193326Sed if (!Res.isInvalid()) 931212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 932245431Sdim return Res; 933193326Sed } 934193326Sed 935193326Sed case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] 936193326Sed // __extension__ silences extension warnings in the subexpression. 937193326Sed ExtensionRAIIObject O(Diags); // Use RAII to do this. 938193326Sed SourceLocation SavedLoc = ConsumeToken(); 939193326Sed Res = ParseCastExpression(false); 940193326Sed if (!Res.isInvalid()) 941212904Sdim Res = Actions.ActOnUnaryOp(getCurScope(), SavedLoc, SavedKind, Res.get()); 942245431Sdim return Res; 943193326Sed } 944245431Sdim case tok::kw__Alignof: // unary-expression: '_Alignof' '(' type-name ')' 945245431Sdim if (!getLangOpts().C11) 946245431Sdim Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); 947245431Sdim // fallthrough 948245431Sdim case tok::kw_alignof: // unary-expression: 'alignof' '(' type-id ')' 949245431Sdim case tok::kw___alignof: // unary-expression: '__alignof' unary-expression 950245431Sdim // unary-expression: '__alignof' '(' type-name ')' 951193326Sed case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression 952193326Sed // unary-expression: 'sizeof' '(' type-name ')' 953221345Sdim case tok::kw_vec_step: // unary-expression: OpenCL 'vec_step' expression 954221345Sdim return ParseUnaryExprOrTypeTraitExpression(); 955193326Sed case tok::ampamp: { // unary-expression: '&&' identifier 956193326Sed SourceLocation AmpAmpLoc = ConsumeToken(); 957193326Sed if (Tok.isNot(tok::identifier)) 958193326Sed return ExprError(Diag(Tok, diag::err_expected_ident)); 959193326Sed 960218893Sdim if (getCurScope()->getFnParent() == 0) 961218893Sdim return ExprError(Diag(Tok, diag::err_address_of_label_outside_fn)); 962218893Sdim 963193326Sed Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); 964218893Sdim LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), 965218893Sdim Tok.getLocation()); 966218893Sdim Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), LD); 967193326Sed ConsumeToken(); 968245431Sdim return Res; 969193326Sed } 970193326Sed case tok::kw_const_cast: 971193326Sed case tok::kw_dynamic_cast: 972193326Sed case tok::kw_reinterpret_cast: 973193326Sed case tok::kw_static_cast: 974193326Sed Res = ParseCXXCasts(); 975212904Sdim break; 976193326Sed case tok::kw_typeid: 977193326Sed Res = ParseCXXTypeid(); 978212904Sdim break; 979218893Sdim case tok::kw___uuidof: 980218893Sdim Res = ParseCXXUuidof(); 981218893Sdim break; 982193326Sed case tok::kw_this: 983193326Sed Res = ParseCXXThis(); 984212904Sdim break; 985193326Sed 986218893Sdim case tok::annot_typename: 987218893Sdim if (isStartOfObjCClassMessageMissingOpenBracket()) { 988218893Sdim ParsedType Type = getTypeAnnotation(Tok); 989252723Sdim 990218893Sdim // Fake up a Declarator to use with ActOnTypeName. 991221345Sdim DeclSpec DS(AttrFactory); 992218893Sdim DS.SetRangeStart(Tok.getLocation()); 993218893Sdim DS.SetRangeEnd(Tok.getLastLoc()); 994218893Sdim 995218893Sdim const char *PrevSpec = 0; 996218893Sdim unsigned DiagID; 997218893Sdim DS.SetTypeSpecType(TST_typename, Tok.getAnnotationEndLoc(), 998218893Sdim PrevSpec, DiagID, Type); 999252723Sdim 1000218893Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 1001218893Sdim TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 1002218893Sdim if (Ty.isInvalid()) 1003218893Sdim break; 1004218893Sdim 1005218893Sdim ConsumeToken(); 1006218893Sdim Res = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), 1007218893Sdim Ty.get(), 0); 1008218893Sdim break; 1009218893Sdim } 1010218893Sdim // Fall through 1011252723Sdim 1012235633Sdim case tok::annot_decltype: 1013193326Sed case tok::kw_char: 1014193326Sed case tok::kw_wchar_t: 1015198092Srdivacky case tok::kw_char16_t: 1016198092Srdivacky case tok::kw_char32_t: 1017193326Sed case tok::kw_bool: 1018193326Sed case tok::kw_short: 1019193326Sed case tok::kw_int: 1020193326Sed case tok::kw_long: 1021221345Sdim case tok::kw___int64: 1022235633Sdim case tok::kw___int128: 1023193326Sed case tok::kw_signed: 1024193326Sed case tok::kw_unsigned: 1025226890Sdim case tok::kw_half: 1026193326Sed case tok::kw_float: 1027193326Sed case tok::kw_double: 1028193326Sed case tok::kw_void: 1029193326Sed case tok::kw_typename: 1030193326Sed case tok::kw_typeof: 1031252723Sdim case tok::kw___vector: 1032252723Sdim case tok::kw_image1d_t: 1033252723Sdim case tok::kw_image1d_array_t: 1034252723Sdim case tok::kw_image1d_buffer_t: 1035252723Sdim case tok::kw_image2d_t: 1036252723Sdim case tok::kw_image2d_array_t: 1037252723Sdim case tok::kw_image3d_t: 1038252723Sdim case tok::kw_sampler_t: 1039252723Sdim case tok::kw_event_t: { 1040235633Sdim if (!getLangOpts().CPlusPlus) { 1041193326Sed Diag(Tok, diag::err_expected_expression); 1042193326Sed return ExprError(); 1043193326Sed } 1044194179Sed 1045194179Sed if (SavedKind == tok::kw_typename) { 1046194179Sed // postfix-expression: typename-specifier '(' expression-list[opt] ')' 1047223017Sdim // typename-specifier braced-init-list 1048204643Srdivacky if (TryAnnotateTypeOrScopeToken()) 1049194179Sed return ExprError(); 1050263509Sdim 1051263509Sdim if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) 1052263509Sdim // We are trying to parse a simple-type-specifier but might not get such 1053263509Sdim // a token after error recovery. 1054263509Sdim return ExprError(); 1055194179Sed } 1056194179Sed 1057193326Sed // postfix-expression: simple-type-specifier '(' expression-list[opt] ')' 1058223017Sdim // simple-type-specifier braced-init-list 1059193326Sed // 1060221345Sdim DeclSpec DS(AttrFactory); 1061263509Sdim 1062193326Sed ParseCXXSimpleTypeSpecifier(DS); 1063223017Sdim if (Tok.isNot(tok::l_paren) && 1064252723Sdim (!getLangOpts().CPlusPlus11 || Tok.isNot(tok::l_brace))) 1065193326Sed return ExprError(Diag(Tok, diag::err_expected_lparen_after_type) 1066193326Sed << DS.getSourceRange()); 1067193326Sed 1068235633Sdim if (Tok.is(tok::l_brace)) 1069235633Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 1070235633Sdim 1071193326Sed Res = ParseCXXTypeConstructExpression(DS); 1072212904Sdim break; 1073193326Sed } 1074193326Sed 1075203955Srdivacky case tok::annot_cxxscope: { // [C++] id-expression: qualified-id 1076207619Srdivacky // If TryAnnotateTypeOrScopeToken annotates the token, tail recurse. 1077207619Srdivacky // (We can end up in this situation after tentative parsing.) 1078207619Srdivacky if (TryAnnotateTypeOrScopeToken()) 1079207619Srdivacky return ExprError(); 1080207619Srdivacky if (!Tok.is(tok::annot_cxxscope)) 1081207619Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1082224145Sdim NotCastExpr, isTypeCast); 1083207619Srdivacky 1084203955Srdivacky Token Next = NextToken(); 1085203955Srdivacky if (Next.is(tok::annot_template_id)) { 1086224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); 1087203955Srdivacky if (TemplateId->Kind == TNK_Type_template) { 1088203955Srdivacky // We have a qualified template-id that we know refers to a 1089203955Srdivacky // type, translate it into a type and continue parsing as a 1090203955Srdivacky // cast expression. 1091203955Srdivacky CXXScopeSpec SS; 1092235633Sdim ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 1093235633Sdim /*EnteringContext=*/false); 1094221345Sdim AnnotateTemplateIdTokenAsType(); 1095203955Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1096224145Sdim NotCastExpr, isTypeCast); 1097203955Srdivacky } 1098203955Srdivacky } 1099203955Srdivacky 1100203955Srdivacky // Parse as an id-expression. 1101203955Srdivacky Res = ParseCXXIdExpression(isAddressOfOperand); 1102212904Sdim break; 1103203955Srdivacky } 1104203955Srdivacky 1105203955Srdivacky case tok::annot_template_id: { // [C++] template-id 1106224145Sdim TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1107203955Srdivacky if (TemplateId->Kind == TNK_Type_template) { 1108203955Srdivacky // We have a template-id that we know refers to a type, 1109203955Srdivacky // translate it into a type and continue parsing as a cast 1110203955Srdivacky // expression. 1111203955Srdivacky AnnotateTemplateIdTokenAsType(); 1112203955Srdivacky return ParseCastExpression(isUnaryExpression, isAddressOfOperand, 1113224145Sdim NotCastExpr, isTypeCast); 1114203955Srdivacky } 1115203955Srdivacky 1116203955Srdivacky // Fall through to treat the template-id as an id-expression. 1117203955Srdivacky } 1118203955Srdivacky 1119193326Sed case tok::kw_operator: // [C++] id-expression: operator/conversion-function-id 1120193326Sed Res = ParseCXXIdExpression(isAddressOfOperand); 1121212904Sdim break; 1122193326Sed 1123193326Sed case tok::coloncolon: { 1124193326Sed // ::foo::bar -> global qualified name etc. If TryAnnotateTypeOrScopeToken 1125193326Sed // annotates the token, tail recurse. 1126193326Sed if (TryAnnotateTypeOrScopeToken()) 1127204643Srdivacky return ExprError(); 1128204643Srdivacky if (!Tok.is(tok::coloncolon)) 1129193326Sed return ParseCastExpression(isUnaryExpression, isAddressOfOperand); 1130193326Sed 1131193326Sed // ::new -> [C++] new-expression 1132193326Sed // ::delete -> [C++] delete-expression 1133193326Sed SourceLocation CCLoc = ConsumeToken(); 1134193326Sed if (Tok.is(tok::kw_new)) 1135193326Sed return ParseCXXNewExpression(true, CCLoc); 1136193326Sed if (Tok.is(tok::kw_delete)) 1137193326Sed return ParseCXXDeleteExpression(true, CCLoc); 1138198092Srdivacky 1139193326Sed // This is not a type name or scope specifier, it is an invalid expression. 1140193326Sed Diag(CCLoc, diag::err_expected_expression); 1141193326Sed return ExprError(); 1142193326Sed } 1143193326Sed 1144193326Sed case tok::kw_new: // [C++] new-expression 1145193326Sed return ParseCXXNewExpression(false, Tok.getLocation()); 1146193326Sed 1147193326Sed case tok::kw_delete: // [C++] delete-expression 1148193326Sed return ParseCXXDeleteExpression(false, Tok.getLocation()); 1149193326Sed 1150218893Sdim case tok::kw_noexcept: { // [C++0x] 'noexcept' '(' expression ')' 1151235633Sdim Diag(Tok, diag::warn_cxx98_compat_noexcept_expr); 1152218893Sdim SourceLocation KeyLoc = ConsumeToken(); 1153226890Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 1154226890Sdim 1155226890Sdim if (T.expectAndConsume(diag::err_expected_lparen_after, "noexcept")) 1156218893Sdim return ExprError(); 1157235633Sdim // C++11 [expr.unary.noexcept]p1: 1158218893Sdim // The noexcept operator determines whether the evaluation of its operand, 1159218893Sdim // which is an unevaluated operand, can throw an exception. 1160218893Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 1161218893Sdim ExprResult Result = ParseExpression(); 1162226890Sdim 1163226890Sdim T.consumeClose(); 1164226890Sdim 1165218893Sdim if (!Result.isInvalid()) 1166226890Sdim Result = Actions.ActOnNoexceptExpr(KeyLoc, T.getOpenLocation(), 1167226890Sdim Result.take(), T.getCloseLocation()); 1168245431Sdim return Result; 1169218893Sdim } 1170218893Sdim 1171221345Sdim case tok::kw___is_abstract: // [GNU] unary-type-trait 1172193326Sed case tok::kw___is_class: 1173221345Sdim case tok::kw___is_empty: 1174193326Sed case tok::kw___is_enum: 1175245431Sdim case tok::kw___is_interface_class: 1176221345Sdim case tok::kw___is_literal: 1177221345Sdim case tok::kw___is_arithmetic: 1178221345Sdim case tok::kw___is_integral: 1179221345Sdim case tok::kw___is_floating_point: 1180221345Sdim case tok::kw___is_complete_type: 1181221345Sdim case tok::kw___is_void: 1182221345Sdim case tok::kw___is_array: 1183221345Sdim case tok::kw___is_function: 1184221345Sdim case tok::kw___is_reference: 1185221345Sdim case tok::kw___is_lvalue_reference: 1186221345Sdim case tok::kw___is_rvalue_reference: 1187221345Sdim case tok::kw___is_fundamental: 1188221345Sdim case tok::kw___is_object: 1189221345Sdim case tok::kw___is_scalar: 1190221345Sdim case tok::kw___is_compound: 1191221345Sdim case tok::kw___is_pointer: 1192221345Sdim case tok::kw___is_member_object_pointer: 1193221345Sdim case tok::kw___is_member_function_pointer: 1194221345Sdim case tok::kw___is_member_pointer: 1195221345Sdim case tok::kw___is_const: 1196221345Sdim case tok::kw___is_volatile: 1197221345Sdim case tok::kw___is_standard_layout: 1198221345Sdim case tok::kw___is_signed: 1199221345Sdim case tok::kw___is_unsigned: 1200221345Sdim case tok::kw___is_literal_type: 1201221345Sdim case tok::kw___is_pod: 1202221345Sdim case tok::kw___is_polymorphic: 1203221345Sdim case tok::kw___is_trivial: 1204223017Sdim case tok::kw___is_trivially_copyable: 1205193326Sed case tok::kw___is_union: 1206235633Sdim case tok::kw___is_final: 1207263509Sdim case tok::kw___is_sealed: 1208193326Sed case tok::kw___has_trivial_constructor: 1209252723Sdim case tok::kw___has_trivial_move_constructor: 1210198092Srdivacky case tok::kw___has_trivial_copy: 1211198092Srdivacky case tok::kw___has_trivial_assign: 1212252723Sdim case tok::kw___has_trivial_move_assign: 1213193326Sed case tok::kw___has_trivial_destructor: 1214212904Sdim case tok::kw___has_nothrow_assign: 1215252723Sdim case tok::kw___has_nothrow_move_assign: 1216212904Sdim case tok::kw___has_nothrow_copy: 1217212904Sdim case tok::kw___has_nothrow_constructor: 1218212904Sdim case tok::kw___has_virtual_destructor: 1219193326Sed return ParseUnaryTypeTrait(); 1220193326Sed 1221218893Sdim case tok::kw___builtin_types_compatible_p: 1222218893Sdim case tok::kw___is_base_of: 1223221345Sdim case tok::kw___is_same: 1224221345Sdim case tok::kw___is_convertible: 1225218893Sdim case tok::kw___is_convertible_to: 1226235633Sdim case tok::kw___is_trivially_assignable: 1227218893Sdim return ParseBinaryTypeTrait(); 1228218893Sdim 1229235633Sdim case tok::kw___is_trivially_constructible: 1230235633Sdim return ParseTypeTrait(); 1231235633Sdim 1232221345Sdim case tok::kw___array_rank: 1233221345Sdim case tok::kw___array_extent: 1234221345Sdim return ParseArrayTypeTrait(); 1235221345Sdim 1236221345Sdim case tok::kw___is_lvalue_expr: 1237221345Sdim case tok::kw___is_rvalue_expr: 1238221345Sdim return ParseExpressionTrait(); 1239221345Sdim 1240193326Sed case tok::at: { 1241193326Sed SourceLocation AtLoc = ConsumeToken(); 1242193326Sed return ParseObjCAtExpression(AtLoc); 1243193326Sed } 1244193326Sed case tok::caret: 1245224145Sdim Res = ParseBlockLiteralExpression(); 1246224145Sdim break; 1247224145Sdim case tok::code_completion: { 1248212904Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 1249226890Sdim cutOffParsing(); 1250226890Sdim return ExprError(); 1251224145Sdim } 1252193326Sed case tok::l_square: 1253252723Sdim if (getLangOpts().CPlusPlus11) { 1254235633Sdim if (getLangOpts().ObjC1) { 1255235633Sdim // C++11 lambda expressions and Objective-C message sends both start with a 1256235633Sdim // square bracket. There are three possibilities here: 1257235633Sdim // we have a valid lambda expression, we have an invalid lambda 1258235633Sdim // expression, or we have something that doesn't appear to be a lambda. 1259235633Sdim // If we're in the last case, we fall back to ParseObjCMessageExpression. 1260226890Sdim Res = TryParseLambdaExpression(); 1261235633Sdim if (!Res.isInvalid() && !Res.get()) 1262226890Sdim Res = ParseObjCMessageExpression(); 1263226890Sdim break; 1264226890Sdim } 1265226890Sdim Res = ParseLambdaExpression(); 1266226890Sdim break; 1267226890Sdim } 1268235633Sdim if (getLangOpts().ObjC1) { 1269224145Sdim Res = ParseObjCMessageExpression(); 1270224145Sdim break; 1271224145Sdim } 1272224145Sdim // FALL THROUGH. 1273193326Sed default: 1274193326Sed NotCastExpr = true; 1275193326Sed return ExprError(); 1276193326Sed } 1277193326Sed 1278212904Sdim // These can be followed by postfix-expr pieces. 1279218893Sdim return ParsePostfixExpressionSuffix(Res); 1280193326Sed} 1281193326Sed 1282245431Sdim/// \brief Once the leading part of a postfix-expression is parsed, this 1283245431Sdim/// method parses any suffixes that apply. 1284193326Sed/// 1285245431Sdim/// \verbatim 1286193326Sed/// postfix-expression: [C99 6.5.2] 1287193326Sed/// primary-expression 1288193326Sed/// postfix-expression '[' expression ']' 1289223017Sdim/// postfix-expression '[' braced-init-list ']' 1290193326Sed/// postfix-expression '(' argument-expression-list[opt] ')' 1291193326Sed/// postfix-expression '.' identifier 1292193326Sed/// postfix-expression '->' identifier 1293193326Sed/// postfix-expression '++' 1294193326Sed/// postfix-expression '--' 1295193326Sed/// '(' type-name ')' '{' initializer-list '}' 1296193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 1297193326Sed/// 1298193326Sed/// argument-expression-list: [C99 6.5.2] 1299218893Sdim/// argument-expression ...[opt] 1300218893Sdim/// argument-expression-list ',' assignment-expression ...[opt] 1301245431Sdim/// \endverbatim 1302212904SdimExprResult 1303212904SdimParser::ParsePostfixExpressionSuffix(ExprResult LHS) { 1304193326Sed // Now that the primary-expression piece of the postfix-expression has been 1305193326Sed // parsed, see if there are any postfix-expression pieces here. 1306193326Sed SourceLocation Loc; 1307193326Sed while (1) { 1308193326Sed switch (Tok.getKind()) { 1309218893Sdim case tok::code_completion: 1310218893Sdim if (InMessageExpression) 1311245431Sdim return LHS; 1312218893Sdim 1313218893Sdim Actions.CodeCompletePostfixExpression(getCurScope(), LHS); 1314226890Sdim cutOffParsing(); 1315226890Sdim return ExprError(); 1316218893Sdim 1317218893Sdim case tok::identifier: 1318218893Sdim // If we see identifier: after an expression, and we're not already in a 1319218893Sdim // message send, then this is probably a message send with a missing 1320218893Sdim // opening bracket '['. 1321235633Sdim if (getLangOpts().ObjC1 && !InMessageExpression && 1322218893Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { 1323218893Sdim LHS = ParseObjCMessageExpressionBody(SourceLocation(), SourceLocation(), 1324218893Sdim ParsedType(), LHS.get()); 1325218893Sdim break; 1326218893Sdim } 1327218893Sdim 1328218893Sdim // Fall through; this isn't a message send. 1329218893Sdim 1330193326Sed default: // Not a postfix-expression suffix. 1331245431Sdim return LHS; 1332193326Sed case tok::l_square: { // postfix-expression: p-e '[' expression ']' 1333210299Sed // If we have a array postfix expression that starts on a new line and 1334210299Sed // Objective-C is enabled, it is highly likely that the user forgot a 1335210299Sed // semicolon after the base expression and that the array postfix-expr is 1336210299Sed // actually another message send. In this case, do some look-ahead to see 1337210299Sed // if the contents of the square brackets are obviously not a valid 1338210299Sed // expression and recover by pretending there is no suffix. 1339235633Sdim if (getLangOpts().ObjC1 && Tok.isAtStartOfLine() && 1340210299Sed isSimpleObjCMessageExpression()) 1341245431Sdim return LHS; 1342235633Sdim 1343235633Sdim // Reject array indices starting with a lambda-expression. '[[' is 1344235633Sdim // reserved for attributes. 1345235633Sdim if (CheckProhibitedCXX11Attribute()) 1346235633Sdim return ExprError(); 1347235633Sdim 1348226890Sdim BalancedDelimiterTracker T(*this, tok::l_square); 1349226890Sdim T.consumeOpen(); 1350226890Sdim Loc = T.getOpenLocation(); 1351223017Sdim ExprResult Idx; 1352252723Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 1353235633Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 1354223017Sdim Idx = ParseBraceInitializer(); 1355235633Sdim } else 1356223017Sdim Idx = ParseExpression(); 1357193326Sed 1358193326Sed SourceLocation RLoc = Tok.getLocation(); 1359193326Sed 1360193326Sed if (!LHS.isInvalid() && !Idx.isInvalid() && Tok.is(tok::r_square)) { 1361212904Sdim LHS = Actions.ActOnArraySubscriptExpr(getCurScope(), LHS.take(), Loc, 1362212904Sdim Idx.take(), RLoc); 1363193326Sed } else 1364193326Sed LHS = ExprError(); 1365193326Sed 1366193326Sed // Match the ']'. 1367226890Sdim T.consumeClose(); 1368193326Sed break; 1369193326Sed } 1370193326Sed 1371218893Sdim case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')' 1372218893Sdim case tok::lesslessless: { // p-e: p-e '<<<' argument-expression-list '>>>' 1373218893Sdim // '(' argument-expression-list[opt] ')' 1374218893Sdim tok::TokenKind OpKind = Tok.getKind(); 1375218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 1376218893Sdim 1377218893Sdim Expr *ExecConfig = 0; 1378193326Sed 1379226890Sdim BalancedDelimiterTracker PT(*this, tok::l_paren); 1380226890Sdim 1381218893Sdim if (OpKind == tok::lesslessless) { 1382245431Sdim ExprVector ExecConfigExprs; 1383218893Sdim CommaLocsTy ExecConfigCommaLocs; 1384235633Sdim SourceLocation OpenLoc = ConsumeToken(); 1385193326Sed 1386263509Sdim if (ParseSimpleExpressionList(ExecConfigExprs, ExecConfigCommaLocs)) { 1387218893Sdim LHS = ExprError(); 1388218893Sdim } 1389218893Sdim 1390235633Sdim SourceLocation CloseLoc = Tok.getLocation(); 1391235633Sdim if (Tok.is(tok::greatergreatergreater)) { 1392235633Sdim ConsumeToken(); 1393235633Sdim } else if (LHS.isInvalid()) { 1394263509Sdim SkipUntil(tok::greatergreatergreater, StopAtSemi); 1395235633Sdim } else { 1396226890Sdim // There was an error closing the brackets 1397235633Sdim Diag(Tok, diag::err_expected_ggg); 1398235633Sdim Diag(OpenLoc, diag::note_matching) << "<<<"; 1399263509Sdim SkipUntil(tok::greatergreatergreater, StopAtSemi); 1400218893Sdim LHS = ExprError(); 1401218893Sdim } 1402218893Sdim 1403218893Sdim if (!LHS.isInvalid()) { 1404218893Sdim if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen, "")) 1405218893Sdim LHS = ExprError(); 1406218893Sdim else 1407218893Sdim Loc = PrevTokLocation; 1408218893Sdim } 1409218893Sdim 1410218893Sdim if (!LHS.isInvalid()) { 1411218893Sdim ExprResult ECResult = Actions.ActOnCUDAExecConfigExpr(getCurScope(), 1412235633Sdim OpenLoc, 1413245431Sdim ExecConfigExprs, 1414235633Sdim CloseLoc); 1415218893Sdim if (ECResult.isInvalid()) 1416218893Sdim LHS = ExprError(); 1417218893Sdim else 1418218893Sdim ExecConfig = ECResult.get(); 1419218893Sdim } 1420218893Sdim } else { 1421226890Sdim PT.consumeOpen(); 1422226890Sdim Loc = PT.getOpenLocation(); 1423210299Sed } 1424210299Sed 1425245431Sdim ExprVector ArgExprs; 1426218893Sdim CommaLocsTy CommaLocs; 1427218893Sdim 1428198092Srdivacky if (Tok.is(tok::code_completion)) { 1429252723Sdim Actions.CodeCompleteCall(getCurScope(), LHS.get(), None); 1430226890Sdim cutOffParsing(); 1431226890Sdim return ExprError(); 1432198092Srdivacky } 1433218893Sdim 1434218893Sdim if (OpKind == tok::l_paren || !LHS.isInvalid()) { 1435218893Sdim if (Tok.isNot(tok::r_paren)) { 1436218893Sdim if (ParseExpressionList(ArgExprs, CommaLocs, &Sema::CodeCompleteCall, 1437218893Sdim LHS.get())) { 1438218893Sdim LHS = ExprError(); 1439218893Sdim } 1440193326Sed } 1441193326Sed } 1442193326Sed 1443193326Sed // Match the ')'. 1444218893Sdim if (LHS.isInvalid()) { 1445263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1446218893Sdim } else if (Tok.isNot(tok::r_paren)) { 1447226890Sdim PT.consumeClose(); 1448218893Sdim LHS = ExprError(); 1449218893Sdim } else { 1450218893Sdim assert((ArgExprs.size() == 0 || 1451218893Sdim ArgExprs.size()-1 == CommaLocs.size())&& 1452193326Sed "Unexpected number of commas!"); 1453212904Sdim LHS = Actions.ActOnCallExpr(getCurScope(), LHS.take(), Loc, 1454245431Sdim ArgExprs, Tok.getLocation(), 1455218893Sdim ExecConfig); 1456226890Sdim PT.consumeClose(); 1457193326Sed } 1458198092Srdivacky 1459193326Sed break; 1460193326Sed } 1461198092Srdivacky case tok::arrow: 1462198092Srdivacky case tok::period: { 1463198092Srdivacky // postfix-expression: p-e '->' template[opt] id-expression 1464198092Srdivacky // postfix-expression: p-e '.' template[opt] id-expression 1465193326Sed tok::TokenKind OpKind = Tok.getKind(); 1466193326Sed SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. 1467193326Sed 1468198092Srdivacky CXXScopeSpec SS; 1469212904Sdim ParsedType ObjectType; 1470204643Srdivacky bool MayBePseudoDestructor = false; 1471235633Sdim if (getLangOpts().CPlusPlus && !LHS.isInvalid()) { 1472263509Sdim Expr *Base = LHS.take(); 1473263509Sdim const Type* BaseType = Base->getType().getTypePtrOrNull(); 1474263509Sdim if (BaseType && Tok.is(tok::l_paren) && 1475263509Sdim (BaseType->isFunctionType() || 1476263509Sdim BaseType->isSpecificPlaceholderType(BuiltinType::BoundMember))) { 1477263509Sdim Diag(OpLoc, diag::err_function_is_not_record) 1478263509Sdim << (OpKind == tok::arrow) << Base->getSourceRange() 1479263509Sdim << FixItHint::CreateRemoval(OpLoc); 1480263509Sdim return ParsePostfixExpressionSuffix(Base); 1481263509Sdim } 1482263509Sdim 1483263509Sdim LHS = Actions.ActOnStartCXXMemberReference(getCurScope(), Base, 1484204643Srdivacky OpLoc, OpKind, ObjectType, 1485204643Srdivacky MayBePseudoDestructor); 1486198092Srdivacky if (LHS.isInvalid()) 1487198092Srdivacky break; 1488204643Srdivacky 1489235633Sdim ParseOptionalCXXScopeSpecifier(SS, ObjectType, 1490235633Sdim /*EnteringContext=*/false, 1491204643Srdivacky &MayBePseudoDestructor); 1492210299Sed if (SS.isNotEmpty()) 1493212904Sdim ObjectType = ParsedType(); 1494198092Srdivacky } 1495198092Srdivacky 1496198092Srdivacky if (Tok.is(tok::code_completion)) { 1497198092Srdivacky // Code completion for a member access expression. 1498210299Sed Actions.CodeCompleteMemberReferenceExpr(getCurScope(), LHS.get(), 1499198092Srdivacky OpLoc, OpKind == tok::arrow); 1500198092Srdivacky 1501226890Sdim cutOffParsing(); 1502226890Sdim return ExprError(); 1503198092Srdivacky } 1504198092Srdivacky 1505212904Sdim if (MayBePseudoDestructor && !LHS.isInvalid()) { 1506212904Sdim LHS = ParseCXXPseudoDestructor(LHS.take(), OpLoc, OpKind, SS, 1507204643Srdivacky ObjectType); 1508204643Srdivacky break; 1509204643Srdivacky } 1510204643Srdivacky 1511204643Srdivacky // Either the action has told is that this cannot be a 1512204643Srdivacky // pseudo-destructor expression (based on the type of base 1513204643Srdivacky // expression), or we didn't see a '~' in the right place. We 1514204643Srdivacky // can still parse a destructor name here, but in that case it 1515204643Srdivacky // names a real destructor. 1516218893Sdim // Allow explicit constructor calls in Microsoft mode. 1517218893Sdim // FIXME: Add support for explicit call of template constructor. 1518235633Sdim SourceLocation TemplateKWLoc; 1519198893Srdivacky UnqualifiedId Name; 1520235633Sdim if (getLangOpts().ObjC2 && OpKind == tok::period && Tok.is(tok::kw_class)) { 1521235633Sdim // Objective-C++: 1522235633Sdim // After a '.' in a member access expression, treat the keyword 1523235633Sdim // 'class' as if it were an identifier. 1524235633Sdim // 1525235633Sdim // This hack allows property access to the 'class' method because it is 1526235633Sdim // such a common method name. For other C++ keywords that are 1527235633Sdim // Objective-C method names, one must use the message send syntax. 1528235633Sdim IdentifierInfo *Id = Tok.getIdentifierInfo(); 1529235633Sdim SourceLocation Loc = ConsumeToken(); 1530235633Sdim Name.setIdentifier(Id, Loc); 1531235633Sdim } else if (ParseUnqualifiedId(SS, 1532235633Sdim /*EnteringContext=*/false, 1533235633Sdim /*AllowDestructorName=*/true, 1534235633Sdim /*AllowConstructorName=*/ 1535235633Sdim getLangOpts().MicrosoftExt, 1536235633Sdim ObjectType, TemplateKWLoc, Name)) 1537218893Sdim LHS = ExprError(); 1538198893Srdivacky 1539198893Srdivacky if (!LHS.isInvalid()) 1540212904Sdim LHS = Actions.ActOnMemberAccessExpr(getCurScope(), LHS.take(), OpLoc, 1541235633Sdim OpKind, SS, TemplateKWLoc, Name, 1542235633Sdim CurParsedObjCImpl ? CurParsedObjCImpl->Dcl : 0, 1543198893Srdivacky Tok.is(tok::l_paren)); 1544193326Sed break; 1545193326Sed } 1546193326Sed case tok::plusplus: // postfix-expression: postfix-expression '++' 1547193326Sed case tok::minusminus: // postfix-expression: postfix-expression '--' 1548193326Sed if (!LHS.isInvalid()) { 1549210299Sed LHS = Actions.ActOnPostfixUnaryOp(getCurScope(), Tok.getLocation(), 1550212904Sdim Tok.getKind(), LHS.take()); 1551193326Sed } 1552193326Sed ConsumeToken(); 1553193326Sed break; 1554193326Sed } 1555193326Sed } 1556193326Sed} 1557193326Sed 1558221345Sdim/// ParseExprAfterUnaryExprOrTypeTrait - We parsed a typeof/sizeof/alignof/ 1559221345Sdim/// vec_step and we are at the start of an expression or a parenthesized 1560221345Sdim/// type-id. OpTok is the operand token (typeof/sizeof/alignof). Returns the 1561221345Sdim/// expression (isCastExpr == false) or the type (isCastExpr == true). 1562193326Sed/// 1563245431Sdim/// \verbatim 1564193326Sed/// unary-expression: [C99 6.5.3] 1565193326Sed/// 'sizeof' unary-expression 1566193326Sed/// 'sizeof' '(' type-name ')' 1567193326Sed/// [GNU] '__alignof' unary-expression 1568193326Sed/// [GNU] '__alignof' '(' type-name ')' 1569245431Sdim/// [C11] '_Alignof' '(' type-name ')' 1570193326Sed/// [C++0x] 'alignof' '(' type-id ')' 1571193326Sed/// 1572193326Sed/// [GNU] typeof-specifier: 1573193326Sed/// typeof ( expressions ) 1574193326Sed/// typeof ( type-name ) 1575193326Sed/// [GNU/C++] typeof unary-expression 1576193326Sed/// 1577221345Sdim/// [OpenCL 1.1 6.11.12] vec_step built-in function: 1578221345Sdim/// vec_step ( expressions ) 1579221345Sdim/// vec_step ( type-name ) 1580245431Sdim/// \endverbatim 1581212904SdimExprResult 1582221345SdimParser::ParseExprAfterUnaryExprOrTypeTrait(const Token &OpTok, 1583221345Sdim bool &isCastExpr, 1584221345Sdim ParsedType &CastTy, 1585221345Sdim SourceRange &CastRange) { 1586198092Srdivacky 1587198092Srdivacky assert((OpTok.is(tok::kw_typeof) || OpTok.is(tok::kw_sizeof) || 1588221345Sdim OpTok.is(tok::kw___alignof) || OpTok.is(tok::kw_alignof) || 1589245431Sdim OpTok.is(tok::kw__Alignof) || OpTok.is(tok::kw_vec_step)) && 1590221345Sdim "Not a typeof/sizeof/alignof/vec_step expression!"); 1591193326Sed 1592212904Sdim ExprResult Operand; 1593198092Srdivacky 1594193326Sed // If the operand doesn't start with an '(', it must be an expression. 1595193326Sed if (Tok.isNot(tok::l_paren)) { 1596263509Sdim // If construct allows a form without parenthesis, user may forget to put 1597263509Sdim // pathenthesis around type name. 1598263509Sdim if (OpTok.is(tok::kw_sizeof) || OpTok.is(tok::kw___alignof) || 1599263509Sdim OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) { 1600263509Sdim bool isAmbiguousTypeId; 1601263509Sdim if (isTypeIdInParens(isAmbiguousTypeId)) { 1602263509Sdim DeclSpec DS(AttrFactory); 1603263509Sdim ParseSpecifierQualifierList(DS); 1604263509Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 1605263509Sdim ParseDeclarator(DeclaratorInfo); 1606263509Sdim 1607263509Sdim SourceLocation LParenLoc = PP.getLocForEndOfToken(OpTok.getLocation()); 1608263509Sdim SourceLocation RParenLoc = PP.getLocForEndOfToken(PrevTokLocation); 1609263509Sdim Diag(LParenLoc, diag::err_expected_parentheses_around_typename) 1610263509Sdim << OpTok.getName() 1611263509Sdim << FixItHint::CreateInsertion(LParenLoc, "(") 1612263509Sdim << FixItHint::CreateInsertion(RParenLoc, ")"); 1613263509Sdim isCastExpr = true; 1614263509Sdim return ExprEmpty(); 1615263509Sdim } 1616263509Sdim } 1617263509Sdim 1618193326Sed isCastExpr = false; 1619235633Sdim if (OpTok.is(tok::kw_typeof) && !getLangOpts().CPlusPlus) { 1620193326Sed Diag(Tok,diag::err_expected_lparen_after_id) << OpTok.getIdentifierInfo(); 1621193326Sed return ExprError(); 1622193326Sed } 1623198092Srdivacky 1624193326Sed Operand = ParseCastExpression(true/*isUnaryExpression*/); 1625193326Sed } else { 1626193326Sed // If it starts with a '(', we know that it is either a parenthesized 1627193326Sed // type-name, or it is a unary-expression that starts with a compound 1628193326Sed // literal, or starts with a primary-expression that is a parenthesized 1629193326Sed // expression. 1630193326Sed ParenParseOption ExprType = CastExpr; 1631193326Sed SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 1632198092Srdivacky 1633199990Srdivacky Operand = ParseParenExpression(ExprType, true/*stopIfCastExpr*/, 1634224145Sdim false, CastTy, RParenLoc); 1635193326Sed CastRange = SourceRange(LParenLoc, RParenLoc); 1636193326Sed 1637193326Sed // If ParseParenExpression parsed a '(typename)' sequence only, then this is 1638193326Sed // a type. 1639193326Sed if (ExprType == CastExpr) { 1640193326Sed isCastExpr = true; 1641193326Sed return ExprEmpty(); 1642193326Sed } 1643193326Sed 1644235633Sdim if (getLangOpts().CPlusPlus || OpTok.isNot(tok::kw_typeof)) { 1645212904Sdim // GNU typeof in C requires the expression to be parenthesized. Not so for 1646212904Sdim // sizeof/alignof or in C++. Therefore, the parenthesized expression is 1647212904Sdim // the start of a unary-expression, but doesn't include any postfix 1648212904Sdim // pieces. Parse these now if present. 1649212904Sdim if (!Operand.isInvalid()) 1650212904Sdim Operand = ParsePostfixExpressionSuffix(Operand.get()); 1651212904Sdim } 1652193326Sed } 1653193326Sed 1654193326Sed // If we get here, the operand to the typeof/sizeof/alignof was an expresion. 1655193326Sed isCastExpr = false; 1656245431Sdim return Operand; 1657193326Sed} 1658193326Sed 1659193326Sed 1660245431Sdim/// \brief Parse a sizeof or alignof expression. 1661245431Sdim/// 1662245431Sdim/// \verbatim 1663193326Sed/// unary-expression: [C99 6.5.3] 1664193326Sed/// 'sizeof' unary-expression 1665193326Sed/// 'sizeof' '(' type-name ')' 1666252723Sdim/// [C++11] 'sizeof' '...' '(' identifier ')' 1667193326Sed/// [GNU] '__alignof' unary-expression 1668193326Sed/// [GNU] '__alignof' '(' type-name ')' 1669245431Sdim/// [C11] '_Alignof' '(' type-name ')' 1670252723Sdim/// [C++11] 'alignof' '(' type-id ')' 1671245431Sdim/// \endverbatim 1672221345SdimExprResult Parser::ParseUnaryExprOrTypeTraitExpression() { 1673245431Sdim assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof) || 1674245431Sdim Tok.is(tok::kw_alignof) || Tok.is(tok::kw__Alignof) || 1675245431Sdim Tok.is(tok::kw_vec_step)) && 1676221345Sdim "Not a sizeof/alignof/vec_step expression!"); 1677193326Sed Token OpTok = Tok; 1678193326Sed ConsumeToken(); 1679198092Srdivacky 1680252723Sdim // [C++11] 'sizeof' '...' '(' identifier ')' 1681218893Sdim if (Tok.is(tok::ellipsis) && OpTok.is(tok::kw_sizeof)) { 1682218893Sdim SourceLocation EllipsisLoc = ConsumeToken(); 1683218893Sdim SourceLocation LParenLoc, RParenLoc; 1684218893Sdim IdentifierInfo *Name = 0; 1685218893Sdim SourceLocation NameLoc; 1686218893Sdim if (Tok.is(tok::l_paren)) { 1687226890Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 1688226890Sdim T.consumeOpen(); 1689226890Sdim LParenLoc = T.getOpenLocation(); 1690218893Sdim if (Tok.is(tok::identifier)) { 1691218893Sdim Name = Tok.getIdentifierInfo(); 1692218893Sdim NameLoc = ConsumeToken(); 1693226890Sdim T.consumeClose(); 1694226890Sdim RParenLoc = T.getCloseLocation(); 1695218893Sdim if (RParenLoc.isInvalid()) 1696218893Sdim RParenLoc = PP.getLocForEndOfToken(NameLoc); 1697218893Sdim } else { 1698218893Sdim Diag(Tok, diag::err_expected_parameter_pack); 1699263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1700218893Sdim } 1701218893Sdim } else if (Tok.is(tok::identifier)) { 1702218893Sdim Name = Tok.getIdentifierInfo(); 1703218893Sdim NameLoc = ConsumeToken(); 1704218893Sdim LParenLoc = PP.getLocForEndOfToken(EllipsisLoc); 1705218893Sdim RParenLoc = PP.getLocForEndOfToken(NameLoc); 1706218893Sdim Diag(LParenLoc, diag::err_paren_sizeof_parameter_pack) 1707218893Sdim << Name 1708218893Sdim << FixItHint::CreateInsertion(LParenLoc, "(") 1709218893Sdim << FixItHint::CreateInsertion(RParenLoc, ")"); 1710218893Sdim } else { 1711218893Sdim Diag(Tok, diag::err_sizeof_parameter_pack); 1712218893Sdim } 1713218893Sdim 1714218893Sdim if (!Name) 1715218893Sdim return ExprError(); 1716218893Sdim 1717263509Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, 1718263509Sdim Sema::ReuseLambdaContextDecl); 1719263509Sdim 1720218893Sdim return Actions.ActOnSizeofParameterPackExpr(getCurScope(), 1721218893Sdim OpTok.getLocation(), 1722218893Sdim *Name, NameLoc, 1723218893Sdim RParenLoc); 1724218893Sdim } 1725235633Sdim 1726245431Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) 1727235633Sdim Diag(OpTok, diag::warn_cxx98_compat_alignof); 1728235633Sdim 1729245431Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, 1730245431Sdim Sema::ReuseLambdaContextDecl); 1731235633Sdim 1732193326Sed bool isCastExpr; 1733212904Sdim ParsedType CastTy; 1734193326Sed SourceRange CastRange; 1735221345Sdim ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, 1736221345Sdim isCastExpr, 1737221345Sdim CastTy, 1738221345Sdim CastRange); 1739193326Sed 1740221345Sdim UnaryExprOrTypeTrait ExprKind = UETT_SizeOf; 1741245431Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw___alignof) || 1742245431Sdim OpTok.is(tok::kw__Alignof)) 1743221345Sdim ExprKind = UETT_AlignOf; 1744221345Sdim else if (OpTok.is(tok::kw_vec_step)) 1745221345Sdim ExprKind = UETT_VecStep; 1746221345Sdim 1747193326Sed if (isCastExpr) 1748221345Sdim return Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), 1749221345Sdim ExprKind, 1750221345Sdim /*isType=*/true, 1751221345Sdim CastTy.getAsOpaquePtr(), 1752221345Sdim CastRange); 1753193326Sed 1754252723Sdim if (OpTok.is(tok::kw_alignof) || OpTok.is(tok::kw__Alignof)) 1755252723Sdim Diag(OpTok, diag::ext_alignof_expr) << OpTok.getIdentifierInfo(); 1756252723Sdim 1757193326Sed // If we get here, the operand to the sizeof/alignof was an expresion. 1758193326Sed if (!Operand.isInvalid()) 1759221345Sdim Operand = Actions.ActOnUnaryExprOrTypeTraitExpr(OpTok.getLocation(), 1760221345Sdim ExprKind, 1761221345Sdim /*isType=*/false, 1762221345Sdim Operand.release(), 1763221345Sdim CastRange); 1764245431Sdim return Operand; 1765193326Sed} 1766193326Sed 1767193326Sed/// ParseBuiltinPrimaryExpression 1768193326Sed/// 1769245431Sdim/// \verbatim 1770193326Sed/// primary-expression: [C99 6.5.1] 1771193326Sed/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 1772193326Sed/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 1773193326Sed/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 1774193326Sed/// assign-expr ')' 1775193326Sed/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 1776235633Sdim/// [OCL] '__builtin_astype' '(' assignment-expression ',' type-name ')' 1777198092Srdivacky/// 1778193326Sed/// [GNU] offsetof-member-designator: 1779193326Sed/// [GNU] identifier 1780193326Sed/// [GNU] offsetof-member-designator '.' identifier 1781193326Sed/// [GNU] offsetof-member-designator '[' expression ']' 1782245431Sdim/// \endverbatim 1783212904SdimExprResult Parser::ParseBuiltinPrimaryExpression() { 1784212904Sdim ExprResult Res; 1785193326Sed const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); 1786193326Sed 1787193326Sed tok::TokenKind T = Tok.getKind(); 1788193326Sed SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. 1789193326Sed 1790193326Sed // All of these start with an open paren. 1791193326Sed if (Tok.isNot(tok::l_paren)) 1792193326Sed return ExprError(Diag(Tok, diag::err_expected_lparen_after_id) 1793193326Sed << BuiltinII); 1794193326Sed 1795226890Sdim BalancedDelimiterTracker PT(*this, tok::l_paren); 1796226890Sdim PT.consumeOpen(); 1797226890Sdim 1798193326Sed // TODO: Build AST. 1799193326Sed 1800193326Sed switch (T) { 1801226890Sdim default: llvm_unreachable("Not a builtin primary expression!"); 1802193326Sed case tok::kw___builtin_va_arg: { 1803212904Sdim ExprResult Expr(ParseAssignmentExpression()); 1804193326Sed 1805193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1806218893Sdim Expr = ExprError(); 1807193326Sed 1808193326Sed TypeResult Ty = ParseTypeName(); 1809193326Sed 1810193326Sed if (Tok.isNot(tok::r_paren)) { 1811193326Sed Diag(Tok, diag::err_expected_rparen); 1812218893Sdim Expr = ExprError(); 1813193326Sed } 1814218893Sdim 1815218893Sdim if (Expr.isInvalid() || Ty.isInvalid()) 1816193326Sed Res = ExprError(); 1817193326Sed else 1818212904Sdim Res = Actions.ActOnVAArg(StartLoc, Expr.take(), Ty.get(), ConsumeParen()); 1819193326Sed break; 1820193326Sed } 1821193326Sed case tok::kw___builtin_offsetof: { 1822193326Sed SourceLocation TypeLoc = Tok.getLocation(); 1823193326Sed TypeResult Ty = ParseTypeName(); 1824193326Sed if (Ty.isInvalid()) { 1825263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1826193326Sed return ExprError(); 1827193326Sed } 1828198092Srdivacky 1829193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1830193326Sed return ExprError(); 1831193326Sed 1832193326Sed // We must have at least one identifier here. 1833193326Sed if (Tok.isNot(tok::identifier)) { 1834193326Sed Diag(Tok, diag::err_expected_ident); 1835263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1836193326Sed return ExprError(); 1837193326Sed } 1838193326Sed 1839193326Sed // Keep track of the various subcomponents we see. 1840226890Sdim SmallVector<Sema::OffsetOfComponent, 4> Comps; 1841193326Sed 1842212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1843193326Sed Comps.back().isBrackets = false; 1844193326Sed Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 1845193326Sed Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); 1846193326Sed 1847193326Sed // FIXME: This loop leaks the index expressions on error. 1848193326Sed while (1) { 1849193326Sed if (Tok.is(tok::period)) { 1850193326Sed // offsetof-member-designator: offsetof-member-designator '.' identifier 1851212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1852193326Sed Comps.back().isBrackets = false; 1853193326Sed Comps.back().LocStart = ConsumeToken(); 1854193326Sed 1855193326Sed if (Tok.isNot(tok::identifier)) { 1856193326Sed Diag(Tok, diag::err_expected_ident); 1857263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1858193326Sed return ExprError(); 1859193326Sed } 1860193326Sed Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 1861193326Sed Comps.back().LocEnd = ConsumeToken(); 1862193326Sed 1863193326Sed } else if (Tok.is(tok::l_square)) { 1864235633Sdim if (CheckProhibitedCXX11Attribute()) 1865235633Sdim return ExprError(); 1866235633Sdim 1867193326Sed // offsetof-member-designator: offsetof-member-design '[' expression ']' 1868212904Sdim Comps.push_back(Sema::OffsetOfComponent()); 1869193326Sed Comps.back().isBrackets = true; 1870226890Sdim BalancedDelimiterTracker ST(*this, tok::l_square); 1871226890Sdim ST.consumeOpen(); 1872226890Sdim Comps.back().LocStart = ST.getOpenLocation(); 1873193326Sed Res = ParseExpression(); 1874193326Sed if (Res.isInvalid()) { 1875263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1876245431Sdim return Res; 1877193326Sed } 1878193326Sed Comps.back().U.E = Res.release(); 1879193326Sed 1880226890Sdim ST.consumeClose(); 1881226890Sdim Comps.back().LocEnd = ST.getCloseLocation(); 1882195341Sed } else { 1883195341Sed if (Tok.isNot(tok::r_paren)) { 1884226890Sdim PT.consumeClose(); 1885193326Sed Res = ExprError(); 1886195341Sed } else if (Ty.isInvalid()) { 1887195341Sed Res = ExprError(); 1888195341Sed } else { 1889226890Sdim PT.consumeClose(); 1890210299Sed Res = Actions.ActOnBuiltinOffsetOf(getCurScope(), StartLoc, TypeLoc, 1891226890Sdim Ty.get(), &Comps[0], Comps.size(), 1892226890Sdim PT.getCloseLocation()); 1893195341Sed } 1894193326Sed break; 1895193326Sed } 1896193326Sed } 1897193326Sed break; 1898193326Sed } 1899193326Sed case tok::kw___builtin_choose_expr: { 1900212904Sdim ExprResult Cond(ParseAssignmentExpression()); 1901193326Sed if (Cond.isInvalid()) { 1902263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1903245431Sdim return Cond; 1904193326Sed } 1905193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1906193326Sed return ExprError(); 1907193326Sed 1908212904Sdim ExprResult Expr1(ParseAssignmentExpression()); 1909193326Sed if (Expr1.isInvalid()) { 1910263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1911245431Sdim return Expr1; 1912193326Sed } 1913193326Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 1914193326Sed return ExprError(); 1915193326Sed 1916212904Sdim ExprResult Expr2(ParseAssignmentExpression()); 1917193326Sed if (Expr2.isInvalid()) { 1918263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1919245431Sdim return Expr2; 1920193326Sed } 1921193326Sed if (Tok.isNot(tok::r_paren)) { 1922193326Sed Diag(Tok, diag::err_expected_rparen); 1923193326Sed return ExprError(); 1924193326Sed } 1925212904Sdim Res = Actions.ActOnChooseExpr(StartLoc, Cond.take(), Expr1.take(), 1926212904Sdim Expr2.take(), ConsumeParen()); 1927193326Sed break; 1928193326Sed } 1929223017Sdim case tok::kw___builtin_astype: { 1930223017Sdim // The first argument is an expression to be converted, followed by a comma. 1931223017Sdim ExprResult Expr(ParseAssignmentExpression()); 1932223017Sdim if (Expr.isInvalid()) { 1933263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1934223017Sdim return ExprError(); 1935223017Sdim } 1936223017Sdim 1937223017Sdim if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", 1938223017Sdim tok::r_paren)) 1939223017Sdim return ExprError(); 1940223017Sdim 1941223017Sdim // Second argument is the type to bitcast to. 1942223017Sdim TypeResult DestTy = ParseTypeName(); 1943223017Sdim if (DestTy.isInvalid()) 1944223017Sdim return ExprError(); 1945223017Sdim 1946223017Sdim // Attempt to consume the r-paren. 1947223017Sdim if (Tok.isNot(tok::r_paren)) { 1948223017Sdim Diag(Tok, diag::err_expected_rparen); 1949263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1950223017Sdim return ExprError(); 1951223017Sdim } 1952223017Sdim 1953223017Sdim Res = Actions.ActOnAsTypeExpr(Expr.take(), DestTy.get(), StartLoc, 1954223017Sdim ConsumeParen()); 1955223017Sdim break; 1956193326Sed } 1957263509Sdim case tok::kw___builtin_convertvector: { 1958263509Sdim // The first argument is an expression to be converted, followed by a comma. 1959263509Sdim ExprResult Expr(ParseAssignmentExpression()); 1960263509Sdim if (Expr.isInvalid()) { 1961263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1962263509Sdim return ExprError(); 1963263509Sdim } 1964263509Sdim 1965263509Sdim if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", 1966263509Sdim tok::r_paren)) 1967263509Sdim return ExprError(); 1968263509Sdim 1969263509Sdim // Second argument is the type to bitcast to. 1970263509Sdim TypeResult DestTy = ParseTypeName(); 1971263509Sdim if (DestTy.isInvalid()) 1972263509Sdim return ExprError(); 1973263509Sdim 1974263509Sdim // Attempt to consume the r-paren. 1975263509Sdim if (Tok.isNot(tok::r_paren)) { 1976263509Sdim Diag(Tok, diag::err_expected_rparen); 1977263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 1978263509Sdim return ExprError(); 1979263509Sdim } 1980263509Sdim 1981263509Sdim Res = Actions.ActOnConvertVectorExpr(Expr.take(), DestTy.get(), StartLoc, 1982263509Sdim ConsumeParen()); 1983263509Sdim break; 1984224145Sdim } 1985263509Sdim } 1986193326Sed 1987212904Sdim if (Res.isInvalid()) 1988212904Sdim return ExprError(); 1989212904Sdim 1990193326Sed // These can be followed by postfix-expr pieces because they are 1991193326Sed // primary-expressions. 1992212904Sdim return ParsePostfixExpressionSuffix(Res.take()); 1993193326Sed} 1994193326Sed 1995193326Sed/// ParseParenExpression - This parses the unit that starts with a '(' token, 1996193326Sed/// based on what is allowed by ExprType. The actual thing parsed is returned 1997193326Sed/// in ExprType. If stopIfCastExpr is true, it will only return the parsed type, 1998193326Sed/// not the parsed cast-expression. 1999193326Sed/// 2000245431Sdim/// \verbatim 2001193326Sed/// primary-expression: [C99 6.5.1] 2002193326Sed/// '(' expression ')' 2003193326Sed/// [GNU] '(' compound-statement ')' (if !ParenExprOnly) 2004193326Sed/// postfix-expression: [C99 6.5.2] 2005193326Sed/// '(' type-name ')' '{' initializer-list '}' 2006193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 2007193326Sed/// cast-expression: [C99 6.5.4] 2008193326Sed/// '(' type-name ')' cast-expression 2009224145Sdim/// [ARC] bridged-cast-expression 2010224145Sdim/// 2011224145Sdim/// [ARC] bridged-cast-expression: 2012224145Sdim/// (__bridge type-name) cast-expression 2013224145Sdim/// (__bridge_transfer type-name) cast-expression 2014224145Sdim/// (__bridge_retained type-name) cast-expression 2015245431Sdim/// \endverbatim 2016212904SdimExprResult 2017193326SedParser::ParseParenExpression(ParenParseOption &ExprType, bool stopIfCastExpr, 2018224145Sdim bool isTypeCast, ParsedType &CastTy, 2019198092Srdivacky SourceLocation &RParenLoc) { 2020193326Sed assert(Tok.is(tok::l_paren) && "Not a paren expr!"); 2021226890Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 2022226890Sdim if (T.consumeOpen()) 2023226890Sdim return ExprError(); 2024226890Sdim SourceLocation OpenLoc = T.getOpenLocation(); 2025226890Sdim 2026212904Sdim ExprResult Result(true); 2027193326Sed bool isAmbiguousTypeId; 2028212904Sdim CastTy = ParsedType(); 2029193326Sed 2030218893Sdim if (Tok.is(tok::code_completion)) { 2031218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), 2032218893Sdim ExprType >= CompoundLiteral? Sema::PCC_ParenthesizedExpression 2033218893Sdim : Sema::PCC_Expression); 2034226890Sdim cutOffParsing(); 2035218893Sdim return ExprError(); 2036218893Sdim } 2037221345Sdim 2038235633Sdim // Diagnose use of bridge casts in non-arc mode. 2039235633Sdim bool BridgeCast = (getLangOpts().ObjC2 && 2040235633Sdim (Tok.is(tok::kw___bridge) || 2041235633Sdim Tok.is(tok::kw___bridge_transfer) || 2042235633Sdim Tok.is(tok::kw___bridge_retained) || 2043235633Sdim Tok.is(tok::kw___bridge_retain))); 2044235633Sdim if (BridgeCast && !getLangOpts().ObjCAutoRefCount) { 2045252723Sdim if (Tok.isNot(tok::kw___bridge)) { 2046252723Sdim StringRef BridgeCastName = Tok.getName(); 2047252723Sdim SourceLocation BridgeKeywordLoc = ConsumeToken(); 2048252723Sdim if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) 2049252723Sdim Diag(BridgeKeywordLoc, diag::warn_arc_bridge_cast_nonarc) 2050252723Sdim << BridgeCastName 2051252723Sdim << FixItHint::CreateReplacement(BridgeKeywordLoc, ""); 2052252723Sdim } 2053252723Sdim else 2054252723Sdim ConsumeToken(); // consume __bridge 2055235633Sdim BridgeCast = false; 2056235633Sdim } 2057235633Sdim 2058221345Sdim // None of these cases should fall through with an invalid Result 2059221345Sdim // unless they've already reported an error. 2060193326Sed if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { 2061193326Sed Diag(Tok, diag::ext_gnu_statement_expr); 2062235633Sdim Actions.ActOnStartStmtExpr(); 2063235633Sdim 2064235633Sdim StmtResult Stmt(ParseCompoundStatement(true)); 2065193326Sed ExprType = CompoundStmt; 2066193326Sed 2067193326Sed // If the substmt parsed correctly, build the AST node. 2068235633Sdim if (!Stmt.isInvalid()) { 2069212904Sdim Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.take(), Tok.getLocation()); 2070235633Sdim } else { 2071235633Sdim Actions.ActOnStmtExprError(); 2072235633Sdim } 2073235633Sdim } else if (ExprType >= CompoundLiteral && BridgeCast) { 2074224145Sdim tok::TokenKind tokenKind = Tok.getKind(); 2075224145Sdim SourceLocation BridgeKeywordLoc = ConsumeToken(); 2076193326Sed 2077224145Sdim // Parse an Objective-C ARC ownership cast expression. 2078224145Sdim ObjCBridgeCastKind Kind; 2079224145Sdim if (tokenKind == tok::kw___bridge) 2080224145Sdim Kind = OBC_Bridge; 2081224145Sdim else if (tokenKind == tok::kw___bridge_transfer) 2082224145Sdim Kind = OBC_BridgeTransfer; 2083224145Sdim else if (tokenKind == tok::kw___bridge_retained) 2084224145Sdim Kind = OBC_BridgeRetained; 2085224145Sdim else { 2086224145Sdim // As a hopefully temporary workaround, allow __bridge_retain as 2087224145Sdim // a synonym for __bridge_retained, but only in system headers. 2088224145Sdim assert(tokenKind == tok::kw___bridge_retain); 2089224145Sdim Kind = OBC_BridgeRetained; 2090224145Sdim if (!PP.getSourceManager().isInSystemHeader(BridgeKeywordLoc)) 2091224145Sdim Diag(BridgeKeywordLoc, diag::err_arc_bridge_retain) 2092224145Sdim << FixItHint::CreateReplacement(BridgeKeywordLoc, 2093224145Sdim "__bridge_retained"); 2094224145Sdim } 2095224145Sdim 2096224145Sdim TypeResult Ty = ParseTypeName(); 2097226890Sdim T.consumeClose(); 2098226890Sdim RParenLoc = T.getCloseLocation(); 2099224145Sdim ExprResult SubExpr = ParseCastExpression(/*isUnaryExpression=*/false); 2100224145Sdim 2101224145Sdim if (Ty.isInvalid() || SubExpr.isInvalid()) 2102224145Sdim return ExprError(); 2103224145Sdim 2104224145Sdim return Actions.ActOnObjCBridgedCast(getCurScope(), OpenLoc, Kind, 2105224145Sdim BridgeKeywordLoc, Ty.get(), 2106224145Sdim RParenLoc, SubExpr.get()); 2107193326Sed } else if (ExprType >= CompoundLiteral && 2108193326Sed isTypeIdInParens(isAmbiguousTypeId)) { 2109198092Srdivacky 2110193326Sed // Otherwise, this is a compound literal expression or cast expression. 2111198092Srdivacky 2112193326Sed // In C++, if the type-id is ambiguous we disambiguate based on context. 2113193326Sed // If stopIfCastExpr is true the context is a typeof/sizeof/alignof 2114193326Sed // in which case we should treat it as type-id. 2115193326Sed // if stopIfCastExpr is false, we need to determine the context past the 2116193326Sed // parens, so we defer to ParseCXXAmbiguousParenExpression for that. 2117226890Sdim if (isAmbiguousTypeId && !stopIfCastExpr) { 2118226890Sdim ExprResult res = ParseCXXAmbiguousParenExpression(ExprType, CastTy, T); 2119226890Sdim RParenLoc = T.getCloseLocation(); 2120226890Sdim return res; 2121226890Sdim } 2122198092Srdivacky 2123224145Sdim // Parse the type declarator. 2124224145Sdim DeclSpec DS(AttrFactory); 2125224145Sdim ParseSpecifierQualifierList(DS); 2126224145Sdim Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 2127224145Sdim ParseDeclarator(DeclaratorInfo); 2128218893Sdim 2129218893Sdim // If our type is followed by an identifier and either ':' or ']', then 2130218893Sdim // this is probably an Objective-C message send where the leading '[' is 2131218893Sdim // missing. Recover as if that were the case. 2132224145Sdim if (!DeclaratorInfo.isInvalidType() && Tok.is(tok::identifier) && 2133235633Sdim !InMessageExpression && getLangOpts().ObjC1 && 2134224145Sdim (NextToken().is(tok::colon) || NextToken().is(tok::r_square))) { 2135224145Sdim TypeResult Ty; 2136224145Sdim { 2137224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2138224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2139224145Sdim } 2140218893Sdim Result = ParseObjCMessageExpressionBody(SourceLocation(), 2141218893Sdim SourceLocation(), 2142218893Sdim Ty.get(), 0); 2143218893Sdim } else { 2144218893Sdim // Match the ')'. 2145226890Sdim T.consumeClose(); 2146226890Sdim RParenLoc = T.getCloseLocation(); 2147218893Sdim if (Tok.is(tok::l_brace)) { 2148218893Sdim ExprType = CompoundLiteral; 2149224145Sdim TypeResult Ty; 2150224145Sdim { 2151224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2152224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2153224145Sdim } 2154218893Sdim return ParseCompoundLiteralExpression(Ty.get(), OpenLoc, RParenLoc); 2155218893Sdim } 2156193326Sed 2157218893Sdim if (ExprType == CastExpr) { 2158218893Sdim // We parsed '(' type-name ')' and the thing after it wasn't a '{'. 2159193326Sed 2160224145Sdim if (DeclaratorInfo.isInvalidType()) 2161218893Sdim return ExprError(); 2162193326Sed 2163218893Sdim // Note that this doesn't parse the subsequent cast-expression, it just 2164218893Sdim // returns the parsed type to the callee. 2165224145Sdim if (stopIfCastExpr) { 2166224145Sdim TypeResult Ty; 2167224145Sdim { 2168224145Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2169224145Sdim Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 2170224145Sdim } 2171224145Sdim CastTy = Ty.get(); 2172218893Sdim return ExprResult(); 2173224145Sdim } 2174218893Sdim 2175218893Sdim // Reject the cast of super idiom in ObjC. 2176235633Sdim if (Tok.is(tok::identifier) && getLangOpts().ObjC1 && 2177218893Sdim Tok.getIdentifierInfo() == Ident_super && 2178218893Sdim getCurScope()->isInObjcMethodScope() && 2179218893Sdim GetLookAheadToken(1).isNot(tok::period)) { 2180218893Sdim Diag(Tok.getLocation(), diag::err_illegal_super_cast) 2181218893Sdim << SourceRange(OpenLoc, RParenLoc); 2182218893Sdim return ExprError(); 2183218893Sdim } 2184193326Sed 2185218893Sdim // Parse the cast-expression that follows it next. 2186218893Sdim // TODO: For cast expression with CastTy. 2187224145Sdim Result = ParseCastExpression(/*isUnaryExpression=*/false, 2188224145Sdim /*isAddressOfOperand=*/false, 2189235633Sdim /*isTypeCast=*/IsTypeCast); 2190224145Sdim if (!Result.isInvalid()) { 2191224145Sdim Result = Actions.ActOnCastExpr(getCurScope(), OpenLoc, 2192224145Sdim DeclaratorInfo, CastTy, 2193218893Sdim RParenLoc, Result.take()); 2194224145Sdim } 2195245431Sdim return Result; 2196193326Sed } 2197193326Sed 2198218893Sdim Diag(Tok, diag::err_expected_lbrace_in_compound_literal); 2199218893Sdim return ExprError(); 2200193326Sed } 2201224145Sdim } else if (isTypeCast) { 2202198092Srdivacky // Parse the expression-list. 2203218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2204218893Sdim 2205245431Sdim ExprVector ArgExprs; 2206198092Srdivacky CommaLocsTy CommaLocs; 2207198092Srdivacky 2208263509Sdim if (!ParseSimpleExpressionList(ArgExprs, CommaLocs)) { 2209198092Srdivacky ExprType = SimpleExpr; 2210235633Sdim Result = Actions.ActOnParenListExpr(OpenLoc, Tok.getLocation(), 2211245431Sdim ArgExprs); 2212198092Srdivacky } 2213193326Sed } else { 2214218893Sdim InMessageExpressionRAIIObject InMessage(*this, false); 2215218893Sdim 2216235633Sdim Result = ParseExpression(MaybeTypeCast); 2217193326Sed ExprType = SimpleExpr; 2218221345Sdim 2219221345Sdim // Don't build a paren expression unless we actually match a ')'. 2220193326Sed if (!Result.isInvalid() && Tok.is(tok::r_paren)) 2221212904Sdim Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.take()); 2222193326Sed } 2223193326Sed 2224193326Sed // Match the ')'. 2225193326Sed if (Result.isInvalid()) { 2226263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2227193326Sed return ExprError(); 2228193326Sed } 2229198092Srdivacky 2230226890Sdim T.consumeClose(); 2231226890Sdim RParenLoc = T.getCloseLocation(); 2232245431Sdim return Result; 2233193326Sed} 2234193326Sed 2235193326Sed/// ParseCompoundLiteralExpression - We have parsed the parenthesized type-name 2236193326Sed/// and we are at the left brace. 2237193326Sed/// 2238245431Sdim/// \verbatim 2239193326Sed/// postfix-expression: [C99 6.5.2] 2240193326Sed/// '(' type-name ')' '{' initializer-list '}' 2241193326Sed/// '(' type-name ')' '{' initializer-list ',' '}' 2242245431Sdim/// \endverbatim 2243212904SdimExprResult 2244212904SdimParser::ParseCompoundLiteralExpression(ParsedType Ty, 2245193326Sed SourceLocation LParenLoc, 2246193326Sed SourceLocation RParenLoc) { 2247193326Sed assert(Tok.is(tok::l_brace) && "Not a compound literal!"); 2248235633Sdim if (!getLangOpts().C99) // Compound literals don't exist in C90. 2249193326Sed Diag(LParenLoc, diag::ext_c99_compound_literal); 2250212904Sdim ExprResult Result = ParseInitializer(); 2251193326Sed if (!Result.isInvalid() && Ty) 2252212904Sdim return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.take()); 2253245431Sdim return Result; 2254193326Sed} 2255193326Sed 2256193326Sed/// ParseStringLiteralExpression - This handles the various token types that 2257193326Sed/// form string literals, and also handles string concatenation [C99 5.1.1.2, 2258193326Sed/// translation phase #6]. 2259193326Sed/// 2260245431Sdim/// \verbatim 2261193326Sed/// primary-expression: [C99 6.5.1] 2262193326Sed/// string-literal 2263245431Sdim/// \verbatim 2264235633SdimExprResult Parser::ParseStringLiteralExpression(bool AllowUserDefinedLiteral) { 2265193326Sed assert(isTokenStringLiteral() && "Not a string literal!"); 2266193326Sed 2267193326Sed // String concat. Note that keywords like __func__ and __FUNCTION__ are not 2268193326Sed // considered to be strings for concatenation purposes. 2269226890Sdim SmallVector<Token, 4> StringToks; 2270193326Sed 2271193326Sed do { 2272193326Sed StringToks.push_back(Tok); 2273193326Sed ConsumeStringToken(); 2274193326Sed } while (isTokenStringLiteral()); 2275193326Sed 2276193326Sed // Pass the set of string tokens, ready for concatenation, to the actions. 2277235633Sdim return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size(), 2278235633Sdim AllowUserDefinedLiteral ? getCurScope() : 0); 2279193326Sed} 2280193326Sed 2281235633Sdim/// ParseGenericSelectionExpression - Parse a C11 generic-selection 2282235633Sdim/// [C11 6.5.1.1]. 2283221345Sdim/// 2284245431Sdim/// \verbatim 2285221345Sdim/// generic-selection: 2286221345Sdim/// _Generic ( assignment-expression , generic-assoc-list ) 2287221345Sdim/// generic-assoc-list: 2288221345Sdim/// generic-association 2289221345Sdim/// generic-assoc-list , generic-association 2290221345Sdim/// generic-association: 2291221345Sdim/// type-name : assignment-expression 2292221345Sdim/// default : assignment-expression 2293245431Sdim/// \endverbatim 2294221345SdimExprResult Parser::ParseGenericSelectionExpression() { 2295221345Sdim assert(Tok.is(tok::kw__Generic) && "_Generic keyword expected"); 2296221345Sdim SourceLocation KeyLoc = ConsumeToken(); 2297221345Sdim 2298235633Sdim if (!getLangOpts().C11) 2299235633Sdim Diag(KeyLoc, diag::ext_c11_generic_selection); 2300221345Sdim 2301226890Sdim BalancedDelimiterTracker T(*this, tok::l_paren); 2302226890Sdim if (T.expectAndConsume(diag::err_expected_lparen)) 2303221345Sdim return ExprError(); 2304221345Sdim 2305221345Sdim ExprResult ControllingExpr; 2306221345Sdim { 2307235633Sdim // C11 6.5.1.1p3 "The controlling expression of a generic selection is 2308221345Sdim // not evaluated." 2309221345Sdim EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); 2310221345Sdim ControllingExpr = ParseAssignmentExpression(); 2311221345Sdim if (ControllingExpr.isInvalid()) { 2312263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2313221345Sdim return ExprError(); 2314221345Sdim } 2315221345Sdim } 2316221345Sdim 2317221345Sdim if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "")) { 2318263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2319221345Sdim return ExprError(); 2320221345Sdim } 2321221345Sdim 2322221345Sdim SourceLocation DefaultLoc; 2323245431Sdim TypeVector Types; 2324245431Sdim ExprVector Exprs; 2325221345Sdim while (1) { 2326221345Sdim ParsedType Ty; 2327221345Sdim if (Tok.is(tok::kw_default)) { 2328235633Sdim // C11 6.5.1.1p2 "A generic selection shall have no more than one default 2329221345Sdim // generic association." 2330221345Sdim if (!DefaultLoc.isInvalid()) { 2331221345Sdim Diag(Tok, diag::err_duplicate_default_assoc); 2332221345Sdim Diag(DefaultLoc, diag::note_previous_default_assoc); 2333263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2334221345Sdim return ExprError(); 2335221345Sdim } 2336221345Sdim DefaultLoc = ConsumeToken(); 2337221345Sdim Ty = ParsedType(); 2338221345Sdim } else { 2339221345Sdim ColonProtectionRAIIObject X(*this); 2340221345Sdim TypeResult TR = ParseTypeName(); 2341221345Sdim if (TR.isInvalid()) { 2342263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2343221345Sdim return ExprError(); 2344221345Sdim } 2345221345Sdim Ty = TR.release(); 2346221345Sdim } 2347221345Sdim Types.push_back(Ty); 2348221345Sdim 2349221345Sdim if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "")) { 2350263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2351221345Sdim return ExprError(); 2352221345Sdim } 2353221345Sdim 2354221345Sdim // FIXME: These expressions should be parsed in a potentially potentially 2355221345Sdim // evaluated context. 2356221345Sdim ExprResult ER(ParseAssignmentExpression()); 2357221345Sdim if (ER.isInvalid()) { 2358263509Sdim SkipUntil(tok::r_paren, StopAtSemi); 2359221345Sdim return ExprError(); 2360221345Sdim } 2361221345Sdim Exprs.push_back(ER.release()); 2362221345Sdim 2363221345Sdim if (Tok.isNot(tok::comma)) 2364221345Sdim break; 2365221345Sdim ConsumeToken(); 2366221345Sdim } 2367221345Sdim 2368226890Sdim T.consumeClose(); 2369226890Sdim if (T.getCloseLocation().isInvalid()) 2370221345Sdim return ExprError(); 2371221345Sdim 2372226890Sdim return Actions.ActOnGenericSelectionExpr(KeyLoc, DefaultLoc, 2373226890Sdim T.getCloseLocation(), 2374221345Sdim ControllingExpr.release(), 2375245431Sdim Types, Exprs); 2376221345Sdim} 2377221345Sdim 2378193326Sed/// ParseExpressionList - Used for C/C++ (argument-)expression-list. 2379193326Sed/// 2380245431Sdim/// \verbatim 2381193326Sed/// argument-expression-list: 2382193326Sed/// assignment-expression 2383193326Sed/// argument-expression-list , assignment-expression 2384193326Sed/// 2385193326Sed/// [C++] expression-list: 2386223017Sdim/// [C++] assignment-expression 2387223017Sdim/// [C++] expression-list , assignment-expression 2388193326Sed/// 2389223017Sdim/// [C++0x] expression-list: 2390223017Sdim/// [C++0x] initializer-list 2391223017Sdim/// 2392223017Sdim/// [C++0x] initializer-list 2393223017Sdim/// [C++0x] initializer-clause ...[opt] 2394223017Sdim/// [C++0x] initializer-list , initializer-clause ...[opt] 2395223017Sdim/// 2396223017Sdim/// [C++0x] initializer-clause: 2397223017Sdim/// [C++0x] assignment-expression 2398223017Sdim/// [C++0x] braced-init-list 2399245431Sdim/// \endverbatim 2400226890Sdimbool Parser::ParseExpressionList(SmallVectorImpl<Expr*> &Exprs, 2401252723Sdim SmallVectorImpl<SourceLocation> &CommaLocs, 2402252723Sdim void (Sema::*Completer)(Scope *S, 2403252723Sdim Expr *Data, 2404252723Sdim ArrayRef<Expr *> Args), 2405212904Sdim Expr *Data) { 2406193326Sed while (1) { 2407198092Srdivacky if (Tok.is(tok::code_completion)) { 2408198092Srdivacky if (Completer) 2409235633Sdim (Actions.*Completer)(getCurScope(), Data, Exprs); 2410218893Sdim else 2411218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Expression); 2412226890Sdim cutOffParsing(); 2413226890Sdim return true; 2414198092Srdivacky } 2415223017Sdim 2416223017Sdim ExprResult Expr; 2417252723Sdim if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 2418235633Sdim Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 2419223017Sdim Expr = ParseBraceInitializer(); 2420235633Sdim } else 2421223017Sdim Expr = ParseAssignmentExpression(); 2422223017Sdim 2423218893Sdim if (Tok.is(tok::ellipsis)) 2424218893Sdim Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken()); 2425193326Sed if (Expr.isInvalid()) 2426193326Sed return true; 2427193326Sed 2428193326Sed Exprs.push_back(Expr.release()); 2429193326Sed 2430193326Sed if (Tok.isNot(tok::comma)) 2431193326Sed return false; 2432193326Sed // Move to the next argument, remember where the comma was. 2433193326Sed CommaLocs.push_back(ConsumeToken()); 2434193326Sed } 2435193326Sed} 2436193326Sed 2437263509Sdim/// ParseSimpleExpressionList - A simple comma-separated list of expressions, 2438263509Sdim/// used for misc language extensions. 2439263509Sdim/// 2440263509Sdim/// \verbatim 2441263509Sdim/// simple-expression-list: 2442263509Sdim/// assignment-expression 2443263509Sdim/// simple-expression-list , assignment-expression 2444263509Sdim/// \endverbatim 2445263509Sdimbool 2446263509SdimParser::ParseSimpleExpressionList(SmallVectorImpl<Expr*> &Exprs, 2447263509Sdim SmallVectorImpl<SourceLocation> &CommaLocs) { 2448263509Sdim while (1) { 2449263509Sdim ExprResult Expr = ParseAssignmentExpression(); 2450263509Sdim if (Expr.isInvalid()) 2451263509Sdim return true; 2452263509Sdim 2453263509Sdim Exprs.push_back(Expr.release()); 2454263509Sdim 2455263509Sdim if (Tok.isNot(tok::comma)) 2456263509Sdim return false; 2457263509Sdim 2458263509Sdim // Move to the next argument, remember where the comma was. 2459263509Sdim CommaLocs.push_back(ConsumeToken()); 2460263509Sdim } 2461263509Sdim} 2462263509Sdim 2463193326Sed/// ParseBlockId - Parse a block-id, which roughly looks like int (int x). 2464193326Sed/// 2465245431Sdim/// \verbatim 2466193326Sed/// [clang] block-id: 2467193326Sed/// [clang] specifier-qualifier-list block-declarator 2468245431Sdim/// \endverbatim 2469245431Sdimvoid Parser::ParseBlockId(SourceLocation CaretLoc) { 2470218893Sdim if (Tok.is(tok::code_completion)) { 2471218893Sdim Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type); 2472226890Sdim return cutOffParsing(); 2473218893Sdim } 2474218893Sdim 2475193326Sed // Parse the specifier-qualifier-list piece. 2476221345Sdim DeclSpec DS(AttrFactory); 2477193326Sed ParseSpecifierQualifierList(DS); 2478193326Sed 2479193326Sed // Parse the block-declarator. 2480193326Sed Declarator DeclaratorInfo(DS, Declarator::BlockLiteralContext); 2481193326Sed ParseDeclarator(DeclaratorInfo); 2482193326Sed 2483193326Sed // We do this for: ^ __attribute__((noreturn)) {, as DS has the attributes. 2484221345Sdim DeclaratorInfo.takeAttributes(DS.getAttributes(), SourceLocation()); 2485193326Sed 2486218893Sdim MaybeParseGNUAttributes(DeclaratorInfo); 2487193326Sed 2488193326Sed // Inform sema that we are starting a block. 2489245431Sdim Actions.ActOnBlockArguments(CaretLoc, DeclaratorInfo, getCurScope()); 2490193326Sed} 2491193326Sed 2492193326Sed/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks 2493193326Sed/// like ^(int x){ return x+1; } 2494193326Sed/// 2495245431Sdim/// \verbatim 2496193326Sed/// block-literal: 2497193326Sed/// [clang] '^' block-args[opt] compound-statement 2498193326Sed/// [clang] '^' block-id compound-statement 2499193326Sed/// [clang] block-args: 2500193326Sed/// [clang] '(' parameter-list ')' 2501245431Sdim/// \endverbatim 2502212904SdimExprResult Parser::ParseBlockLiteralExpression() { 2503193326Sed assert(Tok.is(tok::caret) && "block literal starts with ^"); 2504193326Sed SourceLocation CaretLoc = ConsumeToken(); 2505193326Sed 2506193326Sed PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), CaretLoc, 2507193326Sed "block literal parsing"); 2508193326Sed 2509198092Srdivacky // Enter a scope to hold everything within the block. This includes the 2510193326Sed // argument decls, decls within the compound expression, etc. This also 2511193326Sed // allows determining whether a variable reference inside the block is 2512193326Sed // within or outside of the block. 2513193326Sed ParseScope BlockScope(this, Scope::BlockScope | Scope::FnScope | 2514193326Sed Scope::DeclScope); 2515193326Sed 2516193326Sed // Inform sema that we are starting a block. 2517210299Sed Actions.ActOnBlockStart(CaretLoc, getCurScope()); 2518198092Srdivacky 2519193326Sed // Parse the return type if present. 2520221345Sdim DeclSpec DS(AttrFactory); 2521193326Sed Declarator ParamInfo(DS, Declarator::BlockLiteralContext); 2522193326Sed // FIXME: Since the return type isn't actually parsed, it can't be used to 2523193326Sed // fill ParamInfo with an initial valid range, so do it manually. 2524193326Sed ParamInfo.SetSourceRange(SourceRange(Tok.getLocation(), Tok.getLocation())); 2525193326Sed 2526193326Sed // If this block has arguments, parse them. There is no ambiguity here with 2527193326Sed // the expression case, because the expression case requires a parameter list. 2528193326Sed if (Tok.is(tok::l_paren)) { 2529193326Sed ParseParenDeclarator(ParamInfo); 2530193326Sed // Parse the pieces after the identifier as if we had "int(...)". 2531193326Sed // SetIdentifier sets the source range end, but in this case we're past 2532193326Sed // that location. 2533193326Sed SourceLocation Tmp = ParamInfo.getSourceRange().getEnd(); 2534193326Sed ParamInfo.SetIdentifier(0, CaretLoc); 2535193326Sed ParamInfo.SetRangeEnd(Tmp); 2536193326Sed if (ParamInfo.isInvalidType()) { 2537193326Sed // If there was an error parsing the arguments, they may have 2538193326Sed // tried to use ^(x+y) which requires an argument list. Just 2539193326Sed // skip the whole block literal. 2540210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2541193326Sed return ExprError(); 2542193326Sed } 2543193326Sed 2544218893Sdim MaybeParseGNUAttributes(ParamInfo); 2545193326Sed 2546193326Sed // Inform sema that we are starting a block. 2547245431Sdim Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope()); 2548193326Sed } else if (!Tok.is(tok::l_brace)) { 2549245431Sdim ParseBlockId(CaretLoc); 2550193326Sed } else { 2551193326Sed // Otherwise, pretend we saw (void). 2552221345Sdim ParsedAttributes attrs(AttrFactory); 2553245431Sdim SourceLocation NoLoc; 2554245431Sdim ParamInfo.AddTypeInfo(DeclaratorChunk::getFunction(/*HasProto=*/true, 2555245431Sdim /*IsAmbiguous=*/false, 2556245431Sdim /*RParenLoc=*/NoLoc, 2557245431Sdim /*ArgInfo=*/0, 2558245431Sdim /*NumArgs=*/0, 2559245431Sdim /*EllipsisLoc=*/NoLoc, 2560245431Sdim /*RParenLoc=*/NoLoc, 2561245431Sdim /*TypeQuals=*/0, 2562245431Sdim /*RefQualifierIsLvalueRef=*/true, 2563245431Sdim /*RefQualifierLoc=*/NoLoc, 2564245431Sdim /*ConstQualifierLoc=*/NoLoc, 2565245431Sdim /*VolatileQualifierLoc=*/NoLoc, 2566245431Sdim /*MutableLoc=*/NoLoc, 2567245431Sdim EST_None, 2568245431Sdim /*ESpecLoc=*/NoLoc, 2569245431Sdim /*Exceptions=*/0, 2570245431Sdim /*ExceptionRanges=*/0, 2571245431Sdim /*NumExceptions=*/0, 2572245431Sdim /*NoexceptExpr=*/0, 2573245431Sdim CaretLoc, CaretLoc, 2574245431Sdim ParamInfo), 2575221345Sdim attrs, CaretLoc); 2576193326Sed 2577218893Sdim MaybeParseGNUAttributes(ParamInfo); 2578193326Sed 2579193326Sed // Inform sema that we are starting a block. 2580245431Sdim Actions.ActOnBlockArguments(CaretLoc, ParamInfo, getCurScope()); 2581193326Sed } 2582193326Sed 2583193326Sed 2584212904Sdim ExprResult Result(true); 2585193326Sed if (!Tok.is(tok::l_brace)) { 2586193326Sed // Saw something like: ^expr 2587193326Sed Diag(Tok, diag::err_expected_expression); 2588210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2589193326Sed return ExprError(); 2590193326Sed } 2591198092Srdivacky 2592212904Sdim StmtResult Stmt(ParseCompoundStatementBody()); 2593221345Sdim BlockScope.Exit(); 2594193326Sed if (!Stmt.isInvalid()) 2595212904Sdim Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.take(), getCurScope()); 2596193326Sed else 2597210299Sed Actions.ActOnBlockError(CaretLoc, getCurScope()); 2598245431Sdim return Result; 2599193326Sed} 2600235633Sdim 2601235633Sdim/// ParseObjCBoolLiteral - This handles the objective-c Boolean literals. 2602235633Sdim/// 2603235633Sdim/// '__objc_yes' 2604235633Sdim/// '__objc_no' 2605235633SdimExprResult Parser::ParseObjCBoolLiteral() { 2606235633Sdim tok::TokenKind Kind = Tok.getKind(); 2607235633Sdim return Actions.ActOnObjCBoolLiteral(ConsumeToken(), Kind); 2608235633Sdim} 2609