10b57cec5SDimitry Andric //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Expression parsing implementation for C++. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 1355e4f9d5SDimitry Andric #include "clang/AST/Decl.h" 140b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 1555e4f9d5SDimitry Andric #include "clang/AST/ExprCXX.h" 160b57cec5SDimitry Andric #include "clang/Basic/PrettyStackTrace.h" 17*bdd1243dSDimitry Andric #include "clang/Basic/TokenKinds.h" 180b57cec5SDimitry Andric #include "clang/Lex/LiteralSupport.h" 190b57cec5SDimitry Andric #include "clang/Parse/ParseDiagnostic.h" 20fe6060f1SDimitry Andric #include "clang/Parse/Parser.h" 210b57cec5SDimitry Andric #include "clang/Parse/RAIIObjectsForParser.h" 220b57cec5SDimitry Andric #include "clang/Sema/DeclSpec.h" 230b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h" 240b57cec5SDimitry Andric #include "clang/Sema/Scope.h" 25*bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h" 260b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 270b57cec5SDimitry Andric #include <numeric> 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric using namespace clang; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric static int SelectDigraphErrorMessage(tok::TokenKind Kind) { 320b57cec5SDimitry Andric switch (Kind) { 330b57cec5SDimitry Andric // template name 340b57cec5SDimitry Andric case tok::unknown: return 0; 350b57cec5SDimitry Andric // casts 365ffd83dbSDimitry Andric case tok::kw_addrspace_cast: return 1; 375ffd83dbSDimitry Andric case tok::kw_const_cast: return 2; 385ffd83dbSDimitry Andric case tok::kw_dynamic_cast: return 3; 395ffd83dbSDimitry Andric case tok::kw_reinterpret_cast: return 4; 405ffd83dbSDimitry Andric case tok::kw_static_cast: return 5; 410b57cec5SDimitry Andric default: 420b57cec5SDimitry Andric llvm_unreachable("Unknown type for digraph error message."); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric // Are the two tokens adjacent in the same source file? 470b57cec5SDimitry Andric bool Parser::areTokensAdjacent(const Token &First, const Token &Second) { 480b57cec5SDimitry Andric SourceManager &SM = PP.getSourceManager(); 490b57cec5SDimitry Andric SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation()); 500b57cec5SDimitry Andric SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength()); 510b57cec5SDimitry Andric return FirstEnd == SM.getSpellingLoc(Second.getLocation()); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // Suggest fixit for "<::" after a cast. 550b57cec5SDimitry Andric static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken, 560b57cec5SDimitry Andric Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) { 570b57cec5SDimitry Andric // Pull '<:' and ':' off token stream. 580b57cec5SDimitry Andric if (!AtDigraph) 590b57cec5SDimitry Andric PP.Lex(DigraphToken); 600b57cec5SDimitry Andric PP.Lex(ColonToken); 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric SourceRange Range; 630b57cec5SDimitry Andric Range.setBegin(DigraphToken.getLocation()); 640b57cec5SDimitry Andric Range.setEnd(ColonToken.getLocation()); 650b57cec5SDimitry Andric P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph) 660b57cec5SDimitry Andric << SelectDigraphErrorMessage(Kind) 670b57cec5SDimitry Andric << FixItHint::CreateReplacement(Range, "< ::"); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // Update token information to reflect their change in token type. 700b57cec5SDimitry Andric ColonToken.setKind(tok::coloncolon); 710b57cec5SDimitry Andric ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1)); 720b57cec5SDimitry Andric ColonToken.setLength(2); 730b57cec5SDimitry Andric DigraphToken.setKind(tok::less); 740b57cec5SDimitry Andric DigraphToken.setLength(1); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // Push new tokens back to token stream. 770b57cec5SDimitry Andric PP.EnterToken(ColonToken, /*IsReinject*/ true); 780b57cec5SDimitry Andric if (!AtDigraph) 790b57cec5SDimitry Andric PP.EnterToken(DigraphToken, /*IsReinject*/ true); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Check for '<::' which should be '< ::' instead of '[:' when following 830b57cec5SDimitry Andric // a template name. 840b57cec5SDimitry Andric void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType, 850b57cec5SDimitry Andric bool EnteringContext, 860b57cec5SDimitry Andric IdentifierInfo &II, CXXScopeSpec &SS) { 870b57cec5SDimitry Andric if (!Next.is(tok::l_square) || Next.getLength() != 2) 880b57cec5SDimitry Andric return; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric Token SecondToken = GetLookAheadToken(2); 910b57cec5SDimitry Andric if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken)) 920b57cec5SDimitry Andric return; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric TemplateTy Template; 950b57cec5SDimitry Andric UnqualifiedId TemplateName; 960b57cec5SDimitry Andric TemplateName.setIdentifier(&II, Tok.getLocation()); 970b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 980b57cec5SDimitry Andric if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false, 990b57cec5SDimitry Andric TemplateName, ObjectType, EnteringContext, 1000b57cec5SDimitry Andric Template, MemberOfUnknownSpecialization)) 1010b57cec5SDimitry Andric return; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric FixDigraph(*this, PP, Next, SecondToken, tok::unknown, 1040b57cec5SDimitry Andric /*AtDigraph*/false); 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric /// Parse global scope or nested-name-specifier if present. 1080b57cec5SDimitry Andric /// 1090b57cec5SDimitry Andric /// Parses a C++ global scope specifier ('::') or nested-name-specifier (which 1100b57cec5SDimitry Andric /// may be preceded by '::'). Note that this routine will not parse ::new or 1110b57cec5SDimitry Andric /// ::delete; it will just leave them in the token stream. 1120b57cec5SDimitry Andric /// 1130b57cec5SDimitry Andric /// '::'[opt] nested-name-specifier 1140b57cec5SDimitry Andric /// '::' 1150b57cec5SDimitry Andric /// 1160b57cec5SDimitry Andric /// nested-name-specifier: 1170b57cec5SDimitry Andric /// type-name '::' 1180b57cec5SDimitry Andric /// namespace-name '::' 1190b57cec5SDimitry Andric /// nested-name-specifier identifier '::' 1200b57cec5SDimitry Andric /// nested-name-specifier 'template'[opt] simple-template-id '::' 1210b57cec5SDimitry Andric /// 1220b57cec5SDimitry Andric /// 1230b57cec5SDimitry Andric /// \param SS the scope specifier that will be set to the parsed 1240b57cec5SDimitry Andric /// nested-name-specifier (or empty) 1250b57cec5SDimitry Andric /// 1260b57cec5SDimitry Andric /// \param ObjectType if this nested-name-specifier is being parsed following 1270b57cec5SDimitry Andric /// the "." or "->" of a member access expression, this parameter provides the 1280b57cec5SDimitry Andric /// type of the object whose members are being accessed. 1290b57cec5SDimitry Andric /// 1305ffd83dbSDimitry Andric /// \param ObjectHadErrors if this unqualified-id occurs within a member access 1315ffd83dbSDimitry Andric /// expression, indicates whether the original subexpressions had any errors. 1325ffd83dbSDimitry Andric /// When true, diagnostics for missing 'template' keyword will be supressed. 1335ffd83dbSDimitry Andric /// 1340b57cec5SDimitry Andric /// \param EnteringContext whether we will be entering into the context of 1350b57cec5SDimitry Andric /// the nested-name-specifier after parsing it. 1360b57cec5SDimitry Andric /// 1370b57cec5SDimitry Andric /// \param MayBePseudoDestructor When non-NULL, points to a flag that 1380b57cec5SDimitry Andric /// indicates whether this nested-name-specifier may be part of a 1390b57cec5SDimitry Andric /// pseudo-destructor name. In this case, the flag will be set false 1405ffd83dbSDimitry Andric /// if we don't actually end up parsing a destructor name. Moreover, 1410b57cec5SDimitry Andric /// if we do end up determining that we are parsing a destructor name, 1420b57cec5SDimitry Andric /// the last component of the nested-name-specifier is not parsed as 1430b57cec5SDimitry Andric /// part of the scope specifier. 1440b57cec5SDimitry Andric /// 1450b57cec5SDimitry Andric /// \param IsTypename If \c true, this nested-name-specifier is known to be 1460b57cec5SDimitry Andric /// part of a type name. This is used to improve error recovery. 1470b57cec5SDimitry Andric /// 1480b57cec5SDimitry Andric /// \param LastII When non-NULL, points to an IdentifierInfo* that will be 1490b57cec5SDimitry Andric /// filled in with the leading identifier in the last component of the 1500b57cec5SDimitry Andric /// nested-name-specifier, if any. 1510b57cec5SDimitry Andric /// 1520b57cec5SDimitry Andric /// \param OnlyNamespace If true, only considers namespaces in lookup. 1530b57cec5SDimitry Andric /// 154480093f4SDimitry Andric /// 1550b57cec5SDimitry Andric /// \returns true if there was an error parsing a scope specifier 1565ffd83dbSDimitry Andric bool Parser::ParseOptionalCXXScopeSpecifier( 1575ffd83dbSDimitry Andric CXXScopeSpec &SS, ParsedType ObjectType, bool ObjectHadErrors, 1585ffd83dbSDimitry Andric bool EnteringContext, bool *MayBePseudoDestructor, bool IsTypename, 1595ffd83dbSDimitry Andric IdentifierInfo **LastII, bool OnlyNamespace, bool InUsingDeclaration) { 1600b57cec5SDimitry Andric assert(getLangOpts().CPlusPlus && 1610b57cec5SDimitry Andric "Call sites of this function should be guarded by checking for C++"); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric if (Tok.is(tok::annot_cxxscope)) { 1640b57cec5SDimitry Andric assert(!LastII && "want last identifier but have already annotated scope"); 1650b57cec5SDimitry Andric assert(!MayBePseudoDestructor && "unexpected annot_cxxscope"); 1660b57cec5SDimitry Andric Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), 1670b57cec5SDimitry Andric Tok.getAnnotationRange(), 1680b57cec5SDimitry Andric SS); 1690b57cec5SDimitry Andric ConsumeAnnotationToken(); 1700b57cec5SDimitry Andric return false; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // Has to happen before any "return false"s in this function. 1740b57cec5SDimitry Andric bool CheckForDestructor = false; 1750b57cec5SDimitry Andric if (MayBePseudoDestructor && *MayBePseudoDestructor) { 1760b57cec5SDimitry Andric CheckForDestructor = true; 1770b57cec5SDimitry Andric *MayBePseudoDestructor = false; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric if (LastII) 1810b57cec5SDimitry Andric *LastII = nullptr; 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric bool HasScopeSpecifier = false; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric if (Tok.is(tok::coloncolon)) { 1860b57cec5SDimitry Andric // ::new and ::delete aren't nested-name-specifiers. 1870b57cec5SDimitry Andric tok::TokenKind NextKind = NextToken().getKind(); 1880b57cec5SDimitry Andric if (NextKind == tok::kw_new || NextKind == tok::kw_delete) 1890b57cec5SDimitry Andric return false; 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric if (NextKind == tok::l_brace) { 1920b57cec5SDimitry Andric // It is invalid to have :: {, consume the scope qualifier and pretend 1930b57cec5SDimitry Andric // like we never saw it. 1940b57cec5SDimitry Andric Diag(ConsumeToken(), diag::err_expected) << tok::identifier; 1950b57cec5SDimitry Andric } else { 1960b57cec5SDimitry Andric // '::' - Global scope qualifier. 1970b57cec5SDimitry Andric if (Actions.ActOnCXXGlobalScopeSpecifier(ConsumeToken(), SS)) 1980b57cec5SDimitry Andric return true; 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric HasScopeSpecifier = true; 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric if (Tok.is(tok::kw___super)) { 2050b57cec5SDimitry Andric SourceLocation SuperLoc = ConsumeToken(); 2060b57cec5SDimitry Andric if (!Tok.is(tok::coloncolon)) { 2070b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected_coloncolon_after_super); 2080b57cec5SDimitry Andric return true; 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric return Actions.ActOnSuperScopeSpecifier(SuperLoc, ConsumeToken(), SS); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric if (!HasScopeSpecifier && 2150b57cec5SDimitry Andric Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { 2160b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 2170b57cec5SDimitry Andric SourceLocation DeclLoc = Tok.getLocation(); 2180b57cec5SDimitry Andric SourceLocation EndLoc = ParseDecltypeSpecifier(DS); 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric SourceLocation CCLoc; 2210b57cec5SDimitry Andric // Work around a standard defect: 'decltype(auto)::' is not a 2220b57cec5SDimitry Andric // nested-name-specifier. 2230b57cec5SDimitry Andric if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto || 2240b57cec5SDimitry Andric !TryConsumeToken(tok::coloncolon, CCLoc)) { 2250b57cec5SDimitry Andric AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc); 2260b57cec5SDimitry Andric return false; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc)) 2300b57cec5SDimitry Andric SS.SetInvalid(SourceRange(DeclLoc, CCLoc)); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric HasScopeSpecifier = true; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric // Preferred type might change when parsing qualifiers, we need the original. 2360b57cec5SDimitry Andric auto SavedType = PreferredType; 2370b57cec5SDimitry Andric while (true) { 2380b57cec5SDimitry Andric if (HasScopeSpecifier) { 2390b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 240fe6060f1SDimitry Andric cutOffParsing(); 2410b57cec5SDimitry Andric // Code completion for a nested-name-specifier, where the code 2420b57cec5SDimitry Andric // completion token follows the '::'. 2430b57cec5SDimitry Andric Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext, 244480093f4SDimitry Andric InUsingDeclaration, ObjectType.get(), 2450b57cec5SDimitry Andric SavedType.get(SS.getBeginLoc())); 2460b57cec5SDimitry Andric // Include code completion token into the range of the scope otherwise 2470b57cec5SDimitry Andric // when we try to annotate the scope tokens the dangling code completion 2480b57cec5SDimitry Andric // token will cause assertion in 2490b57cec5SDimitry Andric // Preprocessor::AnnotatePreviousCachedTokens. 2500b57cec5SDimitry Andric SS.setEndLoc(Tok.getLocation()); 2510b57cec5SDimitry Andric return true; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric // C++ [basic.lookup.classref]p5: 2550b57cec5SDimitry Andric // If the qualified-id has the form 2560b57cec5SDimitry Andric // 2570b57cec5SDimitry Andric // ::class-name-or-namespace-name::... 2580b57cec5SDimitry Andric // 2590b57cec5SDimitry Andric // the class-name-or-namespace-name is looked up in global scope as a 2600b57cec5SDimitry Andric // class-name or namespace-name. 2610b57cec5SDimitry Andric // 2620b57cec5SDimitry Andric // To implement this, we clear out the object type as soon as we've 2630b57cec5SDimitry Andric // seen a leading '::' or part of a nested-name-specifier. 2640b57cec5SDimitry Andric ObjectType = nullptr; 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // nested-name-specifier: 2680b57cec5SDimitry Andric // nested-name-specifier 'template'[opt] simple-template-id '::' 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Parse the optional 'template' keyword, then make sure we have 2710b57cec5SDimitry Andric // 'identifier <' after it. 2720b57cec5SDimitry Andric if (Tok.is(tok::kw_template)) { 2730b57cec5SDimitry Andric // If we don't have a scope specifier or an object type, this isn't a 2740b57cec5SDimitry Andric // nested-name-specifier, since they aren't allowed to start with 2750b57cec5SDimitry Andric // 'template'. 2760b57cec5SDimitry Andric if (!HasScopeSpecifier && !ObjectType) 2770b57cec5SDimitry Andric break; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric TentativeParsingAction TPA(*this); 2800b57cec5SDimitry Andric SourceLocation TemplateKWLoc = ConsumeToken(); 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric UnqualifiedId TemplateName; 2830b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 2840b57cec5SDimitry Andric // Consume the identifier. 2850b57cec5SDimitry Andric TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2860b57cec5SDimitry Andric ConsumeToken(); 2870b57cec5SDimitry Andric } else if (Tok.is(tok::kw_operator)) { 2880b57cec5SDimitry Andric // We don't need to actually parse the unqualified-id in this case, 2890b57cec5SDimitry Andric // because a simple-template-id cannot start with 'operator', but 2900b57cec5SDimitry Andric // go ahead and parse it anyway for consistency with the case where 2910b57cec5SDimitry Andric // we already annotated the template-id. 2920b57cec5SDimitry Andric if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, 2930b57cec5SDimitry Andric TemplateName)) { 2940b57cec5SDimitry Andric TPA.Commit(); 2950b57cec5SDimitry Andric break; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric if (TemplateName.getKind() != UnqualifiedIdKind::IK_OperatorFunctionId && 2990b57cec5SDimitry Andric TemplateName.getKind() != UnqualifiedIdKind::IK_LiteralOperatorId) { 3000b57cec5SDimitry Andric Diag(TemplateName.getSourceRange().getBegin(), 3010b57cec5SDimitry Andric diag::err_id_after_template_in_nested_name_spec) 3020b57cec5SDimitry Andric << TemplateName.getSourceRange(); 3030b57cec5SDimitry Andric TPA.Commit(); 3040b57cec5SDimitry Andric break; 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric } else { 3070b57cec5SDimitry Andric TPA.Revert(); 3080b57cec5SDimitry Andric break; 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric // If the next token is not '<', we have a qualified-id that refers 3120b57cec5SDimitry Andric // to a template name, such as T::template apply, but is not a 3130b57cec5SDimitry Andric // template-id. 3140b57cec5SDimitry Andric if (Tok.isNot(tok::less)) { 3150b57cec5SDimitry Andric TPA.Revert(); 3160b57cec5SDimitry Andric break; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // Commit to parsing the template-id. 3200b57cec5SDimitry Andric TPA.Commit(); 3210b57cec5SDimitry Andric TemplateTy Template; 3225ffd83dbSDimitry Andric TemplateNameKind TNK = Actions.ActOnTemplateName( 3230b57cec5SDimitry Andric getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType, 3245ffd83dbSDimitry Andric EnteringContext, Template, /*AllowInjectedClassName*/ true); 3250b57cec5SDimitry Andric if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc, 3260b57cec5SDimitry Andric TemplateName, false)) 3270b57cec5SDimitry Andric return true; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric continue; 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { 3330b57cec5SDimitry Andric // We have 3340b57cec5SDimitry Andric // 3350b57cec5SDimitry Andric // template-id '::' 3360b57cec5SDimitry Andric // 3370b57cec5SDimitry Andric // So we need to check whether the template-id is a simple-template-id of 3380b57cec5SDimitry Andric // the right kind (it should name a type or be dependent), and then 3390b57cec5SDimitry Andric // convert it into a type within the nested-name-specifier. 3400b57cec5SDimitry Andric TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 3410b57cec5SDimitry Andric if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) { 3420b57cec5SDimitry Andric *MayBePseudoDestructor = true; 3430b57cec5SDimitry Andric return false; 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric if (LastII) 3470b57cec5SDimitry Andric *LastII = TemplateId->Name; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric // Consume the template-id token. 3500b57cec5SDimitry Andric ConsumeAnnotationToken(); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); 3530b57cec5SDimitry Andric SourceLocation CCLoc = ConsumeToken(); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric HasScopeSpecifier = true; 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 3580b57cec5SDimitry Andric TemplateId->NumArgs); 3590b57cec5SDimitry Andric 3605ffd83dbSDimitry Andric if (TemplateId->isInvalid() || 3615ffd83dbSDimitry Andric Actions.ActOnCXXNestedNameSpecifier(getCurScope(), 3620b57cec5SDimitry Andric SS, 3630b57cec5SDimitry Andric TemplateId->TemplateKWLoc, 3640b57cec5SDimitry Andric TemplateId->Template, 3650b57cec5SDimitry Andric TemplateId->TemplateNameLoc, 3660b57cec5SDimitry Andric TemplateId->LAngleLoc, 3670b57cec5SDimitry Andric TemplateArgsPtr, 3680b57cec5SDimitry Andric TemplateId->RAngleLoc, 3690b57cec5SDimitry Andric CCLoc, 3700b57cec5SDimitry Andric EnteringContext)) { 3710b57cec5SDimitry Andric SourceLocation StartLoc 3720b57cec5SDimitry Andric = SS.getBeginLoc().isValid()? SS.getBeginLoc() 3730b57cec5SDimitry Andric : TemplateId->TemplateNameLoc; 3740b57cec5SDimitry Andric SS.SetInvalid(SourceRange(StartLoc, CCLoc)); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric continue; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // The rest of the nested-name-specifier possibilities start with 3810b57cec5SDimitry Andric // tok::identifier. 3820b57cec5SDimitry Andric if (Tok.isNot(tok::identifier)) 3830b57cec5SDimitry Andric break; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric IdentifierInfo &II = *Tok.getIdentifierInfo(); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // nested-name-specifier: 3880b57cec5SDimitry Andric // type-name '::' 3890b57cec5SDimitry Andric // namespace-name '::' 3900b57cec5SDimitry Andric // nested-name-specifier identifier '::' 3910b57cec5SDimitry Andric Token Next = NextToken(); 3920b57cec5SDimitry Andric Sema::NestedNameSpecInfo IdInfo(&II, Tok.getLocation(), Next.getLocation(), 3930b57cec5SDimitry Andric ObjectType); 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover 3960b57cec5SDimitry Andric // and emit a fixit hint for it. 3970b57cec5SDimitry Andric if (Next.is(tok::colon) && !ColonIsSacred) { 3980b57cec5SDimitry Andric if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, IdInfo, 3990b57cec5SDimitry Andric EnteringContext) && 4000b57cec5SDimitry Andric // If the token after the colon isn't an identifier, it's still an 4010b57cec5SDimitry Andric // error, but they probably meant something else strange so don't 4020b57cec5SDimitry Andric // recover like this. 4030b57cec5SDimitry Andric PP.LookAhead(1).is(tok::identifier)) { 4040b57cec5SDimitry Andric Diag(Next, diag::err_unexpected_colon_in_nested_name_spec) 4050b57cec5SDimitry Andric << FixItHint::CreateReplacement(Next.getLocation(), "::"); 4060b57cec5SDimitry Andric // Recover as if the user wrote '::'. 4070b57cec5SDimitry Andric Next.setKind(tok::coloncolon); 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric if (Next.is(tok::coloncolon) && GetLookAheadToken(2).is(tok::l_brace)) { 4120b57cec5SDimitry Andric // It is invalid to have :: {, consume the scope qualifier and pretend 4130b57cec5SDimitry Andric // like we never saw it. 4140b57cec5SDimitry Andric Token Identifier = Tok; // Stash away the identifier. 4150b57cec5SDimitry Andric ConsumeToken(); // Eat the identifier, current token is now '::'. 4160b57cec5SDimitry Andric Diag(PP.getLocForEndOfToken(ConsumeToken()), diag::err_expected) 4170b57cec5SDimitry Andric << tok::identifier; 4180b57cec5SDimitry Andric UnconsumeToken(Identifier); // Stick the identifier back. 4190b57cec5SDimitry Andric Next = NextToken(); // Point Next at the '{' token. 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric if (Next.is(tok::coloncolon)) { 4235ffd83dbSDimitry Andric if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) { 4240b57cec5SDimitry Andric *MayBePseudoDestructor = true; 4250b57cec5SDimitry Andric return false; 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric if (ColonIsSacred) { 4290b57cec5SDimitry Andric const Token &Next2 = GetLookAheadToken(2); 4300b57cec5SDimitry Andric if (Next2.is(tok::kw_private) || Next2.is(tok::kw_protected) || 4310b57cec5SDimitry Andric Next2.is(tok::kw_public) || Next2.is(tok::kw_virtual)) { 4320b57cec5SDimitry Andric Diag(Next2, diag::err_unexpected_token_in_nested_name_spec) 4330b57cec5SDimitry Andric << Next2.getName() 4340b57cec5SDimitry Andric << FixItHint::CreateReplacement(Next.getLocation(), ":"); 4350b57cec5SDimitry Andric Token ColonColon; 4360b57cec5SDimitry Andric PP.Lex(ColonColon); 4370b57cec5SDimitry Andric ColonColon.setKind(tok::colon); 4380b57cec5SDimitry Andric PP.EnterToken(ColonColon, /*IsReinject*/ true); 4390b57cec5SDimitry Andric break; 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric if (LastII) 4440b57cec5SDimitry Andric *LastII = &II; 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric // We have an identifier followed by a '::'. Lookup this name 4470b57cec5SDimitry Andric // as the name in a nested-name-specifier. 4480b57cec5SDimitry Andric Token Identifier = Tok; 4490b57cec5SDimitry Andric SourceLocation IdLoc = ConsumeToken(); 4500b57cec5SDimitry Andric assert(Tok.isOneOf(tok::coloncolon, tok::colon) && 4510b57cec5SDimitry Andric "NextToken() not working properly!"); 4520b57cec5SDimitry Andric Token ColonColon = Tok; 4530b57cec5SDimitry Andric SourceLocation CCLoc = ConsumeToken(); 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric bool IsCorrectedToColon = false; 4560b57cec5SDimitry Andric bool *CorrectionFlagPtr = ColonIsSacred ? &IsCorrectedToColon : nullptr; 4570b57cec5SDimitry Andric if (Actions.ActOnCXXNestedNameSpecifier( 45881ad6265SDimitry Andric getCurScope(), IdInfo, EnteringContext, SS, CorrectionFlagPtr, 45981ad6265SDimitry Andric OnlyNamespace)) { 4600b57cec5SDimitry Andric // Identifier is not recognized as a nested name, but we can have 4610b57cec5SDimitry Andric // mistyped '::' instead of ':'. 4620b57cec5SDimitry Andric if (CorrectionFlagPtr && IsCorrectedToColon) { 4630b57cec5SDimitry Andric ColonColon.setKind(tok::colon); 4640b57cec5SDimitry Andric PP.EnterToken(Tok, /*IsReinject*/ true); 4650b57cec5SDimitry Andric PP.EnterToken(ColonColon, /*IsReinject*/ true); 4660b57cec5SDimitry Andric Tok = Identifier; 4670b57cec5SDimitry Andric break; 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric SS.SetInvalid(SourceRange(IdLoc, CCLoc)); 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric HasScopeSpecifier = true; 4720b57cec5SDimitry Andric continue; 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric // nested-name-specifier: 4780b57cec5SDimitry Andric // type-name '<' 4790b57cec5SDimitry Andric if (Next.is(tok::less)) { 480480093f4SDimitry Andric 4810b57cec5SDimitry Andric TemplateTy Template; 4820b57cec5SDimitry Andric UnqualifiedId TemplateName; 4830b57cec5SDimitry Andric TemplateName.setIdentifier(&II, Tok.getLocation()); 4840b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 4850b57cec5SDimitry Andric if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, 4860b57cec5SDimitry Andric /*hasTemplateKeyword=*/false, 4870b57cec5SDimitry Andric TemplateName, 4880b57cec5SDimitry Andric ObjectType, 4890b57cec5SDimitry Andric EnteringContext, 4900b57cec5SDimitry Andric Template, 4910b57cec5SDimitry Andric MemberOfUnknownSpecialization)) { 4920b57cec5SDimitry Andric // If lookup didn't find anything, we treat the name as a template-name 4930b57cec5SDimitry Andric // anyway. C++20 requires this, and in prior language modes it improves 4940b57cec5SDimitry Andric // error recovery. But before we commit to this, check that we actually 4950b57cec5SDimitry Andric // have something that looks like a template-argument-list next. 4960b57cec5SDimitry Andric if (!IsTypename && TNK == TNK_Undeclared_template && 4970b57cec5SDimitry Andric isTemplateArgumentList(1) == TPResult::False) 4980b57cec5SDimitry Andric break; 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric // We have found a template name, so annotate this token 5010b57cec5SDimitry Andric // with a template-id annotation. We do not permit the 5020b57cec5SDimitry Andric // template-id to be translated into a type annotation, 5030b57cec5SDimitry Andric // because some clients (e.g., the parsing of class template 5040b57cec5SDimitry Andric // specializations) still want to see the original template-id 505480093f4SDimitry Andric // token, and it might not be a type at all (e.g. a concept name in a 506480093f4SDimitry Andric // type-constraint). 5070b57cec5SDimitry Andric ConsumeToken(); 5080b57cec5SDimitry Andric if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 5090b57cec5SDimitry Andric TemplateName, false)) 5100b57cec5SDimitry Andric return true; 5110b57cec5SDimitry Andric continue; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) && 5150b57cec5SDimitry Andric (IsTypename || isTemplateArgumentList(1) == TPResult::True)) { 5165ffd83dbSDimitry Andric // If we had errors before, ObjectType can be dependent even without any 5175ffd83dbSDimitry Andric // templates. Do not report missing template keyword in that case. 5185ffd83dbSDimitry Andric if (!ObjectHadErrors) { 5190b57cec5SDimitry Andric // We have something like t::getAs<T>, where getAs is a 5200b57cec5SDimitry Andric // member of an unknown specialization. However, this will only 5210b57cec5SDimitry Andric // parse correctly as a template, so suggest the keyword 'template' 5220b57cec5SDimitry Andric // before 'getAs' and treat this as a dependent template name. 5230b57cec5SDimitry Andric unsigned DiagID = diag::err_missing_dependent_template_keyword; 5240b57cec5SDimitry Andric if (getLangOpts().MicrosoftExt) 5250b57cec5SDimitry Andric DiagID = diag::warn_missing_dependent_template_keyword; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric Diag(Tok.getLocation(), DiagID) 5280b57cec5SDimitry Andric << II.getName() 5290b57cec5SDimitry Andric << FixItHint::CreateInsertion(Tok.getLocation(), "template "); 5305ffd83dbSDimitry Andric } 5310b57cec5SDimitry Andric 5325ffd83dbSDimitry Andric SourceLocation TemplateNameLoc = ConsumeToken(); 5335ffd83dbSDimitry Andric 5345ffd83dbSDimitry Andric TemplateNameKind TNK = Actions.ActOnTemplateName( 5355ffd83dbSDimitry Andric getCurScope(), SS, TemplateNameLoc, TemplateName, ObjectType, 5365ffd83dbSDimitry Andric EnteringContext, Template, /*AllowInjectedClassName*/ true); 5370b57cec5SDimitry Andric if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 5380b57cec5SDimitry Andric TemplateName, false)) 5390b57cec5SDimitry Andric return true; 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric continue; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric // We don't have any tokens that form the beginning of a 5460b57cec5SDimitry Andric // nested-name-specifier, so we're done. 5470b57cec5SDimitry Andric break; 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric // Even if we didn't see any pieces of a nested-name-specifier, we 5510b57cec5SDimitry Andric // still check whether there is a tilde in this position, which 5520b57cec5SDimitry Andric // indicates a potential pseudo-destructor. 5535ffd83dbSDimitry Andric if (CheckForDestructor && !HasScopeSpecifier && Tok.is(tok::tilde)) 5540b57cec5SDimitry Andric *MayBePseudoDestructor = true; 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric return false; 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric 559a7dea167SDimitry Andric ExprResult Parser::tryParseCXXIdExpression(CXXScopeSpec &SS, 560a7dea167SDimitry Andric bool isAddressOfOperand, 5610b57cec5SDimitry Andric Token &Replacement) { 562a7dea167SDimitry Andric ExprResult E; 563a7dea167SDimitry Andric 564a7dea167SDimitry Andric // We may have already annotated this id-expression. 565a7dea167SDimitry Andric switch (Tok.getKind()) { 566a7dea167SDimitry Andric case tok::annot_non_type: { 567a7dea167SDimitry Andric NamedDecl *ND = getNonTypeAnnotation(Tok); 568a7dea167SDimitry Andric SourceLocation Loc = ConsumeAnnotationToken(); 569a7dea167SDimitry Andric E = Actions.ActOnNameClassifiedAsNonType(getCurScope(), SS, ND, Loc, Tok); 570a7dea167SDimitry Andric break; 571a7dea167SDimitry Andric } 572a7dea167SDimitry Andric 573a7dea167SDimitry Andric case tok::annot_non_type_dependent: { 574a7dea167SDimitry Andric IdentifierInfo *II = getIdentifierAnnotation(Tok); 575a7dea167SDimitry Andric SourceLocation Loc = ConsumeAnnotationToken(); 576a7dea167SDimitry Andric 577a7dea167SDimitry Andric // This is only the direct operand of an & operator if it is not 578a7dea167SDimitry Andric // followed by a postfix-expression suffix. 579a7dea167SDimitry Andric if (isAddressOfOperand && isPostfixExpressionSuffixStart()) 580a7dea167SDimitry Andric isAddressOfOperand = false; 581a7dea167SDimitry Andric 582a7dea167SDimitry Andric E = Actions.ActOnNameClassifiedAsDependentNonType(SS, II, Loc, 583a7dea167SDimitry Andric isAddressOfOperand); 584a7dea167SDimitry Andric break; 585a7dea167SDimitry Andric } 586a7dea167SDimitry Andric 587a7dea167SDimitry Andric case tok::annot_non_type_undeclared: { 588a7dea167SDimitry Andric assert(SS.isEmpty() && 589a7dea167SDimitry Andric "undeclared non-type annotation should be unqualified"); 590a7dea167SDimitry Andric IdentifierInfo *II = getIdentifierAnnotation(Tok); 591a7dea167SDimitry Andric SourceLocation Loc = ConsumeAnnotationToken(); 592a7dea167SDimitry Andric E = Actions.ActOnNameClassifiedAsUndeclaredNonType(II, Loc); 593a7dea167SDimitry Andric break; 594a7dea167SDimitry Andric } 595a7dea167SDimitry Andric 596a7dea167SDimitry Andric default: 5970b57cec5SDimitry Andric SourceLocation TemplateKWLoc; 5980b57cec5SDimitry Andric UnqualifiedId Name; 5995ffd83dbSDimitry Andric if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, 6005ffd83dbSDimitry Andric /*ObjectHadErrors=*/false, 6010b57cec5SDimitry Andric /*EnteringContext=*/false, 6020b57cec5SDimitry Andric /*AllowDestructorName=*/false, 6030b57cec5SDimitry Andric /*AllowConstructorName=*/false, 6045ffd83dbSDimitry Andric /*AllowDeductionGuide=*/false, &TemplateKWLoc, Name)) 6050b57cec5SDimitry Andric return ExprError(); 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric // This is only the direct operand of an & operator if it is not 6080b57cec5SDimitry Andric // followed by a postfix-expression suffix. 6090b57cec5SDimitry Andric if (isAddressOfOperand && isPostfixExpressionSuffixStart()) 6100b57cec5SDimitry Andric isAddressOfOperand = false; 6110b57cec5SDimitry Andric 612a7dea167SDimitry Andric E = Actions.ActOnIdExpression( 6130b57cec5SDimitry Andric getCurScope(), SS, TemplateKWLoc, Name, Tok.is(tok::l_paren), 6140b57cec5SDimitry Andric isAddressOfOperand, /*CCC=*/nullptr, /*IsInlineAsmIdentifier=*/false, 6150b57cec5SDimitry Andric &Replacement); 616a7dea167SDimitry Andric break; 617a7dea167SDimitry Andric } 618a7dea167SDimitry Andric 6190b57cec5SDimitry Andric if (!E.isInvalid() && !E.isUnset() && Tok.is(tok::less)) 6200b57cec5SDimitry Andric checkPotentialAngleBracket(E); 6210b57cec5SDimitry Andric return E; 6220b57cec5SDimitry Andric } 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric /// ParseCXXIdExpression - Handle id-expression. 6250b57cec5SDimitry Andric /// 6260b57cec5SDimitry Andric /// id-expression: 6270b57cec5SDimitry Andric /// unqualified-id 6280b57cec5SDimitry Andric /// qualified-id 6290b57cec5SDimitry Andric /// 6300b57cec5SDimitry Andric /// qualified-id: 6310b57cec5SDimitry Andric /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 6320b57cec5SDimitry Andric /// '::' identifier 6330b57cec5SDimitry Andric /// '::' operator-function-id 6340b57cec5SDimitry Andric /// '::' template-id 6350b57cec5SDimitry Andric /// 6360b57cec5SDimitry Andric /// NOTE: The standard specifies that, for qualified-id, the parser does not 6370b57cec5SDimitry Andric /// expect: 6380b57cec5SDimitry Andric /// 6390b57cec5SDimitry Andric /// '::' conversion-function-id 6400b57cec5SDimitry Andric /// '::' '~' class-name 6410b57cec5SDimitry Andric /// 6420b57cec5SDimitry Andric /// This may cause a slight inconsistency on diagnostics: 6430b57cec5SDimitry Andric /// 6440b57cec5SDimitry Andric /// class C {}; 6450b57cec5SDimitry Andric /// namespace A {} 6460b57cec5SDimitry Andric /// void f() { 6470b57cec5SDimitry Andric /// :: A :: ~ C(); // Some Sema error about using destructor with a 6480b57cec5SDimitry Andric /// // namespace. 6490b57cec5SDimitry Andric /// :: ~ C(); // Some Parser error like 'unexpected ~'. 6500b57cec5SDimitry Andric /// } 6510b57cec5SDimitry Andric /// 6520b57cec5SDimitry Andric /// We simplify the parser a bit and make it work like: 6530b57cec5SDimitry Andric /// 6540b57cec5SDimitry Andric /// qualified-id: 6550b57cec5SDimitry Andric /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 6560b57cec5SDimitry Andric /// '::' unqualified-id 6570b57cec5SDimitry Andric /// 6580b57cec5SDimitry Andric /// That way Sema can handle and report similar errors for namespaces and the 6590b57cec5SDimitry Andric /// global scope. 6600b57cec5SDimitry Andric /// 6610b57cec5SDimitry Andric /// The isAddressOfOperand parameter indicates that this id-expression is a 6620b57cec5SDimitry Andric /// direct operand of the address-of operator. This is, besides member contexts, 6630b57cec5SDimitry Andric /// the only place where a qualified-id naming a non-static class member may 6640b57cec5SDimitry Andric /// appear. 6650b57cec5SDimitry Andric /// 6660b57cec5SDimitry Andric ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { 6670b57cec5SDimitry Andric // qualified-id: 6680b57cec5SDimitry Andric // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id 6690b57cec5SDimitry Andric // '::' unqualified-id 6700b57cec5SDimitry Andric // 6710b57cec5SDimitry Andric CXXScopeSpec SS; 6725ffd83dbSDimitry Andric ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 67304eeddc0SDimitry Andric /*ObjectHasErrors=*/false, 6745ffd83dbSDimitry Andric /*EnteringContext=*/false); 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric Token Replacement; 6770b57cec5SDimitry Andric ExprResult Result = 6780b57cec5SDimitry Andric tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement); 6790b57cec5SDimitry Andric if (Result.isUnset()) { 6800b57cec5SDimitry Andric // If the ExprResult is valid but null, then typo correction suggested a 6810b57cec5SDimitry Andric // keyword replacement that needs to be reparsed. 6820b57cec5SDimitry Andric UnconsumeToken(Replacement); 6830b57cec5SDimitry Andric Result = tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement); 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric assert(!Result.isUnset() && "Typo correction suggested a keyword replacement " 6860b57cec5SDimitry Andric "for a previous keyword suggestion"); 6870b57cec5SDimitry Andric return Result; 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric /// ParseLambdaExpression - Parse a C++11 lambda expression. 6910b57cec5SDimitry Andric /// 6920b57cec5SDimitry Andric /// lambda-expression: 693fe6060f1SDimitry Andric /// lambda-introducer lambda-declarator compound-statement 6940b57cec5SDimitry Andric /// lambda-introducer '<' template-parameter-list '>' 695fe6060f1SDimitry Andric /// requires-clause[opt] lambda-declarator compound-statement 6960b57cec5SDimitry Andric /// 6970b57cec5SDimitry Andric /// lambda-introducer: 6980b57cec5SDimitry Andric /// '[' lambda-capture[opt] ']' 6990b57cec5SDimitry Andric /// 7000b57cec5SDimitry Andric /// lambda-capture: 7010b57cec5SDimitry Andric /// capture-default 7020b57cec5SDimitry Andric /// capture-list 7030b57cec5SDimitry Andric /// capture-default ',' capture-list 7040b57cec5SDimitry Andric /// 7050b57cec5SDimitry Andric /// capture-default: 7060b57cec5SDimitry Andric /// '&' 7070b57cec5SDimitry Andric /// '=' 7080b57cec5SDimitry Andric /// 7090b57cec5SDimitry Andric /// capture-list: 7100b57cec5SDimitry Andric /// capture 7110b57cec5SDimitry Andric /// capture-list ',' capture 7120b57cec5SDimitry Andric /// 7130b57cec5SDimitry Andric /// capture: 7140b57cec5SDimitry Andric /// simple-capture 7150b57cec5SDimitry Andric /// init-capture [C++1y] 7160b57cec5SDimitry Andric /// 7170b57cec5SDimitry Andric /// simple-capture: 7180b57cec5SDimitry Andric /// identifier 7190b57cec5SDimitry Andric /// '&' identifier 7200b57cec5SDimitry Andric /// 'this' 7210b57cec5SDimitry Andric /// 7220b57cec5SDimitry Andric /// init-capture: [C++1y] 7230b57cec5SDimitry Andric /// identifier initializer 7240b57cec5SDimitry Andric /// '&' identifier initializer 7250b57cec5SDimitry Andric /// 7260b57cec5SDimitry Andric /// lambda-declarator: 727fe6060f1SDimitry Andric /// lambda-specifiers [C++2b] 728fe6060f1SDimitry Andric /// '(' parameter-declaration-clause ')' lambda-specifiers 729fe6060f1SDimitry Andric /// requires-clause[opt] 730fe6060f1SDimitry Andric /// 731fe6060f1SDimitry Andric /// lambda-specifiers: 732fe6060f1SDimitry Andric /// decl-specifier-seq[opt] noexcept-specifier[opt] 733fe6060f1SDimitry Andric /// attribute-specifier-seq[opt] trailing-return-type[opt] 7340b57cec5SDimitry Andric /// 7350b57cec5SDimitry Andric ExprResult Parser::ParseLambdaExpression() { 7360b57cec5SDimitry Andric // Parse lambda-introducer. 7370b57cec5SDimitry Andric LambdaIntroducer Intro; 7380b57cec5SDimitry Andric if (ParseLambdaIntroducer(Intro)) { 7390b57cec5SDimitry Andric SkipUntil(tok::r_square, StopAtSemi); 7400b57cec5SDimitry Andric SkipUntil(tok::l_brace, StopAtSemi); 7410b57cec5SDimitry Andric SkipUntil(tok::r_brace, StopAtSemi); 7420b57cec5SDimitry Andric return ExprError(); 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric return ParseLambdaExpressionAfterIntroducer(Intro); 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric /// Use lookahead and potentially tentative parsing to determine if we are 7490b57cec5SDimitry Andric /// looking at a C++11 lambda expression, and parse it if we are. 7500b57cec5SDimitry Andric /// 7510b57cec5SDimitry Andric /// If we are not looking at a lambda expression, returns ExprError(). 7520b57cec5SDimitry Andric ExprResult Parser::TryParseLambdaExpression() { 7530b57cec5SDimitry Andric assert(getLangOpts().CPlusPlus11 7540b57cec5SDimitry Andric && Tok.is(tok::l_square) 7550b57cec5SDimitry Andric && "Not at the start of a possible lambda expression."); 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric const Token Next = NextToken(); 7580b57cec5SDimitry Andric if (Next.is(tok::eof)) // Nothing else to lookup here... 7590b57cec5SDimitry Andric return ExprEmpty(); 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric const Token After = GetLookAheadToken(2); 7620b57cec5SDimitry Andric // If lookahead indicates this is a lambda... 7630b57cec5SDimitry Andric if (Next.is(tok::r_square) || // [] 7640b57cec5SDimitry Andric Next.is(tok::equal) || // [= 7650b57cec5SDimitry Andric (Next.is(tok::amp) && // [&] or [&, 7660b57cec5SDimitry Andric After.isOneOf(tok::r_square, tok::comma)) || 7670b57cec5SDimitry Andric (Next.is(tok::identifier) && // [identifier] 7680b57cec5SDimitry Andric After.is(tok::r_square)) || 7690b57cec5SDimitry Andric Next.is(tok::ellipsis)) { // [... 7700b57cec5SDimitry Andric return ParseLambdaExpression(); 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric // If lookahead indicates an ObjC message send... 7740b57cec5SDimitry Andric // [identifier identifier 7750b57cec5SDimitry Andric if (Next.is(tok::identifier) && After.is(tok::identifier)) 7760b57cec5SDimitry Andric return ExprEmpty(); 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric // Here, we're stuck: lambda introducers and Objective-C message sends are 7790b57cec5SDimitry Andric // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a 7800b57cec5SDimitry Andric // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of 7810b57cec5SDimitry Andric // writing two routines to parse a lambda introducer, just try to parse 7820b57cec5SDimitry Andric // a lambda introducer first, and fall back if that fails. 7830b57cec5SDimitry Andric LambdaIntroducer Intro; 7840b57cec5SDimitry Andric { 7850b57cec5SDimitry Andric TentativeParsingAction TPA(*this); 7860b57cec5SDimitry Andric LambdaIntroducerTentativeParse Tentative; 7870b57cec5SDimitry Andric if (ParseLambdaIntroducer(Intro, &Tentative)) { 7880b57cec5SDimitry Andric TPA.Commit(); 7890b57cec5SDimitry Andric return ExprError(); 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric switch (Tentative) { 7930b57cec5SDimitry Andric case LambdaIntroducerTentativeParse::Success: 7940b57cec5SDimitry Andric TPA.Commit(); 7950b57cec5SDimitry Andric break; 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric case LambdaIntroducerTentativeParse::Incomplete: 7980b57cec5SDimitry Andric // Didn't fully parse the lambda-introducer, try again with a 7990b57cec5SDimitry Andric // non-tentative parse. 8000b57cec5SDimitry Andric TPA.Revert(); 8010b57cec5SDimitry Andric Intro = LambdaIntroducer(); 8020b57cec5SDimitry Andric if (ParseLambdaIntroducer(Intro)) 8030b57cec5SDimitry Andric return ExprError(); 8040b57cec5SDimitry Andric break; 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric case LambdaIntroducerTentativeParse::MessageSend: 8070b57cec5SDimitry Andric case LambdaIntroducerTentativeParse::Invalid: 8080b57cec5SDimitry Andric // Not a lambda-introducer, might be a message send. 8090b57cec5SDimitry Andric TPA.Revert(); 8100b57cec5SDimitry Andric return ExprEmpty(); 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric } 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric return ParseLambdaExpressionAfterIntroducer(Intro); 8150b57cec5SDimitry Andric } 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric /// Parse a lambda introducer. 8180b57cec5SDimitry Andric /// \param Intro A LambdaIntroducer filled in with information about the 8190b57cec5SDimitry Andric /// contents of the lambda-introducer. 8200b57cec5SDimitry Andric /// \param Tentative If non-null, we are disambiguating between a 8210b57cec5SDimitry Andric /// lambda-introducer and some other construct. In this mode, we do not 8220b57cec5SDimitry Andric /// produce any diagnostics or take any other irreversible action unless 8230b57cec5SDimitry Andric /// we're sure that this is a lambda-expression. 8240b57cec5SDimitry Andric /// \return \c true if parsing (or disambiguation) failed with a diagnostic and 8250b57cec5SDimitry Andric /// the caller should bail out / recover. 8260b57cec5SDimitry Andric bool Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, 8270b57cec5SDimitry Andric LambdaIntroducerTentativeParse *Tentative) { 8280b57cec5SDimitry Andric if (Tentative) 8290b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::Success; 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['."); 8320b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_square); 8330b57cec5SDimitry Andric T.consumeOpen(); 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric Intro.Range.setBegin(T.getOpenLocation()); 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric bool First = true; 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric // Produce a diagnostic if we're not tentatively parsing; otherwise track 8400b57cec5SDimitry Andric // that our parse has failed. 8410b57cec5SDimitry Andric auto Invalid = [&](llvm::function_ref<void()> Action) { 8420b57cec5SDimitry Andric if (Tentative) { 8430b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::Invalid; 8440b57cec5SDimitry Andric return false; 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric Action(); 8470b57cec5SDimitry Andric return true; 8480b57cec5SDimitry Andric }; 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric // Perform some irreversible action if this is a non-tentative parse; 8510b57cec5SDimitry Andric // otherwise note that our actions were incomplete. 8520b57cec5SDimitry Andric auto NonTentativeAction = [&](llvm::function_ref<void()> Action) { 8530b57cec5SDimitry Andric if (Tentative) 8540b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::Incomplete; 8550b57cec5SDimitry Andric else 8560b57cec5SDimitry Andric Action(); 8570b57cec5SDimitry Andric }; 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric // Parse capture-default. 8600b57cec5SDimitry Andric if (Tok.is(tok::amp) && 8610b57cec5SDimitry Andric (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) { 8620b57cec5SDimitry Andric Intro.Default = LCD_ByRef; 8630b57cec5SDimitry Andric Intro.DefaultLoc = ConsumeToken(); 8640b57cec5SDimitry Andric First = false; 8650b57cec5SDimitry Andric if (!Tok.getIdentifierInfo()) { 8660b57cec5SDimitry Andric // This can only be a lambda; no need for tentative parsing any more. 8670b57cec5SDimitry Andric // '[[and]]' can still be an attribute, though. 8680b57cec5SDimitry Andric Tentative = nullptr; 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric } else if (Tok.is(tok::equal)) { 8710b57cec5SDimitry Andric Intro.Default = LCD_ByCopy; 8720b57cec5SDimitry Andric Intro.DefaultLoc = ConsumeToken(); 8730b57cec5SDimitry Andric First = false; 8740b57cec5SDimitry Andric Tentative = nullptr; 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric while (Tok.isNot(tok::r_square)) { 8780b57cec5SDimitry Andric if (!First) { 8790b57cec5SDimitry Andric if (Tok.isNot(tok::comma)) { 8800b57cec5SDimitry Andric // Provide a completion for a lambda introducer here. Except 8810b57cec5SDimitry Andric // in Objective-C, where this is Almost Surely meant to be a message 8820b57cec5SDimitry Andric // send. In that case, fail here and let the ObjC message 8830b57cec5SDimitry Andric // expression parser perform the completion. 8840b57cec5SDimitry Andric if (Tok.is(tok::code_completion) && 8850b57cec5SDimitry Andric !(getLangOpts().ObjC && Tentative)) { 886fe6060f1SDimitry Andric cutOffParsing(); 8870b57cec5SDimitry Andric Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, 8880b57cec5SDimitry Andric /*AfterAmpersand=*/false); 8890b57cec5SDimitry Andric break; 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric return Invalid([&] { 8930b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected_comma_or_rsquare); 8940b57cec5SDimitry Andric }); 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric ConsumeToken(); 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 900fe6060f1SDimitry Andric cutOffParsing(); 9010b57cec5SDimitry Andric // If we're in Objective-C++ and we have a bare '[', then this is more 9020b57cec5SDimitry Andric // likely to be a message receiver. 9030b57cec5SDimitry Andric if (getLangOpts().ObjC && Tentative && First) 9040b57cec5SDimitry Andric Actions.CodeCompleteObjCMessageReceiver(getCurScope()); 9050b57cec5SDimitry Andric else 9060b57cec5SDimitry Andric Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, 9070b57cec5SDimitry Andric /*AfterAmpersand=*/false); 9080b57cec5SDimitry Andric break; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric First = false; 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric // Parse capture. 9140b57cec5SDimitry Andric LambdaCaptureKind Kind = LCK_ByCopy; 9150b57cec5SDimitry Andric LambdaCaptureInitKind InitKind = LambdaCaptureInitKind::NoInit; 9160b57cec5SDimitry Andric SourceLocation Loc; 9170b57cec5SDimitry Andric IdentifierInfo *Id = nullptr; 9180b57cec5SDimitry Andric SourceLocation EllipsisLocs[4]; 9190b57cec5SDimitry Andric ExprResult Init; 9200b57cec5SDimitry Andric SourceLocation LocStart = Tok.getLocation(); 9210b57cec5SDimitry Andric 9220b57cec5SDimitry Andric if (Tok.is(tok::star)) { 9230b57cec5SDimitry Andric Loc = ConsumeToken(); 9240b57cec5SDimitry Andric if (Tok.is(tok::kw_this)) { 9250b57cec5SDimitry Andric ConsumeToken(); 9260b57cec5SDimitry Andric Kind = LCK_StarThis; 9270b57cec5SDimitry Andric } else { 9280b57cec5SDimitry Andric return Invalid([&] { 9290b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected_star_this_capture); 9300b57cec5SDimitry Andric }); 9310b57cec5SDimitry Andric } 9320b57cec5SDimitry Andric } else if (Tok.is(tok::kw_this)) { 9330b57cec5SDimitry Andric Kind = LCK_This; 9340b57cec5SDimitry Andric Loc = ConsumeToken(); 935e8d8bef9SDimitry Andric } else if (Tok.isOneOf(tok::amp, tok::equal) && 936e8d8bef9SDimitry Andric NextToken().isOneOf(tok::comma, tok::r_square) && 937e8d8bef9SDimitry Andric Intro.Default == LCD_None) { 938e8d8bef9SDimitry Andric // We have a lone "&" or "=" which is either a misplaced capture-default 939e8d8bef9SDimitry Andric // or the start of a capture (in the "&" case) with the rest of the 940e8d8bef9SDimitry Andric // capture missing. Both are an error but a misplaced capture-default 941e8d8bef9SDimitry Andric // is more likely if we don't already have a capture default. 942e8d8bef9SDimitry Andric return Invalid( 943e8d8bef9SDimitry Andric [&] { Diag(Tok.getLocation(), diag::err_capture_default_first); }); 9440b57cec5SDimitry Andric } else { 9450b57cec5SDimitry Andric TryConsumeToken(tok::ellipsis, EllipsisLocs[0]); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric if (Tok.is(tok::amp)) { 9480b57cec5SDimitry Andric Kind = LCK_ByRef; 9490b57cec5SDimitry Andric ConsumeToken(); 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 952fe6060f1SDimitry Andric cutOffParsing(); 9530b57cec5SDimitry Andric Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, 9540b57cec5SDimitry Andric /*AfterAmpersand=*/true); 9550b57cec5SDimitry Andric break; 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric TryConsumeToken(tok::ellipsis, EllipsisLocs[1]); 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 9620b57cec5SDimitry Andric Id = Tok.getIdentifierInfo(); 9630b57cec5SDimitry Andric Loc = ConsumeToken(); 9640b57cec5SDimitry Andric } else if (Tok.is(tok::kw_this)) { 9650b57cec5SDimitry Andric return Invalid([&] { 9660b57cec5SDimitry Andric // FIXME: Suggest a fixit here. 9670b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_this_captured_by_reference); 9680b57cec5SDimitry Andric }); 9690b57cec5SDimitry Andric } else { 9700b57cec5SDimitry Andric return Invalid([&] { 9710b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected_capture); 9720b57cec5SDimitry Andric }); 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric TryConsumeToken(tok::ellipsis, EllipsisLocs[2]); 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric if (Tok.is(tok::l_paren)) { 9780b57cec5SDimitry Andric BalancedDelimiterTracker Parens(*this, tok::l_paren); 9790b57cec5SDimitry Andric Parens.consumeOpen(); 9800b57cec5SDimitry Andric 9810b57cec5SDimitry Andric InitKind = LambdaCaptureInitKind::DirectInit; 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric ExprVector Exprs; 9840b57cec5SDimitry Andric if (Tentative) { 9850b57cec5SDimitry Andric Parens.skipToEnd(); 9860b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::Incomplete; 987*bdd1243dSDimitry Andric } else if (ParseExpressionList(Exprs)) { 9880b57cec5SDimitry Andric Parens.skipToEnd(); 9890b57cec5SDimitry Andric Init = ExprError(); 9900b57cec5SDimitry Andric } else { 9910b57cec5SDimitry Andric Parens.consumeClose(); 9920b57cec5SDimitry Andric Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(), 9930b57cec5SDimitry Andric Parens.getCloseLocation(), 9940b57cec5SDimitry Andric Exprs); 9950b57cec5SDimitry Andric } 9960b57cec5SDimitry Andric } else if (Tok.isOneOf(tok::l_brace, tok::equal)) { 9970b57cec5SDimitry Andric // Each lambda init-capture forms its own full expression, which clears 9980b57cec5SDimitry Andric // Actions.MaybeODRUseExprs. So create an expression evaluation context 9990b57cec5SDimitry Andric // to save the necessary state, and restore it later. 10000b57cec5SDimitry Andric EnterExpressionEvaluationContext EC( 10010b57cec5SDimitry Andric Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric if (TryConsumeToken(tok::equal)) 10040b57cec5SDimitry Andric InitKind = LambdaCaptureInitKind::CopyInit; 10050b57cec5SDimitry Andric else 10060b57cec5SDimitry Andric InitKind = LambdaCaptureInitKind::ListInit; 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric if (!Tentative) { 10090b57cec5SDimitry Andric Init = ParseInitializer(); 10100b57cec5SDimitry Andric } else if (Tok.is(tok::l_brace)) { 10110b57cec5SDimitry Andric BalancedDelimiterTracker Braces(*this, tok::l_brace); 10120b57cec5SDimitry Andric Braces.consumeOpen(); 10130b57cec5SDimitry Andric Braces.skipToEnd(); 10140b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::Incomplete; 10150b57cec5SDimitry Andric } else { 10160b57cec5SDimitry Andric // We're disambiguating this: 10170b57cec5SDimitry Andric // 10180b57cec5SDimitry Andric // [..., x = expr 10190b57cec5SDimitry Andric // 10200b57cec5SDimitry Andric // We need to find the end of the following expression in order to 10210b57cec5SDimitry Andric // determine whether this is an Obj-C message send's receiver, a 10220b57cec5SDimitry Andric // C99 designator, or a lambda init-capture. 10230b57cec5SDimitry Andric // 10240b57cec5SDimitry Andric // Parse the expression to find where it ends, and annotate it back 10250b57cec5SDimitry Andric // onto the tokens. We would have parsed this expression the same way 10260b57cec5SDimitry Andric // in either case: both the RHS of an init-capture and the RHS of an 10270b57cec5SDimitry Andric // assignment expression are parsed as an initializer-clause, and in 10280b57cec5SDimitry Andric // neither case can anything be added to the scope between the '[' and 10290b57cec5SDimitry Andric // here. 10300b57cec5SDimitry Andric // 10310b57cec5SDimitry Andric // FIXME: This is horrible. Adding a mechanism to skip an expression 10320b57cec5SDimitry Andric // would be much cleaner. 10330b57cec5SDimitry Andric // FIXME: If there is a ',' before the next ']' or ':', we can skip to 10340b57cec5SDimitry Andric // that instead. (And if we see a ':' with no matching '?', we can 10350b57cec5SDimitry Andric // classify this as an Obj-C message send.) 10360b57cec5SDimitry Andric SourceLocation StartLoc = Tok.getLocation(); 10370b57cec5SDimitry Andric InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true); 10380b57cec5SDimitry Andric Init = ParseInitializer(); 10390b57cec5SDimitry Andric if (!Init.isInvalid()) 10400b57cec5SDimitry Andric Init = Actions.CorrectDelayedTyposInExpr(Init.get()); 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric if (Tok.getLocation() != StartLoc) { 10430b57cec5SDimitry Andric // Back out the lexing of the token after the initializer. 10440b57cec5SDimitry Andric PP.RevertCachedTokens(1); 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric // Replace the consumed tokens with an appropriate annotation. 10470b57cec5SDimitry Andric Tok.setLocation(StartLoc); 10480b57cec5SDimitry Andric Tok.setKind(tok::annot_primary_expr); 10490b57cec5SDimitry Andric setExprAnnotation(Tok, Init); 10500b57cec5SDimitry Andric Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation()); 10510b57cec5SDimitry Andric PP.AnnotateCachedTokens(Tok); 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric // Consume the annotated initializer. 10540b57cec5SDimitry Andric ConsumeAnnotationToken(); 10550b57cec5SDimitry Andric } 10560b57cec5SDimitry Andric } 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric TryConsumeToken(tok::ellipsis, EllipsisLocs[3]); 10600b57cec5SDimitry Andric } 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric // Check if this is a message send before we act on a possible init-capture. 10630b57cec5SDimitry Andric if (Tentative && Tok.is(tok::identifier) && 10640b57cec5SDimitry Andric NextToken().isOneOf(tok::colon, tok::r_square)) { 10650b57cec5SDimitry Andric // This can only be a message send. We're done with disambiguation. 10660b57cec5SDimitry Andric *Tentative = LambdaIntroducerTentativeParse::MessageSend; 10670b57cec5SDimitry Andric return false; 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric // Ensure that any ellipsis was in the right place. 10710b57cec5SDimitry Andric SourceLocation EllipsisLoc; 1072349cc55cSDimitry Andric if (llvm::any_of(EllipsisLocs, 10730b57cec5SDimitry Andric [](SourceLocation Loc) { return Loc.isValid(); })) { 10740b57cec5SDimitry Andric // The '...' should appear before the identifier in an init-capture, and 10750b57cec5SDimitry Andric // after the identifier otherwise. 10760b57cec5SDimitry Andric bool InitCapture = InitKind != LambdaCaptureInitKind::NoInit; 10770b57cec5SDimitry Andric SourceLocation *ExpectedEllipsisLoc = 10780b57cec5SDimitry Andric !InitCapture ? &EllipsisLocs[2] : 10790b57cec5SDimitry Andric Kind == LCK_ByRef ? &EllipsisLocs[1] : 10800b57cec5SDimitry Andric &EllipsisLocs[0]; 10810b57cec5SDimitry Andric EllipsisLoc = *ExpectedEllipsisLoc; 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric unsigned DiagID = 0; 10840b57cec5SDimitry Andric if (EllipsisLoc.isInvalid()) { 10850b57cec5SDimitry Andric DiagID = diag::err_lambda_capture_misplaced_ellipsis; 10860b57cec5SDimitry Andric for (SourceLocation Loc : EllipsisLocs) { 10870b57cec5SDimitry Andric if (Loc.isValid()) 10880b57cec5SDimitry Andric EllipsisLoc = Loc; 10890b57cec5SDimitry Andric } 10900b57cec5SDimitry Andric } else { 10910b57cec5SDimitry Andric unsigned NumEllipses = std::accumulate( 10920b57cec5SDimitry Andric std::begin(EllipsisLocs), std::end(EllipsisLocs), 0, 10930b57cec5SDimitry Andric [](int N, SourceLocation Loc) { return N + Loc.isValid(); }); 10940b57cec5SDimitry Andric if (NumEllipses > 1) 10950b57cec5SDimitry Andric DiagID = diag::err_lambda_capture_multiple_ellipses; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric if (DiagID) { 10980b57cec5SDimitry Andric NonTentativeAction([&] { 10990b57cec5SDimitry Andric // Point the diagnostic at the first misplaced ellipsis. 11000b57cec5SDimitry Andric SourceLocation DiagLoc; 11010b57cec5SDimitry Andric for (SourceLocation &Loc : EllipsisLocs) { 11020b57cec5SDimitry Andric if (&Loc != ExpectedEllipsisLoc && Loc.isValid()) { 11030b57cec5SDimitry Andric DiagLoc = Loc; 11040b57cec5SDimitry Andric break; 11050b57cec5SDimitry Andric } 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric assert(DiagLoc.isValid() && "no location for diagnostic"); 11080b57cec5SDimitry Andric 11090b57cec5SDimitry Andric // Issue the diagnostic and produce fixits showing where the ellipsis 11100b57cec5SDimitry Andric // should have been written. 11110b57cec5SDimitry Andric auto &&D = Diag(DiagLoc, DiagID); 11120b57cec5SDimitry Andric if (DiagID == diag::err_lambda_capture_misplaced_ellipsis) { 11130b57cec5SDimitry Andric SourceLocation ExpectedLoc = 11140b57cec5SDimitry Andric InitCapture ? Loc 11150b57cec5SDimitry Andric : Lexer::getLocForEndOfToken( 11160b57cec5SDimitry Andric Loc, 0, PP.getSourceManager(), getLangOpts()); 11170b57cec5SDimitry Andric D << InitCapture << FixItHint::CreateInsertion(ExpectedLoc, "..."); 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric for (SourceLocation &Loc : EllipsisLocs) { 11200b57cec5SDimitry Andric if (&Loc != ExpectedEllipsisLoc && Loc.isValid()) 11210b57cec5SDimitry Andric D << FixItHint::CreateRemoval(Loc); 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric }); 11240b57cec5SDimitry Andric } 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric // Process the init-capture initializers now rather than delaying until we 11280b57cec5SDimitry Andric // form the lambda-expression so that they can be handled in the context 11290b57cec5SDimitry Andric // enclosing the lambda-expression, rather than in the context of the 11300b57cec5SDimitry Andric // lambda-expression itself. 11310b57cec5SDimitry Andric ParsedType InitCaptureType; 11320b57cec5SDimitry Andric if (Init.isUsable()) 11330b57cec5SDimitry Andric Init = Actions.CorrectDelayedTyposInExpr(Init.get()); 11340b57cec5SDimitry Andric if (Init.isUsable()) { 11350b57cec5SDimitry Andric NonTentativeAction([&] { 11360b57cec5SDimitry Andric // Get the pointer and store it in an lvalue, so we can use it as an 11370b57cec5SDimitry Andric // out argument. 11380b57cec5SDimitry Andric Expr *InitExpr = Init.get(); 11390b57cec5SDimitry Andric // This performs any lvalue-to-rvalue conversions if necessary, which 11400b57cec5SDimitry Andric // can affect what gets captured in the containing decl-context. 11410b57cec5SDimitry Andric InitCaptureType = Actions.actOnLambdaInitCaptureInitialization( 11420b57cec5SDimitry Andric Loc, Kind == LCK_ByRef, EllipsisLoc, Id, InitKind, InitExpr); 11430b57cec5SDimitry Andric Init = InitExpr; 11440b57cec5SDimitry Andric }); 11450b57cec5SDimitry Andric } 11460b57cec5SDimitry Andric 11470b57cec5SDimitry Andric SourceLocation LocEnd = PrevTokLocation; 11480b57cec5SDimitry Andric 11490b57cec5SDimitry Andric Intro.addCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init, 11500b57cec5SDimitry Andric InitCaptureType, SourceRange(LocStart, LocEnd)); 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric T.consumeClose(); 11540b57cec5SDimitry Andric Intro.Range.setEnd(T.getCloseLocation()); 11550b57cec5SDimitry Andric return false; 11560b57cec5SDimitry Andric } 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric static void tryConsumeLambdaSpecifierToken(Parser &P, 11590b57cec5SDimitry Andric SourceLocation &MutableLoc, 1160*bdd1243dSDimitry Andric SourceLocation &StaticLoc, 11610b57cec5SDimitry Andric SourceLocation &ConstexprLoc, 11620b57cec5SDimitry Andric SourceLocation &ConstevalLoc, 11630b57cec5SDimitry Andric SourceLocation &DeclEndLoc) { 11640b57cec5SDimitry Andric assert(MutableLoc.isInvalid()); 1165*bdd1243dSDimitry Andric assert(StaticLoc.isInvalid()); 11660b57cec5SDimitry Andric assert(ConstexprLoc.isInvalid()); 1167*bdd1243dSDimitry Andric assert(ConstevalLoc.isInvalid()); 11680b57cec5SDimitry Andric // Consume constexpr-opt mutable-opt in any sequence, and set the DeclEndLoc 11690b57cec5SDimitry Andric // to the final of those locations. Emit an error if we have multiple 11700b57cec5SDimitry Andric // copies of those keywords and recover. 11710b57cec5SDimitry Andric 1172*bdd1243dSDimitry Andric auto ConsumeLocation = [&P, &DeclEndLoc](SourceLocation &SpecifierLoc, 1173*bdd1243dSDimitry Andric int DiagIndex) { 1174*bdd1243dSDimitry Andric if (SpecifierLoc.isValid()) { 1175*bdd1243dSDimitry Andric P.Diag(P.getCurToken().getLocation(), 1176*bdd1243dSDimitry Andric diag::err_lambda_decl_specifier_repeated) 1177*bdd1243dSDimitry Andric << DiagIndex 1178*bdd1243dSDimitry Andric << FixItHint::CreateRemoval(P.getCurToken().getLocation()); 1179*bdd1243dSDimitry Andric } 1180*bdd1243dSDimitry Andric SpecifierLoc = P.ConsumeToken(); 1181*bdd1243dSDimitry Andric DeclEndLoc = SpecifierLoc; 1182*bdd1243dSDimitry Andric }; 1183*bdd1243dSDimitry Andric 11840b57cec5SDimitry Andric while (true) { 11850b57cec5SDimitry Andric switch (P.getCurToken().getKind()) { 1186*bdd1243dSDimitry Andric case tok::kw_mutable: 1187*bdd1243dSDimitry Andric ConsumeLocation(MutableLoc, 0); 1188*bdd1243dSDimitry Andric break; 1189*bdd1243dSDimitry Andric case tok::kw_static: 1190*bdd1243dSDimitry Andric ConsumeLocation(StaticLoc, 1); 1191*bdd1243dSDimitry Andric break; 11920b57cec5SDimitry Andric case tok::kw_constexpr: 1193*bdd1243dSDimitry Andric ConsumeLocation(ConstexprLoc, 2); 1194*bdd1243dSDimitry Andric break; 11950b57cec5SDimitry Andric case tok::kw_consteval: 1196*bdd1243dSDimitry Andric ConsumeLocation(ConstevalLoc, 3); 1197*bdd1243dSDimitry Andric break; 11980b57cec5SDimitry Andric default: 11990b57cec5SDimitry Andric return; 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric } 12030b57cec5SDimitry Andric 1204*bdd1243dSDimitry Andric static void addStaticToLambdaDeclSpecifier(Parser &P, SourceLocation StaticLoc, 1205*bdd1243dSDimitry Andric DeclSpec &DS) { 1206*bdd1243dSDimitry Andric if (StaticLoc.isValid()) { 1207*bdd1243dSDimitry Andric P.Diag(StaticLoc, !P.getLangOpts().CPlusPlus2b 1208*bdd1243dSDimitry Andric ? diag::err_static_lambda 1209*bdd1243dSDimitry Andric : diag::warn_cxx20_compat_static_lambda); 1210*bdd1243dSDimitry Andric const char *PrevSpec = nullptr; 1211*bdd1243dSDimitry Andric unsigned DiagID = 0; 1212*bdd1243dSDimitry Andric DS.SetStorageClassSpec(P.getActions(), DeclSpec::SCS_static, StaticLoc, 1213*bdd1243dSDimitry Andric PrevSpec, DiagID, 1214*bdd1243dSDimitry Andric P.getActions().getASTContext().getPrintingPolicy()); 1215*bdd1243dSDimitry Andric assert(PrevSpec == nullptr && DiagID == 0 && 1216*bdd1243dSDimitry Andric "Static cannot have been set previously!"); 1217*bdd1243dSDimitry Andric } 1218*bdd1243dSDimitry Andric } 1219*bdd1243dSDimitry Andric 12200b57cec5SDimitry Andric static void 12210b57cec5SDimitry Andric addConstexprToLambdaDeclSpecifier(Parser &P, SourceLocation ConstexprLoc, 12220b57cec5SDimitry Andric DeclSpec &DS) { 12230b57cec5SDimitry Andric if (ConstexprLoc.isValid()) { 12240b57cec5SDimitry Andric P.Diag(ConstexprLoc, !P.getLangOpts().CPlusPlus17 12250b57cec5SDimitry Andric ? diag::ext_constexpr_on_lambda_cxx17 12260b57cec5SDimitry Andric : diag::warn_cxx14_compat_constexpr_on_lambda); 12270b57cec5SDimitry Andric const char *PrevSpec = nullptr; 12280b57cec5SDimitry Andric unsigned DiagID = 0; 1229e8d8bef9SDimitry Andric DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, ConstexprLoc, PrevSpec, 1230e8d8bef9SDimitry Andric DiagID); 12310b57cec5SDimitry Andric assert(PrevSpec == nullptr && DiagID == 0 && 12320b57cec5SDimitry Andric "Constexpr cannot have been set previously!"); 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric static void addConstevalToLambdaDeclSpecifier(Parser &P, 12370b57cec5SDimitry Andric SourceLocation ConstevalLoc, 12380b57cec5SDimitry Andric DeclSpec &DS) { 12390b57cec5SDimitry Andric if (ConstevalLoc.isValid()) { 12400b57cec5SDimitry Andric P.Diag(ConstevalLoc, diag::warn_cxx20_compat_consteval); 12410b57cec5SDimitry Andric const char *PrevSpec = nullptr; 12420b57cec5SDimitry Andric unsigned DiagID = 0; 1243e8d8bef9SDimitry Andric DS.SetConstexprSpec(ConstexprSpecKind::Consteval, ConstevalLoc, PrevSpec, 1244e8d8bef9SDimitry Andric DiagID); 12450b57cec5SDimitry Andric if (DiagID != 0) 12460b57cec5SDimitry Andric P.Diag(ConstevalLoc, DiagID) << PrevSpec; 12470b57cec5SDimitry Andric } 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric 1250*bdd1243dSDimitry Andric static void DiagnoseStaticSpecifierRestrictions(Parser &P, 1251*bdd1243dSDimitry Andric SourceLocation StaticLoc, 1252*bdd1243dSDimitry Andric SourceLocation MutableLoc, 1253*bdd1243dSDimitry Andric const LambdaIntroducer &Intro) { 1254*bdd1243dSDimitry Andric if (StaticLoc.isInvalid()) 1255*bdd1243dSDimitry Andric return; 1256*bdd1243dSDimitry Andric 1257*bdd1243dSDimitry Andric // [expr.prim.lambda.general] p4 1258*bdd1243dSDimitry Andric // The lambda-specifier-seq shall not contain both mutable and static. 1259*bdd1243dSDimitry Andric // If the lambda-specifier-seq contains static, there shall be no 1260*bdd1243dSDimitry Andric // lambda-capture. 1261*bdd1243dSDimitry Andric if (MutableLoc.isValid()) 1262*bdd1243dSDimitry Andric P.Diag(StaticLoc, diag::err_static_mutable_lambda); 1263*bdd1243dSDimitry Andric if (Intro.hasLambdaCapture()) { 1264*bdd1243dSDimitry Andric P.Diag(StaticLoc, diag::err_static_lambda_captures); 1265*bdd1243dSDimitry Andric } 1266*bdd1243dSDimitry Andric } 1267*bdd1243dSDimitry Andric 12680b57cec5SDimitry Andric /// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda 12690b57cec5SDimitry Andric /// expression. 12700b57cec5SDimitry Andric ExprResult Parser::ParseLambdaExpressionAfterIntroducer( 12710b57cec5SDimitry Andric LambdaIntroducer &Intro) { 12720b57cec5SDimitry Andric SourceLocation LambdaBeginLoc = Intro.Range.getBegin(); 12730b57cec5SDimitry Andric Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda); 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc, 12760b57cec5SDimitry Andric "lambda expression parsing"); 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric 12800b57cec5SDimitry Andric // FIXME: Call into Actions to add any init-capture declarations to the 12810b57cec5SDimitry Andric // scope while parsing the lambda-declarator and compound-statement. 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric // Parse lambda-declarator[opt]. 12840b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 128581ad6265SDimitry Andric Declarator D(DS, ParsedAttributesView::none(), DeclaratorContext::LambdaExpr); 12860b57cec5SDimitry Andric TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 12870b57cec5SDimitry Andric Actions.PushLambdaScope(); 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric ParsedAttributes Attr(AttrFactory); 12900b57cec5SDimitry Andric if (getLangOpts().CUDA) { 12910b57cec5SDimitry Andric // In CUDA code, GNU attributes are allowed to appear immediately after the 12920b57cec5SDimitry Andric // "[...]", even if there is no "(...)" before the lambda body. 1293*bdd1243dSDimitry Andric // 1294*bdd1243dSDimitry Andric // Note that we support __noinline__ as a keyword in this mode and thus 1295*bdd1243dSDimitry Andric // it has to be separately handled. 1296*bdd1243dSDimitry Andric while (true) { 1297*bdd1243dSDimitry Andric if (Tok.is(tok::kw___noinline__)) { 1298*bdd1243dSDimitry Andric IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 1299*bdd1243dSDimitry Andric SourceLocation AttrNameLoc = ConsumeToken(); 1300*bdd1243dSDimitry Andric Attr.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, 1301*bdd1243dSDimitry Andric ParsedAttr::AS_Keyword); 1302*bdd1243dSDimitry Andric } else if (Tok.is(tok::kw___attribute)) 1303*bdd1243dSDimitry Andric ParseGNUAttributes(Attr, nullptr, &D); 1304*bdd1243dSDimitry Andric else 1305*bdd1243dSDimitry Andric break; 1306*bdd1243dSDimitry Andric } 1307*bdd1243dSDimitry Andric 1308*bdd1243dSDimitry Andric D.takeAttributes(Attr); 13090b57cec5SDimitry Andric } 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric // Helper to emit a warning if we see a CUDA host/device/global attribute 13120b57cec5SDimitry Andric // after '(...)'. nvcc doesn't accept this. 13130b57cec5SDimitry Andric auto WarnIfHasCUDATargetAttr = [&] { 13140b57cec5SDimitry Andric if (getLangOpts().CUDA) 13150b57cec5SDimitry Andric for (const ParsedAttr &A : Attr) 13160b57cec5SDimitry Andric if (A.getKind() == ParsedAttr::AT_CUDADevice || 13170b57cec5SDimitry Andric A.getKind() == ParsedAttr::AT_CUDAHost || 13180b57cec5SDimitry Andric A.getKind() == ParsedAttr::AT_CUDAGlobal) 13190b57cec5SDimitry Andric Diag(A.getLoc(), diag::warn_cuda_attr_lambda_position) 1320a7dea167SDimitry Andric << A.getAttrName()->getName(); 13210b57cec5SDimitry Andric }; 13220b57cec5SDimitry Andric 13235ffd83dbSDimitry Andric MultiParseScope TemplateParamScope(*this); 13245ffd83dbSDimitry Andric if (Tok.is(tok::less)) { 13255ffd83dbSDimitry Andric Diag(Tok, getLangOpts().CPlusPlus20 13260b57cec5SDimitry Andric ? diag::warn_cxx17_compat_lambda_template_parameter_list 13270b57cec5SDimitry Andric : diag::ext_lambda_template_parameter_list); 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric SmallVector<NamedDecl*, 4> TemplateParams; 13300b57cec5SDimitry Andric SourceLocation LAngleLoc, RAngleLoc; 13315ffd83dbSDimitry Andric if (ParseTemplateParameters(TemplateParamScope, 13325ffd83dbSDimitry Andric CurTemplateDepthTracker.getDepth(), 13330b57cec5SDimitry Andric TemplateParams, LAngleLoc, RAngleLoc)) { 13340b57cec5SDimitry Andric Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); 13350b57cec5SDimitry Andric return ExprError(); 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric if (TemplateParams.empty()) { 13390b57cec5SDimitry Andric Diag(RAngleLoc, 13400b57cec5SDimitry Andric diag::err_lambda_template_parameter_list_empty); 13410b57cec5SDimitry Andric } else { 1342e8d8bef9SDimitry Andric ExprResult RequiresClause; 1343e8d8bef9SDimitry Andric if (TryConsumeToken(tok::kw_requires)) { 1344e8d8bef9SDimitry Andric RequiresClause = 1345e8d8bef9SDimitry Andric Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression( 1346e8d8bef9SDimitry Andric /*IsTrailingRequiresClause=*/false)); 1347e8d8bef9SDimitry Andric if (RequiresClause.isInvalid()) 1348e8d8bef9SDimitry Andric SkipUntil({tok::l_brace, tok::l_paren}, StopAtSemi | StopBeforeMatch); 1349e8d8bef9SDimitry Andric } 1350e8d8bef9SDimitry Andric 13510b57cec5SDimitry Andric Actions.ActOnLambdaExplicitTemplateParameterList( 1352e8d8bef9SDimitry Andric LAngleLoc, TemplateParams, RAngleLoc, RequiresClause); 13530b57cec5SDimitry Andric ++CurTemplateDepthTracker; 13540b57cec5SDimitry Andric } 13550b57cec5SDimitry Andric } 13560b57cec5SDimitry Andric 1357fe6060f1SDimitry Andric // Implement WG21 P2173, which allows attributes immediately before the 1358fe6060f1SDimitry Andric // lambda declarator and applies them to the corresponding function operator 1359fe6060f1SDimitry Andric // or operator template declaration. We accept this as a conforming extension 1360fe6060f1SDimitry Andric // in all language modes that support lambdas. 1361fe6060f1SDimitry Andric if (isCXX11AttributeSpecifier()) { 1362fe6060f1SDimitry Andric Diag(Tok, getLangOpts().CPlusPlus2b 1363fe6060f1SDimitry Andric ? diag::warn_cxx20_compat_decl_attrs_on_lambda 1364fe6060f1SDimitry Andric : diag::ext_decl_attrs_on_lambda); 1365fe6060f1SDimitry Andric MaybeParseCXX11Attributes(D); 1366fe6060f1SDimitry Andric } 1367fe6060f1SDimitry Andric 13680b57cec5SDimitry Andric TypeResult TrailingReturnType; 1369e8d8bef9SDimitry Andric SourceLocation TrailingReturnTypeLoc; 1370fe6060f1SDimitry Andric 1371fe6060f1SDimitry Andric auto ParseLambdaSpecifiers = 1372fe6060f1SDimitry Andric [&](SourceLocation LParenLoc, SourceLocation RParenLoc, 1373fe6060f1SDimitry Andric MutableArrayRef<DeclaratorChunk::ParamInfo> ParamInfo, 1374fe6060f1SDimitry Andric SourceLocation EllipsisLoc) { 1375fe6060f1SDimitry Andric SourceLocation DeclEndLoc = RParenLoc; 1376fe6060f1SDimitry Andric 1377fe6060f1SDimitry Andric // GNU-style attributes must be parsed before the mutable specifier to 1378fe6060f1SDimitry Andric // be compatible with GCC. MSVC-style attributes must be parsed before 1379fe6060f1SDimitry Andric // the mutable specifier to be compatible with MSVC. 1380fe6060f1SDimitry Andric MaybeParseAttributes(PAKM_GNU | PAKM_Declspec, Attr); 1381fe6060f1SDimitry Andric 1382*bdd1243dSDimitry Andric // Parse lambda specifiers and update the DeclEndLoc. 1383fe6060f1SDimitry Andric SourceLocation MutableLoc; 1384*bdd1243dSDimitry Andric SourceLocation StaticLoc; 1385fe6060f1SDimitry Andric SourceLocation ConstexprLoc; 1386fe6060f1SDimitry Andric SourceLocation ConstevalLoc; 1387*bdd1243dSDimitry Andric tryConsumeLambdaSpecifierToken(*this, MutableLoc, StaticLoc, 1388*bdd1243dSDimitry Andric ConstexprLoc, ConstevalLoc, DeclEndLoc); 1389fe6060f1SDimitry Andric 1390*bdd1243dSDimitry Andric DiagnoseStaticSpecifierRestrictions(*this, StaticLoc, MutableLoc, 1391*bdd1243dSDimitry Andric Intro); 1392*bdd1243dSDimitry Andric 1393*bdd1243dSDimitry Andric addStaticToLambdaDeclSpecifier(*this, StaticLoc, DS); 1394fe6060f1SDimitry Andric addConstexprToLambdaDeclSpecifier(*this, ConstexprLoc, DS); 1395fe6060f1SDimitry Andric addConstevalToLambdaDeclSpecifier(*this, ConstevalLoc, DS); 1396fe6060f1SDimitry Andric // Parse exception-specification[opt]. 1397fe6060f1SDimitry Andric ExceptionSpecificationType ESpecType = EST_None; 1398fe6060f1SDimitry Andric SourceRange ESpecRange; 1399fe6060f1SDimitry Andric SmallVector<ParsedType, 2> DynamicExceptions; 1400fe6060f1SDimitry Andric SmallVector<SourceRange, 2> DynamicExceptionRanges; 1401fe6060f1SDimitry Andric ExprResult NoexceptExpr; 1402fe6060f1SDimitry Andric CachedTokens *ExceptionSpecTokens; 1403fe6060f1SDimitry Andric ESpecType = tryParseExceptionSpecification( 1404fe6060f1SDimitry Andric /*Delayed=*/false, ESpecRange, DynamicExceptions, 1405fe6060f1SDimitry Andric DynamicExceptionRanges, NoexceptExpr, ExceptionSpecTokens); 1406fe6060f1SDimitry Andric 1407fe6060f1SDimitry Andric if (ESpecType != EST_None) 1408fe6060f1SDimitry Andric DeclEndLoc = ESpecRange.getEnd(); 1409fe6060f1SDimitry Andric 1410fe6060f1SDimitry Andric // Parse attribute-specifier[opt]. 141181ad6265SDimitry Andric if (MaybeParseCXX11Attributes(Attr)) 141281ad6265SDimitry Andric DeclEndLoc = Attr.Range.getEnd(); 1413fe6060f1SDimitry Andric 1414fe6060f1SDimitry Andric // Parse OpenCL addr space attribute. 1415fe6060f1SDimitry Andric if (Tok.isOneOf(tok::kw___private, tok::kw___global, tok::kw___local, 1416fe6060f1SDimitry Andric tok::kw___constant, tok::kw___generic)) { 1417fe6060f1SDimitry Andric ParseOpenCLQualifiers(DS.getAttributes()); 1418fe6060f1SDimitry Andric ConsumeToken(); 1419fe6060f1SDimitry Andric } 1420fe6060f1SDimitry Andric 1421fe6060f1SDimitry Andric SourceLocation FunLocalRangeEnd = DeclEndLoc; 1422fe6060f1SDimitry Andric 1423fe6060f1SDimitry Andric // Parse trailing-return-type[opt]. 1424fe6060f1SDimitry Andric if (Tok.is(tok::arrow)) { 1425fe6060f1SDimitry Andric FunLocalRangeEnd = Tok.getLocation(); 1426fe6060f1SDimitry Andric SourceRange Range; 1427fe6060f1SDimitry Andric TrailingReturnType = ParseTrailingReturnType( 1428fe6060f1SDimitry Andric Range, /*MayBeFollowedByDirectInit*/ false); 1429fe6060f1SDimitry Andric TrailingReturnTypeLoc = Range.getBegin(); 1430fe6060f1SDimitry Andric if (Range.getEnd().isValid()) 1431fe6060f1SDimitry Andric DeclEndLoc = Range.getEnd(); 1432fe6060f1SDimitry Andric } 1433fe6060f1SDimitry Andric 1434fe6060f1SDimitry Andric SourceLocation NoLoc; 1435fe6060f1SDimitry Andric D.AddTypeInfo( 1436fe6060f1SDimitry Andric DeclaratorChunk::getFunction( 1437fe6060f1SDimitry Andric /*HasProto=*/true, 1438fe6060f1SDimitry Andric /*IsAmbiguous=*/false, LParenLoc, ParamInfo.data(), 1439fe6060f1SDimitry Andric ParamInfo.size(), EllipsisLoc, RParenLoc, 1440fe6060f1SDimitry Andric /*RefQualifierIsLvalueRef=*/true, 1441fe6060f1SDimitry Andric /*RefQualifierLoc=*/NoLoc, MutableLoc, ESpecType, ESpecRange, 1442fe6060f1SDimitry Andric DynamicExceptions.data(), DynamicExceptionRanges.data(), 1443fe6060f1SDimitry Andric DynamicExceptions.size(), 1444fe6060f1SDimitry Andric NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr, 1445fe6060f1SDimitry Andric /*ExceptionSpecTokens*/ nullptr, 1446*bdd1243dSDimitry Andric /*DeclsInPrototype=*/std::nullopt, LParenLoc, FunLocalRangeEnd, 1447*bdd1243dSDimitry Andric D, TrailingReturnType, TrailingReturnTypeLoc, &DS), 1448fe6060f1SDimitry Andric std::move(Attr), DeclEndLoc); 1449fe6060f1SDimitry Andric }; 1450fe6060f1SDimitry Andric 14510b57cec5SDimitry Andric if (Tok.is(tok::l_paren)) { 1452fe6060f1SDimitry Andric ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 14530b57cec5SDimitry Andric Scope::FunctionDeclarationScope | 14540b57cec5SDimitry Andric Scope::DeclScope); 14550b57cec5SDimitry Andric 14560b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 14570b57cec5SDimitry Andric T.consumeOpen(); 14580b57cec5SDimitry Andric SourceLocation LParenLoc = T.getOpenLocation(); 14590b57cec5SDimitry Andric 14600b57cec5SDimitry Andric // Parse parameter-declaration-clause. 14610b57cec5SDimitry Andric SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; 14620b57cec5SDimitry Andric SourceLocation EllipsisLoc; 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric if (Tok.isNot(tok::r_paren)) { 14650b57cec5SDimitry Andric Actions.RecordParsingTemplateParameterDepth( 14660b57cec5SDimitry Andric CurTemplateDepthTracker.getOriginalDepth()); 14670b57cec5SDimitry Andric 1468*bdd1243dSDimitry Andric ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc); 14690b57cec5SDimitry Andric // For a generic lambda, each 'auto' within the parameter declaration 14700b57cec5SDimitry Andric // clause creates a template type parameter, so increment the depth. 14710b57cec5SDimitry Andric // If we've parsed any explicit template parameters, then the depth will 14720b57cec5SDimitry Andric // have already been incremented. So we make sure that at most a single 14730b57cec5SDimitry Andric // depth level is added. 14740b57cec5SDimitry Andric if (Actions.getCurGenericLambda()) 14750b57cec5SDimitry Andric CurTemplateDepthTracker.setAddedDepth(1); 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric T.consumeClose(); 14790b57cec5SDimitry Andric 1480fe6060f1SDimitry Andric // Parse lambda-specifiers. 1481fe6060f1SDimitry Andric ParseLambdaSpecifiers(LParenLoc, /*DeclEndLoc=*/T.getCloseLocation(), 1482fe6060f1SDimitry Andric ParamInfo, EllipsisLoc); 1483480093f4SDimitry Andric 1484480093f4SDimitry Andric // Parse requires-clause[opt]. 1485480093f4SDimitry Andric if (Tok.is(tok::kw_requires)) 1486480093f4SDimitry Andric ParseTrailingRequiresClause(D); 14870b57cec5SDimitry Andric } else if (Tok.isOneOf(tok::kw_mutable, tok::arrow, tok::kw___attribute, 1488*bdd1243dSDimitry Andric tok::kw_constexpr, tok::kw_consteval, tok::kw_static, 1489480093f4SDimitry Andric tok::kw___private, tok::kw___global, tok::kw___local, 1490480093f4SDimitry Andric tok::kw___constant, tok::kw___generic, 1491*bdd1243dSDimitry Andric tok::kw_groupshared, tok::kw_requires, 1492*bdd1243dSDimitry Andric tok::kw_noexcept) || 14930b57cec5SDimitry Andric (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) { 1494fe6060f1SDimitry Andric if (!getLangOpts().CPlusPlus2b) 1495fe6060f1SDimitry Andric // It's common to forget that one needs '()' before 'mutable', an 1496fe6060f1SDimitry Andric // attribute specifier, the result type, or the requires clause. Deal with 1497fe6060f1SDimitry Andric // this. 1498fe6060f1SDimitry Andric Diag(Tok, diag::ext_lambda_missing_parens) 14990b57cec5SDimitry Andric << FixItHint::CreateInsertion(Tok.getLocation(), "() "); 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric SourceLocation NoLoc; 1502fe6060f1SDimitry Andric // Parse lambda-specifiers. 1503fe6060f1SDimitry Andric std::vector<DeclaratorChunk::ParamInfo> EmptyParamInfo; 1504fe6060f1SDimitry Andric ParseLambdaSpecifiers(/*LParenLoc=*/NoLoc, /*RParenLoc=*/NoLoc, 1505fe6060f1SDimitry Andric EmptyParamInfo, /*EllipsisLoc=*/NoLoc); 1506fe6060f1SDimitry Andric } 1507480093f4SDimitry Andric 1508480093f4SDimitry Andric WarnIfHasCUDATargetAttr(); 15090b57cec5SDimitry Andric 15100b57cec5SDimitry Andric // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using 15110b57cec5SDimitry Andric // it. 15120b57cec5SDimitry Andric unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope | 15130b57cec5SDimitry Andric Scope::CompoundStmtScope; 15140b57cec5SDimitry Andric ParseScope BodyScope(this, ScopeFlags); 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope()); 15170b57cec5SDimitry Andric 15180b57cec5SDimitry Andric // Parse compound-statement. 15190b57cec5SDimitry Andric if (!Tok.is(tok::l_brace)) { 15200b57cec5SDimitry Andric Diag(Tok, diag::err_expected_lambda_body); 15210b57cec5SDimitry Andric Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); 15220b57cec5SDimitry Andric return ExprError(); 15230b57cec5SDimitry Andric } 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric StmtResult Stmt(ParseCompoundStatementBody()); 15260b57cec5SDimitry Andric BodyScope.Exit(); 15270b57cec5SDimitry Andric TemplateParamScope.Exit(); 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid()) 15300b57cec5SDimitry Andric return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope()); 15310b57cec5SDimitry Andric 15320b57cec5SDimitry Andric Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); 15330b57cec5SDimitry Andric return ExprError(); 15340b57cec5SDimitry Andric } 15350b57cec5SDimitry Andric 15360b57cec5SDimitry Andric /// ParseCXXCasts - This handles the various ways to cast expressions to another 15370b57cec5SDimitry Andric /// type. 15380b57cec5SDimitry Andric /// 15390b57cec5SDimitry Andric /// postfix-expression: [C++ 5.2p1] 15400b57cec5SDimitry Andric /// 'dynamic_cast' '<' type-name '>' '(' expression ')' 15410b57cec5SDimitry Andric /// 'static_cast' '<' type-name '>' '(' expression ')' 15420b57cec5SDimitry Andric /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' 15430b57cec5SDimitry Andric /// 'const_cast' '<' type-name '>' '(' expression ')' 15440b57cec5SDimitry Andric /// 15455ffd83dbSDimitry Andric /// C++ for OpenCL s2.3.1 adds: 15465ffd83dbSDimitry Andric /// 'addrspace_cast' '<' type-name '>' '(' expression ')' 15470b57cec5SDimitry Andric ExprResult Parser::ParseCXXCasts() { 15480b57cec5SDimitry Andric tok::TokenKind Kind = Tok.getKind(); 15490b57cec5SDimitry Andric const char *CastName = nullptr; // For error messages 15500b57cec5SDimitry Andric 15510b57cec5SDimitry Andric switch (Kind) { 15520b57cec5SDimitry Andric default: llvm_unreachable("Unknown C++ cast!"); 15535ffd83dbSDimitry Andric case tok::kw_addrspace_cast: CastName = "addrspace_cast"; break; 15540b57cec5SDimitry Andric case tok::kw_const_cast: CastName = "const_cast"; break; 15550b57cec5SDimitry Andric case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; 15560b57cec5SDimitry Andric case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; 15570b57cec5SDimitry Andric case tok::kw_static_cast: CastName = "static_cast"; break; 15580b57cec5SDimitry Andric } 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric SourceLocation OpLoc = ConsumeToken(); 15610b57cec5SDimitry Andric SourceLocation LAngleBracketLoc = Tok.getLocation(); 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric // Check for "<::" which is parsed as "[:". If found, fix token stream, 15640b57cec5SDimitry Andric // diagnose error, suggest fix, and recover parsing. 15650b57cec5SDimitry Andric if (Tok.is(tok::l_square) && Tok.getLength() == 2) { 15660b57cec5SDimitry Andric Token Next = NextToken(); 15670b57cec5SDimitry Andric if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next)) 15680b57cec5SDimitry Andric FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true); 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) 15720b57cec5SDimitry Andric return ExprError(); 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric // Parse the common declaration-specifiers piece. 15750b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 1576*bdd1243dSDimitry Andric ParseSpecifierQualifierList(DS, /*AccessSpecifier=*/AS_none, 1577*bdd1243dSDimitry Andric DeclSpecContext::DSC_type_specifier); 15780b57cec5SDimitry Andric 15790b57cec5SDimitry Andric // Parse the abstract-declarator, if present. 158081ad6265SDimitry Andric Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 158181ad6265SDimitry Andric DeclaratorContext::TypeName); 15820b57cec5SDimitry Andric ParseDeclarator(DeclaratorInfo); 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric SourceLocation RAngleBracketLoc = Tok.getLocation(); 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric if (ExpectAndConsume(tok::greater)) 15870b57cec5SDimitry Andric return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less); 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 15900b57cec5SDimitry Andric 15910b57cec5SDimitry Andric if (T.expectAndConsume(diag::err_expected_lparen_after, CastName)) 15920b57cec5SDimitry Andric return ExprError(); 15930b57cec5SDimitry Andric 15940b57cec5SDimitry Andric ExprResult Result = ParseExpression(); 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric // Match the ')'. 15970b57cec5SDimitry Andric T.consumeClose(); 15980b57cec5SDimitry Andric 15990b57cec5SDimitry Andric if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType()) 16000b57cec5SDimitry Andric Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, 16010b57cec5SDimitry Andric LAngleBracketLoc, DeclaratorInfo, 16020b57cec5SDimitry Andric RAngleBracketLoc, 16030b57cec5SDimitry Andric T.getOpenLocation(), Result.get(), 16040b57cec5SDimitry Andric T.getCloseLocation()); 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric return Result; 16070b57cec5SDimitry Andric } 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric /// ParseCXXTypeid - This handles the C++ typeid expression. 16100b57cec5SDimitry Andric /// 16110b57cec5SDimitry Andric /// postfix-expression: [C++ 5.2p1] 16120b57cec5SDimitry Andric /// 'typeid' '(' expression ')' 16130b57cec5SDimitry Andric /// 'typeid' '(' type-id ')' 16140b57cec5SDimitry Andric /// 16150b57cec5SDimitry Andric ExprResult Parser::ParseCXXTypeid() { 16160b57cec5SDimitry Andric assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); 16170b57cec5SDimitry Andric 16180b57cec5SDimitry Andric SourceLocation OpLoc = ConsumeToken(); 16190b57cec5SDimitry Andric SourceLocation LParenLoc, RParenLoc; 16200b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 16210b57cec5SDimitry Andric 16220b57cec5SDimitry Andric // typeid expressions are always parenthesized. 16230b57cec5SDimitry Andric if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid")) 16240b57cec5SDimitry Andric return ExprError(); 16250b57cec5SDimitry Andric LParenLoc = T.getOpenLocation(); 16260b57cec5SDimitry Andric 16270b57cec5SDimitry Andric ExprResult Result; 16280b57cec5SDimitry Andric 16290b57cec5SDimitry Andric // C++0x [expr.typeid]p3: 16300b57cec5SDimitry Andric // When typeid is applied to an expression other than an lvalue of a 16310b57cec5SDimitry Andric // polymorphic class type [...] The expression is an unevaluated 16320b57cec5SDimitry Andric // operand (Clause 5). 16330b57cec5SDimitry Andric // 16340b57cec5SDimitry Andric // Note that we can't tell whether the expression is an lvalue of a 16350b57cec5SDimitry Andric // polymorphic class type until after we've parsed the expression; we 16360b57cec5SDimitry Andric // speculatively assume the subexpression is unevaluated, and fix it up 16370b57cec5SDimitry Andric // later. 16380b57cec5SDimitry Andric // 16390b57cec5SDimitry Andric // We enter the unevaluated context before trying to determine whether we 16400b57cec5SDimitry Andric // have a type-id, because the tentative parse logic will try to resolve 16410b57cec5SDimitry Andric // names, and must treat them as unevaluated. 16420b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 16430b57cec5SDimitry Andric Actions, Sema::ExpressionEvaluationContext::Unevaluated, 16440b57cec5SDimitry Andric Sema::ReuseLambdaContextDecl); 16450b57cec5SDimitry Andric 16460b57cec5SDimitry Andric if (isTypeIdInParens()) { 16470b57cec5SDimitry Andric TypeResult Ty = ParseTypeName(); 16480b57cec5SDimitry Andric 16490b57cec5SDimitry Andric // Match the ')'. 16500b57cec5SDimitry Andric T.consumeClose(); 16510b57cec5SDimitry Andric RParenLoc = T.getCloseLocation(); 16520b57cec5SDimitry Andric if (Ty.isInvalid() || RParenLoc.isInvalid()) 16530b57cec5SDimitry Andric return ExprError(); 16540b57cec5SDimitry Andric 16550b57cec5SDimitry Andric Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, 16560b57cec5SDimitry Andric Ty.get().getAsOpaquePtr(), RParenLoc); 16570b57cec5SDimitry Andric } else { 16580b57cec5SDimitry Andric Result = ParseExpression(); 16590b57cec5SDimitry Andric 16600b57cec5SDimitry Andric // Match the ')'. 16610b57cec5SDimitry Andric if (Result.isInvalid()) 16620b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 16630b57cec5SDimitry Andric else { 16640b57cec5SDimitry Andric T.consumeClose(); 16650b57cec5SDimitry Andric RParenLoc = T.getCloseLocation(); 16660b57cec5SDimitry Andric if (RParenLoc.isInvalid()) 16670b57cec5SDimitry Andric return ExprError(); 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, 16700b57cec5SDimitry Andric Result.get(), RParenLoc); 16710b57cec5SDimitry Andric } 16720b57cec5SDimitry Andric } 16730b57cec5SDimitry Andric 16740b57cec5SDimitry Andric return Result; 16750b57cec5SDimitry Andric } 16760b57cec5SDimitry Andric 16770b57cec5SDimitry Andric /// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression. 16780b57cec5SDimitry Andric /// 16790b57cec5SDimitry Andric /// '__uuidof' '(' expression ')' 16800b57cec5SDimitry Andric /// '__uuidof' '(' type-id ')' 16810b57cec5SDimitry Andric /// 16820b57cec5SDimitry Andric ExprResult Parser::ParseCXXUuidof() { 16830b57cec5SDimitry Andric assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!"); 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric SourceLocation OpLoc = ConsumeToken(); 16860b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 16870b57cec5SDimitry Andric 16880b57cec5SDimitry Andric // __uuidof expressions are always parenthesized. 16890b57cec5SDimitry Andric if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof")) 16900b57cec5SDimitry Andric return ExprError(); 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric ExprResult Result; 16930b57cec5SDimitry Andric 16940b57cec5SDimitry Andric if (isTypeIdInParens()) { 16950b57cec5SDimitry Andric TypeResult Ty = ParseTypeName(); 16960b57cec5SDimitry Andric 16970b57cec5SDimitry Andric // Match the ')'. 16980b57cec5SDimitry Andric T.consumeClose(); 16990b57cec5SDimitry Andric 17000b57cec5SDimitry Andric if (Ty.isInvalid()) 17010b57cec5SDimitry Andric return ExprError(); 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true, 17040b57cec5SDimitry Andric Ty.get().getAsOpaquePtr(), 17050b57cec5SDimitry Andric T.getCloseLocation()); 17060b57cec5SDimitry Andric } else { 17070b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 17080b57cec5SDimitry Andric Actions, Sema::ExpressionEvaluationContext::Unevaluated); 17090b57cec5SDimitry Andric Result = ParseExpression(); 17100b57cec5SDimitry Andric 17110b57cec5SDimitry Andric // Match the ')'. 17120b57cec5SDimitry Andric if (Result.isInvalid()) 17130b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 17140b57cec5SDimitry Andric else { 17150b57cec5SDimitry Andric T.consumeClose(); 17160b57cec5SDimitry Andric 17170b57cec5SDimitry Andric Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), 17180b57cec5SDimitry Andric /*isType=*/false, 17190b57cec5SDimitry Andric Result.get(), T.getCloseLocation()); 17200b57cec5SDimitry Andric } 17210b57cec5SDimitry Andric } 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric return Result; 17240b57cec5SDimitry Andric } 17250b57cec5SDimitry Andric 17260b57cec5SDimitry Andric /// Parse a C++ pseudo-destructor expression after the base, 17270b57cec5SDimitry Andric /// . or -> operator, and nested-name-specifier have already been 17285ffd83dbSDimitry Andric /// parsed. We're handling this fragment of the grammar: 17290b57cec5SDimitry Andric /// 17305ffd83dbSDimitry Andric /// postfix-expression: [C++2a expr.post] 17315ffd83dbSDimitry Andric /// postfix-expression . template[opt] id-expression 17325ffd83dbSDimitry Andric /// postfix-expression -> template[opt] id-expression 17330b57cec5SDimitry Andric /// 17345ffd83dbSDimitry Andric /// id-expression: 17355ffd83dbSDimitry Andric /// qualified-id 17365ffd83dbSDimitry Andric /// unqualified-id 17375ffd83dbSDimitry Andric /// 17385ffd83dbSDimitry Andric /// qualified-id: 17395ffd83dbSDimitry Andric /// nested-name-specifier template[opt] unqualified-id 17405ffd83dbSDimitry Andric /// 17415ffd83dbSDimitry Andric /// nested-name-specifier: 17425ffd83dbSDimitry Andric /// type-name :: 17435ffd83dbSDimitry Andric /// decltype-specifier :: FIXME: not implemented, but probably only 17445ffd83dbSDimitry Andric /// allowed in C++ grammar by accident 17455ffd83dbSDimitry Andric /// nested-name-specifier identifier :: 17465ffd83dbSDimitry Andric /// nested-name-specifier template[opt] simple-template-id :: 17475ffd83dbSDimitry Andric /// [...] 17485ffd83dbSDimitry Andric /// 17495ffd83dbSDimitry Andric /// unqualified-id: 17500b57cec5SDimitry Andric /// ~ type-name 17515ffd83dbSDimitry Andric /// ~ decltype-specifier 17525ffd83dbSDimitry Andric /// [...] 17530b57cec5SDimitry Andric /// 17545ffd83dbSDimitry Andric /// ... where the all but the last component of the nested-name-specifier 17555ffd83dbSDimitry Andric /// has already been parsed, and the base expression is not of a non-dependent 17565ffd83dbSDimitry Andric /// class type. 17570b57cec5SDimitry Andric ExprResult 17580b57cec5SDimitry Andric Parser::ParseCXXPseudoDestructor(Expr *Base, SourceLocation OpLoc, 17590b57cec5SDimitry Andric tok::TokenKind OpKind, 17600b57cec5SDimitry Andric CXXScopeSpec &SS, 17610b57cec5SDimitry Andric ParsedType ObjectType) { 17625ffd83dbSDimitry Andric // If the last component of the (optional) nested-name-specifier is 17635ffd83dbSDimitry Andric // template[opt] simple-template-id, it has already been annotated. 17640b57cec5SDimitry Andric UnqualifiedId FirstTypeName; 17650b57cec5SDimitry Andric SourceLocation CCLoc; 17660b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 17670b57cec5SDimitry Andric FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 17680b57cec5SDimitry Andric ConsumeToken(); 17690b57cec5SDimitry Andric assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); 17700b57cec5SDimitry Andric CCLoc = ConsumeToken(); 17710b57cec5SDimitry Andric } else if (Tok.is(tok::annot_template_id)) { 17725ffd83dbSDimitry Andric TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 17735ffd83dbSDimitry Andric // FIXME: Carry on and build an AST representation for tooling. 17745ffd83dbSDimitry Andric if (TemplateId->isInvalid()) 17755ffd83dbSDimitry Andric return ExprError(); 17765ffd83dbSDimitry Andric FirstTypeName.setTemplateId(TemplateId); 17770b57cec5SDimitry Andric ConsumeAnnotationToken(); 17780b57cec5SDimitry Andric assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); 17790b57cec5SDimitry Andric CCLoc = ConsumeToken(); 17800b57cec5SDimitry Andric } else { 17815ffd83dbSDimitry Andric assert(SS.isEmpty() && "missing last component of nested name specifier"); 17820b57cec5SDimitry Andric FirstTypeName.setIdentifier(nullptr, SourceLocation()); 17830b57cec5SDimitry Andric } 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric // Parse the tilde. 17860b57cec5SDimitry Andric assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail"); 17870b57cec5SDimitry Andric SourceLocation TildeLoc = ConsumeToken(); 17880b57cec5SDimitry Andric 17895ffd83dbSDimitry Andric if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid()) { 17900b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 17910b57cec5SDimitry Andric ParseDecltypeSpecifier(DS); 17920b57cec5SDimitry Andric if (DS.getTypeSpecType() == TST_error) 17930b57cec5SDimitry Andric return ExprError(); 17940b57cec5SDimitry Andric return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind, 17950b57cec5SDimitry Andric TildeLoc, DS); 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric if (!Tok.is(tok::identifier)) { 17990b57cec5SDimitry Andric Diag(Tok, diag::err_destructor_tilde_identifier); 18000b57cec5SDimitry Andric return ExprError(); 18010b57cec5SDimitry Andric } 18020b57cec5SDimitry Andric 18030b57cec5SDimitry Andric // Parse the second type. 18040b57cec5SDimitry Andric UnqualifiedId SecondTypeName; 18050b57cec5SDimitry Andric IdentifierInfo *Name = Tok.getIdentifierInfo(); 18060b57cec5SDimitry Andric SourceLocation NameLoc = ConsumeToken(); 18070b57cec5SDimitry Andric SecondTypeName.setIdentifier(Name, NameLoc); 18080b57cec5SDimitry Andric 18090b57cec5SDimitry Andric // If there is a '<', the second type name is a template-id. Parse 18100b57cec5SDimitry Andric // it as such. 18115ffd83dbSDimitry Andric // 18125ffd83dbSDimitry Andric // FIXME: This is not a context in which a '<' is assumed to start a template 18135ffd83dbSDimitry Andric // argument list. This affects examples such as 18145ffd83dbSDimitry Andric // void f(auto *p) { p->~X<int>(); } 18155ffd83dbSDimitry Andric // ... but there's no ambiguity, and nowhere to write 'template' in such an 18165ffd83dbSDimitry Andric // example, so we accept it anyway. 18170b57cec5SDimitry Andric if (Tok.is(tok::less) && 18185ffd83dbSDimitry Andric ParseUnqualifiedIdTemplateId( 18195ffd83dbSDimitry Andric SS, ObjectType, Base && Base->containsErrors(), SourceLocation(), 18205ffd83dbSDimitry Andric Name, NameLoc, false, SecondTypeName, 18210b57cec5SDimitry Andric /*AssumeTemplateId=*/true)) 18220b57cec5SDimitry Andric return ExprError(); 18230b57cec5SDimitry Andric 18240b57cec5SDimitry Andric return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind, 18250b57cec5SDimitry Andric SS, FirstTypeName, CCLoc, TildeLoc, 18260b57cec5SDimitry Andric SecondTypeName); 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric 18290b57cec5SDimitry Andric /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. 18300b57cec5SDimitry Andric /// 18310b57cec5SDimitry Andric /// boolean-literal: [C++ 2.13.5] 18320b57cec5SDimitry Andric /// 'true' 18330b57cec5SDimitry Andric /// 'false' 18340b57cec5SDimitry Andric ExprResult Parser::ParseCXXBoolLiteral() { 18350b57cec5SDimitry Andric tok::TokenKind Kind = Tok.getKind(); 18360b57cec5SDimitry Andric return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); 18370b57cec5SDimitry Andric } 18380b57cec5SDimitry Andric 18390b57cec5SDimitry Andric /// ParseThrowExpression - This handles the C++ throw expression. 18400b57cec5SDimitry Andric /// 18410b57cec5SDimitry Andric /// throw-expression: [C++ 15] 18420b57cec5SDimitry Andric /// 'throw' assignment-expression[opt] 18430b57cec5SDimitry Andric ExprResult Parser::ParseThrowExpression() { 18440b57cec5SDimitry Andric assert(Tok.is(tok::kw_throw) && "Not throw!"); 18450b57cec5SDimitry Andric SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. 18460b57cec5SDimitry Andric 18470b57cec5SDimitry Andric // If the current token isn't the start of an assignment-expression, 18480b57cec5SDimitry Andric // then the expression is not present. This handles things like: 18490b57cec5SDimitry Andric // "C ? throw : (void)42", which is crazy but legal. 18500b57cec5SDimitry Andric switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. 18510b57cec5SDimitry Andric case tok::semi: 18520b57cec5SDimitry Andric case tok::r_paren: 18530b57cec5SDimitry Andric case tok::r_square: 18540b57cec5SDimitry Andric case tok::r_brace: 18550b57cec5SDimitry Andric case tok::colon: 18560b57cec5SDimitry Andric case tok::comma: 18570b57cec5SDimitry Andric return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, nullptr); 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric default: 18600b57cec5SDimitry Andric ExprResult Expr(ParseAssignmentExpression()); 18610b57cec5SDimitry Andric if (Expr.isInvalid()) return Expr; 18620b57cec5SDimitry Andric return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.get()); 18630b57cec5SDimitry Andric } 18640b57cec5SDimitry Andric } 18650b57cec5SDimitry Andric 18660b57cec5SDimitry Andric /// Parse the C++ Coroutines co_yield expression. 18670b57cec5SDimitry Andric /// 18680b57cec5SDimitry Andric /// co_yield-expression: 18690b57cec5SDimitry Andric /// 'co_yield' assignment-expression[opt] 18700b57cec5SDimitry Andric ExprResult Parser::ParseCoyieldExpression() { 18710b57cec5SDimitry Andric assert(Tok.is(tok::kw_co_yield) && "Not co_yield!"); 18720b57cec5SDimitry Andric 18730b57cec5SDimitry Andric SourceLocation Loc = ConsumeToken(); 18740b57cec5SDimitry Andric ExprResult Expr = Tok.is(tok::l_brace) ? ParseBraceInitializer() 18750b57cec5SDimitry Andric : ParseAssignmentExpression(); 18760b57cec5SDimitry Andric if (!Expr.isInvalid()) 18770b57cec5SDimitry Andric Expr = Actions.ActOnCoyieldExpr(getCurScope(), Loc, Expr.get()); 18780b57cec5SDimitry Andric return Expr; 18790b57cec5SDimitry Andric } 18800b57cec5SDimitry Andric 18810b57cec5SDimitry Andric /// ParseCXXThis - This handles the C++ 'this' pointer. 18820b57cec5SDimitry Andric /// 18830b57cec5SDimitry Andric /// C++ 9.3.2: In the body of a non-static member function, the keyword this is 18840b57cec5SDimitry Andric /// a non-lvalue expression whose value is the address of the object for which 18850b57cec5SDimitry Andric /// the function is called. 18860b57cec5SDimitry Andric ExprResult Parser::ParseCXXThis() { 18870b57cec5SDimitry Andric assert(Tok.is(tok::kw_this) && "Not 'this'!"); 18880b57cec5SDimitry Andric SourceLocation ThisLoc = ConsumeToken(); 18890b57cec5SDimitry Andric return Actions.ActOnCXXThis(ThisLoc); 18900b57cec5SDimitry Andric } 18910b57cec5SDimitry Andric 18920b57cec5SDimitry Andric /// ParseCXXTypeConstructExpression - Parse construction of a specified type. 18930b57cec5SDimitry Andric /// Can be interpreted either as function-style casting ("int(x)") 18940b57cec5SDimitry Andric /// or class type construction ("ClassType(x,y,z)") 18950b57cec5SDimitry Andric /// or creation of a value-initialized type ("int()"). 18960b57cec5SDimitry Andric /// See [C++ 5.2.3]. 18970b57cec5SDimitry Andric /// 18980b57cec5SDimitry Andric /// postfix-expression: [C++ 5.2p1] 18990b57cec5SDimitry Andric /// simple-type-specifier '(' expression-list[opt] ')' 19000b57cec5SDimitry Andric /// [C++0x] simple-type-specifier braced-init-list 19010b57cec5SDimitry Andric /// typename-specifier '(' expression-list[opt] ')' 19020b57cec5SDimitry Andric /// [C++0x] typename-specifier braced-init-list 19030b57cec5SDimitry Andric /// 19040b57cec5SDimitry Andric /// In C++1z onwards, the type specifier can also be a template-name. 19050b57cec5SDimitry Andric ExprResult 19060b57cec5SDimitry Andric Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { 190781ad6265SDimitry Andric Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 190881ad6265SDimitry Andric DeclaratorContext::FunctionalCast); 19090b57cec5SDimitry Andric ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric assert((Tok.is(tok::l_paren) || 19120b57cec5SDimitry Andric (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) 19130b57cec5SDimitry Andric && "Expected '(' or '{'!"); 19140b57cec5SDimitry Andric 19150b57cec5SDimitry Andric if (Tok.is(tok::l_brace)) { 19165ffd83dbSDimitry Andric PreferredType.enterTypeCast(Tok.getLocation(), TypeRep.get()); 19170b57cec5SDimitry Andric ExprResult Init = ParseBraceInitializer(); 19180b57cec5SDimitry Andric if (Init.isInvalid()) 19190b57cec5SDimitry Andric return Init; 19200b57cec5SDimitry Andric Expr *InitList = Init.get(); 19210b57cec5SDimitry Andric return Actions.ActOnCXXTypeConstructExpr( 19220b57cec5SDimitry Andric TypeRep, InitList->getBeginLoc(), MultiExprArg(&InitList, 1), 19230b57cec5SDimitry Andric InitList->getEndLoc(), /*ListInitialization=*/true); 19240b57cec5SDimitry Andric } else { 19250b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 19260b57cec5SDimitry Andric T.consumeOpen(); 19270b57cec5SDimitry Andric 19280b57cec5SDimitry Andric PreferredType.enterTypeCast(Tok.getLocation(), TypeRep.get()); 19290b57cec5SDimitry Andric 19300b57cec5SDimitry Andric ExprVector Exprs; 19310b57cec5SDimitry Andric 19320b57cec5SDimitry Andric auto RunSignatureHelp = [&]() { 1933480093f4SDimitry Andric QualType PreferredType; 1934480093f4SDimitry Andric if (TypeRep) 1935480093f4SDimitry Andric PreferredType = Actions.ProduceConstructorSignatureHelp( 193604eeddc0SDimitry Andric TypeRep.get()->getCanonicalTypeInternal(), DS.getEndLoc(), Exprs, 193704eeddc0SDimitry Andric T.getOpenLocation(), /*Braced=*/false); 19380b57cec5SDimitry Andric CalledSignatureHelp = true; 19390b57cec5SDimitry Andric return PreferredType; 19400b57cec5SDimitry Andric }; 19410b57cec5SDimitry Andric 19420b57cec5SDimitry Andric if (Tok.isNot(tok::r_paren)) { 1943*bdd1243dSDimitry Andric if (ParseExpressionList(Exprs, [&] { 19440b57cec5SDimitry Andric PreferredType.enterFunctionArgument(Tok.getLocation(), 19450b57cec5SDimitry Andric RunSignatureHelp); 19460b57cec5SDimitry Andric })) { 19470b57cec5SDimitry Andric if (PP.isCodeCompletionReached() && !CalledSignatureHelp) 19480b57cec5SDimitry Andric RunSignatureHelp(); 19490b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 19500b57cec5SDimitry Andric return ExprError(); 19510b57cec5SDimitry Andric } 19520b57cec5SDimitry Andric } 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric // Match the ')'. 19550b57cec5SDimitry Andric T.consumeClose(); 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric // TypeRep could be null, if it references an invalid typedef. 19580b57cec5SDimitry Andric if (!TypeRep) 19590b57cec5SDimitry Andric return ExprError(); 19600b57cec5SDimitry Andric 19610b57cec5SDimitry Andric return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(), 19620b57cec5SDimitry Andric Exprs, T.getCloseLocation(), 19630b57cec5SDimitry Andric /*ListInitialization=*/false); 19640b57cec5SDimitry Andric } 19650b57cec5SDimitry Andric } 19660b57cec5SDimitry Andric 1967349cc55cSDimitry Andric Parser::DeclGroupPtrTy 1968349cc55cSDimitry Andric Parser::ParseAliasDeclarationInInitStatement(DeclaratorContext Context, 196981ad6265SDimitry Andric ParsedAttributes &Attrs) { 1970349cc55cSDimitry Andric assert(Tok.is(tok::kw_using) && "Expected using"); 1971349cc55cSDimitry Andric assert((Context == DeclaratorContext::ForInit || 1972349cc55cSDimitry Andric Context == DeclaratorContext::SelectionInit) && 1973349cc55cSDimitry Andric "Unexpected Declarator Context"); 1974349cc55cSDimitry Andric DeclGroupPtrTy DG; 1975349cc55cSDimitry Andric SourceLocation DeclStart = ConsumeToken(), DeclEnd; 1976349cc55cSDimitry Andric 1977349cc55cSDimitry Andric DG = ParseUsingDeclaration(Context, {}, DeclStart, DeclEnd, Attrs, AS_none); 1978349cc55cSDimitry Andric if (!DG) 1979349cc55cSDimitry Andric return DG; 1980349cc55cSDimitry Andric 1981349cc55cSDimitry Andric Diag(DeclStart, !getLangOpts().CPlusPlus2b 1982349cc55cSDimitry Andric ? diag::ext_alias_in_init_statement 1983349cc55cSDimitry Andric : diag::warn_cxx20_alias_in_init_statement) 1984349cc55cSDimitry Andric << SourceRange(DeclStart, DeclEnd); 1985349cc55cSDimitry Andric 1986349cc55cSDimitry Andric return DG; 1987349cc55cSDimitry Andric } 1988349cc55cSDimitry Andric 19890b57cec5SDimitry Andric /// ParseCXXCondition - if/switch/while condition expression. 19900b57cec5SDimitry Andric /// 19910b57cec5SDimitry Andric /// condition: 19920b57cec5SDimitry Andric /// expression 19930b57cec5SDimitry Andric /// type-specifier-seq declarator '=' assignment-expression 19940b57cec5SDimitry Andric /// [C++11] type-specifier-seq declarator '=' initializer-clause 19950b57cec5SDimitry Andric /// [C++11] type-specifier-seq declarator braced-init-list 19960b57cec5SDimitry Andric /// [Clang] type-specifier-seq ref-qualifier[opt] '[' identifier-list ']' 19970b57cec5SDimitry Andric /// brace-or-equal-initializer 19980b57cec5SDimitry Andric /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] 19990b57cec5SDimitry Andric /// '=' assignment-expression 20000b57cec5SDimitry Andric /// 20010b57cec5SDimitry Andric /// In C++1z, a condition may in some contexts be preceded by an 20020b57cec5SDimitry Andric /// optional init-statement. This function will parse that too. 20030b57cec5SDimitry Andric /// 20040b57cec5SDimitry Andric /// \param InitStmt If non-null, an init-statement is permitted, and if present 20050b57cec5SDimitry Andric /// will be parsed and stored here. 20060b57cec5SDimitry Andric /// 20070b57cec5SDimitry Andric /// \param Loc The location of the start of the statement that requires this 20080b57cec5SDimitry Andric /// condition, e.g., the "for" in a for loop. 20090b57cec5SDimitry Andric /// 201004eeddc0SDimitry Andric /// \param MissingOK Whether an empty condition is acceptable here. Otherwise 201104eeddc0SDimitry Andric /// it is considered an error to be recovered from. 201204eeddc0SDimitry Andric /// 20130b57cec5SDimitry Andric /// \param FRI If non-null, a for range declaration is permitted, and if 20140b57cec5SDimitry Andric /// present will be parsed and stored here, and a null result will be returned. 20150b57cec5SDimitry Andric /// 2016fe6060f1SDimitry Andric /// \param EnterForConditionScope If true, enter a continue/break scope at the 2017fe6060f1SDimitry Andric /// appropriate moment for a 'for' loop. 2018fe6060f1SDimitry Andric /// 20190b57cec5SDimitry Andric /// \returns The parsed condition. 202004eeddc0SDimitry Andric Sema::ConditionResult 202104eeddc0SDimitry Andric Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc, 202204eeddc0SDimitry Andric Sema::ConditionKind CK, bool MissingOK, 202304eeddc0SDimitry Andric ForRangeInfo *FRI, bool EnterForConditionScope) { 2024fe6060f1SDimitry Andric // Helper to ensure we always enter a continue/break scope if requested. 2025fe6060f1SDimitry Andric struct ForConditionScopeRAII { 2026fe6060f1SDimitry Andric Scope *S; 2027fe6060f1SDimitry Andric void enter(bool IsConditionVariable) { 2028fe6060f1SDimitry Andric if (S) { 2029fe6060f1SDimitry Andric S->AddFlags(Scope::BreakScope | Scope::ContinueScope); 2030fe6060f1SDimitry Andric S->setIsConditionVarScope(IsConditionVariable); 2031fe6060f1SDimitry Andric } 2032fe6060f1SDimitry Andric } 2033fe6060f1SDimitry Andric ~ForConditionScopeRAII() { 2034fe6060f1SDimitry Andric if (S) 2035fe6060f1SDimitry Andric S->setIsConditionVarScope(false); 2036fe6060f1SDimitry Andric } 2037fe6060f1SDimitry Andric } ForConditionScope{EnterForConditionScope ? getCurScope() : nullptr}; 2038fe6060f1SDimitry Andric 20390b57cec5SDimitry Andric ParenBraceBracketBalancer BalancerRAIIObj(*this); 20400b57cec5SDimitry Andric PreferredType.enterCondition(Actions, Tok.getLocation()); 20410b57cec5SDimitry Andric 20420b57cec5SDimitry Andric if (Tok.is(tok::code_completion)) { 20430b57cec5SDimitry Andric cutOffParsing(); 2044fe6060f1SDimitry Andric Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition); 20450b57cec5SDimitry Andric return Sema::ConditionError(); 20460b57cec5SDimitry Andric } 20470b57cec5SDimitry Andric 204881ad6265SDimitry Andric ParsedAttributes attrs(AttrFactory); 20490b57cec5SDimitry Andric MaybeParseCXX11Attributes(attrs); 20500b57cec5SDimitry Andric 20510b57cec5SDimitry Andric const auto WarnOnInit = [this, &CK] { 20520b57cec5SDimitry Andric Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 20530b57cec5SDimitry Andric ? diag::warn_cxx14_compat_init_statement 20540b57cec5SDimitry Andric : diag::ext_init_statement) 20550b57cec5SDimitry Andric << (CK == Sema::ConditionKind::Switch); 20560b57cec5SDimitry Andric }; 20570b57cec5SDimitry Andric 20580b57cec5SDimitry Andric // Determine what kind of thing we have. 20590b57cec5SDimitry Andric switch (isCXXConditionDeclarationOrInitStatement(InitStmt, FRI)) { 20600b57cec5SDimitry Andric case ConditionOrInitStatement::Expression: { 2061fe6060f1SDimitry Andric // If this is a for loop, we're entering its condition. 2062fe6060f1SDimitry Andric ForConditionScope.enter(/*IsConditionVariable=*/false); 2063fe6060f1SDimitry Andric 20640b57cec5SDimitry Andric ProhibitAttributes(attrs); 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric // We can have an empty expression here. 20670b57cec5SDimitry Andric // if (; true); 20680b57cec5SDimitry Andric if (InitStmt && Tok.is(tok::semi)) { 20690b57cec5SDimitry Andric WarnOnInit(); 20700b57cec5SDimitry Andric SourceLocation SemiLoc = Tok.getLocation(); 20710b57cec5SDimitry Andric if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID()) { 20720b57cec5SDimitry Andric Diag(SemiLoc, diag::warn_empty_init_statement) 20730b57cec5SDimitry Andric << (CK == Sema::ConditionKind::Switch) 20740b57cec5SDimitry Andric << FixItHint::CreateRemoval(SemiLoc); 20750b57cec5SDimitry Andric } 20760b57cec5SDimitry Andric ConsumeToken(); 20770b57cec5SDimitry Andric *InitStmt = Actions.ActOnNullStmt(SemiLoc); 207804eeddc0SDimitry Andric return ParseCXXCondition(nullptr, Loc, CK, MissingOK); 20790b57cec5SDimitry Andric } 20800b57cec5SDimitry Andric 20810b57cec5SDimitry Andric // Parse the expression. 20820b57cec5SDimitry Andric ExprResult Expr = ParseExpression(); // expression 20830b57cec5SDimitry Andric if (Expr.isInvalid()) 20840b57cec5SDimitry Andric return Sema::ConditionError(); 20850b57cec5SDimitry Andric 20860b57cec5SDimitry Andric if (InitStmt && Tok.is(tok::semi)) { 20870b57cec5SDimitry Andric WarnOnInit(); 20880b57cec5SDimitry Andric *InitStmt = Actions.ActOnExprStmt(Expr.get()); 20890b57cec5SDimitry Andric ConsumeToken(); 209004eeddc0SDimitry Andric return ParseCXXCondition(nullptr, Loc, CK, MissingOK); 20910b57cec5SDimitry Andric } 20920b57cec5SDimitry Andric 209304eeddc0SDimitry Andric return Actions.ActOnCondition(getCurScope(), Loc, Expr.get(), CK, 209404eeddc0SDimitry Andric MissingOK); 20950b57cec5SDimitry Andric } 20960b57cec5SDimitry Andric 20970b57cec5SDimitry Andric case ConditionOrInitStatement::InitStmtDecl: { 20980b57cec5SDimitry Andric WarnOnInit(); 2099349cc55cSDimitry Andric DeclGroupPtrTy DG; 21000b57cec5SDimitry Andric SourceLocation DeclStart = Tok.getLocation(), DeclEnd; 2101349cc55cSDimitry Andric if (Tok.is(tok::kw_using)) 2102349cc55cSDimitry Andric DG = ParseAliasDeclarationInInitStatement( 2103349cc55cSDimitry Andric DeclaratorContext::SelectionInit, attrs); 210481ad6265SDimitry Andric else { 210581ad6265SDimitry Andric ParsedAttributes DeclSpecAttrs(AttrFactory); 2106349cc55cSDimitry Andric DG = ParseSimpleDeclaration(DeclaratorContext::SelectionInit, DeclEnd, 210781ad6265SDimitry Andric attrs, DeclSpecAttrs, /*RequireSemi=*/true); 210881ad6265SDimitry Andric } 21090b57cec5SDimitry Andric *InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, DeclEnd); 211004eeddc0SDimitry Andric return ParseCXXCondition(nullptr, Loc, CK, MissingOK); 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric 21130b57cec5SDimitry Andric case ConditionOrInitStatement::ForRangeDecl: { 2114fe6060f1SDimitry Andric // This is 'for (init-stmt; for-range-decl : range-expr)'. 2115fe6060f1SDimitry Andric // We're not actually in a for loop yet, so 'break' and 'continue' aren't 2116fe6060f1SDimitry Andric // permitted here. 21170b57cec5SDimitry Andric assert(FRI && "should not parse a for range declaration here"); 21180b57cec5SDimitry Andric SourceLocation DeclStart = Tok.getLocation(), DeclEnd; 211981ad6265SDimitry Andric ParsedAttributes DeclSpecAttrs(AttrFactory); 212081ad6265SDimitry Andric DeclGroupPtrTy DG = ParseSimpleDeclaration( 212181ad6265SDimitry Andric DeclaratorContext::ForInit, DeclEnd, attrs, DeclSpecAttrs, false, FRI); 21220b57cec5SDimitry Andric FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); 2123fe6060f1SDimitry Andric assert((FRI->ColonLoc.isValid() || !DG) && 2124fe6060f1SDimitry Andric "cannot find for range declaration"); 21250b57cec5SDimitry Andric return Sema::ConditionResult(); 21260b57cec5SDimitry Andric } 21270b57cec5SDimitry Andric 21280b57cec5SDimitry Andric case ConditionOrInitStatement::ConditionDecl: 21290b57cec5SDimitry Andric case ConditionOrInitStatement::Error: 21300b57cec5SDimitry Andric break; 21310b57cec5SDimitry Andric } 21320b57cec5SDimitry Andric 2133fe6060f1SDimitry Andric // If this is a for loop, we're entering its condition. 2134fe6060f1SDimitry Andric ForConditionScope.enter(/*IsConditionVariable=*/true); 2135fe6060f1SDimitry Andric 21360b57cec5SDimitry Andric // type-specifier-seq 21370b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 21380b57cec5SDimitry Andric ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_condition); 21390b57cec5SDimitry Andric 21400b57cec5SDimitry Andric // declarator 214181ad6265SDimitry Andric Declarator DeclaratorInfo(DS, attrs, DeclaratorContext::Condition); 21420b57cec5SDimitry Andric ParseDeclarator(DeclaratorInfo); 21430b57cec5SDimitry Andric 21440b57cec5SDimitry Andric // simple-asm-expr[opt] 21450b57cec5SDimitry Andric if (Tok.is(tok::kw_asm)) { 21460b57cec5SDimitry Andric SourceLocation Loc; 2147480093f4SDimitry Andric ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc)); 21480b57cec5SDimitry Andric if (AsmLabel.isInvalid()) { 21490b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi); 21500b57cec5SDimitry Andric return Sema::ConditionError(); 21510b57cec5SDimitry Andric } 21520b57cec5SDimitry Andric DeclaratorInfo.setAsmLabel(AsmLabel.get()); 21530b57cec5SDimitry Andric DeclaratorInfo.SetRangeEnd(Loc); 21540b57cec5SDimitry Andric } 21550b57cec5SDimitry Andric 21560b57cec5SDimitry Andric // If attributes are present, parse them. 21570b57cec5SDimitry Andric MaybeParseGNUAttributes(DeclaratorInfo); 21580b57cec5SDimitry Andric 21590b57cec5SDimitry Andric // Type-check the declaration itself. 21600b57cec5SDimitry Andric DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(), 21610b57cec5SDimitry Andric DeclaratorInfo); 21620b57cec5SDimitry Andric if (Dcl.isInvalid()) 21630b57cec5SDimitry Andric return Sema::ConditionError(); 21640b57cec5SDimitry Andric Decl *DeclOut = Dcl.get(); 21650b57cec5SDimitry Andric 21660b57cec5SDimitry Andric // '=' assignment-expression 21670b57cec5SDimitry Andric // If a '==' or '+=' is found, suggest a fixit to '='. 21680b57cec5SDimitry Andric bool CopyInitialization = isTokenEqualOrEqualTypo(); 21690b57cec5SDimitry Andric if (CopyInitialization) 21700b57cec5SDimitry Andric ConsumeToken(); 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric ExprResult InitExpr = ExprError(); 21730b57cec5SDimitry Andric if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 21740b57cec5SDimitry Andric Diag(Tok.getLocation(), 21750b57cec5SDimitry Andric diag::warn_cxx98_compat_generalized_initializer_lists); 21760b57cec5SDimitry Andric InitExpr = ParseBraceInitializer(); 21770b57cec5SDimitry Andric } else if (CopyInitialization) { 21780b57cec5SDimitry Andric PreferredType.enterVariableInit(Tok.getLocation(), DeclOut); 21790b57cec5SDimitry Andric InitExpr = ParseAssignmentExpression(); 21800b57cec5SDimitry Andric } else if (Tok.is(tok::l_paren)) { 21810b57cec5SDimitry Andric // This was probably an attempt to initialize the variable. 21820b57cec5SDimitry Andric SourceLocation LParen = ConsumeParen(), RParen = LParen; 21830b57cec5SDimitry Andric if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) 21840b57cec5SDimitry Andric RParen = ConsumeParen(); 21850b57cec5SDimitry Andric Diag(DeclOut->getLocation(), 21860b57cec5SDimitry Andric diag::err_expected_init_in_condition_lparen) 21870b57cec5SDimitry Andric << SourceRange(LParen, RParen); 21880b57cec5SDimitry Andric } else { 21890b57cec5SDimitry Andric Diag(DeclOut->getLocation(), diag::err_expected_init_in_condition); 21900b57cec5SDimitry Andric } 21910b57cec5SDimitry Andric 21920b57cec5SDimitry Andric if (!InitExpr.isInvalid()) 21930b57cec5SDimitry Andric Actions.AddInitializerToDecl(DeclOut, InitExpr.get(), !CopyInitialization); 21940b57cec5SDimitry Andric else 21950b57cec5SDimitry Andric Actions.ActOnInitializerError(DeclOut); 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric Actions.FinalizeDeclaration(DeclOut); 21980b57cec5SDimitry Andric return Actions.ActOnConditionVariable(DeclOut, Loc, CK); 21990b57cec5SDimitry Andric } 22000b57cec5SDimitry Andric 22010b57cec5SDimitry Andric /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. 22020b57cec5SDimitry Andric /// This should only be called when the current token is known to be part of 22030b57cec5SDimitry Andric /// simple-type-specifier. 22040b57cec5SDimitry Andric /// 22050b57cec5SDimitry Andric /// simple-type-specifier: 22060b57cec5SDimitry Andric /// '::'[opt] nested-name-specifier[opt] type-name 22070b57cec5SDimitry Andric /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] 22080b57cec5SDimitry Andric /// char 22090b57cec5SDimitry Andric /// wchar_t 22100b57cec5SDimitry Andric /// bool 22110b57cec5SDimitry Andric /// short 22120b57cec5SDimitry Andric /// int 22130b57cec5SDimitry Andric /// long 22140b57cec5SDimitry Andric /// signed 22150b57cec5SDimitry Andric /// unsigned 22160b57cec5SDimitry Andric /// float 22170b57cec5SDimitry Andric /// double 22180b57cec5SDimitry Andric /// void 22190b57cec5SDimitry Andric /// [GNU] typeof-specifier 22200b57cec5SDimitry Andric /// [C++0x] auto [TODO] 22210b57cec5SDimitry Andric /// 22220b57cec5SDimitry Andric /// type-name: 22230b57cec5SDimitry Andric /// class-name 22240b57cec5SDimitry Andric /// enum-name 22250b57cec5SDimitry Andric /// typedef-name 22260b57cec5SDimitry Andric /// 22270b57cec5SDimitry Andric void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { 22280b57cec5SDimitry Andric DS.SetRangeStart(Tok.getLocation()); 22290b57cec5SDimitry Andric const char *PrevSpec; 22300b57cec5SDimitry Andric unsigned DiagID; 22310b57cec5SDimitry Andric SourceLocation Loc = Tok.getLocation(); 22320b57cec5SDimitry Andric const clang::PrintingPolicy &Policy = 22330b57cec5SDimitry Andric Actions.getASTContext().getPrintingPolicy(); 22340b57cec5SDimitry Andric 22350b57cec5SDimitry Andric switch (Tok.getKind()) { 22360b57cec5SDimitry Andric case tok::identifier: // foo::bar 22370b57cec5SDimitry Andric case tok::coloncolon: // ::foo::bar 22380b57cec5SDimitry Andric llvm_unreachable("Annotation token should already be formed!"); 22390b57cec5SDimitry Andric default: 22400b57cec5SDimitry Andric llvm_unreachable("Not a simple-type-specifier token!"); 22410b57cec5SDimitry Andric 22420b57cec5SDimitry Andric // type-name 22430b57cec5SDimitry Andric case tok::annot_typename: { 22440b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, 22450b57cec5SDimitry Andric getTypeAnnotation(Tok), Policy); 22460b57cec5SDimitry Andric DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 22470b57cec5SDimitry Andric ConsumeAnnotationToken(); 22480b57cec5SDimitry Andric 22490b57cec5SDimitry Andric DS.Finish(Actions, Policy); 22500b57cec5SDimitry Andric return; 22510b57cec5SDimitry Andric } 22520b57cec5SDimitry Andric 22530eae32dcSDimitry Andric case tok::kw__ExtInt: 22540eae32dcSDimitry Andric case tok::kw__BitInt: { 22550eae32dcSDimitry Andric DiagnoseBitIntUse(Tok); 22565ffd83dbSDimitry Andric ExprResult ER = ParseExtIntegerArgument(); 22575ffd83dbSDimitry Andric if (ER.isInvalid()) 22585ffd83dbSDimitry Andric DS.SetTypeSpecError(); 22595ffd83dbSDimitry Andric else 22600eae32dcSDimitry Andric DS.SetBitIntType(Loc, ER.get(), PrevSpec, DiagID, Policy); 22615ffd83dbSDimitry Andric 22625ffd83dbSDimitry Andric // Do this here because we have already consumed the close paren. 22635ffd83dbSDimitry Andric DS.SetRangeEnd(PrevTokLocation); 22645ffd83dbSDimitry Andric DS.Finish(Actions, Policy); 22655ffd83dbSDimitry Andric return; 22665ffd83dbSDimitry Andric } 22675ffd83dbSDimitry Andric 22680b57cec5SDimitry Andric // builtin types 22690b57cec5SDimitry Andric case tok::kw_short: 2270e8d8bef9SDimitry Andric DS.SetTypeSpecWidth(TypeSpecifierWidth::Short, Loc, PrevSpec, DiagID, 2271e8d8bef9SDimitry Andric Policy); 22720b57cec5SDimitry Andric break; 22730b57cec5SDimitry Andric case tok::kw_long: 2274e8d8bef9SDimitry Andric DS.SetTypeSpecWidth(TypeSpecifierWidth::Long, Loc, PrevSpec, DiagID, 2275e8d8bef9SDimitry Andric Policy); 22760b57cec5SDimitry Andric break; 22770b57cec5SDimitry Andric case tok::kw___int64: 2278e8d8bef9SDimitry Andric DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc, PrevSpec, DiagID, 2279e8d8bef9SDimitry Andric Policy); 22800b57cec5SDimitry Andric break; 22810b57cec5SDimitry Andric case tok::kw_signed: 2282e8d8bef9SDimitry Andric DS.SetTypeSpecSign(TypeSpecifierSign::Signed, Loc, PrevSpec, DiagID); 22830b57cec5SDimitry Andric break; 22840b57cec5SDimitry Andric case tok::kw_unsigned: 2285e8d8bef9SDimitry Andric DS.SetTypeSpecSign(TypeSpecifierSign::Unsigned, Loc, PrevSpec, DiagID); 22860b57cec5SDimitry Andric break; 22870b57cec5SDimitry Andric case tok::kw_void: 22880b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy); 22890b57cec5SDimitry Andric break; 229081ad6265SDimitry Andric case tok::kw_auto: 229181ad6265SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID, Policy); 229281ad6265SDimitry Andric break; 22930b57cec5SDimitry Andric case tok::kw_char: 22940b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy); 22950b57cec5SDimitry Andric break; 22960b57cec5SDimitry Andric case tok::kw_int: 22970b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy); 22980b57cec5SDimitry Andric break; 22990b57cec5SDimitry Andric case tok::kw___int128: 23000b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy); 23010b57cec5SDimitry Andric break; 23025ffd83dbSDimitry Andric case tok::kw___bf16: 23035ffd83dbSDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_BFloat16, Loc, PrevSpec, DiagID, Policy); 23045ffd83dbSDimitry Andric break; 23050b57cec5SDimitry Andric case tok::kw_half: 23060b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy); 23070b57cec5SDimitry Andric break; 23080b57cec5SDimitry Andric case tok::kw_float: 23090b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy); 23100b57cec5SDimitry Andric break; 23110b57cec5SDimitry Andric case tok::kw_double: 23120b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy); 23130b57cec5SDimitry Andric break; 23140b57cec5SDimitry Andric case tok::kw__Float16: 23150b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, DiagID, Policy); 23160b57cec5SDimitry Andric break; 23170b57cec5SDimitry Andric case tok::kw___float128: 23180b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, DiagID, Policy); 23190b57cec5SDimitry Andric break; 2320349cc55cSDimitry Andric case tok::kw___ibm128: 2321349cc55cSDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_ibm128, Loc, PrevSpec, DiagID, Policy); 2322349cc55cSDimitry Andric break; 23230b57cec5SDimitry Andric case tok::kw_wchar_t: 23240b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy); 23250b57cec5SDimitry Andric break; 23260b57cec5SDimitry Andric case tok::kw_char8_t: 23270b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec, DiagID, Policy); 23280b57cec5SDimitry Andric break; 23290b57cec5SDimitry Andric case tok::kw_char16_t: 23300b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy); 23310b57cec5SDimitry Andric break; 23320b57cec5SDimitry Andric case tok::kw_char32_t: 23330b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy); 23340b57cec5SDimitry Andric break; 23350b57cec5SDimitry Andric case tok::kw_bool: 23360b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy); 23370b57cec5SDimitry Andric break; 23380b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) \ 23390b57cec5SDimitry Andric case tok::kw_##ImgType##_t: \ 23400b57cec5SDimitry Andric DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, DiagID, \ 23410b57cec5SDimitry Andric Policy); \ 23420b57cec5SDimitry Andric break; 23430b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def" 23440b57cec5SDimitry Andric 23450b57cec5SDimitry Andric case tok::annot_decltype: 23460b57cec5SDimitry Andric case tok::kw_decltype: 23470b57cec5SDimitry Andric DS.SetRangeEnd(ParseDecltypeSpecifier(DS)); 23480b57cec5SDimitry Andric return DS.Finish(Actions, Policy); 23490b57cec5SDimitry Andric 23500b57cec5SDimitry Andric // GNU typeof support. 23510b57cec5SDimitry Andric case tok::kw_typeof: 23520b57cec5SDimitry Andric ParseTypeofSpecifier(DS); 23530b57cec5SDimitry Andric DS.Finish(Actions, Policy); 23540b57cec5SDimitry Andric return; 23550b57cec5SDimitry Andric } 23560b57cec5SDimitry Andric ConsumeAnyToken(); 23570b57cec5SDimitry Andric DS.SetRangeEnd(PrevTokLocation); 23580b57cec5SDimitry Andric DS.Finish(Actions, Policy); 23590b57cec5SDimitry Andric } 23600b57cec5SDimitry Andric 23610b57cec5SDimitry Andric /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ 23620b57cec5SDimitry Andric /// [dcl.name]), which is a non-empty sequence of type-specifiers, 23630b57cec5SDimitry Andric /// e.g., "const short int". Note that the DeclSpec is *not* finished 23640b57cec5SDimitry Andric /// by parsing the type-specifier-seq, because these sequences are 23650b57cec5SDimitry Andric /// typically followed by some form of declarator. Returns true and 23660b57cec5SDimitry Andric /// emits diagnostics if this is not a type-specifier-seq, false 23670b57cec5SDimitry Andric /// otherwise. 23680b57cec5SDimitry Andric /// 23690b57cec5SDimitry Andric /// type-specifier-seq: [C++ 8.1] 23700b57cec5SDimitry Andric /// type-specifier type-specifier-seq[opt] 23710b57cec5SDimitry Andric /// 2372*bdd1243dSDimitry Andric bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS, DeclaratorContext Context) { 2373*bdd1243dSDimitry Andric ParseSpecifierQualifierList(DS, AS_none, 2374*bdd1243dSDimitry Andric getDeclSpecContextFromDeclaratorContext(Context)); 23750b57cec5SDimitry Andric DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); 23760b57cec5SDimitry Andric return false; 23770b57cec5SDimitry Andric } 23780b57cec5SDimitry Andric 23790b57cec5SDimitry Andric /// Finish parsing a C++ unqualified-id that is a template-id of 23800b57cec5SDimitry Andric /// some form. 23810b57cec5SDimitry Andric /// 23820b57cec5SDimitry Andric /// This routine is invoked when a '<' is encountered after an identifier or 23830b57cec5SDimitry Andric /// operator-function-id is parsed by \c ParseUnqualifiedId() to determine 23840b57cec5SDimitry Andric /// whether the unqualified-id is actually a template-id. This routine will 23850b57cec5SDimitry Andric /// then parse the template arguments and form the appropriate template-id to 23860b57cec5SDimitry Andric /// return to the caller. 23870b57cec5SDimitry Andric /// 23880b57cec5SDimitry Andric /// \param SS the nested-name-specifier that precedes this template-id, if 23890b57cec5SDimitry Andric /// we're actually parsing a qualified-id. 23900b57cec5SDimitry Andric /// 23915ffd83dbSDimitry Andric /// \param ObjectType if this unqualified-id occurs within a member access 23925ffd83dbSDimitry Andric /// expression, the type of the base object whose member is being accessed. 23935ffd83dbSDimitry Andric /// 23945ffd83dbSDimitry Andric /// \param ObjectHadErrors this unqualified-id occurs within a member access 23955ffd83dbSDimitry Andric /// expression, indicates whether the original subexpressions had any errors. 23965ffd83dbSDimitry Andric /// 23970b57cec5SDimitry Andric /// \param Name for constructor and destructor names, this is the actual 23980b57cec5SDimitry Andric /// identifier that may be a template-name. 23990b57cec5SDimitry Andric /// 24000b57cec5SDimitry Andric /// \param NameLoc the location of the class-name in a constructor or 24010b57cec5SDimitry Andric /// destructor. 24020b57cec5SDimitry Andric /// 24030b57cec5SDimitry Andric /// \param EnteringContext whether we're entering the scope of the 24040b57cec5SDimitry Andric /// nested-name-specifier. 24050b57cec5SDimitry Andric /// 24060b57cec5SDimitry Andric /// \param Id as input, describes the template-name or operator-function-id 24070b57cec5SDimitry Andric /// that precedes the '<'. If template arguments were parsed successfully, 24080b57cec5SDimitry Andric /// will be updated with the template-id. 24090b57cec5SDimitry Andric /// 24100b57cec5SDimitry Andric /// \param AssumeTemplateId When true, this routine will assume that the name 24110b57cec5SDimitry Andric /// refers to a template without performing name lookup to verify. 24120b57cec5SDimitry Andric /// 24130b57cec5SDimitry Andric /// \returns true if a parse error occurred, false otherwise. 24145ffd83dbSDimitry Andric bool Parser::ParseUnqualifiedIdTemplateId( 24155ffd83dbSDimitry Andric CXXScopeSpec &SS, ParsedType ObjectType, bool ObjectHadErrors, 24165ffd83dbSDimitry Andric SourceLocation TemplateKWLoc, IdentifierInfo *Name, SourceLocation NameLoc, 24175ffd83dbSDimitry Andric bool EnteringContext, UnqualifiedId &Id, bool AssumeTemplateId) { 24180b57cec5SDimitry Andric assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id"); 24190b57cec5SDimitry Andric 24200b57cec5SDimitry Andric TemplateTy Template; 24210b57cec5SDimitry Andric TemplateNameKind TNK = TNK_Non_template; 24220b57cec5SDimitry Andric switch (Id.getKind()) { 24230b57cec5SDimitry Andric case UnqualifiedIdKind::IK_Identifier: 24240b57cec5SDimitry Andric case UnqualifiedIdKind::IK_OperatorFunctionId: 24250b57cec5SDimitry Andric case UnqualifiedIdKind::IK_LiteralOperatorId: 24260b57cec5SDimitry Andric if (AssumeTemplateId) { 24270b57cec5SDimitry Andric // We defer the injected-class-name checks until we've found whether 24280b57cec5SDimitry Andric // this template-id is used to form a nested-name-specifier or not. 24295ffd83dbSDimitry Andric TNK = Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Id, 24305ffd83dbSDimitry Andric ObjectType, EnteringContext, Template, 24315ffd83dbSDimitry Andric /*AllowInjectedClassName*/ true); 24320b57cec5SDimitry Andric } else { 24330b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 24340b57cec5SDimitry Andric TNK = Actions.isTemplateName(getCurScope(), SS, 24350b57cec5SDimitry Andric TemplateKWLoc.isValid(), Id, 24360b57cec5SDimitry Andric ObjectType, EnteringContext, Template, 24370b57cec5SDimitry Andric MemberOfUnknownSpecialization); 24380b57cec5SDimitry Andric // If lookup found nothing but we're assuming that this is a template 24390b57cec5SDimitry Andric // name, double-check that makes sense syntactically before committing 24400b57cec5SDimitry Andric // to it. 24410b57cec5SDimitry Andric if (TNK == TNK_Undeclared_template && 24420b57cec5SDimitry Andric isTemplateArgumentList(0) == TPResult::False) 24430b57cec5SDimitry Andric return false; 24440b57cec5SDimitry Andric 24450b57cec5SDimitry Andric if (TNK == TNK_Non_template && MemberOfUnknownSpecialization && 24460b57cec5SDimitry Andric ObjectType && isTemplateArgumentList(0) == TPResult::True) { 24475ffd83dbSDimitry Andric // If we had errors before, ObjectType can be dependent even without any 24485ffd83dbSDimitry Andric // templates, do not report missing template keyword in that case. 24495ffd83dbSDimitry Andric if (!ObjectHadErrors) { 24500b57cec5SDimitry Andric // We have something like t->getAs<T>(), where getAs is a 24510b57cec5SDimitry Andric // member of an unknown specialization. However, this will only 24520b57cec5SDimitry Andric // parse correctly as a template, so suggest the keyword 'template' 24530b57cec5SDimitry Andric // before 'getAs' and treat this as a dependent template name. 24540b57cec5SDimitry Andric std::string Name; 24550b57cec5SDimitry Andric if (Id.getKind() == UnqualifiedIdKind::IK_Identifier) 24565ffd83dbSDimitry Andric Name = std::string(Id.Identifier->getName()); 24570b57cec5SDimitry Andric else { 24580b57cec5SDimitry Andric Name = "operator "; 24590b57cec5SDimitry Andric if (Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId) 24600b57cec5SDimitry Andric Name += getOperatorSpelling(Id.OperatorFunctionId.Operator); 24610b57cec5SDimitry Andric else 24620b57cec5SDimitry Andric Name += Id.Identifier->getName(); 24630b57cec5SDimitry Andric } 24640b57cec5SDimitry Andric Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword) 24650b57cec5SDimitry Andric << Name 24660b57cec5SDimitry Andric << FixItHint::CreateInsertion(Id.StartLocation, "template "); 24675ffd83dbSDimitry Andric } 24685ffd83dbSDimitry Andric TNK = Actions.ActOnTemplateName( 24690b57cec5SDimitry Andric getCurScope(), SS, TemplateKWLoc, Id, ObjectType, EnteringContext, 24700b57cec5SDimitry Andric Template, /*AllowInjectedClassName*/ true); 24715ffd83dbSDimitry Andric } else if (TNK == TNK_Non_template) { 24725ffd83dbSDimitry Andric return false; 24730b57cec5SDimitry Andric } 24740b57cec5SDimitry Andric } 24750b57cec5SDimitry Andric break; 24760b57cec5SDimitry Andric 24770b57cec5SDimitry Andric case UnqualifiedIdKind::IK_ConstructorName: { 24780b57cec5SDimitry Andric UnqualifiedId TemplateName; 24790b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 24800b57cec5SDimitry Andric TemplateName.setIdentifier(Name, NameLoc); 24810b57cec5SDimitry Andric TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), 24820b57cec5SDimitry Andric TemplateName, ObjectType, 24830b57cec5SDimitry Andric EnteringContext, Template, 24840b57cec5SDimitry Andric MemberOfUnknownSpecialization); 24855ffd83dbSDimitry Andric if (TNK == TNK_Non_template) 24865ffd83dbSDimitry Andric return false; 24870b57cec5SDimitry Andric break; 24880b57cec5SDimitry Andric } 24890b57cec5SDimitry Andric 24900b57cec5SDimitry Andric case UnqualifiedIdKind::IK_DestructorName: { 24910b57cec5SDimitry Andric UnqualifiedId TemplateName; 24920b57cec5SDimitry Andric bool MemberOfUnknownSpecialization; 24930b57cec5SDimitry Andric TemplateName.setIdentifier(Name, NameLoc); 24940b57cec5SDimitry Andric if (ObjectType) { 24955ffd83dbSDimitry Andric TNK = Actions.ActOnTemplateName( 24960b57cec5SDimitry Andric getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType, 24970b57cec5SDimitry Andric EnteringContext, Template, /*AllowInjectedClassName*/ true); 24980b57cec5SDimitry Andric } else { 24990b57cec5SDimitry Andric TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), 25000b57cec5SDimitry Andric TemplateName, ObjectType, 25010b57cec5SDimitry Andric EnteringContext, Template, 25020b57cec5SDimitry Andric MemberOfUnknownSpecialization); 25030b57cec5SDimitry Andric 25040b57cec5SDimitry Andric if (TNK == TNK_Non_template && !Id.DestructorName.get()) { 25050b57cec5SDimitry Andric Diag(NameLoc, diag::err_destructor_template_id) 25060b57cec5SDimitry Andric << Name << SS.getRange(); 25075ffd83dbSDimitry Andric // Carry on to parse the template arguments before bailing out. 25080b57cec5SDimitry Andric } 25090b57cec5SDimitry Andric } 25100b57cec5SDimitry Andric break; 25110b57cec5SDimitry Andric } 25120b57cec5SDimitry Andric 25130b57cec5SDimitry Andric default: 25140b57cec5SDimitry Andric return false; 25150b57cec5SDimitry Andric } 25160b57cec5SDimitry Andric 25170b57cec5SDimitry Andric // Parse the enclosed template argument list. 25180b57cec5SDimitry Andric SourceLocation LAngleLoc, RAngleLoc; 25190b57cec5SDimitry Andric TemplateArgList TemplateArgs; 252004eeddc0SDimitry Andric if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, RAngleLoc, 252104eeddc0SDimitry Andric Template)) 25220b57cec5SDimitry Andric return true; 25230b57cec5SDimitry Andric 25245ffd83dbSDimitry Andric // If this is a non-template, we already issued a diagnostic. 25255ffd83dbSDimitry Andric if (TNK == TNK_Non_template) 25265ffd83dbSDimitry Andric return true; 25275ffd83dbSDimitry Andric 25280b57cec5SDimitry Andric if (Id.getKind() == UnqualifiedIdKind::IK_Identifier || 25290b57cec5SDimitry Andric Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId || 25300b57cec5SDimitry Andric Id.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) { 25310b57cec5SDimitry Andric // Form a parsed representation of the template-id to be stored in the 25320b57cec5SDimitry Andric // UnqualifiedId. 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric // FIXME: Store name for literal operator too. 25350b57cec5SDimitry Andric IdentifierInfo *TemplateII = 25360b57cec5SDimitry Andric Id.getKind() == UnqualifiedIdKind::IK_Identifier ? Id.Identifier 25370b57cec5SDimitry Andric : nullptr; 25380b57cec5SDimitry Andric OverloadedOperatorKind OpKind = 25390b57cec5SDimitry Andric Id.getKind() == UnqualifiedIdKind::IK_Identifier 25400b57cec5SDimitry Andric ? OO_None 25410b57cec5SDimitry Andric : Id.OperatorFunctionId.Operator; 25420b57cec5SDimitry Andric 25430b57cec5SDimitry Andric TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( 254455e4f9d5SDimitry Andric TemplateKWLoc, Id.StartLocation, TemplateII, OpKind, Template, TNK, 25455ffd83dbSDimitry Andric LAngleLoc, RAngleLoc, TemplateArgs, /*ArgsInvalid*/false, TemplateIds); 25460b57cec5SDimitry Andric 25470b57cec5SDimitry Andric Id.setTemplateId(TemplateId); 25480b57cec5SDimitry Andric return false; 25490b57cec5SDimitry Andric } 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric // Bundle the template arguments together. 25520b57cec5SDimitry Andric ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); 25530b57cec5SDimitry Andric 25540b57cec5SDimitry Andric // Constructor and destructor names. 25550b57cec5SDimitry Andric TypeResult Type = Actions.ActOnTemplateIdType( 25560b57cec5SDimitry Andric getCurScope(), SS, TemplateKWLoc, Template, Name, NameLoc, LAngleLoc, 25570b57cec5SDimitry Andric TemplateArgsPtr, RAngleLoc, /*IsCtorOrDtorName=*/true); 25580b57cec5SDimitry Andric if (Type.isInvalid()) 25590b57cec5SDimitry Andric return true; 25600b57cec5SDimitry Andric 25610b57cec5SDimitry Andric if (Id.getKind() == UnqualifiedIdKind::IK_ConstructorName) 25620b57cec5SDimitry Andric Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); 25630b57cec5SDimitry Andric else 25640b57cec5SDimitry Andric Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); 25650b57cec5SDimitry Andric 25660b57cec5SDimitry Andric return false; 25670b57cec5SDimitry Andric } 25680b57cec5SDimitry Andric 25690b57cec5SDimitry Andric /// Parse an operator-function-id or conversion-function-id as part 25700b57cec5SDimitry Andric /// of a C++ unqualified-id. 25710b57cec5SDimitry Andric /// 25720b57cec5SDimitry Andric /// This routine is responsible only for parsing the operator-function-id or 25730b57cec5SDimitry Andric /// conversion-function-id; it does not handle template arguments in any way. 25740b57cec5SDimitry Andric /// 25750b57cec5SDimitry Andric /// \code 25760b57cec5SDimitry Andric /// operator-function-id: [C++ 13.5] 25770b57cec5SDimitry Andric /// 'operator' operator 25780b57cec5SDimitry Andric /// 25790b57cec5SDimitry Andric /// operator: one of 25800b57cec5SDimitry Andric /// new delete new[] delete[] 25810b57cec5SDimitry Andric /// + - * / % ^ & | ~ 25820b57cec5SDimitry Andric /// ! = < > += -= *= /= %= 25830b57cec5SDimitry Andric /// ^= &= |= << >> >>= <<= == != 25840b57cec5SDimitry Andric /// <= >= && || ++ -- , ->* -> 25850b57cec5SDimitry Andric /// () [] <=> 25860b57cec5SDimitry Andric /// 25870b57cec5SDimitry Andric /// conversion-function-id: [C++ 12.3.2] 25880b57cec5SDimitry Andric /// operator conversion-type-id 25890b57cec5SDimitry Andric /// 25900b57cec5SDimitry Andric /// conversion-type-id: 25910b57cec5SDimitry Andric /// type-specifier-seq conversion-declarator[opt] 25920b57cec5SDimitry Andric /// 25930b57cec5SDimitry Andric /// conversion-declarator: 25940b57cec5SDimitry Andric /// ptr-operator conversion-declarator[opt] 25950b57cec5SDimitry Andric /// \endcode 25960b57cec5SDimitry Andric /// 25970b57cec5SDimitry Andric /// \param SS The nested-name-specifier that preceded this unqualified-id. If 25980b57cec5SDimitry Andric /// non-empty, then we are parsing the unqualified-id of a qualified-id. 25990b57cec5SDimitry Andric /// 26000b57cec5SDimitry Andric /// \param EnteringContext whether we are entering the scope of the 26010b57cec5SDimitry Andric /// nested-name-specifier. 26020b57cec5SDimitry Andric /// 26030b57cec5SDimitry Andric /// \param ObjectType if this unqualified-id occurs within a member access 26040b57cec5SDimitry Andric /// expression, the type of the base object whose member is being accessed. 26050b57cec5SDimitry Andric /// 26060b57cec5SDimitry Andric /// \param Result on a successful parse, contains the parsed unqualified-id. 26070b57cec5SDimitry Andric /// 26080b57cec5SDimitry Andric /// \returns true if parsing fails, false otherwise. 26090b57cec5SDimitry Andric bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, 26100b57cec5SDimitry Andric ParsedType ObjectType, 26110b57cec5SDimitry Andric UnqualifiedId &Result) { 26120b57cec5SDimitry Andric assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); 26130b57cec5SDimitry Andric 26140b57cec5SDimitry Andric // Consume the 'operator' keyword. 26150b57cec5SDimitry Andric SourceLocation KeywordLoc = ConsumeToken(); 26160b57cec5SDimitry Andric 26170b57cec5SDimitry Andric // Determine what kind of operator name we have. 26180b57cec5SDimitry Andric unsigned SymbolIdx = 0; 26190b57cec5SDimitry Andric SourceLocation SymbolLocations[3]; 26200b57cec5SDimitry Andric OverloadedOperatorKind Op = OO_None; 26210b57cec5SDimitry Andric switch (Tok.getKind()) { 26220b57cec5SDimitry Andric case tok::kw_new: 26230b57cec5SDimitry Andric case tok::kw_delete: { 26240b57cec5SDimitry Andric bool isNew = Tok.getKind() == tok::kw_new; 26250b57cec5SDimitry Andric // Consume the 'new' or 'delete'. 26260b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = ConsumeToken(); 26270b57cec5SDimitry Andric // Check for array new/delete. 26280b57cec5SDimitry Andric if (Tok.is(tok::l_square) && 26290b57cec5SDimitry Andric (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) { 26300b57cec5SDimitry Andric // Consume the '[' and ']'. 26310b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_square); 26320b57cec5SDimitry Andric T.consumeOpen(); 26330b57cec5SDimitry Andric T.consumeClose(); 26340b57cec5SDimitry Andric if (T.getCloseLocation().isInvalid()) 26350b57cec5SDimitry Andric return true; 26360b57cec5SDimitry Andric 26370b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getOpenLocation(); 26380b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getCloseLocation(); 26390b57cec5SDimitry Andric Op = isNew? OO_Array_New : OO_Array_Delete; 26400b57cec5SDimitry Andric } else { 26410b57cec5SDimitry Andric Op = isNew? OO_New : OO_Delete; 26420b57cec5SDimitry Andric } 26430b57cec5SDimitry Andric break; 26440b57cec5SDimitry Andric } 26450b57cec5SDimitry Andric 26460b57cec5SDimitry Andric #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 26470b57cec5SDimitry Andric case tok::Token: \ 26480b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = ConsumeToken(); \ 26490b57cec5SDimitry Andric Op = OO_##Name; \ 26500b57cec5SDimitry Andric break; 26510b57cec5SDimitry Andric #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) 26520b57cec5SDimitry Andric #include "clang/Basic/OperatorKinds.def" 26530b57cec5SDimitry Andric 26540b57cec5SDimitry Andric case tok::l_paren: { 26550b57cec5SDimitry Andric // Consume the '(' and ')'. 26560b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 26570b57cec5SDimitry Andric T.consumeOpen(); 26580b57cec5SDimitry Andric T.consumeClose(); 26590b57cec5SDimitry Andric if (T.getCloseLocation().isInvalid()) 26600b57cec5SDimitry Andric return true; 26610b57cec5SDimitry Andric 26620b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getOpenLocation(); 26630b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getCloseLocation(); 26640b57cec5SDimitry Andric Op = OO_Call; 26650b57cec5SDimitry Andric break; 26660b57cec5SDimitry Andric } 26670b57cec5SDimitry Andric 26680b57cec5SDimitry Andric case tok::l_square: { 26690b57cec5SDimitry Andric // Consume the '[' and ']'. 26700b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_square); 26710b57cec5SDimitry Andric T.consumeOpen(); 26720b57cec5SDimitry Andric T.consumeClose(); 26730b57cec5SDimitry Andric if (T.getCloseLocation().isInvalid()) 26740b57cec5SDimitry Andric return true; 26750b57cec5SDimitry Andric 26760b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getOpenLocation(); 26770b57cec5SDimitry Andric SymbolLocations[SymbolIdx++] = T.getCloseLocation(); 26780b57cec5SDimitry Andric Op = OO_Subscript; 26790b57cec5SDimitry Andric break; 26800b57cec5SDimitry Andric } 26810b57cec5SDimitry Andric 26820b57cec5SDimitry Andric case tok::code_completion: { 2683fe6060f1SDimitry Andric // Don't try to parse any further. 2684fe6060f1SDimitry Andric cutOffParsing(); 26850b57cec5SDimitry Andric // Code completion for the operator name. 26860b57cec5SDimitry Andric Actions.CodeCompleteOperatorName(getCurScope()); 26870b57cec5SDimitry Andric return true; 26880b57cec5SDimitry Andric } 26890b57cec5SDimitry Andric 26900b57cec5SDimitry Andric default: 26910b57cec5SDimitry Andric break; 26920b57cec5SDimitry Andric } 26930b57cec5SDimitry Andric 26940b57cec5SDimitry Andric if (Op != OO_None) { 26950b57cec5SDimitry Andric // We have parsed an operator-function-id. 26960b57cec5SDimitry Andric Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); 26970b57cec5SDimitry Andric return false; 26980b57cec5SDimitry Andric } 26990b57cec5SDimitry Andric 27000b57cec5SDimitry Andric // Parse a literal-operator-id. 27010b57cec5SDimitry Andric // 27020b57cec5SDimitry Andric // literal-operator-id: C++11 [over.literal] 27030b57cec5SDimitry Andric // operator string-literal identifier 27040b57cec5SDimitry Andric // operator user-defined-string-literal 27050b57cec5SDimitry Andric 27060b57cec5SDimitry Andric if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) { 27070b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator); 27080b57cec5SDimitry Andric 27090b57cec5SDimitry Andric SourceLocation DiagLoc; 27100b57cec5SDimitry Andric unsigned DiagId = 0; 27110b57cec5SDimitry Andric 27120b57cec5SDimitry Andric // We're past translation phase 6, so perform string literal concatenation 27130b57cec5SDimitry Andric // before checking for "". 27140b57cec5SDimitry Andric SmallVector<Token, 4> Toks; 27150b57cec5SDimitry Andric SmallVector<SourceLocation, 4> TokLocs; 27160b57cec5SDimitry Andric while (isTokenStringLiteral()) { 27170b57cec5SDimitry Andric if (!Tok.is(tok::string_literal) && !DiagId) { 27180b57cec5SDimitry Andric // C++11 [over.literal]p1: 27190b57cec5SDimitry Andric // The string-literal or user-defined-string-literal in a 27200b57cec5SDimitry Andric // literal-operator-id shall have no encoding-prefix [...]. 27210b57cec5SDimitry Andric DiagLoc = Tok.getLocation(); 27220b57cec5SDimitry Andric DiagId = diag::err_literal_operator_string_prefix; 27230b57cec5SDimitry Andric } 27240b57cec5SDimitry Andric Toks.push_back(Tok); 27250b57cec5SDimitry Andric TokLocs.push_back(ConsumeStringToken()); 27260b57cec5SDimitry Andric } 27270b57cec5SDimitry Andric 27280b57cec5SDimitry Andric StringLiteralParser Literal(Toks, PP); 27290b57cec5SDimitry Andric if (Literal.hadError) 27300b57cec5SDimitry Andric return true; 27310b57cec5SDimitry Andric 27320b57cec5SDimitry Andric // Grab the literal operator's suffix, which will be either the next token 27330b57cec5SDimitry Andric // or a ud-suffix from the string literal. 2734fe6060f1SDimitry Andric bool IsUDSuffix = !Literal.getUDSuffix().empty(); 27350b57cec5SDimitry Andric IdentifierInfo *II = nullptr; 27360b57cec5SDimitry Andric SourceLocation SuffixLoc; 2737fe6060f1SDimitry Andric if (IsUDSuffix) { 27380b57cec5SDimitry Andric II = &PP.getIdentifierTable().get(Literal.getUDSuffix()); 27390b57cec5SDimitry Andric SuffixLoc = 27400b57cec5SDimitry Andric Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()], 27410b57cec5SDimitry Andric Literal.getUDSuffixOffset(), 27420b57cec5SDimitry Andric PP.getSourceManager(), getLangOpts()); 27430b57cec5SDimitry Andric } else if (Tok.is(tok::identifier)) { 27440b57cec5SDimitry Andric II = Tok.getIdentifierInfo(); 27450b57cec5SDimitry Andric SuffixLoc = ConsumeToken(); 27460b57cec5SDimitry Andric TokLocs.push_back(SuffixLoc); 27470b57cec5SDimitry Andric } else { 27480b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; 27490b57cec5SDimitry Andric return true; 27500b57cec5SDimitry Andric } 27510b57cec5SDimitry Andric 27520b57cec5SDimitry Andric // The string literal must be empty. 27530b57cec5SDimitry Andric if (!Literal.GetString().empty() || Literal.Pascal) { 27540b57cec5SDimitry Andric // C++11 [over.literal]p1: 27550b57cec5SDimitry Andric // The string-literal or user-defined-string-literal in a 27560b57cec5SDimitry Andric // literal-operator-id shall [...] contain no characters 27570b57cec5SDimitry Andric // other than the implicit terminating '\0'. 27580b57cec5SDimitry Andric DiagLoc = TokLocs.front(); 27590b57cec5SDimitry Andric DiagId = diag::err_literal_operator_string_not_empty; 27600b57cec5SDimitry Andric } 27610b57cec5SDimitry Andric 27620b57cec5SDimitry Andric if (DiagId) { 27630b57cec5SDimitry Andric // This isn't a valid literal-operator-id, but we think we know 27640b57cec5SDimitry Andric // what the user meant. Tell them what they should have written. 27650b57cec5SDimitry Andric SmallString<32> Str; 27660b57cec5SDimitry Andric Str += "\"\""; 27670b57cec5SDimitry Andric Str += II->getName(); 27680b57cec5SDimitry Andric Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement( 27690b57cec5SDimitry Andric SourceRange(TokLocs.front(), TokLocs.back()), Str); 27700b57cec5SDimitry Andric } 27710b57cec5SDimitry Andric 27720b57cec5SDimitry Andric Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc); 27730b57cec5SDimitry Andric 2774fe6060f1SDimitry Andric return Actions.checkLiteralOperatorId(SS, Result, IsUDSuffix); 27750b57cec5SDimitry Andric } 27760b57cec5SDimitry Andric 27770b57cec5SDimitry Andric // Parse a conversion-function-id. 27780b57cec5SDimitry Andric // 27790b57cec5SDimitry Andric // conversion-function-id: [C++ 12.3.2] 27800b57cec5SDimitry Andric // operator conversion-type-id 27810b57cec5SDimitry Andric // 27820b57cec5SDimitry Andric // conversion-type-id: 27830b57cec5SDimitry Andric // type-specifier-seq conversion-declarator[opt] 27840b57cec5SDimitry Andric // 27850b57cec5SDimitry Andric // conversion-declarator: 27860b57cec5SDimitry Andric // ptr-operator conversion-declarator[opt] 27870b57cec5SDimitry Andric 27880b57cec5SDimitry Andric // Parse the type-specifier-seq. 27890b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 2790*bdd1243dSDimitry Andric if (ParseCXXTypeSpecifierSeq( 2791*bdd1243dSDimitry Andric DS, DeclaratorContext::ConversionId)) // FIXME: ObjectType? 27920b57cec5SDimitry Andric return true; 27930b57cec5SDimitry Andric 27940b57cec5SDimitry Andric // Parse the conversion-declarator, which is merely a sequence of 27950b57cec5SDimitry Andric // ptr-operators. 279681ad6265SDimitry Andric Declarator D(DS, ParsedAttributesView::none(), 279781ad6265SDimitry Andric DeclaratorContext::ConversionId); 27980b57cec5SDimitry Andric ParseDeclaratorInternal(D, /*DirectDeclParser=*/nullptr); 27990b57cec5SDimitry Andric 28000b57cec5SDimitry Andric // Finish up the type. 28010b57cec5SDimitry Andric TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D); 28020b57cec5SDimitry Andric if (Ty.isInvalid()) 28030b57cec5SDimitry Andric return true; 28040b57cec5SDimitry Andric 28050b57cec5SDimitry Andric // Note that this is a conversion-function-id. 28060b57cec5SDimitry Andric Result.setConversionFunctionId(KeywordLoc, Ty.get(), 28070b57cec5SDimitry Andric D.getSourceRange().getEnd()); 28080b57cec5SDimitry Andric return false; 28090b57cec5SDimitry Andric } 28100b57cec5SDimitry Andric 28110b57cec5SDimitry Andric /// Parse a C++ unqualified-id (or a C identifier), which describes the 28120b57cec5SDimitry Andric /// name of an entity. 28130b57cec5SDimitry Andric /// 28140b57cec5SDimitry Andric /// \code 28150b57cec5SDimitry Andric /// unqualified-id: [C++ expr.prim.general] 28160b57cec5SDimitry Andric /// identifier 28170b57cec5SDimitry Andric /// operator-function-id 28180b57cec5SDimitry Andric /// conversion-function-id 28190b57cec5SDimitry Andric /// [C++0x] literal-operator-id [TODO] 28200b57cec5SDimitry Andric /// ~ class-name 28210b57cec5SDimitry Andric /// template-id 28220b57cec5SDimitry Andric /// 28230b57cec5SDimitry Andric /// \endcode 28240b57cec5SDimitry Andric /// 28250b57cec5SDimitry Andric /// \param SS The nested-name-specifier that preceded this unqualified-id. If 28260b57cec5SDimitry Andric /// non-empty, then we are parsing the unqualified-id of a qualified-id. 28270b57cec5SDimitry Andric /// 28285ffd83dbSDimitry Andric /// \param ObjectType if this unqualified-id occurs within a member access 28295ffd83dbSDimitry Andric /// expression, the type of the base object whose member is being accessed. 28305ffd83dbSDimitry Andric /// 28315ffd83dbSDimitry Andric /// \param ObjectHadErrors if this unqualified-id occurs within a member access 28325ffd83dbSDimitry Andric /// expression, indicates whether the original subexpressions had any errors. 28335ffd83dbSDimitry Andric /// When true, diagnostics for missing 'template' keyword will be supressed. 28345ffd83dbSDimitry Andric /// 28350b57cec5SDimitry Andric /// \param EnteringContext whether we are entering the scope of the 28360b57cec5SDimitry Andric /// nested-name-specifier. 28370b57cec5SDimitry Andric /// 28380b57cec5SDimitry Andric /// \param AllowDestructorName whether we allow parsing of a destructor name. 28390b57cec5SDimitry Andric /// 28400b57cec5SDimitry Andric /// \param AllowConstructorName whether we allow parsing a constructor name. 28410b57cec5SDimitry Andric /// 28420b57cec5SDimitry Andric /// \param AllowDeductionGuide whether we allow parsing a deduction guide name. 28430b57cec5SDimitry Andric /// 28440b57cec5SDimitry Andric /// \param Result on a successful parse, contains the parsed unqualified-id. 28450b57cec5SDimitry Andric /// 28460b57cec5SDimitry Andric /// \returns true if parsing fails, false otherwise. 28475ffd83dbSDimitry Andric bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, ParsedType ObjectType, 28485ffd83dbSDimitry Andric bool ObjectHadErrors, bool EnteringContext, 28490b57cec5SDimitry Andric bool AllowDestructorName, 28500b57cec5SDimitry Andric bool AllowConstructorName, 28510b57cec5SDimitry Andric bool AllowDeductionGuide, 28520b57cec5SDimitry Andric SourceLocation *TemplateKWLoc, 28530b57cec5SDimitry Andric UnqualifiedId &Result) { 28540b57cec5SDimitry Andric if (TemplateKWLoc) 28550b57cec5SDimitry Andric *TemplateKWLoc = SourceLocation(); 28560b57cec5SDimitry Andric 28570b57cec5SDimitry Andric // Handle 'A::template B'. This is for template-ids which have not 28580b57cec5SDimitry Andric // already been annotated by ParseOptionalCXXScopeSpecifier(). 28590b57cec5SDimitry Andric bool TemplateSpecified = false; 28600b57cec5SDimitry Andric if (Tok.is(tok::kw_template)) { 28610b57cec5SDimitry Andric if (TemplateKWLoc && (ObjectType || SS.isSet())) { 28620b57cec5SDimitry Andric TemplateSpecified = true; 28630b57cec5SDimitry Andric *TemplateKWLoc = ConsumeToken(); 28640b57cec5SDimitry Andric } else { 28650b57cec5SDimitry Andric SourceLocation TemplateLoc = ConsumeToken(); 28660b57cec5SDimitry Andric Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id) 28670b57cec5SDimitry Andric << FixItHint::CreateRemoval(TemplateLoc); 28680b57cec5SDimitry Andric } 28690b57cec5SDimitry Andric } 28700b57cec5SDimitry Andric 28710b57cec5SDimitry Andric // unqualified-id: 28720b57cec5SDimitry Andric // identifier 28730b57cec5SDimitry Andric // template-id (when it hasn't already been annotated) 28740b57cec5SDimitry Andric if (Tok.is(tok::identifier)) { 2875*bdd1243dSDimitry Andric ParseIdentifier: 28760b57cec5SDimitry Andric // Consume the identifier. 28770b57cec5SDimitry Andric IdentifierInfo *Id = Tok.getIdentifierInfo(); 28780b57cec5SDimitry Andric SourceLocation IdLoc = ConsumeToken(); 28790b57cec5SDimitry Andric 28800b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus) { 28810b57cec5SDimitry Andric // If we're not in C++, only identifiers matter. Record the 28820b57cec5SDimitry Andric // identifier and return. 28830b57cec5SDimitry Andric Result.setIdentifier(Id, IdLoc); 28840b57cec5SDimitry Andric return false; 28850b57cec5SDimitry Andric } 28860b57cec5SDimitry Andric 28870b57cec5SDimitry Andric ParsedTemplateTy TemplateName; 28880b57cec5SDimitry Andric if (AllowConstructorName && 28890b57cec5SDimitry Andric Actions.isCurrentClassName(*Id, getCurScope(), &SS)) { 28900b57cec5SDimitry Andric // We have parsed a constructor name. 28910b57cec5SDimitry Andric ParsedType Ty = Actions.getConstructorName(*Id, IdLoc, getCurScope(), SS, 28920b57cec5SDimitry Andric EnteringContext); 28930b57cec5SDimitry Andric if (!Ty) 28940b57cec5SDimitry Andric return true; 28950b57cec5SDimitry Andric Result.setConstructorName(Ty, IdLoc, IdLoc); 28960b57cec5SDimitry Andric } else if (getLangOpts().CPlusPlus17 && 28970b57cec5SDimitry Andric AllowDeductionGuide && SS.isEmpty() && 28980b57cec5SDimitry Andric Actions.isDeductionGuideName(getCurScope(), *Id, IdLoc, 28990b57cec5SDimitry Andric &TemplateName)) { 29000b57cec5SDimitry Andric // We have parsed a template-name naming a deduction guide. 29010b57cec5SDimitry Andric Result.setDeductionGuideName(TemplateName, IdLoc); 29020b57cec5SDimitry Andric } else { 29030b57cec5SDimitry Andric // We have parsed an identifier. 29040b57cec5SDimitry Andric Result.setIdentifier(Id, IdLoc); 29050b57cec5SDimitry Andric } 29060b57cec5SDimitry Andric 29070b57cec5SDimitry Andric // If the next token is a '<', we may have a template. 29080b57cec5SDimitry Andric TemplateTy Template; 29090b57cec5SDimitry Andric if (Tok.is(tok::less)) 29100b57cec5SDimitry Andric return ParseUnqualifiedIdTemplateId( 29115ffd83dbSDimitry Andric SS, ObjectType, ObjectHadErrors, 29125ffd83dbSDimitry Andric TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), Id, IdLoc, 29135ffd83dbSDimitry Andric EnteringContext, Result, TemplateSpecified); 29140b57cec5SDimitry Andric else if (TemplateSpecified && 29155ffd83dbSDimitry Andric Actions.ActOnTemplateName( 29160b57cec5SDimitry Andric getCurScope(), SS, *TemplateKWLoc, Result, ObjectType, 29170b57cec5SDimitry Andric EnteringContext, Template, 29180b57cec5SDimitry Andric /*AllowInjectedClassName*/ true) == TNK_Non_template) 29190b57cec5SDimitry Andric return true; 29200b57cec5SDimitry Andric 29210b57cec5SDimitry Andric return false; 29220b57cec5SDimitry Andric } 29230b57cec5SDimitry Andric 29240b57cec5SDimitry Andric // unqualified-id: 29250b57cec5SDimitry Andric // template-id (already parsed and annotated) 29260b57cec5SDimitry Andric if (Tok.is(tok::annot_template_id)) { 29270b57cec5SDimitry Andric TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 29280b57cec5SDimitry Andric 29295ffd83dbSDimitry Andric // FIXME: Consider passing invalid template-ids on to callers; they may 29305ffd83dbSDimitry Andric // be able to recover better than we can. 29315ffd83dbSDimitry Andric if (TemplateId->isInvalid()) { 29325ffd83dbSDimitry Andric ConsumeAnnotationToken(); 29335ffd83dbSDimitry Andric return true; 29345ffd83dbSDimitry Andric } 29355ffd83dbSDimitry Andric 29360b57cec5SDimitry Andric // If the template-name names the current class, then this is a constructor 29370b57cec5SDimitry Andric if (AllowConstructorName && TemplateId->Name && 29380b57cec5SDimitry Andric Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { 29390b57cec5SDimitry Andric if (SS.isSet()) { 29400b57cec5SDimitry Andric // C++ [class.qual]p2 specifies that a qualified template-name 29410b57cec5SDimitry Andric // is taken as the constructor name where a constructor can be 29420b57cec5SDimitry Andric // declared. Thus, the template arguments are extraneous, so 29430b57cec5SDimitry Andric // complain about them and remove them entirely. 29440b57cec5SDimitry Andric Diag(TemplateId->TemplateNameLoc, 29450b57cec5SDimitry Andric diag::err_out_of_line_constructor_template_id) 29460b57cec5SDimitry Andric << TemplateId->Name 29470b57cec5SDimitry Andric << FixItHint::CreateRemoval( 29480b57cec5SDimitry Andric SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc)); 29490b57cec5SDimitry Andric ParsedType Ty = Actions.getConstructorName( 29500b57cec5SDimitry Andric *TemplateId->Name, TemplateId->TemplateNameLoc, getCurScope(), SS, 29510b57cec5SDimitry Andric EnteringContext); 29520b57cec5SDimitry Andric if (!Ty) 29530b57cec5SDimitry Andric return true; 29540b57cec5SDimitry Andric Result.setConstructorName(Ty, TemplateId->TemplateNameLoc, 29550b57cec5SDimitry Andric TemplateId->RAngleLoc); 29560b57cec5SDimitry Andric ConsumeAnnotationToken(); 29570b57cec5SDimitry Andric return false; 29580b57cec5SDimitry Andric } 29590b57cec5SDimitry Andric 29600b57cec5SDimitry Andric Result.setConstructorTemplateId(TemplateId); 29610b57cec5SDimitry Andric ConsumeAnnotationToken(); 29620b57cec5SDimitry Andric return false; 29630b57cec5SDimitry Andric } 29640b57cec5SDimitry Andric 29650b57cec5SDimitry Andric // We have already parsed a template-id; consume the annotation token as 29660b57cec5SDimitry Andric // our unqualified-id. 29670b57cec5SDimitry Andric Result.setTemplateId(TemplateId); 29680b57cec5SDimitry Andric SourceLocation TemplateLoc = TemplateId->TemplateKWLoc; 29690b57cec5SDimitry Andric if (TemplateLoc.isValid()) { 29700b57cec5SDimitry Andric if (TemplateKWLoc && (ObjectType || SS.isSet())) 29710b57cec5SDimitry Andric *TemplateKWLoc = TemplateLoc; 29720b57cec5SDimitry Andric else 29730b57cec5SDimitry Andric Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id) 29740b57cec5SDimitry Andric << FixItHint::CreateRemoval(TemplateLoc); 29750b57cec5SDimitry Andric } 29760b57cec5SDimitry Andric ConsumeAnnotationToken(); 29770b57cec5SDimitry Andric return false; 29780b57cec5SDimitry Andric } 29790b57cec5SDimitry Andric 29800b57cec5SDimitry Andric // unqualified-id: 29810b57cec5SDimitry Andric // operator-function-id 29820b57cec5SDimitry Andric // conversion-function-id 29830b57cec5SDimitry Andric if (Tok.is(tok::kw_operator)) { 29840b57cec5SDimitry Andric if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) 29850b57cec5SDimitry Andric return true; 29860b57cec5SDimitry Andric 29870b57cec5SDimitry Andric // If we have an operator-function-id or a literal-operator-id and the next 29880b57cec5SDimitry Andric // token is a '<', we may have a 29890b57cec5SDimitry Andric // 29900b57cec5SDimitry Andric // template-id: 29910b57cec5SDimitry Andric // operator-function-id < template-argument-list[opt] > 29920b57cec5SDimitry Andric TemplateTy Template; 29930b57cec5SDimitry Andric if ((Result.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId || 29940b57cec5SDimitry Andric Result.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) && 29950b57cec5SDimitry Andric Tok.is(tok::less)) 29960b57cec5SDimitry Andric return ParseUnqualifiedIdTemplateId( 29975ffd83dbSDimitry Andric SS, ObjectType, ObjectHadErrors, 29985ffd83dbSDimitry Andric TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), nullptr, 29995ffd83dbSDimitry Andric SourceLocation(), EnteringContext, Result, TemplateSpecified); 30000b57cec5SDimitry Andric else if (TemplateSpecified && 30015ffd83dbSDimitry Andric Actions.ActOnTemplateName( 30020b57cec5SDimitry Andric getCurScope(), SS, *TemplateKWLoc, Result, ObjectType, 30030b57cec5SDimitry Andric EnteringContext, Template, 30040b57cec5SDimitry Andric /*AllowInjectedClassName*/ true) == TNK_Non_template) 30050b57cec5SDimitry Andric return true; 30060b57cec5SDimitry Andric 30070b57cec5SDimitry Andric return false; 30080b57cec5SDimitry Andric } 30090b57cec5SDimitry Andric 30100b57cec5SDimitry Andric if (getLangOpts().CPlusPlus && 30110b57cec5SDimitry Andric (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { 30120b57cec5SDimitry Andric // C++ [expr.unary.op]p10: 30130b57cec5SDimitry Andric // There is an ambiguity in the unary-expression ~X(), where X is a 30140b57cec5SDimitry Andric // class-name. The ambiguity is resolved in favor of treating ~ as a 30150b57cec5SDimitry Andric // unary complement rather than treating ~X as referring to a destructor. 30160b57cec5SDimitry Andric 30170b57cec5SDimitry Andric // Parse the '~'. 30180b57cec5SDimitry Andric SourceLocation TildeLoc = ConsumeToken(); 30190b57cec5SDimitry Andric 30205ffd83dbSDimitry Andric if (TemplateSpecified) { 30215ffd83dbSDimitry Andric // C++ [temp.names]p3: 30225ffd83dbSDimitry Andric // A name prefixed by the keyword template shall be a template-id [...] 30235ffd83dbSDimitry Andric // 30245ffd83dbSDimitry Andric // A template-id cannot begin with a '~' token. This would never work 30255ffd83dbSDimitry Andric // anyway: x.~A<int>() would specify that the destructor is a template, 30265ffd83dbSDimitry Andric // not that 'A' is a template. 30275ffd83dbSDimitry Andric // 30285ffd83dbSDimitry Andric // FIXME: Suggest replacing the attempted destructor name with a correct 30295ffd83dbSDimitry Andric // destructor name and recover. (This is not trivial if this would become 30305ffd83dbSDimitry Andric // a pseudo-destructor name). 30315ffd83dbSDimitry Andric Diag(*TemplateKWLoc, diag::err_unexpected_template_in_destructor_name) 30325ffd83dbSDimitry Andric << Tok.getLocation(); 30335ffd83dbSDimitry Andric return true; 30345ffd83dbSDimitry Andric } 30355ffd83dbSDimitry Andric 30360b57cec5SDimitry Andric if (SS.isEmpty() && Tok.is(tok::kw_decltype)) { 30370b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 30380b57cec5SDimitry Andric SourceLocation EndLoc = ParseDecltypeSpecifier(DS); 30390b57cec5SDimitry Andric if (ParsedType Type = 30400b57cec5SDimitry Andric Actions.getDestructorTypeForDecltype(DS, ObjectType)) { 30410b57cec5SDimitry Andric Result.setDestructorName(TildeLoc, Type, EndLoc); 30420b57cec5SDimitry Andric return false; 30430b57cec5SDimitry Andric } 30440b57cec5SDimitry Andric return true; 30450b57cec5SDimitry Andric } 30460b57cec5SDimitry Andric 30470b57cec5SDimitry Andric // Parse the class-name. 30480b57cec5SDimitry Andric if (Tok.isNot(tok::identifier)) { 30490b57cec5SDimitry Andric Diag(Tok, diag::err_destructor_tilde_identifier); 30500b57cec5SDimitry Andric return true; 30510b57cec5SDimitry Andric } 30520b57cec5SDimitry Andric 30530b57cec5SDimitry Andric // If the user wrote ~T::T, correct it to T::~T. 30540b57cec5SDimitry Andric DeclaratorScopeObj DeclScopeObj(*this, SS); 30555ffd83dbSDimitry Andric if (NextToken().is(tok::coloncolon)) { 30560b57cec5SDimitry Andric // Don't let ParseOptionalCXXScopeSpecifier() "correct" 30570b57cec5SDimitry Andric // `int A; struct { ~A::A(); };` to `int A; struct { ~A:A(); };`, 30580b57cec5SDimitry Andric // it will confuse this recovery logic. 30590b57cec5SDimitry Andric ColonProtectionRAIIObject ColonRAII(*this, false); 30600b57cec5SDimitry Andric 30610b57cec5SDimitry Andric if (SS.isSet()) { 30620b57cec5SDimitry Andric AnnotateScopeToken(SS, /*NewAnnotation*/true); 30630b57cec5SDimitry Andric SS.clear(); 30640b57cec5SDimitry Andric } 30655ffd83dbSDimitry Andric if (ParseOptionalCXXScopeSpecifier(SS, ObjectType, ObjectHadErrors, 30665ffd83dbSDimitry Andric EnteringContext)) 30670b57cec5SDimitry Andric return true; 30680b57cec5SDimitry Andric if (SS.isNotEmpty()) 30690b57cec5SDimitry Andric ObjectType = nullptr; 30700b57cec5SDimitry Andric if (Tok.isNot(tok::identifier) || NextToken().is(tok::coloncolon) || 30710b57cec5SDimitry Andric !SS.isSet()) { 30720b57cec5SDimitry Andric Diag(TildeLoc, diag::err_destructor_tilde_scope); 30730b57cec5SDimitry Andric return true; 30740b57cec5SDimitry Andric } 30750b57cec5SDimitry Andric 30760b57cec5SDimitry Andric // Recover as if the tilde had been written before the identifier. 30770b57cec5SDimitry Andric Diag(TildeLoc, diag::err_destructor_tilde_scope) 30780b57cec5SDimitry Andric << FixItHint::CreateRemoval(TildeLoc) 30790b57cec5SDimitry Andric << FixItHint::CreateInsertion(Tok.getLocation(), "~"); 30800b57cec5SDimitry Andric 30810b57cec5SDimitry Andric // Temporarily enter the scope for the rest of this function. 30820b57cec5SDimitry Andric if (Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) 30830b57cec5SDimitry Andric DeclScopeObj.EnterDeclaratorScope(); 30840b57cec5SDimitry Andric } 30850b57cec5SDimitry Andric 30860b57cec5SDimitry Andric // Parse the class-name (or template-name in a simple-template-id). 30870b57cec5SDimitry Andric IdentifierInfo *ClassName = Tok.getIdentifierInfo(); 30880b57cec5SDimitry Andric SourceLocation ClassNameLoc = ConsumeToken(); 30890b57cec5SDimitry Andric 30900b57cec5SDimitry Andric if (Tok.is(tok::less)) { 30910b57cec5SDimitry Andric Result.setDestructorName(TildeLoc, nullptr, ClassNameLoc); 30920b57cec5SDimitry Andric return ParseUnqualifiedIdTemplateId( 30935ffd83dbSDimitry Andric SS, ObjectType, ObjectHadErrors, 30945ffd83dbSDimitry Andric TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), ClassName, 30955ffd83dbSDimitry Andric ClassNameLoc, EnteringContext, Result, TemplateSpecified); 30960b57cec5SDimitry Andric } 30970b57cec5SDimitry Andric 30980b57cec5SDimitry Andric // Note that this is a destructor name. 30990b57cec5SDimitry Andric ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName, 31000b57cec5SDimitry Andric ClassNameLoc, getCurScope(), 31010b57cec5SDimitry Andric SS, ObjectType, 31020b57cec5SDimitry Andric EnteringContext); 31030b57cec5SDimitry Andric if (!Ty) 31040b57cec5SDimitry Andric return true; 31050b57cec5SDimitry Andric 31060b57cec5SDimitry Andric Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); 31070b57cec5SDimitry Andric return false; 31080b57cec5SDimitry Andric } 31090b57cec5SDimitry Andric 3110*bdd1243dSDimitry Andric switch (Tok.getKind()) { 3111*bdd1243dSDimitry Andric #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: 3112*bdd1243dSDimitry Andric #include "clang/Basic/TransformTypeTraits.def" 3113*bdd1243dSDimitry Andric if (!NextToken().is(tok::l_paren)) { 3114*bdd1243dSDimitry Andric Tok.setKind(tok::identifier); 3115*bdd1243dSDimitry Andric Diag(Tok, diag::ext_keyword_as_ident) 3116*bdd1243dSDimitry Andric << Tok.getIdentifierInfo()->getName() << 0; 3117*bdd1243dSDimitry Andric goto ParseIdentifier; 3118*bdd1243dSDimitry Andric } 3119*bdd1243dSDimitry Andric [[fallthrough]]; 3120*bdd1243dSDimitry Andric default: 3121*bdd1243dSDimitry Andric Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; 31220b57cec5SDimitry Andric return true; 31230b57cec5SDimitry Andric } 3124*bdd1243dSDimitry Andric } 31250b57cec5SDimitry Andric 31260b57cec5SDimitry Andric /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate 31270b57cec5SDimitry Andric /// memory in a typesafe manner and call constructors. 31280b57cec5SDimitry Andric /// 31290b57cec5SDimitry Andric /// This method is called to parse the new expression after the optional :: has 31300b57cec5SDimitry Andric /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" 31310b57cec5SDimitry Andric /// is its location. Otherwise, "Start" is the location of the 'new' token. 31320b57cec5SDimitry Andric /// 31330b57cec5SDimitry Andric /// new-expression: 31340b57cec5SDimitry Andric /// '::'[opt] 'new' new-placement[opt] new-type-id 31350b57cec5SDimitry Andric /// new-initializer[opt] 31360b57cec5SDimitry Andric /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 31370b57cec5SDimitry Andric /// new-initializer[opt] 31380b57cec5SDimitry Andric /// 31390b57cec5SDimitry Andric /// new-placement: 31400b57cec5SDimitry Andric /// '(' expression-list ')' 31410b57cec5SDimitry Andric /// 31420b57cec5SDimitry Andric /// new-type-id: 31430b57cec5SDimitry Andric /// type-specifier-seq new-declarator[opt] 31440b57cec5SDimitry Andric /// [GNU] attributes type-specifier-seq new-declarator[opt] 31450b57cec5SDimitry Andric /// 31460b57cec5SDimitry Andric /// new-declarator: 31470b57cec5SDimitry Andric /// ptr-operator new-declarator[opt] 31480b57cec5SDimitry Andric /// direct-new-declarator 31490b57cec5SDimitry Andric /// 31500b57cec5SDimitry Andric /// new-initializer: 31510b57cec5SDimitry Andric /// '(' expression-list[opt] ')' 31520b57cec5SDimitry Andric /// [C++0x] braced-init-list 31530b57cec5SDimitry Andric /// 31540b57cec5SDimitry Andric ExprResult 31550b57cec5SDimitry Andric Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { 31560b57cec5SDimitry Andric assert(Tok.is(tok::kw_new) && "expected 'new' token"); 31570b57cec5SDimitry Andric ConsumeToken(); // Consume 'new' 31580b57cec5SDimitry Andric 31590b57cec5SDimitry Andric // A '(' now can be a new-placement or the '(' wrapping the type-id in the 31600b57cec5SDimitry Andric // second form of new-expression. It can't be a new-type-id. 31610b57cec5SDimitry Andric 31620b57cec5SDimitry Andric ExprVector PlacementArgs; 31630b57cec5SDimitry Andric SourceLocation PlacementLParen, PlacementRParen; 31640b57cec5SDimitry Andric 31650b57cec5SDimitry Andric SourceRange TypeIdParens; 31660b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 316781ad6265SDimitry Andric Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 316881ad6265SDimitry Andric DeclaratorContext::CXXNew); 31690b57cec5SDimitry Andric if (Tok.is(tok::l_paren)) { 31700b57cec5SDimitry Andric // If it turns out to be a placement, we change the type location. 31710b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 31720b57cec5SDimitry Andric T.consumeOpen(); 31730b57cec5SDimitry Andric PlacementLParen = T.getOpenLocation(); 31740b57cec5SDimitry Andric if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { 31750b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); 31760b57cec5SDimitry Andric return ExprError(); 31770b57cec5SDimitry Andric } 31780b57cec5SDimitry Andric 31790b57cec5SDimitry Andric T.consumeClose(); 31800b57cec5SDimitry Andric PlacementRParen = T.getCloseLocation(); 31810b57cec5SDimitry Andric if (PlacementRParen.isInvalid()) { 31820b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); 31830b57cec5SDimitry Andric return ExprError(); 31840b57cec5SDimitry Andric } 31850b57cec5SDimitry Andric 31860b57cec5SDimitry Andric if (PlacementArgs.empty()) { 31870b57cec5SDimitry Andric // Reset the placement locations. There was no placement. 31880b57cec5SDimitry Andric TypeIdParens = T.getRange(); 31890b57cec5SDimitry Andric PlacementLParen = PlacementRParen = SourceLocation(); 31900b57cec5SDimitry Andric } else { 31910b57cec5SDimitry Andric // We still need the type. 31920b57cec5SDimitry Andric if (Tok.is(tok::l_paren)) { 31930b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 31940b57cec5SDimitry Andric T.consumeOpen(); 31950b57cec5SDimitry Andric MaybeParseGNUAttributes(DeclaratorInfo); 31960b57cec5SDimitry Andric ParseSpecifierQualifierList(DS); 31970b57cec5SDimitry Andric DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 31980b57cec5SDimitry Andric ParseDeclarator(DeclaratorInfo); 31990b57cec5SDimitry Andric T.consumeClose(); 32000b57cec5SDimitry Andric TypeIdParens = T.getRange(); 32010b57cec5SDimitry Andric } else { 32020b57cec5SDimitry Andric MaybeParseGNUAttributes(DeclaratorInfo); 32030b57cec5SDimitry Andric if (ParseCXXTypeSpecifierSeq(DS)) 32040b57cec5SDimitry Andric DeclaratorInfo.setInvalidType(true); 32050b57cec5SDimitry Andric else { 32060b57cec5SDimitry Andric DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 32070b57cec5SDimitry Andric ParseDeclaratorInternal(DeclaratorInfo, 32080b57cec5SDimitry Andric &Parser::ParseDirectNewDeclarator); 32090b57cec5SDimitry Andric } 32100b57cec5SDimitry Andric } 32110b57cec5SDimitry Andric } 32120b57cec5SDimitry Andric } else { 32130b57cec5SDimitry Andric // A new-type-id is a simplified type-id, where essentially the 32140b57cec5SDimitry Andric // direct-declarator is replaced by a direct-new-declarator. 32150b57cec5SDimitry Andric MaybeParseGNUAttributes(DeclaratorInfo); 32160b57cec5SDimitry Andric if (ParseCXXTypeSpecifierSeq(DS)) 32170b57cec5SDimitry Andric DeclaratorInfo.setInvalidType(true); 32180b57cec5SDimitry Andric else { 32190b57cec5SDimitry Andric DeclaratorInfo.SetSourceRange(DS.getSourceRange()); 32200b57cec5SDimitry Andric ParseDeclaratorInternal(DeclaratorInfo, 32210b57cec5SDimitry Andric &Parser::ParseDirectNewDeclarator); 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric } 32240b57cec5SDimitry Andric if (DeclaratorInfo.isInvalidType()) { 32250b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); 32260b57cec5SDimitry Andric return ExprError(); 32270b57cec5SDimitry Andric } 32280b57cec5SDimitry Andric 32290b57cec5SDimitry Andric ExprResult Initializer; 32300b57cec5SDimitry Andric 32310b57cec5SDimitry Andric if (Tok.is(tok::l_paren)) { 32320b57cec5SDimitry Andric SourceLocation ConstructorLParen, ConstructorRParen; 32330b57cec5SDimitry Andric ExprVector ConstructorArgs; 32340b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 32350b57cec5SDimitry Andric T.consumeOpen(); 32360b57cec5SDimitry Andric ConstructorLParen = T.getOpenLocation(); 32370b57cec5SDimitry Andric if (Tok.isNot(tok::r_paren)) { 32380b57cec5SDimitry Andric auto RunSignatureHelp = [&]() { 32390b57cec5SDimitry Andric ParsedType TypeRep = 32400b57cec5SDimitry Andric Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); 32415ffd83dbSDimitry Andric QualType PreferredType; 32425ffd83dbSDimitry Andric // ActOnTypeName might adjust DeclaratorInfo and return a null type even 32435ffd83dbSDimitry Andric // the passing DeclaratorInfo is valid, e.g. running SignatureHelp on 32445ffd83dbSDimitry Andric // `new decltype(invalid) (^)`. 32455ffd83dbSDimitry Andric if (TypeRep) 32465ffd83dbSDimitry Andric PreferredType = Actions.ProduceConstructorSignatureHelp( 324704eeddc0SDimitry Andric TypeRep.get()->getCanonicalTypeInternal(), 324804eeddc0SDimitry Andric DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen, 324904eeddc0SDimitry Andric /*Braced=*/false); 32500b57cec5SDimitry Andric CalledSignatureHelp = true; 32510b57cec5SDimitry Andric return PreferredType; 32520b57cec5SDimitry Andric }; 3253*bdd1243dSDimitry Andric if (ParseExpressionList(ConstructorArgs, [&] { 32540b57cec5SDimitry Andric PreferredType.enterFunctionArgument(Tok.getLocation(), 32550b57cec5SDimitry Andric RunSignatureHelp); 32560b57cec5SDimitry Andric })) { 32570b57cec5SDimitry Andric if (PP.isCodeCompletionReached() && !CalledSignatureHelp) 32580b57cec5SDimitry Andric RunSignatureHelp(); 32590b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); 32600b57cec5SDimitry Andric return ExprError(); 32610b57cec5SDimitry Andric } 32620b57cec5SDimitry Andric } 32630b57cec5SDimitry Andric T.consumeClose(); 32640b57cec5SDimitry Andric ConstructorRParen = T.getCloseLocation(); 32650b57cec5SDimitry Andric if (ConstructorRParen.isInvalid()) { 32660b57cec5SDimitry Andric SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); 32670b57cec5SDimitry Andric return ExprError(); 32680b57cec5SDimitry Andric } 32690b57cec5SDimitry Andric Initializer = Actions.ActOnParenListExpr(ConstructorLParen, 32700b57cec5SDimitry Andric ConstructorRParen, 32710b57cec5SDimitry Andric ConstructorArgs); 32720b57cec5SDimitry Andric } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) { 32730b57cec5SDimitry Andric Diag(Tok.getLocation(), 32740b57cec5SDimitry Andric diag::warn_cxx98_compat_generalized_initializer_lists); 32750b57cec5SDimitry Andric Initializer = ParseBraceInitializer(); 32760b57cec5SDimitry Andric } 32770b57cec5SDimitry Andric if (Initializer.isInvalid()) 32780b57cec5SDimitry Andric return Initializer; 32790b57cec5SDimitry Andric 32800b57cec5SDimitry Andric return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, 32810b57cec5SDimitry Andric PlacementArgs, PlacementRParen, 32820b57cec5SDimitry Andric TypeIdParens, DeclaratorInfo, Initializer.get()); 32830b57cec5SDimitry Andric } 32840b57cec5SDimitry Andric 32850b57cec5SDimitry Andric /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be 32860b57cec5SDimitry Andric /// passed to ParseDeclaratorInternal. 32870b57cec5SDimitry Andric /// 32880b57cec5SDimitry Andric /// direct-new-declarator: 32890b57cec5SDimitry Andric /// '[' expression[opt] ']' 32900b57cec5SDimitry Andric /// direct-new-declarator '[' constant-expression ']' 32910b57cec5SDimitry Andric /// 32920b57cec5SDimitry Andric void Parser::ParseDirectNewDeclarator(Declarator &D) { 32930b57cec5SDimitry Andric // Parse the array dimensions. 32940b57cec5SDimitry Andric bool First = true; 32950b57cec5SDimitry Andric while (Tok.is(tok::l_square)) { 32960b57cec5SDimitry Andric // An array-size expression can't start with a lambda. 32970b57cec5SDimitry Andric if (CheckProhibitedCXX11Attribute()) 32980b57cec5SDimitry Andric continue; 32990b57cec5SDimitry Andric 33000b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_square); 33010b57cec5SDimitry Andric T.consumeOpen(); 33020b57cec5SDimitry Andric 33030b57cec5SDimitry Andric ExprResult Size = 33040b57cec5SDimitry Andric First ? (Tok.is(tok::r_square) ? ExprResult() : ParseExpression()) 33050b57cec5SDimitry Andric : ParseConstantExpression(); 33060b57cec5SDimitry Andric if (Size.isInvalid()) { 33070b57cec5SDimitry Andric // Recover 33080b57cec5SDimitry Andric SkipUntil(tok::r_square, StopAtSemi); 33090b57cec5SDimitry Andric return; 33100b57cec5SDimitry Andric } 33110b57cec5SDimitry Andric First = false; 33120b57cec5SDimitry Andric 33130b57cec5SDimitry Andric T.consumeClose(); 33140b57cec5SDimitry Andric 33150b57cec5SDimitry Andric // Attributes here appertain to the array type. C++11 [expr.new]p5. 33160b57cec5SDimitry Andric ParsedAttributes Attrs(AttrFactory); 33170b57cec5SDimitry Andric MaybeParseCXX11Attributes(Attrs); 33180b57cec5SDimitry Andric 33190b57cec5SDimitry Andric D.AddTypeInfo(DeclaratorChunk::getArray(0, 33200b57cec5SDimitry Andric /*isStatic=*/false, /*isStar=*/false, 33210b57cec5SDimitry Andric Size.get(), T.getOpenLocation(), 33220b57cec5SDimitry Andric T.getCloseLocation()), 33230b57cec5SDimitry Andric std::move(Attrs), T.getCloseLocation()); 33240b57cec5SDimitry Andric 33250b57cec5SDimitry Andric if (T.getCloseLocation().isInvalid()) 33260b57cec5SDimitry Andric return; 33270b57cec5SDimitry Andric } 33280b57cec5SDimitry Andric } 33290b57cec5SDimitry Andric 33300b57cec5SDimitry Andric /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. 33310b57cec5SDimitry Andric /// This ambiguity appears in the syntax of the C++ new operator. 33320b57cec5SDimitry Andric /// 33330b57cec5SDimitry Andric /// new-expression: 33340b57cec5SDimitry Andric /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' 33350b57cec5SDimitry Andric /// new-initializer[opt] 33360b57cec5SDimitry Andric /// 33370b57cec5SDimitry Andric /// new-placement: 33380b57cec5SDimitry Andric /// '(' expression-list ')' 33390b57cec5SDimitry Andric /// 33400b57cec5SDimitry Andric bool Parser::ParseExpressionListOrTypeId( 33410b57cec5SDimitry Andric SmallVectorImpl<Expr*> &PlacementArgs, 33420b57cec5SDimitry Andric Declarator &D) { 33430b57cec5SDimitry Andric // The '(' was already consumed. 33440b57cec5SDimitry Andric if (isTypeIdInParens()) { 33450b57cec5SDimitry Andric ParseSpecifierQualifierList(D.getMutableDeclSpec()); 33460b57cec5SDimitry Andric D.SetSourceRange(D.getDeclSpec().getSourceRange()); 33470b57cec5SDimitry Andric ParseDeclarator(D); 33480b57cec5SDimitry Andric return D.isInvalidType(); 33490b57cec5SDimitry Andric } 33500b57cec5SDimitry Andric 33510b57cec5SDimitry Andric // It's not a type, it has to be an expression list. 3352*bdd1243dSDimitry Andric return ParseExpressionList(PlacementArgs); 33530b57cec5SDimitry Andric } 33540b57cec5SDimitry Andric 33550b57cec5SDimitry Andric /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used 33560b57cec5SDimitry Andric /// to free memory allocated by new. 33570b57cec5SDimitry Andric /// 33580b57cec5SDimitry Andric /// This method is called to parse the 'delete' expression after the optional 33590b57cec5SDimitry Andric /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true 33600b57cec5SDimitry Andric /// and "Start" is its location. Otherwise, "Start" is the location of the 33610b57cec5SDimitry Andric /// 'delete' token. 33620b57cec5SDimitry Andric /// 33630b57cec5SDimitry Andric /// delete-expression: 33640b57cec5SDimitry Andric /// '::'[opt] 'delete' cast-expression 33650b57cec5SDimitry Andric /// '::'[opt] 'delete' '[' ']' cast-expression 33660b57cec5SDimitry Andric ExprResult 33670b57cec5SDimitry Andric Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { 33680b57cec5SDimitry Andric assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); 33690b57cec5SDimitry Andric ConsumeToken(); // Consume 'delete' 33700b57cec5SDimitry Andric 33710b57cec5SDimitry Andric // Array delete? 33720b57cec5SDimitry Andric bool ArrayDelete = false; 33730b57cec5SDimitry Andric if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) { 33740b57cec5SDimitry Andric // C++11 [expr.delete]p1: 33750b57cec5SDimitry Andric // Whenever the delete keyword is followed by empty square brackets, it 33760b57cec5SDimitry Andric // shall be interpreted as [array delete]. 33770b57cec5SDimitry Andric // [Footnote: A lambda expression with a lambda-introducer that consists 33780b57cec5SDimitry Andric // of empty square brackets can follow the delete keyword if 33790b57cec5SDimitry Andric // the lambda expression is enclosed in parentheses.] 33800b57cec5SDimitry Andric 33810b57cec5SDimitry Andric const Token Next = GetLookAheadToken(2); 33820b57cec5SDimitry Andric 33830b57cec5SDimitry Andric // Basic lookahead to check if we have a lambda expression. 33840b57cec5SDimitry Andric if (Next.isOneOf(tok::l_brace, tok::less) || 33850b57cec5SDimitry Andric (Next.is(tok::l_paren) && 33860b57cec5SDimitry Andric (GetLookAheadToken(3).is(tok::r_paren) || 33870b57cec5SDimitry Andric (GetLookAheadToken(3).is(tok::identifier) && 33880b57cec5SDimitry Andric GetLookAheadToken(4).is(tok::identifier))))) { 33890b57cec5SDimitry Andric TentativeParsingAction TPA(*this); 33900b57cec5SDimitry Andric SourceLocation LSquareLoc = Tok.getLocation(); 33910b57cec5SDimitry Andric SourceLocation RSquareLoc = NextToken().getLocation(); 33920b57cec5SDimitry Andric 33930b57cec5SDimitry Andric // SkipUntil can't skip pairs of </*...*/>; don't emit a FixIt in this 33940b57cec5SDimitry Andric // case. 33950b57cec5SDimitry Andric SkipUntil({tok::l_brace, tok::less}, StopBeforeMatch); 33960b57cec5SDimitry Andric SourceLocation RBraceLoc; 33970b57cec5SDimitry Andric bool EmitFixIt = false; 33980b57cec5SDimitry Andric if (Tok.is(tok::l_brace)) { 33990b57cec5SDimitry Andric ConsumeBrace(); 34000b57cec5SDimitry Andric SkipUntil(tok::r_brace, StopBeforeMatch); 34010b57cec5SDimitry Andric RBraceLoc = Tok.getLocation(); 34020b57cec5SDimitry Andric EmitFixIt = true; 34030b57cec5SDimitry Andric } 34040b57cec5SDimitry Andric 34050b57cec5SDimitry Andric TPA.Revert(); 34060b57cec5SDimitry Andric 34070b57cec5SDimitry Andric if (EmitFixIt) 34080b57cec5SDimitry Andric Diag(Start, diag::err_lambda_after_delete) 34090b57cec5SDimitry Andric << SourceRange(Start, RSquareLoc) 34100b57cec5SDimitry Andric << FixItHint::CreateInsertion(LSquareLoc, "(") 34110b57cec5SDimitry Andric << FixItHint::CreateInsertion( 34120b57cec5SDimitry Andric Lexer::getLocForEndOfToken( 34130b57cec5SDimitry Andric RBraceLoc, 0, Actions.getSourceManager(), getLangOpts()), 34140b57cec5SDimitry Andric ")"); 34150b57cec5SDimitry Andric else 34160b57cec5SDimitry Andric Diag(Start, diag::err_lambda_after_delete) 34170b57cec5SDimitry Andric << SourceRange(Start, RSquareLoc); 34180b57cec5SDimitry Andric 34190b57cec5SDimitry Andric // Warn that the non-capturing lambda isn't surrounded by parentheses 34200b57cec5SDimitry Andric // to disambiguate it from 'delete[]'. 34210b57cec5SDimitry Andric ExprResult Lambda = ParseLambdaExpression(); 34220b57cec5SDimitry Andric if (Lambda.isInvalid()) 34230b57cec5SDimitry Andric return ExprError(); 34240b57cec5SDimitry Andric 34250b57cec5SDimitry Andric // Evaluate any postfix expressions used on the lambda. 34260b57cec5SDimitry Andric Lambda = ParsePostfixExpressionSuffix(Lambda); 34270b57cec5SDimitry Andric if (Lambda.isInvalid()) 34280b57cec5SDimitry Andric return ExprError(); 34290b57cec5SDimitry Andric return Actions.ActOnCXXDelete(Start, UseGlobal, /*ArrayForm=*/false, 34300b57cec5SDimitry Andric Lambda.get()); 34310b57cec5SDimitry Andric } 34320b57cec5SDimitry Andric 34330b57cec5SDimitry Andric ArrayDelete = true; 34340b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_square); 34350b57cec5SDimitry Andric 34360b57cec5SDimitry Andric T.consumeOpen(); 34370b57cec5SDimitry Andric T.consumeClose(); 34380b57cec5SDimitry Andric if (T.getCloseLocation().isInvalid()) 34390b57cec5SDimitry Andric return ExprError(); 34400b57cec5SDimitry Andric } 34410b57cec5SDimitry Andric 3442480093f4SDimitry Andric ExprResult Operand(ParseCastExpression(AnyCastExpr)); 34430b57cec5SDimitry Andric if (Operand.isInvalid()) 34440b57cec5SDimitry Andric return Operand; 34450b57cec5SDimitry Andric 34460b57cec5SDimitry Andric return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.get()); 34470b57cec5SDimitry Andric } 34480b57cec5SDimitry Andric 344955e4f9d5SDimitry Andric /// ParseRequiresExpression - Parse a C++2a requires-expression. 345055e4f9d5SDimitry Andric /// C++2a [expr.prim.req]p1 345155e4f9d5SDimitry Andric /// A requires-expression provides a concise way to express requirements on 345255e4f9d5SDimitry Andric /// template arguments. A requirement is one that can be checked by name 345355e4f9d5SDimitry Andric /// lookup (6.4) or by checking properties of types and expressions. 345455e4f9d5SDimitry Andric /// 345555e4f9d5SDimitry Andric /// requires-expression: 345655e4f9d5SDimitry Andric /// 'requires' requirement-parameter-list[opt] requirement-body 345755e4f9d5SDimitry Andric /// 345855e4f9d5SDimitry Andric /// requirement-parameter-list: 345955e4f9d5SDimitry Andric /// '(' parameter-declaration-clause[opt] ')' 346055e4f9d5SDimitry Andric /// 346155e4f9d5SDimitry Andric /// requirement-body: 346255e4f9d5SDimitry Andric /// '{' requirement-seq '}' 346355e4f9d5SDimitry Andric /// 346455e4f9d5SDimitry Andric /// requirement-seq: 346555e4f9d5SDimitry Andric /// requirement 346655e4f9d5SDimitry Andric /// requirement-seq requirement 346755e4f9d5SDimitry Andric /// 346855e4f9d5SDimitry Andric /// requirement: 346955e4f9d5SDimitry Andric /// simple-requirement 347055e4f9d5SDimitry Andric /// type-requirement 347155e4f9d5SDimitry Andric /// compound-requirement 347255e4f9d5SDimitry Andric /// nested-requirement 347355e4f9d5SDimitry Andric ExprResult Parser::ParseRequiresExpression() { 347455e4f9d5SDimitry Andric assert(Tok.is(tok::kw_requires) && "Expected 'requires' keyword"); 347555e4f9d5SDimitry Andric SourceLocation RequiresKWLoc = ConsumeToken(); // Consume 'requires' 347655e4f9d5SDimitry Andric 347755e4f9d5SDimitry Andric llvm::SmallVector<ParmVarDecl *, 2> LocalParameterDecls; 347855e4f9d5SDimitry Andric if (Tok.is(tok::l_paren)) { 347955e4f9d5SDimitry Andric // requirement parameter list is present. 348055e4f9d5SDimitry Andric ParseScope LocalParametersScope(this, Scope::FunctionPrototypeScope | 348155e4f9d5SDimitry Andric Scope::DeclScope); 348255e4f9d5SDimitry Andric BalancedDelimiterTracker Parens(*this, tok::l_paren); 348355e4f9d5SDimitry Andric Parens.consumeOpen(); 348455e4f9d5SDimitry Andric if (!Tok.is(tok::r_paren)) { 348555e4f9d5SDimitry Andric ParsedAttributes FirstArgAttrs(getAttrFactory()); 348655e4f9d5SDimitry Andric SourceLocation EllipsisLoc; 348755e4f9d5SDimitry Andric llvm::SmallVector<DeclaratorChunk::ParamInfo, 2> LocalParameters; 3488e8d8bef9SDimitry Andric ParseParameterDeclarationClause(DeclaratorContext::RequiresExpr, 348955e4f9d5SDimitry Andric FirstArgAttrs, LocalParameters, 349055e4f9d5SDimitry Andric EllipsisLoc); 349155e4f9d5SDimitry Andric if (EllipsisLoc.isValid()) 349255e4f9d5SDimitry Andric Diag(EllipsisLoc, diag::err_requires_expr_parameter_list_ellipsis); 349355e4f9d5SDimitry Andric for (auto &ParamInfo : LocalParameters) 349455e4f9d5SDimitry Andric LocalParameterDecls.push_back(cast<ParmVarDecl>(ParamInfo.Param)); 349555e4f9d5SDimitry Andric } 349655e4f9d5SDimitry Andric Parens.consumeClose(); 349755e4f9d5SDimitry Andric } 349855e4f9d5SDimitry Andric 349955e4f9d5SDimitry Andric BalancedDelimiterTracker Braces(*this, tok::l_brace); 350055e4f9d5SDimitry Andric if (Braces.expectAndConsume()) 350155e4f9d5SDimitry Andric return ExprError(); 350255e4f9d5SDimitry Andric 350355e4f9d5SDimitry Andric // Start of requirement list 350455e4f9d5SDimitry Andric llvm::SmallVector<concepts::Requirement *, 2> Requirements; 350555e4f9d5SDimitry Andric 350655e4f9d5SDimitry Andric // C++2a [expr.prim.req]p2 350755e4f9d5SDimitry Andric // Expressions appearing within a requirement-body are unevaluated operands. 350855e4f9d5SDimitry Andric EnterExpressionEvaluationContext Ctx( 350955e4f9d5SDimitry Andric Actions, Sema::ExpressionEvaluationContext::Unevaluated); 351055e4f9d5SDimitry Andric 351155e4f9d5SDimitry Andric ParseScope BodyScope(this, Scope::DeclScope); 3512*bdd1243dSDimitry Andric // Create a separate diagnostic pool for RequiresExprBodyDecl. 3513*bdd1243dSDimitry Andric // Dependent diagnostics are attached to this Decl and non-depenedent 3514*bdd1243dSDimitry Andric // diagnostics are surfaced after this parse. 3515*bdd1243dSDimitry Andric ParsingDeclRAIIObject ParsingBodyDecl(*this, ParsingDeclRAIIObject::NoParent); 351655e4f9d5SDimitry Andric RequiresExprBodyDecl *Body = Actions.ActOnStartRequiresExpr( 351755e4f9d5SDimitry Andric RequiresKWLoc, LocalParameterDecls, getCurScope()); 351855e4f9d5SDimitry Andric 351955e4f9d5SDimitry Andric if (Tok.is(tok::r_brace)) { 352055e4f9d5SDimitry Andric // Grammar does not allow an empty body. 352155e4f9d5SDimitry Andric // requirement-body: 352255e4f9d5SDimitry Andric // { requirement-seq } 352355e4f9d5SDimitry Andric // requirement-seq: 352455e4f9d5SDimitry Andric // requirement 352555e4f9d5SDimitry Andric // requirement-seq requirement 352655e4f9d5SDimitry Andric Diag(Tok, diag::err_empty_requires_expr); 352755e4f9d5SDimitry Andric // Continue anyway and produce a requires expr with no requirements. 352855e4f9d5SDimitry Andric } else { 352955e4f9d5SDimitry Andric while (!Tok.is(tok::r_brace)) { 353055e4f9d5SDimitry Andric switch (Tok.getKind()) { 353155e4f9d5SDimitry Andric case tok::l_brace: { 353255e4f9d5SDimitry Andric // Compound requirement 353355e4f9d5SDimitry Andric // C++ [expr.prim.req.compound] 353455e4f9d5SDimitry Andric // compound-requirement: 353555e4f9d5SDimitry Andric // '{' expression '}' 'noexcept'[opt] 353655e4f9d5SDimitry Andric // return-type-requirement[opt] ';' 353755e4f9d5SDimitry Andric // return-type-requirement: 353855e4f9d5SDimitry Andric // trailing-return-type 353955e4f9d5SDimitry Andric // '->' cv-qualifier-seq[opt] constrained-parameter 354055e4f9d5SDimitry Andric // cv-qualifier-seq[opt] abstract-declarator[opt] 354155e4f9d5SDimitry Andric BalancedDelimiterTracker ExprBraces(*this, tok::l_brace); 354255e4f9d5SDimitry Andric ExprBraces.consumeOpen(); 354355e4f9d5SDimitry Andric ExprResult Expression = 354455e4f9d5SDimitry Andric Actions.CorrectDelayedTyposInExpr(ParseExpression()); 354555e4f9d5SDimitry Andric if (!Expression.isUsable()) { 354655e4f9d5SDimitry Andric ExprBraces.skipToEnd(); 354755e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 354855e4f9d5SDimitry Andric break; 354955e4f9d5SDimitry Andric } 355055e4f9d5SDimitry Andric if (ExprBraces.consumeClose()) 355155e4f9d5SDimitry Andric ExprBraces.skipToEnd(); 355255e4f9d5SDimitry Andric 355355e4f9d5SDimitry Andric concepts::Requirement *Req = nullptr; 355455e4f9d5SDimitry Andric SourceLocation NoexceptLoc; 355555e4f9d5SDimitry Andric TryConsumeToken(tok::kw_noexcept, NoexceptLoc); 355655e4f9d5SDimitry Andric if (Tok.is(tok::semi)) { 355755e4f9d5SDimitry Andric Req = Actions.ActOnCompoundRequirement(Expression.get(), NoexceptLoc); 355855e4f9d5SDimitry Andric if (Req) 355955e4f9d5SDimitry Andric Requirements.push_back(Req); 356055e4f9d5SDimitry Andric break; 356155e4f9d5SDimitry Andric } 356255e4f9d5SDimitry Andric if (!TryConsumeToken(tok::arrow)) 356355e4f9d5SDimitry Andric // User probably forgot the arrow, remind them and try to continue. 356455e4f9d5SDimitry Andric Diag(Tok, diag::err_requires_expr_missing_arrow) 356555e4f9d5SDimitry Andric << FixItHint::CreateInsertion(Tok.getLocation(), "->"); 356655e4f9d5SDimitry Andric // Try to parse a 'type-constraint' 356755e4f9d5SDimitry Andric if (TryAnnotateTypeConstraint()) { 356855e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 356955e4f9d5SDimitry Andric break; 357055e4f9d5SDimitry Andric } 357155e4f9d5SDimitry Andric if (!isTypeConstraintAnnotation()) { 357255e4f9d5SDimitry Andric Diag(Tok, diag::err_requires_expr_expected_type_constraint); 357355e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 357455e4f9d5SDimitry Andric break; 357555e4f9d5SDimitry Andric } 357613138422SDimitry Andric CXXScopeSpec SS; 357713138422SDimitry Andric if (Tok.is(tok::annot_cxxscope)) { 357813138422SDimitry Andric Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), 357913138422SDimitry Andric Tok.getAnnotationRange(), 358013138422SDimitry Andric SS); 358155e4f9d5SDimitry Andric ConsumeAnnotationToken(); 358213138422SDimitry Andric } 358355e4f9d5SDimitry Andric 358455e4f9d5SDimitry Andric Req = Actions.ActOnCompoundRequirement( 358555e4f9d5SDimitry Andric Expression.get(), NoexceptLoc, SS, takeTemplateIdAnnotation(Tok), 358655e4f9d5SDimitry Andric TemplateParameterDepth); 358755e4f9d5SDimitry Andric ConsumeAnnotationToken(); 358855e4f9d5SDimitry Andric if (Req) 358955e4f9d5SDimitry Andric Requirements.push_back(Req); 359055e4f9d5SDimitry Andric break; 359155e4f9d5SDimitry Andric } 359255e4f9d5SDimitry Andric default: { 359355e4f9d5SDimitry Andric bool PossibleRequiresExprInSimpleRequirement = false; 359455e4f9d5SDimitry Andric if (Tok.is(tok::kw_requires)) { 359555e4f9d5SDimitry Andric auto IsNestedRequirement = [&] { 359655e4f9d5SDimitry Andric RevertingTentativeParsingAction TPA(*this); 359755e4f9d5SDimitry Andric ConsumeToken(); // 'requires' 359855e4f9d5SDimitry Andric if (Tok.is(tok::l_brace)) 359955e4f9d5SDimitry Andric // This is a requires expression 360055e4f9d5SDimitry Andric // requires (T t) { 360155e4f9d5SDimitry Andric // requires { t++; }; 360255e4f9d5SDimitry Andric // ... ^ 360355e4f9d5SDimitry Andric // } 360455e4f9d5SDimitry Andric return false; 360555e4f9d5SDimitry Andric if (Tok.is(tok::l_paren)) { 360655e4f9d5SDimitry Andric // This might be the parameter list of a requires expression 360755e4f9d5SDimitry Andric ConsumeParen(); 360855e4f9d5SDimitry Andric auto Res = TryParseParameterDeclarationClause(); 360955e4f9d5SDimitry Andric if (Res != TPResult::False) { 361055e4f9d5SDimitry Andric // Skip to the closing parenthesis 361155e4f9d5SDimitry Andric // FIXME: Don't traverse these tokens twice (here and in 361255e4f9d5SDimitry Andric // TryParseParameterDeclarationClause). 361355e4f9d5SDimitry Andric unsigned Depth = 1; 361455e4f9d5SDimitry Andric while (Depth != 0) { 361555e4f9d5SDimitry Andric if (Tok.is(tok::l_paren)) 361655e4f9d5SDimitry Andric Depth++; 361755e4f9d5SDimitry Andric else if (Tok.is(tok::r_paren)) 361855e4f9d5SDimitry Andric Depth--; 361955e4f9d5SDimitry Andric ConsumeAnyToken(); 362055e4f9d5SDimitry Andric } 362155e4f9d5SDimitry Andric // requires (T t) { 362255e4f9d5SDimitry Andric // requires () ? 362355e4f9d5SDimitry Andric // ... ^ 362455e4f9d5SDimitry Andric // - OR - 362555e4f9d5SDimitry Andric // requires (int x) ? 362655e4f9d5SDimitry Andric // ... ^ 362755e4f9d5SDimitry Andric // } 362855e4f9d5SDimitry Andric if (Tok.is(tok::l_brace)) 362955e4f9d5SDimitry Andric // requires (...) { 363055e4f9d5SDimitry Andric // ^ - a requires expression as a 363155e4f9d5SDimitry Andric // simple-requirement. 363255e4f9d5SDimitry Andric return false; 363355e4f9d5SDimitry Andric } 363455e4f9d5SDimitry Andric } 363555e4f9d5SDimitry Andric return true; 363655e4f9d5SDimitry Andric }; 363755e4f9d5SDimitry Andric if (IsNestedRequirement()) { 363855e4f9d5SDimitry Andric ConsumeToken(); 363955e4f9d5SDimitry Andric // Nested requirement 364055e4f9d5SDimitry Andric // C++ [expr.prim.req.nested] 364155e4f9d5SDimitry Andric // nested-requirement: 364255e4f9d5SDimitry Andric // 'requires' constraint-expression ';' 364355e4f9d5SDimitry Andric ExprResult ConstraintExpr = 364455e4f9d5SDimitry Andric Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression()); 364555e4f9d5SDimitry Andric if (ConstraintExpr.isInvalid() || !ConstraintExpr.isUsable()) { 364655e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, 364755e4f9d5SDimitry Andric SkipUntilFlags::StopBeforeMatch); 364855e4f9d5SDimitry Andric break; 364955e4f9d5SDimitry Andric } 365055e4f9d5SDimitry Andric if (auto *Req = 365155e4f9d5SDimitry Andric Actions.ActOnNestedRequirement(ConstraintExpr.get())) 365255e4f9d5SDimitry Andric Requirements.push_back(Req); 365355e4f9d5SDimitry Andric else { 365455e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, 365555e4f9d5SDimitry Andric SkipUntilFlags::StopBeforeMatch); 365655e4f9d5SDimitry Andric break; 365755e4f9d5SDimitry Andric } 365855e4f9d5SDimitry Andric break; 365955e4f9d5SDimitry Andric } else 366055e4f9d5SDimitry Andric PossibleRequiresExprInSimpleRequirement = true; 366155e4f9d5SDimitry Andric } else if (Tok.is(tok::kw_typename)) { 366255e4f9d5SDimitry Andric // This might be 'typename T::value_type;' (a type requirement) or 366355e4f9d5SDimitry Andric // 'typename T::value_type{};' (a simple requirement). 366455e4f9d5SDimitry Andric TentativeParsingAction TPA(*this); 366555e4f9d5SDimitry Andric 366655e4f9d5SDimitry Andric // We need to consume the typename to allow 'requires { typename a; }' 366755e4f9d5SDimitry Andric SourceLocation TypenameKWLoc = ConsumeToken(); 366804eeddc0SDimitry Andric if (TryAnnotateOptionalCXXScopeToken()) { 366913138422SDimitry Andric TPA.Commit(); 367055e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 367155e4f9d5SDimitry Andric break; 367255e4f9d5SDimitry Andric } 367355e4f9d5SDimitry Andric CXXScopeSpec SS; 367455e4f9d5SDimitry Andric if (Tok.is(tok::annot_cxxscope)) { 367555e4f9d5SDimitry Andric Actions.RestoreNestedNameSpecifierAnnotation( 367655e4f9d5SDimitry Andric Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); 367755e4f9d5SDimitry Andric ConsumeAnnotationToken(); 367855e4f9d5SDimitry Andric } 367955e4f9d5SDimitry Andric 368055e4f9d5SDimitry Andric if (Tok.isOneOf(tok::identifier, tok::annot_template_id) && 368155e4f9d5SDimitry Andric !NextToken().isOneOf(tok::l_brace, tok::l_paren)) { 368255e4f9d5SDimitry Andric TPA.Commit(); 368355e4f9d5SDimitry Andric SourceLocation NameLoc = Tok.getLocation(); 368455e4f9d5SDimitry Andric IdentifierInfo *II = nullptr; 368555e4f9d5SDimitry Andric TemplateIdAnnotation *TemplateId = nullptr; 368655e4f9d5SDimitry Andric if (Tok.is(tok::identifier)) { 368755e4f9d5SDimitry Andric II = Tok.getIdentifierInfo(); 368855e4f9d5SDimitry Andric ConsumeToken(); 368955e4f9d5SDimitry Andric } else { 369055e4f9d5SDimitry Andric TemplateId = takeTemplateIdAnnotation(Tok); 369155e4f9d5SDimitry Andric ConsumeAnnotationToken(); 36925ffd83dbSDimitry Andric if (TemplateId->isInvalid()) 36935ffd83dbSDimitry Andric break; 369455e4f9d5SDimitry Andric } 369555e4f9d5SDimitry Andric 369655e4f9d5SDimitry Andric if (auto *Req = Actions.ActOnTypeRequirement(TypenameKWLoc, SS, 369755e4f9d5SDimitry Andric NameLoc, II, 369855e4f9d5SDimitry Andric TemplateId)) { 369955e4f9d5SDimitry Andric Requirements.push_back(Req); 370055e4f9d5SDimitry Andric } 370155e4f9d5SDimitry Andric break; 370255e4f9d5SDimitry Andric } 370355e4f9d5SDimitry Andric TPA.Revert(); 370455e4f9d5SDimitry Andric } 370555e4f9d5SDimitry Andric // Simple requirement 370655e4f9d5SDimitry Andric // C++ [expr.prim.req.simple] 370755e4f9d5SDimitry Andric // simple-requirement: 370855e4f9d5SDimitry Andric // expression ';' 370955e4f9d5SDimitry Andric SourceLocation StartLoc = Tok.getLocation(); 371055e4f9d5SDimitry Andric ExprResult Expression = 371155e4f9d5SDimitry Andric Actions.CorrectDelayedTyposInExpr(ParseExpression()); 371255e4f9d5SDimitry Andric if (!Expression.isUsable()) { 371355e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 371455e4f9d5SDimitry Andric break; 371555e4f9d5SDimitry Andric } 371655e4f9d5SDimitry Andric if (!Expression.isInvalid() && PossibleRequiresExprInSimpleRequirement) 3717349cc55cSDimitry Andric Diag(StartLoc, diag::err_requires_expr_in_simple_requirement) 371855e4f9d5SDimitry Andric << FixItHint::CreateInsertion(StartLoc, "requires"); 371955e4f9d5SDimitry Andric if (auto *Req = Actions.ActOnSimpleRequirement(Expression.get())) 372055e4f9d5SDimitry Andric Requirements.push_back(Req); 372155e4f9d5SDimitry Andric else { 372255e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 372355e4f9d5SDimitry Andric break; 372455e4f9d5SDimitry Andric } 372555e4f9d5SDimitry Andric // User may have tried to put some compound requirement stuff here 372655e4f9d5SDimitry Andric if (Tok.is(tok::kw_noexcept)) { 372755e4f9d5SDimitry Andric Diag(Tok, diag::err_requires_expr_simple_requirement_noexcept) 372855e4f9d5SDimitry Andric << FixItHint::CreateInsertion(StartLoc, "{") 372955e4f9d5SDimitry Andric << FixItHint::CreateInsertion(Tok.getLocation(), "}"); 373055e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 373155e4f9d5SDimitry Andric break; 373255e4f9d5SDimitry Andric } 373355e4f9d5SDimitry Andric break; 373455e4f9d5SDimitry Andric } 373555e4f9d5SDimitry Andric } 373655e4f9d5SDimitry Andric if (ExpectAndConsumeSemi(diag::err_expected_semi_requirement)) { 373755e4f9d5SDimitry Andric SkipUntil(tok::semi, tok::r_brace, SkipUntilFlags::StopBeforeMatch); 373855e4f9d5SDimitry Andric TryConsumeToken(tok::semi); 373955e4f9d5SDimitry Andric break; 374055e4f9d5SDimitry Andric } 374155e4f9d5SDimitry Andric } 374255e4f9d5SDimitry Andric if (Requirements.empty()) { 374355e4f9d5SDimitry Andric // Don't emit an empty requires expr here to avoid confusing the user with 374455e4f9d5SDimitry Andric // other diagnostics quoting an empty requires expression they never 374555e4f9d5SDimitry Andric // wrote. 374655e4f9d5SDimitry Andric Braces.consumeClose(); 374755e4f9d5SDimitry Andric Actions.ActOnFinishRequiresExpr(); 374855e4f9d5SDimitry Andric return ExprError(); 374955e4f9d5SDimitry Andric } 375055e4f9d5SDimitry Andric } 375155e4f9d5SDimitry Andric Braces.consumeClose(); 375255e4f9d5SDimitry Andric Actions.ActOnFinishRequiresExpr(); 3753*bdd1243dSDimitry Andric ParsingBodyDecl.complete(Body); 375455e4f9d5SDimitry Andric return Actions.ActOnRequiresExpr(RequiresKWLoc, Body, LocalParameterDecls, 375555e4f9d5SDimitry Andric Requirements, Braces.getCloseLocation()); 375655e4f9d5SDimitry Andric } 375755e4f9d5SDimitry Andric 37580b57cec5SDimitry Andric static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) { 37590b57cec5SDimitry Andric switch (kind) { 37600b57cec5SDimitry Andric default: llvm_unreachable("Not a known type trait"); 37610b57cec5SDimitry Andric #define TYPE_TRAIT_1(Spelling, Name, Key) \ 37620b57cec5SDimitry Andric case tok::kw_ ## Spelling: return UTT_ ## Name; 37630b57cec5SDimitry Andric #define TYPE_TRAIT_2(Spelling, Name, Key) \ 37640b57cec5SDimitry Andric case tok::kw_ ## Spelling: return BTT_ ## Name; 37650b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.def" 37660b57cec5SDimitry Andric #define TYPE_TRAIT_N(Spelling, Name, Key) \ 37670b57cec5SDimitry Andric case tok::kw_ ## Spelling: return TT_ ## Name; 37680b57cec5SDimitry Andric #include "clang/Basic/TokenKinds.def" 37690b57cec5SDimitry Andric } 37700b57cec5SDimitry Andric } 37710b57cec5SDimitry Andric 37720b57cec5SDimitry Andric static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) { 37730b57cec5SDimitry Andric switch (kind) { 37745ffd83dbSDimitry Andric default: 37755ffd83dbSDimitry Andric llvm_unreachable("Not a known array type trait"); 37765ffd83dbSDimitry Andric #define ARRAY_TYPE_TRAIT(Spelling, Name, Key) \ 37775ffd83dbSDimitry Andric case tok::kw_##Spelling: \ 37785ffd83dbSDimitry Andric return ATT_##Name; 37795ffd83dbSDimitry Andric #include "clang/Basic/TokenKinds.def" 37800b57cec5SDimitry Andric } 37810b57cec5SDimitry Andric } 37820b57cec5SDimitry Andric 37830b57cec5SDimitry Andric static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) { 37840b57cec5SDimitry Andric switch (kind) { 37855ffd83dbSDimitry Andric default: 37865ffd83dbSDimitry Andric llvm_unreachable("Not a known unary expression trait."); 37875ffd83dbSDimitry Andric #define EXPRESSION_TRAIT(Spelling, Name, Key) \ 37885ffd83dbSDimitry Andric case tok::kw_##Spelling: \ 37895ffd83dbSDimitry Andric return ET_##Name; 37905ffd83dbSDimitry Andric #include "clang/Basic/TokenKinds.def" 37910b57cec5SDimitry Andric } 37920b57cec5SDimitry Andric } 37930b57cec5SDimitry Andric 37940b57cec5SDimitry Andric /// Parse the built-in type-trait pseudo-functions that allow 37950b57cec5SDimitry Andric /// implementation of the TR1/C++11 type traits templates. 37960b57cec5SDimitry Andric /// 37970b57cec5SDimitry Andric /// primary-expression: 37980b57cec5SDimitry Andric /// unary-type-trait '(' type-id ')' 37990b57cec5SDimitry Andric /// binary-type-trait '(' type-id ',' type-id ')' 38000b57cec5SDimitry Andric /// type-trait '(' type-id-seq ')' 38010b57cec5SDimitry Andric /// 38020b57cec5SDimitry Andric /// type-id-seq: 38030b57cec5SDimitry Andric /// type-id ...[opt] type-id-seq[opt] 38040b57cec5SDimitry Andric /// 38050b57cec5SDimitry Andric ExprResult Parser::ParseTypeTrait() { 38060b57cec5SDimitry Andric tok::TokenKind Kind = Tok.getKind(); 38070b57cec5SDimitry Andric 38080b57cec5SDimitry Andric SourceLocation Loc = ConsumeToken(); 38090b57cec5SDimitry Andric 38100b57cec5SDimitry Andric BalancedDelimiterTracker Parens(*this, tok::l_paren); 38110b57cec5SDimitry Andric if (Parens.expectAndConsume()) 38120b57cec5SDimitry Andric return ExprError(); 38130b57cec5SDimitry Andric 38140b57cec5SDimitry Andric SmallVector<ParsedType, 2> Args; 38150b57cec5SDimitry Andric do { 38160b57cec5SDimitry Andric // Parse the next type. 38170b57cec5SDimitry Andric TypeResult Ty = ParseTypeName(); 38180b57cec5SDimitry Andric if (Ty.isInvalid()) { 38190b57cec5SDimitry Andric Parens.skipToEnd(); 38200b57cec5SDimitry Andric return ExprError(); 38210b57cec5SDimitry Andric } 38220b57cec5SDimitry Andric 38230b57cec5SDimitry Andric // Parse the ellipsis, if present. 38240b57cec5SDimitry Andric if (Tok.is(tok::ellipsis)) { 38250b57cec5SDimitry Andric Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken()); 38260b57cec5SDimitry Andric if (Ty.isInvalid()) { 38270b57cec5SDimitry Andric Parens.skipToEnd(); 38280b57cec5SDimitry Andric return ExprError(); 38290b57cec5SDimitry Andric } 38300b57cec5SDimitry Andric } 38310b57cec5SDimitry Andric 38320b57cec5SDimitry Andric // Add this type to the list of arguments. 38330b57cec5SDimitry Andric Args.push_back(Ty.get()); 38340b57cec5SDimitry Andric } while (TryConsumeToken(tok::comma)); 38350b57cec5SDimitry Andric 38360b57cec5SDimitry Andric if (Parens.consumeClose()) 38370b57cec5SDimitry Andric return ExprError(); 38380b57cec5SDimitry Andric 38390b57cec5SDimitry Andric SourceLocation EndLoc = Parens.getCloseLocation(); 38400b57cec5SDimitry Andric 38410b57cec5SDimitry Andric return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc); 38420b57cec5SDimitry Andric } 38430b57cec5SDimitry Andric 38440b57cec5SDimitry Andric /// ParseArrayTypeTrait - Parse the built-in array type-trait 38450b57cec5SDimitry Andric /// pseudo-functions. 38460b57cec5SDimitry Andric /// 38470b57cec5SDimitry Andric /// primary-expression: 38480b57cec5SDimitry Andric /// [Embarcadero] '__array_rank' '(' type-id ')' 38490b57cec5SDimitry Andric /// [Embarcadero] '__array_extent' '(' type-id ',' expression ')' 38500b57cec5SDimitry Andric /// 38510b57cec5SDimitry Andric ExprResult Parser::ParseArrayTypeTrait() { 38520b57cec5SDimitry Andric ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind()); 38530b57cec5SDimitry Andric SourceLocation Loc = ConsumeToken(); 38540b57cec5SDimitry Andric 38550b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 38560b57cec5SDimitry Andric if (T.expectAndConsume()) 38570b57cec5SDimitry Andric return ExprError(); 38580b57cec5SDimitry Andric 38590b57cec5SDimitry Andric TypeResult Ty = ParseTypeName(); 38600b57cec5SDimitry Andric if (Ty.isInvalid()) { 38610b57cec5SDimitry Andric SkipUntil(tok::comma, StopAtSemi); 38620b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 38630b57cec5SDimitry Andric return ExprError(); 38640b57cec5SDimitry Andric } 38650b57cec5SDimitry Andric 38660b57cec5SDimitry Andric switch (ATT) { 38670b57cec5SDimitry Andric case ATT_ArrayRank: { 38680b57cec5SDimitry Andric T.consumeClose(); 38690b57cec5SDimitry Andric return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), nullptr, 38700b57cec5SDimitry Andric T.getCloseLocation()); 38710b57cec5SDimitry Andric } 38720b57cec5SDimitry Andric case ATT_ArrayExtent: { 38730b57cec5SDimitry Andric if (ExpectAndConsume(tok::comma)) { 38740b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 38750b57cec5SDimitry Andric return ExprError(); 38760b57cec5SDimitry Andric } 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric ExprResult DimExpr = ParseExpression(); 38790b57cec5SDimitry Andric T.consumeClose(); 38800b57cec5SDimitry Andric 38810b57cec5SDimitry Andric return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), 38820b57cec5SDimitry Andric T.getCloseLocation()); 38830b57cec5SDimitry Andric } 38840b57cec5SDimitry Andric } 38850b57cec5SDimitry Andric llvm_unreachable("Invalid ArrayTypeTrait!"); 38860b57cec5SDimitry Andric } 38870b57cec5SDimitry Andric 38880b57cec5SDimitry Andric /// ParseExpressionTrait - Parse built-in expression-trait 38890b57cec5SDimitry Andric /// pseudo-functions like __is_lvalue_expr( xxx ). 38900b57cec5SDimitry Andric /// 38910b57cec5SDimitry Andric /// primary-expression: 38920b57cec5SDimitry Andric /// [Embarcadero] expression-trait '(' expression ')' 38930b57cec5SDimitry Andric /// 38940b57cec5SDimitry Andric ExprResult Parser::ParseExpressionTrait() { 38950b57cec5SDimitry Andric ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind()); 38960b57cec5SDimitry Andric SourceLocation Loc = ConsumeToken(); 38970b57cec5SDimitry Andric 38980b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 38990b57cec5SDimitry Andric if (T.expectAndConsume()) 39000b57cec5SDimitry Andric return ExprError(); 39010b57cec5SDimitry Andric 39020b57cec5SDimitry Andric ExprResult Expr = ParseExpression(); 39030b57cec5SDimitry Andric 39040b57cec5SDimitry Andric T.consumeClose(); 39050b57cec5SDimitry Andric 39060b57cec5SDimitry Andric return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), 39070b57cec5SDimitry Andric T.getCloseLocation()); 39080b57cec5SDimitry Andric } 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric 39110b57cec5SDimitry Andric /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a 39120b57cec5SDimitry Andric /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate 39130b57cec5SDimitry Andric /// based on the context past the parens. 39140b57cec5SDimitry Andric ExprResult 39150b57cec5SDimitry Andric Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, 39160b57cec5SDimitry Andric ParsedType &CastTy, 39170b57cec5SDimitry Andric BalancedDelimiterTracker &Tracker, 39180b57cec5SDimitry Andric ColonProtectionRAIIObject &ColonProt) { 39190b57cec5SDimitry Andric assert(getLangOpts().CPlusPlus && "Should only be called for C++!"); 39200b57cec5SDimitry Andric assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); 39210b57cec5SDimitry Andric assert(isTypeIdInParens() && "Not a type-id!"); 39220b57cec5SDimitry Andric 39230b57cec5SDimitry Andric ExprResult Result(true); 39240b57cec5SDimitry Andric CastTy = nullptr; 39250b57cec5SDimitry Andric 39260b57cec5SDimitry Andric // We need to disambiguate a very ugly part of the C++ syntax: 39270b57cec5SDimitry Andric // 39280b57cec5SDimitry Andric // (T())x; - type-id 39290b57cec5SDimitry Andric // (T())*x; - type-id 39300b57cec5SDimitry Andric // (T())/x; - expression 39310b57cec5SDimitry Andric // (T()); - expression 39320b57cec5SDimitry Andric // 39330b57cec5SDimitry Andric // The bad news is that we cannot use the specialized tentative parser, since 39340b57cec5SDimitry Andric // it can only verify that the thing inside the parens can be parsed as 39350b57cec5SDimitry Andric // type-id, it is not useful for determining the context past the parens. 39360b57cec5SDimitry Andric // 39370b57cec5SDimitry Andric // The good news is that the parser can disambiguate this part without 39380b57cec5SDimitry Andric // making any unnecessary Action calls. 39390b57cec5SDimitry Andric // 39400b57cec5SDimitry Andric // It uses a scheme similar to parsing inline methods. The parenthesized 39410b57cec5SDimitry Andric // tokens are cached, the context that follows is determined (possibly by 39420b57cec5SDimitry Andric // parsing a cast-expression), and then we re-introduce the cached tokens 39430b57cec5SDimitry Andric // into the token stream and parse them appropriately. 39440b57cec5SDimitry Andric 39450b57cec5SDimitry Andric ParenParseOption ParseAs; 39460b57cec5SDimitry Andric CachedTokens Toks; 39470b57cec5SDimitry Andric 39480b57cec5SDimitry Andric // Store the tokens of the parentheses. We will parse them after we determine 39490b57cec5SDimitry Andric // the context that follows them. 39500b57cec5SDimitry Andric if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) { 39510b57cec5SDimitry Andric // We didn't find the ')' we expected. 39520b57cec5SDimitry Andric Tracker.consumeClose(); 39530b57cec5SDimitry Andric return ExprError(); 39540b57cec5SDimitry Andric } 39550b57cec5SDimitry Andric 39560b57cec5SDimitry Andric if (Tok.is(tok::l_brace)) { 39570b57cec5SDimitry Andric ParseAs = CompoundLiteral; 39580b57cec5SDimitry Andric } else { 39590b57cec5SDimitry Andric bool NotCastExpr; 39600b57cec5SDimitry Andric if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { 39610b57cec5SDimitry Andric NotCastExpr = true; 39620b57cec5SDimitry Andric } else { 39630b57cec5SDimitry Andric // Try parsing the cast-expression that may follow. 39640b57cec5SDimitry Andric // If it is not a cast-expression, NotCastExpr will be true and no token 39650b57cec5SDimitry Andric // will be consumed. 39660b57cec5SDimitry Andric ColonProt.restore(); 3967480093f4SDimitry Andric Result = ParseCastExpression(AnyCastExpr, 39680b57cec5SDimitry Andric false/*isAddressofOperand*/, 39690b57cec5SDimitry Andric NotCastExpr, 39700b57cec5SDimitry Andric // type-id has priority. 39710b57cec5SDimitry Andric IsTypeCast); 39720b57cec5SDimitry Andric } 39730b57cec5SDimitry Andric 39740b57cec5SDimitry Andric // If we parsed a cast-expression, it's really a type-id, otherwise it's 39750b57cec5SDimitry Andric // an expression. 39760b57cec5SDimitry Andric ParseAs = NotCastExpr ? SimpleExpr : CastExpr; 39770b57cec5SDimitry Andric } 39780b57cec5SDimitry Andric 39790b57cec5SDimitry Andric // Create a fake EOF to mark end of Toks buffer. 39800b57cec5SDimitry Andric Token AttrEnd; 39810b57cec5SDimitry Andric AttrEnd.startToken(); 39820b57cec5SDimitry Andric AttrEnd.setKind(tok::eof); 39830b57cec5SDimitry Andric AttrEnd.setLocation(Tok.getLocation()); 39840b57cec5SDimitry Andric AttrEnd.setEofData(Toks.data()); 39850b57cec5SDimitry Andric Toks.push_back(AttrEnd); 39860b57cec5SDimitry Andric 39870b57cec5SDimitry Andric // The current token should go after the cached tokens. 39880b57cec5SDimitry Andric Toks.push_back(Tok); 39890b57cec5SDimitry Andric // Re-enter the stored parenthesized tokens into the token stream, so we may 39900b57cec5SDimitry Andric // parse them now. 39910b57cec5SDimitry Andric PP.EnterTokenStream(Toks, /*DisableMacroExpansion*/ true, 39920b57cec5SDimitry Andric /*IsReinject*/ true); 39930b57cec5SDimitry Andric // Drop the current token and bring the first cached one. It's the same token 39940b57cec5SDimitry Andric // as when we entered this function. 39950b57cec5SDimitry Andric ConsumeAnyToken(); 39960b57cec5SDimitry Andric 39970b57cec5SDimitry Andric if (ParseAs >= CompoundLiteral) { 39980b57cec5SDimitry Andric // Parse the type declarator. 39990b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 400081ad6265SDimitry Andric Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 400181ad6265SDimitry Andric DeclaratorContext::TypeName); 40020b57cec5SDimitry Andric { 40030b57cec5SDimitry Andric ColonProtectionRAIIObject InnerColonProtection(*this); 40040b57cec5SDimitry Andric ParseSpecifierQualifierList(DS); 40050b57cec5SDimitry Andric ParseDeclarator(DeclaratorInfo); 40060b57cec5SDimitry Andric } 40070b57cec5SDimitry Andric 40080b57cec5SDimitry Andric // Match the ')'. 40090b57cec5SDimitry Andric Tracker.consumeClose(); 40100b57cec5SDimitry Andric ColonProt.restore(); 40110b57cec5SDimitry Andric 40120b57cec5SDimitry Andric // Consume EOF marker for Toks buffer. 40130b57cec5SDimitry Andric assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()); 40140b57cec5SDimitry Andric ConsumeAnyToken(); 40150b57cec5SDimitry Andric 40160b57cec5SDimitry Andric if (ParseAs == CompoundLiteral) { 40170b57cec5SDimitry Andric ExprType = CompoundLiteral; 40180b57cec5SDimitry Andric if (DeclaratorInfo.isInvalidType()) 40190b57cec5SDimitry Andric return ExprError(); 40200b57cec5SDimitry Andric 40210b57cec5SDimitry Andric TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); 40220b57cec5SDimitry Andric return ParseCompoundLiteralExpression(Ty.get(), 40230b57cec5SDimitry Andric Tracker.getOpenLocation(), 40240b57cec5SDimitry Andric Tracker.getCloseLocation()); 40250b57cec5SDimitry Andric } 40260b57cec5SDimitry Andric 40270b57cec5SDimitry Andric // We parsed '(' type-id ')' and the thing after it wasn't a '{'. 40280b57cec5SDimitry Andric assert(ParseAs == CastExpr); 40290b57cec5SDimitry Andric 40300b57cec5SDimitry Andric if (DeclaratorInfo.isInvalidType()) 40310b57cec5SDimitry Andric return ExprError(); 40320b57cec5SDimitry Andric 40330b57cec5SDimitry Andric // Result is what ParseCastExpression returned earlier. 40340b57cec5SDimitry Andric if (!Result.isInvalid()) 40350b57cec5SDimitry Andric Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(), 40360b57cec5SDimitry Andric DeclaratorInfo, CastTy, 40370b57cec5SDimitry Andric Tracker.getCloseLocation(), Result.get()); 40380b57cec5SDimitry Andric return Result; 40390b57cec5SDimitry Andric } 40400b57cec5SDimitry Andric 40410b57cec5SDimitry Andric // Not a compound literal, and not followed by a cast-expression. 40420b57cec5SDimitry Andric assert(ParseAs == SimpleExpr); 40430b57cec5SDimitry Andric 40440b57cec5SDimitry Andric ExprType = SimpleExpr; 40450b57cec5SDimitry Andric Result = ParseExpression(); 40460b57cec5SDimitry Andric if (!Result.isInvalid() && Tok.is(tok::r_paren)) 40470b57cec5SDimitry Andric Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(), 40480b57cec5SDimitry Andric Tok.getLocation(), Result.get()); 40490b57cec5SDimitry Andric 40500b57cec5SDimitry Andric // Match the ')'. 40510b57cec5SDimitry Andric if (Result.isInvalid()) { 40520b57cec5SDimitry Andric while (Tok.isNot(tok::eof)) 40530b57cec5SDimitry Andric ConsumeAnyToken(); 40540b57cec5SDimitry Andric assert(Tok.getEofData() == AttrEnd.getEofData()); 40550b57cec5SDimitry Andric ConsumeAnyToken(); 40560b57cec5SDimitry Andric return ExprError(); 40570b57cec5SDimitry Andric } 40580b57cec5SDimitry Andric 40590b57cec5SDimitry Andric Tracker.consumeClose(); 40600b57cec5SDimitry Andric // Consume EOF marker for Toks buffer. 40610b57cec5SDimitry Andric assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()); 40620b57cec5SDimitry Andric ConsumeAnyToken(); 40630b57cec5SDimitry Andric return Result; 40640b57cec5SDimitry Andric } 40650b57cec5SDimitry Andric 40660b57cec5SDimitry Andric /// Parse a __builtin_bit_cast(T, E). 40670b57cec5SDimitry Andric ExprResult Parser::ParseBuiltinBitCast() { 40680b57cec5SDimitry Andric SourceLocation KWLoc = ConsumeToken(); 40690b57cec5SDimitry Andric 40700b57cec5SDimitry Andric BalancedDelimiterTracker T(*this, tok::l_paren); 40710b57cec5SDimitry Andric if (T.expectAndConsume(diag::err_expected_lparen_after, "__builtin_bit_cast")) 40720b57cec5SDimitry Andric return ExprError(); 40730b57cec5SDimitry Andric 40740b57cec5SDimitry Andric // Parse the common declaration-specifiers piece. 40750b57cec5SDimitry Andric DeclSpec DS(AttrFactory); 40760b57cec5SDimitry Andric ParseSpecifierQualifierList(DS); 40770b57cec5SDimitry Andric 40780b57cec5SDimitry Andric // Parse the abstract-declarator, if present. 407981ad6265SDimitry Andric Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 408081ad6265SDimitry Andric DeclaratorContext::TypeName); 40810b57cec5SDimitry Andric ParseDeclarator(DeclaratorInfo); 40820b57cec5SDimitry Andric 40830b57cec5SDimitry Andric if (ExpectAndConsume(tok::comma)) { 40840b57cec5SDimitry Andric Diag(Tok.getLocation(), diag::err_expected) << tok::comma; 40850b57cec5SDimitry Andric SkipUntil(tok::r_paren, StopAtSemi); 40860b57cec5SDimitry Andric return ExprError(); 40870b57cec5SDimitry Andric } 40880b57cec5SDimitry Andric 40890b57cec5SDimitry Andric ExprResult Operand = ParseExpression(); 40900b57cec5SDimitry Andric 40910b57cec5SDimitry Andric if (T.consumeClose()) 40920b57cec5SDimitry Andric return ExprError(); 40930b57cec5SDimitry Andric 40940b57cec5SDimitry Andric if (Operand.isInvalid() || DeclaratorInfo.isInvalidType()) 40950b57cec5SDimitry Andric return ExprError(); 40960b57cec5SDimitry Andric 40970b57cec5SDimitry Andric return Actions.ActOnBuiltinBitCastExpr(KWLoc, DeclaratorInfo, Operand, 40980b57cec5SDimitry Andric T.getCloseLocation()); 40990b57cec5SDimitry Andric } 4100