17330f729Sjoerg //===--- ParseTemplate.cpp - Template Parsing -----------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements parsing of C++ templates.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/AST/ASTContext.h"
147330f729Sjoerg #include "clang/AST/DeclTemplate.h"
15*e038c9c4Sjoerg #include "clang/AST/ExprCXX.h"
167330f729Sjoerg #include "clang/Parse/ParseDiagnostic.h"
177330f729Sjoerg #include "clang/Parse/Parser.h"
187330f729Sjoerg #include "clang/Parse/RAIIObjectsForParser.h"
197330f729Sjoerg #include "clang/Sema/DeclSpec.h"
207330f729Sjoerg #include "clang/Sema/ParsedTemplate.h"
217330f729Sjoerg #include "clang/Sema/Scope.h"
227330f729Sjoerg #include "llvm/Support/TimeProfiler.h"
237330f729Sjoerg using namespace clang;
247330f729Sjoerg
25*e038c9c4Sjoerg /// Re-enter a possible template scope, creating as many template parameter
26*e038c9c4Sjoerg /// scopes as necessary.
27*e038c9c4Sjoerg /// \return The number of template parameter scopes entered.
ReenterTemplateScopes(MultiParseScope & S,Decl * D)28*e038c9c4Sjoerg unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) {
29*e038c9c4Sjoerg return Actions.ActOnReenterTemplateScope(D, [&] {
30*e038c9c4Sjoerg S.Enter(Scope::TemplateParamScope);
31*e038c9c4Sjoerg return Actions.getCurScope();
32*e038c9c4Sjoerg });
33*e038c9c4Sjoerg }
34*e038c9c4Sjoerg
357330f729Sjoerg /// Parse a template declaration, explicit instantiation, or
367330f729Sjoerg /// explicit specialization.
ParseDeclarationStartingWithTemplate(DeclaratorContext Context,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)377330f729Sjoerg Decl *Parser::ParseDeclarationStartingWithTemplate(
387330f729Sjoerg DeclaratorContext Context, SourceLocation &DeclEnd,
397330f729Sjoerg ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
407330f729Sjoerg ObjCDeclContextSwitch ObjCDC(*this);
417330f729Sjoerg
427330f729Sjoerg if (Tok.is(tok::kw_template) && NextToken().isNot(tok::less)) {
437330f729Sjoerg return ParseExplicitInstantiation(Context, SourceLocation(), ConsumeToken(),
447330f729Sjoerg DeclEnd, AccessAttrs, AS);
457330f729Sjoerg }
467330f729Sjoerg return ParseTemplateDeclarationOrSpecialization(Context, DeclEnd, AccessAttrs,
477330f729Sjoerg AS);
487330f729Sjoerg }
497330f729Sjoerg
507330f729Sjoerg /// Parse a template declaration or an explicit specialization.
517330f729Sjoerg ///
527330f729Sjoerg /// Template declarations include one or more template parameter lists
537330f729Sjoerg /// and either the function or class template declaration. Explicit
547330f729Sjoerg /// specializations contain one or more 'template < >' prefixes
557330f729Sjoerg /// followed by a (possibly templated) declaration. Since the
567330f729Sjoerg /// syntactic form of both features is nearly identical, we parse all
577330f729Sjoerg /// of the template headers together and let semantic analysis sort
587330f729Sjoerg /// the declarations from the explicit specializations.
597330f729Sjoerg ///
607330f729Sjoerg /// template-declaration: [C++ temp]
617330f729Sjoerg /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration
627330f729Sjoerg ///
637330f729Sjoerg /// template-declaration: [C++2a]
647330f729Sjoerg /// template-head declaration
657330f729Sjoerg /// template-head concept-definition
667330f729Sjoerg ///
677330f729Sjoerg /// TODO: requires-clause
687330f729Sjoerg /// template-head: [C++2a]
697330f729Sjoerg /// 'template' '<' template-parameter-list '>'
707330f729Sjoerg /// requires-clause[opt]
717330f729Sjoerg ///
727330f729Sjoerg /// explicit-specialization: [ C++ temp.expl.spec]
737330f729Sjoerg /// 'template' '<' '>' declaration
ParseTemplateDeclarationOrSpecialization(DeclaratorContext Context,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)747330f729Sjoerg Decl *Parser::ParseTemplateDeclarationOrSpecialization(
757330f729Sjoerg DeclaratorContext Context, SourceLocation &DeclEnd,
767330f729Sjoerg ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
777330f729Sjoerg assert(Tok.isOneOf(tok::kw_export, tok::kw_template) &&
787330f729Sjoerg "Token does not start a template declaration.");
797330f729Sjoerg
80*e038c9c4Sjoerg MultiParseScope TemplateParamScopes(*this);
817330f729Sjoerg
827330f729Sjoerg // Tell the action that names should be checked in the context of
837330f729Sjoerg // the declaration to come.
847330f729Sjoerg ParsingDeclRAIIObject
857330f729Sjoerg ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
867330f729Sjoerg
877330f729Sjoerg // Parse multiple levels of template headers within this template
887330f729Sjoerg // parameter scope, e.g.,
897330f729Sjoerg //
907330f729Sjoerg // template<typename T>
917330f729Sjoerg // template<typename U>
927330f729Sjoerg // class A<T>::B { ... };
937330f729Sjoerg //
947330f729Sjoerg // We parse multiple levels non-recursively so that we can build a
957330f729Sjoerg // single data structure containing all of the template parameter
967330f729Sjoerg // lists to easily differentiate between the case above and:
977330f729Sjoerg //
987330f729Sjoerg // template<typename T>
997330f729Sjoerg // class A {
1007330f729Sjoerg // template<typename U> class B;
1017330f729Sjoerg // };
1027330f729Sjoerg //
1037330f729Sjoerg // In the first case, the action for declaring A<T>::B receives
1047330f729Sjoerg // both template parameter lists. In the second case, the action for
1057330f729Sjoerg // defining A<T>::B receives just the inner template parameter list
1067330f729Sjoerg // (and retrieves the outer template parameter list from its
1077330f729Sjoerg // context).
1087330f729Sjoerg bool isSpecialization = true;
1097330f729Sjoerg bool LastParamListWasEmpty = false;
1107330f729Sjoerg TemplateParameterLists ParamLists;
1117330f729Sjoerg TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
1127330f729Sjoerg
1137330f729Sjoerg do {
1147330f729Sjoerg // Consume the 'export', if any.
1157330f729Sjoerg SourceLocation ExportLoc;
1167330f729Sjoerg TryConsumeToken(tok::kw_export, ExportLoc);
1177330f729Sjoerg
1187330f729Sjoerg // Consume the 'template', which should be here.
1197330f729Sjoerg SourceLocation TemplateLoc;
1207330f729Sjoerg if (!TryConsumeToken(tok::kw_template, TemplateLoc)) {
1217330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected_template);
1227330f729Sjoerg return nullptr;
1237330f729Sjoerg }
1247330f729Sjoerg
1257330f729Sjoerg // Parse the '<' template-parameter-list '>'
1267330f729Sjoerg SourceLocation LAngleLoc, RAngleLoc;
1277330f729Sjoerg SmallVector<NamedDecl*, 4> TemplateParams;
128*e038c9c4Sjoerg if (ParseTemplateParameters(TemplateParamScopes,
129*e038c9c4Sjoerg CurTemplateDepthTracker.getDepth(),
1307330f729Sjoerg TemplateParams, LAngleLoc, RAngleLoc)) {
1317330f729Sjoerg // Skip until the semi-colon or a '}'.
1327330f729Sjoerg SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
1337330f729Sjoerg TryConsumeToken(tok::semi);
1347330f729Sjoerg return nullptr;
1357330f729Sjoerg }
1367330f729Sjoerg
1377330f729Sjoerg ExprResult OptionalRequiresClauseConstraintER;
1387330f729Sjoerg if (!TemplateParams.empty()) {
1397330f729Sjoerg isSpecialization = false;
1407330f729Sjoerg ++CurTemplateDepthTracker;
1417330f729Sjoerg
1427330f729Sjoerg if (TryConsumeToken(tok::kw_requires)) {
1437330f729Sjoerg OptionalRequiresClauseConstraintER =
144*e038c9c4Sjoerg Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression(
145*e038c9c4Sjoerg /*IsTrailingRequiresClause=*/false));
1467330f729Sjoerg if (!OptionalRequiresClauseConstraintER.isUsable()) {
1477330f729Sjoerg // Skip until the semi-colon or a '}'.
1487330f729Sjoerg SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
1497330f729Sjoerg TryConsumeToken(tok::semi);
1507330f729Sjoerg return nullptr;
1517330f729Sjoerg }
1527330f729Sjoerg }
1537330f729Sjoerg } else {
1547330f729Sjoerg LastParamListWasEmpty = true;
1557330f729Sjoerg }
1567330f729Sjoerg
1577330f729Sjoerg ParamLists.push_back(Actions.ActOnTemplateParameterList(
1587330f729Sjoerg CurTemplateDepthTracker.getDepth(), ExportLoc, TemplateLoc, LAngleLoc,
1597330f729Sjoerg TemplateParams, RAngleLoc, OptionalRequiresClauseConstraintER.get()));
1607330f729Sjoerg } while (Tok.isOneOf(tok::kw_export, tok::kw_template));
1617330f729Sjoerg
1627330f729Sjoerg // Parse the actual template declaration.
1637330f729Sjoerg if (Tok.is(tok::kw_concept))
1647330f729Sjoerg return ParseConceptDefinition(
1657330f729Sjoerg ParsedTemplateInfo(&ParamLists, isSpecialization,
1667330f729Sjoerg LastParamListWasEmpty),
1677330f729Sjoerg DeclEnd);
1687330f729Sjoerg
1697330f729Sjoerg return ParseSingleDeclarationAfterTemplate(
1707330f729Sjoerg Context,
1717330f729Sjoerg ParsedTemplateInfo(&ParamLists, isSpecialization, LastParamListWasEmpty),
1727330f729Sjoerg ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
1737330f729Sjoerg }
1747330f729Sjoerg
1757330f729Sjoerg /// Parse a single declaration that declares a template,
1767330f729Sjoerg /// template specialization, or explicit instantiation of a template.
1777330f729Sjoerg ///
1787330f729Sjoerg /// \param DeclEnd will receive the source location of the last token
1797330f729Sjoerg /// within this declaration.
1807330f729Sjoerg ///
1817330f729Sjoerg /// \param AS the access specifier associated with this
1827330f729Sjoerg /// declaration. Will be AS_none for namespace-scope declarations.
1837330f729Sjoerg ///
1847330f729Sjoerg /// \returns the new declaration.
ParseSingleDeclarationAfterTemplate(DeclaratorContext Context,const ParsedTemplateInfo & TemplateInfo,ParsingDeclRAIIObject & DiagsFromTParams,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)1857330f729Sjoerg Decl *Parser::ParseSingleDeclarationAfterTemplate(
1867330f729Sjoerg DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
1877330f729Sjoerg ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd,
1887330f729Sjoerg ParsedAttributes &AccessAttrs, AccessSpecifier AS) {
1897330f729Sjoerg assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1907330f729Sjoerg "Template information required");
1917330f729Sjoerg
1927330f729Sjoerg if (Tok.is(tok::kw_static_assert)) {
1937330f729Sjoerg // A static_assert declaration may not be templated.
1947330f729Sjoerg Diag(Tok.getLocation(), diag::err_templated_invalid_declaration)
1957330f729Sjoerg << TemplateInfo.getSourceRange();
1967330f729Sjoerg // Parse the static_assert declaration to improve error recovery.
1977330f729Sjoerg return ParseStaticAssertDeclaration(DeclEnd);
1987330f729Sjoerg }
1997330f729Sjoerg
200*e038c9c4Sjoerg if (Context == DeclaratorContext::Member) {
2017330f729Sjoerg // We are parsing a member template.
2027330f729Sjoerg ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
2037330f729Sjoerg &DiagsFromTParams);
2047330f729Sjoerg return nullptr;
2057330f729Sjoerg }
2067330f729Sjoerg
2077330f729Sjoerg ParsedAttributesWithRange prefixAttrs(AttrFactory);
2087330f729Sjoerg MaybeParseCXX11Attributes(prefixAttrs);
2097330f729Sjoerg
2107330f729Sjoerg if (Tok.is(tok::kw_using)) {
2117330f729Sjoerg auto usingDeclPtr = ParseUsingDirectiveOrDeclaration(Context, TemplateInfo, DeclEnd,
2127330f729Sjoerg prefixAttrs);
2137330f729Sjoerg if (!usingDeclPtr || !usingDeclPtr.get().isSingleDecl())
2147330f729Sjoerg return nullptr;
2157330f729Sjoerg return usingDeclPtr.get().getSingleDecl();
2167330f729Sjoerg }
2177330f729Sjoerg
2187330f729Sjoerg // Parse the declaration specifiers, stealing any diagnostics from
2197330f729Sjoerg // the template parameters.
2207330f729Sjoerg ParsingDeclSpec DS(*this, &DiagsFromTParams);
2217330f729Sjoerg
2227330f729Sjoerg ParseDeclarationSpecifiers(DS, TemplateInfo, AS,
2237330f729Sjoerg getDeclSpecContextFromDeclaratorContext(Context));
2247330f729Sjoerg
2257330f729Sjoerg if (Tok.is(tok::semi)) {
2267330f729Sjoerg ProhibitAttributes(prefixAttrs);
2277330f729Sjoerg DeclEnd = ConsumeToken();
2287330f729Sjoerg RecordDecl *AnonRecord = nullptr;
2297330f729Sjoerg Decl *Decl = Actions.ParsedFreeStandingDeclSpec(
2307330f729Sjoerg getCurScope(), AS, DS,
2317330f729Sjoerg TemplateInfo.TemplateParams ? *TemplateInfo.TemplateParams
2327330f729Sjoerg : MultiTemplateParamsArg(),
2337330f729Sjoerg TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation,
2347330f729Sjoerg AnonRecord);
2357330f729Sjoerg assert(!AnonRecord &&
2367330f729Sjoerg "Anonymous unions/structs should not be valid with template");
2377330f729Sjoerg DS.complete(Decl);
2387330f729Sjoerg return Decl;
2397330f729Sjoerg }
2407330f729Sjoerg
2417330f729Sjoerg // Move the attributes from the prefix into the DS.
2427330f729Sjoerg if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
2437330f729Sjoerg ProhibitAttributes(prefixAttrs);
2447330f729Sjoerg else
2457330f729Sjoerg DS.takeAttributesFrom(prefixAttrs);
2467330f729Sjoerg
2477330f729Sjoerg // Parse the declarator.
2487330f729Sjoerg ParsingDeclarator DeclaratorInfo(*this, DS, (DeclaratorContext)Context);
249*e038c9c4Sjoerg if (TemplateInfo.TemplateParams)
250*e038c9c4Sjoerg DeclaratorInfo.setTemplateParameterLists(*TemplateInfo.TemplateParams);
2517330f729Sjoerg ParseDeclarator(DeclaratorInfo);
2527330f729Sjoerg // Error parsing the declarator?
2537330f729Sjoerg if (!DeclaratorInfo.hasName()) {
2547330f729Sjoerg // If so, skip until the semi-colon or a }.
2557330f729Sjoerg SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2567330f729Sjoerg if (Tok.is(tok::semi))
2577330f729Sjoerg ConsumeToken();
2587330f729Sjoerg return nullptr;
2597330f729Sjoerg }
2607330f729Sjoerg
2617330f729Sjoerg llvm::TimeTraceScope TimeScope("ParseTemplate", [&]() {
262*e038c9c4Sjoerg return std::string(DeclaratorInfo.getIdentifier() != nullptr
2637330f729Sjoerg ? DeclaratorInfo.getIdentifier()->getName()
264*e038c9c4Sjoerg : "<unknown>");
2657330f729Sjoerg });
2667330f729Sjoerg
2677330f729Sjoerg LateParsedAttrList LateParsedAttrs(true);
268*e038c9c4Sjoerg if (DeclaratorInfo.isFunctionDeclarator()) {
269*e038c9c4Sjoerg if (Tok.is(tok::kw_requires))
270*e038c9c4Sjoerg ParseTrailingRequiresClause(DeclaratorInfo);
271*e038c9c4Sjoerg
2727330f729Sjoerg MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
273*e038c9c4Sjoerg }
2747330f729Sjoerg
2757330f729Sjoerg if (DeclaratorInfo.isFunctionDeclarator() &&
2767330f729Sjoerg isStartOfFunctionDefinition(DeclaratorInfo)) {
2777330f729Sjoerg
2787330f729Sjoerg // Function definitions are only allowed at file scope and in C++ classes.
2797330f729Sjoerg // The C++ inline method definition case is handled elsewhere, so we only
2807330f729Sjoerg // need to handle the file scope definition case.
281*e038c9c4Sjoerg if (Context != DeclaratorContext::File) {
2827330f729Sjoerg Diag(Tok, diag::err_function_definition_not_allowed);
2837330f729Sjoerg SkipMalformedDecl();
2847330f729Sjoerg return nullptr;
2857330f729Sjoerg }
2867330f729Sjoerg
2877330f729Sjoerg if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2887330f729Sjoerg // Recover by ignoring the 'typedef'. This was probably supposed to be
2897330f729Sjoerg // the 'typename' keyword, which we should have already suggested adding
2907330f729Sjoerg // if it's appropriate.
2917330f729Sjoerg Diag(DS.getStorageClassSpecLoc(), diag::err_function_declared_typedef)
2927330f729Sjoerg << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
2937330f729Sjoerg DS.ClearStorageClassSpecs();
2947330f729Sjoerg }
2957330f729Sjoerg
2967330f729Sjoerg if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2977330f729Sjoerg if (DeclaratorInfo.getName().getKind() !=
2987330f729Sjoerg UnqualifiedIdKind::IK_TemplateId) {
2997330f729Sjoerg // If the declarator-id is not a template-id, issue a diagnostic and
3007330f729Sjoerg // recover by ignoring the 'template' keyword.
3017330f729Sjoerg Diag(Tok, diag::err_template_defn_explicit_instantiation) << 0;
3027330f729Sjoerg return ParseFunctionDefinition(DeclaratorInfo, ParsedTemplateInfo(),
3037330f729Sjoerg &LateParsedAttrs);
3047330f729Sjoerg } else {
3057330f729Sjoerg SourceLocation LAngleLoc
3067330f729Sjoerg = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
3077330f729Sjoerg Diag(DeclaratorInfo.getIdentifierLoc(),
3087330f729Sjoerg diag::err_explicit_instantiation_with_definition)
3097330f729Sjoerg << SourceRange(TemplateInfo.TemplateLoc)
3107330f729Sjoerg << FixItHint::CreateInsertion(LAngleLoc, "<>");
3117330f729Sjoerg
3127330f729Sjoerg // Recover as if it were an explicit specialization.
3137330f729Sjoerg TemplateParameterLists FakedParamLists;
3147330f729Sjoerg FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
3157330f729Sjoerg 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
3167330f729Sjoerg LAngleLoc, nullptr));
3177330f729Sjoerg
3187330f729Sjoerg return ParseFunctionDefinition(
3197330f729Sjoerg DeclaratorInfo, ParsedTemplateInfo(&FakedParamLists,
3207330f729Sjoerg /*isSpecialization=*/true,
3217330f729Sjoerg /*lastParameterListWasEmpty=*/true),
3227330f729Sjoerg &LateParsedAttrs);
3237330f729Sjoerg }
3247330f729Sjoerg }
3257330f729Sjoerg return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo,
3267330f729Sjoerg &LateParsedAttrs);
3277330f729Sjoerg }
3287330f729Sjoerg
3297330f729Sjoerg // Parse this declaration.
3307330f729Sjoerg Decl *ThisDecl = ParseDeclarationAfterDeclarator(DeclaratorInfo,
3317330f729Sjoerg TemplateInfo);
3327330f729Sjoerg
3337330f729Sjoerg if (Tok.is(tok::comma)) {
3347330f729Sjoerg Diag(Tok, diag::err_multiple_template_declarators)
3357330f729Sjoerg << (int)TemplateInfo.Kind;
3367330f729Sjoerg SkipUntil(tok::semi);
3377330f729Sjoerg return ThisDecl;
3387330f729Sjoerg }
3397330f729Sjoerg
3407330f729Sjoerg // Eat the semi colon after the declaration.
3417330f729Sjoerg ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
3427330f729Sjoerg if (LateParsedAttrs.size() > 0)
3437330f729Sjoerg ParseLexedAttributeList(LateParsedAttrs, ThisDecl, true, false);
3447330f729Sjoerg DeclaratorInfo.complete(ThisDecl);
3457330f729Sjoerg return ThisDecl;
3467330f729Sjoerg }
3477330f729Sjoerg
3487330f729Sjoerg /// \brief Parse a single declaration that declares a concept.
3497330f729Sjoerg ///
3507330f729Sjoerg /// \param DeclEnd will receive the source location of the last token
3517330f729Sjoerg /// within this declaration.
3527330f729Sjoerg ///
3537330f729Sjoerg /// \returns the new declaration.
3547330f729Sjoerg Decl *
ParseConceptDefinition(const ParsedTemplateInfo & TemplateInfo,SourceLocation & DeclEnd)3557330f729Sjoerg Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo,
3567330f729Sjoerg SourceLocation &DeclEnd) {
3577330f729Sjoerg assert(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
3587330f729Sjoerg "Template information required");
3597330f729Sjoerg assert(Tok.is(tok::kw_concept) &&
3607330f729Sjoerg "ParseConceptDefinition must be called when at a 'concept' keyword");
3617330f729Sjoerg
3627330f729Sjoerg ConsumeToken(); // Consume 'concept'
3637330f729Sjoerg
3647330f729Sjoerg SourceLocation BoolKWLoc;
3657330f729Sjoerg if (TryConsumeToken(tok::kw_bool, BoolKWLoc))
3667330f729Sjoerg Diag(Tok.getLocation(), diag::ext_concept_legacy_bool_keyword) <<
3677330f729Sjoerg FixItHint::CreateRemoval(SourceLocation(BoolKWLoc));
3687330f729Sjoerg
3697330f729Sjoerg DiagnoseAndSkipCXX11Attributes();
3707330f729Sjoerg
3717330f729Sjoerg CXXScopeSpec SS;
372*e038c9c4Sjoerg if (ParseOptionalCXXScopeSpecifier(
373*e038c9c4Sjoerg SS, /*ObjectType=*/nullptr,
374*e038c9c4Sjoerg /*ObjectHadErrors=*/false, /*EnteringContext=*/false,
375*e038c9c4Sjoerg /*MayBePseudoDestructor=*/nullptr,
3767330f729Sjoerg /*IsTypename=*/false, /*LastII=*/nullptr, /*OnlyNamespace=*/true) ||
3777330f729Sjoerg SS.isInvalid()) {
3787330f729Sjoerg SkipUntil(tok::semi);
3797330f729Sjoerg return nullptr;
3807330f729Sjoerg }
3817330f729Sjoerg
3827330f729Sjoerg if (SS.isNotEmpty())
3837330f729Sjoerg Diag(SS.getBeginLoc(),
3847330f729Sjoerg diag::err_concept_definition_not_identifier);
3857330f729Sjoerg
3867330f729Sjoerg UnqualifiedId Result;
387*e038c9c4Sjoerg if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr,
388*e038c9c4Sjoerg /*ObjectHadErrors=*/false, /*EnteringContext=*/false,
3897330f729Sjoerg /*AllowDestructorName=*/false,
3907330f729Sjoerg /*AllowConstructorName=*/false,
3917330f729Sjoerg /*AllowDeductionGuide=*/false,
392*e038c9c4Sjoerg /*TemplateKWLoc=*/nullptr, Result)) {
3937330f729Sjoerg SkipUntil(tok::semi);
3947330f729Sjoerg return nullptr;
3957330f729Sjoerg }
3967330f729Sjoerg
3977330f729Sjoerg if (Result.getKind() != UnqualifiedIdKind::IK_Identifier) {
3987330f729Sjoerg Diag(Result.getBeginLoc(), diag::err_concept_definition_not_identifier);
3997330f729Sjoerg SkipUntil(tok::semi);
4007330f729Sjoerg return nullptr;
4017330f729Sjoerg }
4027330f729Sjoerg
4037330f729Sjoerg IdentifierInfo *Id = Result.Identifier;
4047330f729Sjoerg SourceLocation IdLoc = Result.getBeginLoc();
4057330f729Sjoerg
4067330f729Sjoerg DiagnoseAndSkipCXX11Attributes();
4077330f729Sjoerg
4087330f729Sjoerg if (!TryConsumeToken(tok::equal)) {
4097330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected) << tok::equal;
4107330f729Sjoerg SkipUntil(tok::semi);
4117330f729Sjoerg return nullptr;
4127330f729Sjoerg }
4137330f729Sjoerg
4147330f729Sjoerg ExprResult ConstraintExprResult =
4157330f729Sjoerg Actions.CorrectDelayedTyposInExpr(ParseConstraintExpression());
4167330f729Sjoerg if (ConstraintExprResult.isInvalid()) {
4177330f729Sjoerg SkipUntil(tok::semi);
4187330f729Sjoerg return nullptr;
4197330f729Sjoerg }
4207330f729Sjoerg
4217330f729Sjoerg DeclEnd = Tok.getLocation();
4227330f729Sjoerg ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
4237330f729Sjoerg Expr *ConstraintExpr = ConstraintExprResult.get();
4247330f729Sjoerg return Actions.ActOnConceptDefinition(getCurScope(),
4257330f729Sjoerg *TemplateInfo.TemplateParams,
4267330f729Sjoerg Id, IdLoc, ConstraintExpr);
4277330f729Sjoerg }
4287330f729Sjoerg
4297330f729Sjoerg /// ParseTemplateParameters - Parses a template-parameter-list enclosed in
4307330f729Sjoerg /// angle brackets. Depth is the depth of this template-parameter-list, which
4317330f729Sjoerg /// is the number of template headers directly enclosing this template header.
4327330f729Sjoerg /// TemplateParams is the current list of template parameters we're building.
4337330f729Sjoerg /// The template parameter we parse will be added to this list. LAngleLoc and
4347330f729Sjoerg /// RAngleLoc will receive the positions of the '<' and '>', respectively,
4357330f729Sjoerg /// that enclose this template parameter list.
4367330f729Sjoerg ///
4377330f729Sjoerg /// \returns true if an error occurred, false otherwise.
ParseTemplateParameters(MultiParseScope & TemplateScopes,unsigned Depth,SmallVectorImpl<NamedDecl * > & TemplateParams,SourceLocation & LAngleLoc,SourceLocation & RAngleLoc)4387330f729Sjoerg bool Parser::ParseTemplateParameters(
439*e038c9c4Sjoerg MultiParseScope &TemplateScopes, unsigned Depth,
440*e038c9c4Sjoerg SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc,
441*e038c9c4Sjoerg SourceLocation &RAngleLoc) {
4427330f729Sjoerg // Get the template parameter list.
4437330f729Sjoerg if (!TryConsumeToken(tok::less, LAngleLoc)) {
4447330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected_less_after) << "template";
4457330f729Sjoerg return true;
4467330f729Sjoerg }
4477330f729Sjoerg
4487330f729Sjoerg // Try to parse the template parameter list.
4497330f729Sjoerg bool Failed = false;
450*e038c9c4Sjoerg // FIXME: Missing greatergreatergreater support.
451*e038c9c4Sjoerg if (!Tok.is(tok::greater) && !Tok.is(tok::greatergreater)) {
452*e038c9c4Sjoerg TemplateScopes.Enter(Scope::TemplateParamScope);
4537330f729Sjoerg Failed = ParseTemplateParameterList(Depth, TemplateParams);
454*e038c9c4Sjoerg }
4557330f729Sjoerg
4567330f729Sjoerg if (Tok.is(tok::greatergreater)) {
4577330f729Sjoerg // No diagnostic required here: a template-parameter-list can only be
4587330f729Sjoerg // followed by a declaration or, for a template template parameter, the
4597330f729Sjoerg // 'class' keyword. Therefore, the second '>' will be diagnosed later.
4607330f729Sjoerg // This matters for elegant diagnosis of:
4617330f729Sjoerg // template<template<typename>> struct S;
4627330f729Sjoerg Tok.setKind(tok::greater);
4637330f729Sjoerg RAngleLoc = Tok.getLocation();
4647330f729Sjoerg Tok.setLocation(Tok.getLocation().getLocWithOffset(1));
4657330f729Sjoerg } else if (!TryConsumeToken(tok::greater, RAngleLoc) && Failed) {
4667330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected) << tok::greater;
4677330f729Sjoerg return true;
4687330f729Sjoerg }
4697330f729Sjoerg return false;
4707330f729Sjoerg }
4717330f729Sjoerg
4727330f729Sjoerg /// ParseTemplateParameterList - Parse a template parameter list. If
4737330f729Sjoerg /// the parsing fails badly (i.e., closing bracket was left out), this
4747330f729Sjoerg /// will try to put the token stream in a reasonable position (closing
4757330f729Sjoerg /// a statement, etc.) and return false.
4767330f729Sjoerg ///
4777330f729Sjoerg /// template-parameter-list: [C++ temp]
4787330f729Sjoerg /// template-parameter
4797330f729Sjoerg /// template-parameter-list ',' template-parameter
4807330f729Sjoerg bool
ParseTemplateParameterList(const unsigned Depth,SmallVectorImpl<NamedDecl * > & TemplateParams)4817330f729Sjoerg Parser::ParseTemplateParameterList(const unsigned Depth,
4827330f729Sjoerg SmallVectorImpl<NamedDecl*> &TemplateParams) {
4837330f729Sjoerg while (1) {
4847330f729Sjoerg
4857330f729Sjoerg if (NamedDecl *TmpParam
4867330f729Sjoerg = ParseTemplateParameter(Depth, TemplateParams.size())) {
4877330f729Sjoerg TemplateParams.push_back(TmpParam);
4887330f729Sjoerg } else {
4897330f729Sjoerg // If we failed to parse a template parameter, skip until we find
4907330f729Sjoerg // a comma or closing brace.
4917330f729Sjoerg SkipUntil(tok::comma, tok::greater, tok::greatergreater,
4927330f729Sjoerg StopAtSemi | StopBeforeMatch);
4937330f729Sjoerg }
4947330f729Sjoerg
4957330f729Sjoerg // Did we find a comma or the end of the template parameter list?
4967330f729Sjoerg if (Tok.is(tok::comma)) {
4977330f729Sjoerg ConsumeToken();
4987330f729Sjoerg } else if (Tok.isOneOf(tok::greater, tok::greatergreater)) {
4997330f729Sjoerg // Don't consume this... that's done by template parser.
5007330f729Sjoerg break;
5017330f729Sjoerg } else {
5027330f729Sjoerg // Somebody probably forgot to close the template. Skip ahead and
5037330f729Sjoerg // try to get out of the expression. This error is currently
5047330f729Sjoerg // subsumed by whatever goes on in ParseTemplateParameter.
5057330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected_comma_greater);
5067330f729Sjoerg SkipUntil(tok::comma, tok::greater, tok::greatergreater,
5077330f729Sjoerg StopAtSemi | StopBeforeMatch);
5087330f729Sjoerg return false;
5097330f729Sjoerg }
5107330f729Sjoerg }
5117330f729Sjoerg return true;
5127330f729Sjoerg }
5137330f729Sjoerg
5147330f729Sjoerg /// Determine whether the parser is at the start of a template
5157330f729Sjoerg /// type parameter.
isStartOfTemplateTypeParameter()516*e038c9c4Sjoerg Parser::TPResult Parser::isStartOfTemplateTypeParameter() {
5177330f729Sjoerg if (Tok.is(tok::kw_class)) {
5187330f729Sjoerg // "class" may be the start of an elaborated-type-specifier or a
5197330f729Sjoerg // type-parameter. Per C++ [temp.param]p3, we prefer the type-parameter.
5207330f729Sjoerg switch (NextToken().getKind()) {
5217330f729Sjoerg case tok::equal:
5227330f729Sjoerg case tok::comma:
5237330f729Sjoerg case tok::greater:
5247330f729Sjoerg case tok::greatergreater:
5257330f729Sjoerg case tok::ellipsis:
526*e038c9c4Sjoerg return TPResult::True;
5277330f729Sjoerg
5287330f729Sjoerg case tok::identifier:
5297330f729Sjoerg // This may be either a type-parameter or an elaborated-type-specifier.
5307330f729Sjoerg // We have to look further.
5317330f729Sjoerg break;
5327330f729Sjoerg
5337330f729Sjoerg default:
534*e038c9c4Sjoerg return TPResult::False;
5357330f729Sjoerg }
5367330f729Sjoerg
5377330f729Sjoerg switch (GetLookAheadToken(2).getKind()) {
5387330f729Sjoerg case tok::equal:
5397330f729Sjoerg case tok::comma:
5407330f729Sjoerg case tok::greater:
5417330f729Sjoerg case tok::greatergreater:
542*e038c9c4Sjoerg return TPResult::True;
5437330f729Sjoerg
5447330f729Sjoerg default:
545*e038c9c4Sjoerg return TPResult::False;
5467330f729Sjoerg }
5477330f729Sjoerg }
5487330f729Sjoerg
549*e038c9c4Sjoerg if (TryAnnotateTypeConstraint())
550*e038c9c4Sjoerg return TPResult::Error;
551*e038c9c4Sjoerg
552*e038c9c4Sjoerg if (isTypeConstraintAnnotation() &&
553*e038c9c4Sjoerg // Next token might be 'auto' or 'decltype', indicating that this
554*e038c9c4Sjoerg // type-constraint is in fact part of a placeholder-type-specifier of a
555*e038c9c4Sjoerg // non-type template parameter.
556*e038c9c4Sjoerg !GetLookAheadToken(Tok.is(tok::annot_cxxscope) ? 2 : 1)
557*e038c9c4Sjoerg .isOneOf(tok::kw_auto, tok::kw_decltype))
558*e038c9c4Sjoerg return TPResult::True;
559*e038c9c4Sjoerg
5607330f729Sjoerg // 'typedef' is a reasonably-common typo/thinko for 'typename', and is
5617330f729Sjoerg // ill-formed otherwise.
5627330f729Sjoerg if (Tok.isNot(tok::kw_typename) && Tok.isNot(tok::kw_typedef))
563*e038c9c4Sjoerg return TPResult::False;
5647330f729Sjoerg
5657330f729Sjoerg // C++ [temp.param]p2:
5667330f729Sjoerg // There is no semantic difference between class and typename in a
5677330f729Sjoerg // template-parameter. typename followed by an unqualified-id
5687330f729Sjoerg // names a template type parameter. typename followed by a
5697330f729Sjoerg // qualified-id denotes the type in a non-type
5707330f729Sjoerg // parameter-declaration.
5717330f729Sjoerg Token Next = NextToken();
5727330f729Sjoerg
5737330f729Sjoerg // If we have an identifier, skip over it.
5747330f729Sjoerg if (Next.getKind() == tok::identifier)
5757330f729Sjoerg Next = GetLookAheadToken(2);
5767330f729Sjoerg
5777330f729Sjoerg switch (Next.getKind()) {
5787330f729Sjoerg case tok::equal:
5797330f729Sjoerg case tok::comma:
5807330f729Sjoerg case tok::greater:
5817330f729Sjoerg case tok::greatergreater:
5827330f729Sjoerg case tok::ellipsis:
583*e038c9c4Sjoerg return TPResult::True;
5847330f729Sjoerg
5857330f729Sjoerg case tok::kw_typename:
5867330f729Sjoerg case tok::kw_typedef:
5877330f729Sjoerg case tok::kw_class:
5887330f729Sjoerg // These indicate that a comma was missed after a type parameter, not that
5897330f729Sjoerg // we have found a non-type parameter.
590*e038c9c4Sjoerg return TPResult::True;
5917330f729Sjoerg
5927330f729Sjoerg default:
593*e038c9c4Sjoerg return TPResult::False;
5947330f729Sjoerg }
5957330f729Sjoerg }
5967330f729Sjoerg
5977330f729Sjoerg /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]).
5987330f729Sjoerg ///
5997330f729Sjoerg /// template-parameter: [C++ temp.param]
6007330f729Sjoerg /// type-parameter
6017330f729Sjoerg /// parameter-declaration
6027330f729Sjoerg ///
603*e038c9c4Sjoerg /// type-parameter: (See below)
604*e038c9c4Sjoerg /// type-parameter-key ...[opt] identifier[opt]
605*e038c9c4Sjoerg /// type-parameter-key identifier[opt] = type-id
606*e038c9c4Sjoerg /// (C++2a) type-constraint ...[opt] identifier[opt]
607*e038c9c4Sjoerg /// (C++2a) type-constraint identifier[opt] = type-id
608*e038c9c4Sjoerg /// 'template' '<' template-parameter-list '>' type-parameter-key
609*e038c9c4Sjoerg /// ...[opt] identifier[opt]
610*e038c9c4Sjoerg /// 'template' '<' template-parameter-list '>' type-parameter-key
611*e038c9c4Sjoerg /// identifier[opt] '=' id-expression
612*e038c9c4Sjoerg ///
613*e038c9c4Sjoerg /// type-parameter-key:
614*e038c9c4Sjoerg /// class
615*e038c9c4Sjoerg /// typename
616*e038c9c4Sjoerg ///
ParseTemplateParameter(unsigned Depth,unsigned Position)6177330f729Sjoerg NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) {
618*e038c9c4Sjoerg
619*e038c9c4Sjoerg switch (isStartOfTemplateTypeParameter()) {
620*e038c9c4Sjoerg case TPResult::True:
621*e038c9c4Sjoerg // Is there just a typo in the input code? ('typedef' instead of
622*e038c9c4Sjoerg // 'typename')
6237330f729Sjoerg if (Tok.is(tok::kw_typedef)) {
6247330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected_template_parameter);
6257330f729Sjoerg
6267330f729Sjoerg Diag(Tok.getLocation(), diag::note_meant_to_use_typename)
6277330f729Sjoerg << FixItHint::CreateReplacement(CharSourceRange::getCharRange(
628*e038c9c4Sjoerg Tok.getLocation(),
629*e038c9c4Sjoerg Tok.getEndLoc()),
6307330f729Sjoerg "typename");
6317330f729Sjoerg
6327330f729Sjoerg Tok.setKind(tok::kw_typename);
6337330f729Sjoerg }
6347330f729Sjoerg
6357330f729Sjoerg return ParseTypeParameter(Depth, Position);
636*e038c9c4Sjoerg case TPResult::False:
637*e038c9c4Sjoerg break;
638*e038c9c4Sjoerg
639*e038c9c4Sjoerg case TPResult::Error: {
640*e038c9c4Sjoerg // We return an invalid parameter as opposed to null to avoid having bogus
641*e038c9c4Sjoerg // diagnostics about an empty template parameter list.
642*e038c9c4Sjoerg // FIXME: Fix ParseTemplateParameterList to better handle nullptr results
643*e038c9c4Sjoerg // from here.
644*e038c9c4Sjoerg // Return a NTTP as if there was an error in a scope specifier, the user
645*e038c9c4Sjoerg // probably meant to write the type of a NTTP.
646*e038c9c4Sjoerg DeclSpec DS(getAttrFactory());
647*e038c9c4Sjoerg DS.SetTypeSpecError();
648*e038c9c4Sjoerg Declarator D(DS, DeclaratorContext::TemplateParam);
649*e038c9c4Sjoerg D.SetIdentifier(nullptr, Tok.getLocation());
650*e038c9c4Sjoerg D.setInvalidType(true);
651*e038c9c4Sjoerg NamedDecl *ErrorParam = Actions.ActOnNonTypeTemplateParameter(
652*e038c9c4Sjoerg getCurScope(), D, Depth, Position, /*EqualLoc=*/SourceLocation(),
653*e038c9c4Sjoerg /*DefaultArg=*/nullptr);
654*e038c9c4Sjoerg ErrorParam->setInvalidDecl(true);
655*e038c9c4Sjoerg SkipUntil(tok::comma, tok::greater, tok::greatergreater,
656*e038c9c4Sjoerg StopAtSemi | StopBeforeMatch);
657*e038c9c4Sjoerg return ErrorParam;
658*e038c9c4Sjoerg }
659*e038c9c4Sjoerg
660*e038c9c4Sjoerg case TPResult::Ambiguous:
661*e038c9c4Sjoerg llvm_unreachable("template param classification can't be ambiguous");
6627330f729Sjoerg }
6637330f729Sjoerg
6647330f729Sjoerg if (Tok.is(tok::kw_template))
6657330f729Sjoerg return ParseTemplateTemplateParameter(Depth, Position);
6667330f729Sjoerg
6677330f729Sjoerg // If it's none of the above, then it must be a parameter declaration.
6687330f729Sjoerg // NOTE: This will pick up errors in the closure of the template parameter
6697330f729Sjoerg // list (e.g., template < ; Check here to implement >> style closures.
6707330f729Sjoerg return ParseNonTypeTemplateParameter(Depth, Position);
6717330f729Sjoerg }
6727330f729Sjoerg
673*e038c9c4Sjoerg /// Check whether the current token is a template-id annotation denoting a
674*e038c9c4Sjoerg /// type-constraint.
isTypeConstraintAnnotation()675*e038c9c4Sjoerg bool Parser::isTypeConstraintAnnotation() {
676*e038c9c4Sjoerg const Token &T = Tok.is(tok::annot_cxxscope) ? NextToken() : Tok;
677*e038c9c4Sjoerg if (T.isNot(tok::annot_template_id))
678*e038c9c4Sjoerg return false;
679*e038c9c4Sjoerg const auto *ExistingAnnot =
680*e038c9c4Sjoerg static_cast<TemplateIdAnnotation *>(T.getAnnotationValue());
681*e038c9c4Sjoerg return ExistingAnnot->Kind == TNK_Concept_template;
682*e038c9c4Sjoerg }
683*e038c9c4Sjoerg
684*e038c9c4Sjoerg /// Try parsing a type-constraint at the current location.
685*e038c9c4Sjoerg ///
686*e038c9c4Sjoerg /// type-constraint:
687*e038c9c4Sjoerg /// nested-name-specifier[opt] concept-name
688*e038c9c4Sjoerg /// nested-name-specifier[opt] concept-name
689*e038c9c4Sjoerg /// '<' template-argument-list[opt] '>'[opt]
690*e038c9c4Sjoerg ///
691*e038c9c4Sjoerg /// \returns true if an error occurred, and false otherwise.
TryAnnotateTypeConstraint()692*e038c9c4Sjoerg bool Parser::TryAnnotateTypeConstraint() {
693*e038c9c4Sjoerg if (!getLangOpts().CPlusPlus20)
694*e038c9c4Sjoerg return false;
695*e038c9c4Sjoerg CXXScopeSpec SS;
696*e038c9c4Sjoerg bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
697*e038c9c4Sjoerg if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
698*e038c9c4Sjoerg /*ObjectHadErrors=*/false,
699*e038c9c4Sjoerg /*EnteringContext=*/false,
700*e038c9c4Sjoerg /*MayBePseudoDestructor=*/nullptr,
701*e038c9c4Sjoerg // If this is not a type-constraint, then
702*e038c9c4Sjoerg // this scope-spec is part of the typename
703*e038c9c4Sjoerg // of a non-type template parameter
704*e038c9c4Sjoerg /*IsTypename=*/true, /*LastII=*/nullptr,
705*e038c9c4Sjoerg // We won't find concepts in
706*e038c9c4Sjoerg // non-namespaces anyway, so might as well
707*e038c9c4Sjoerg // parse this correctly for possible type
708*e038c9c4Sjoerg // names.
709*e038c9c4Sjoerg /*OnlyNamespace=*/false))
710*e038c9c4Sjoerg return true;
711*e038c9c4Sjoerg
712*e038c9c4Sjoerg if (Tok.is(tok::identifier)) {
713*e038c9c4Sjoerg UnqualifiedId PossibleConceptName;
714*e038c9c4Sjoerg PossibleConceptName.setIdentifier(Tok.getIdentifierInfo(),
715*e038c9c4Sjoerg Tok.getLocation());
716*e038c9c4Sjoerg
717*e038c9c4Sjoerg TemplateTy PossibleConcept;
718*e038c9c4Sjoerg bool MemberOfUnknownSpecialization = false;
719*e038c9c4Sjoerg auto TNK = Actions.isTemplateName(getCurScope(), SS,
720*e038c9c4Sjoerg /*hasTemplateKeyword=*/false,
721*e038c9c4Sjoerg PossibleConceptName,
722*e038c9c4Sjoerg /*ObjectType=*/ParsedType(),
723*e038c9c4Sjoerg /*EnteringContext=*/false,
724*e038c9c4Sjoerg PossibleConcept,
725*e038c9c4Sjoerg MemberOfUnknownSpecialization,
726*e038c9c4Sjoerg /*Disambiguation=*/true);
727*e038c9c4Sjoerg if (MemberOfUnknownSpecialization || !PossibleConcept ||
728*e038c9c4Sjoerg TNK != TNK_Concept_template) {
729*e038c9c4Sjoerg if (SS.isNotEmpty())
730*e038c9c4Sjoerg AnnotateScopeToken(SS, !WasScopeAnnotation);
731*e038c9c4Sjoerg return false;
732*e038c9c4Sjoerg }
733*e038c9c4Sjoerg
734*e038c9c4Sjoerg // At this point we're sure we're dealing with a constrained parameter. It
735*e038c9c4Sjoerg // may or may not have a template parameter list following the concept
736*e038c9c4Sjoerg // name.
737*e038c9c4Sjoerg if (AnnotateTemplateIdToken(PossibleConcept, TNK, SS,
738*e038c9c4Sjoerg /*TemplateKWLoc=*/SourceLocation(),
739*e038c9c4Sjoerg PossibleConceptName,
740*e038c9c4Sjoerg /*AllowTypeAnnotation=*/false,
741*e038c9c4Sjoerg /*TypeConstraint=*/true))
742*e038c9c4Sjoerg return true;
743*e038c9c4Sjoerg }
744*e038c9c4Sjoerg
745*e038c9c4Sjoerg if (SS.isNotEmpty())
746*e038c9c4Sjoerg AnnotateScopeToken(SS, !WasScopeAnnotation);
747*e038c9c4Sjoerg return false;
748*e038c9c4Sjoerg }
749*e038c9c4Sjoerg
7507330f729Sjoerg /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]).
7517330f729Sjoerg /// Other kinds of template parameters are parsed in
7527330f729Sjoerg /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter.
7537330f729Sjoerg ///
7547330f729Sjoerg /// type-parameter: [C++ temp.param]
7557330f729Sjoerg /// 'class' ...[opt][C++0x] identifier[opt]
7567330f729Sjoerg /// 'class' identifier[opt] '=' type-id
7577330f729Sjoerg /// 'typename' ...[opt][C++0x] identifier[opt]
7587330f729Sjoerg /// 'typename' identifier[opt] '=' type-id
ParseTypeParameter(unsigned Depth,unsigned Position)7597330f729Sjoerg NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) {
760*e038c9c4Sjoerg assert((Tok.isOneOf(tok::kw_class, tok::kw_typename) ||
761*e038c9c4Sjoerg isTypeConstraintAnnotation()) &&
762*e038c9c4Sjoerg "A type-parameter starts with 'class', 'typename' or a "
763*e038c9c4Sjoerg "type-constraint");
764*e038c9c4Sjoerg
765*e038c9c4Sjoerg CXXScopeSpec TypeConstraintSS;
766*e038c9c4Sjoerg TemplateIdAnnotation *TypeConstraint = nullptr;
767*e038c9c4Sjoerg bool TypenameKeyword = false;
768*e038c9c4Sjoerg SourceLocation KeyLoc;
769*e038c9c4Sjoerg ParseOptionalCXXScopeSpecifier(TypeConstraintSS, /*ObjectType=*/nullptr,
770*e038c9c4Sjoerg /*ObjectHadErrors=*/false,
771*e038c9c4Sjoerg /*EnteringContext*/ false);
772*e038c9c4Sjoerg if (Tok.is(tok::annot_template_id)) {
773*e038c9c4Sjoerg // Consume the 'type-constraint'.
774*e038c9c4Sjoerg TypeConstraint =
775*e038c9c4Sjoerg static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
776*e038c9c4Sjoerg assert(TypeConstraint->Kind == TNK_Concept_template &&
777*e038c9c4Sjoerg "stray non-concept template-id annotation");
778*e038c9c4Sjoerg KeyLoc = ConsumeAnnotationToken();
779*e038c9c4Sjoerg } else {
780*e038c9c4Sjoerg assert(TypeConstraintSS.isEmpty() &&
781*e038c9c4Sjoerg "expected type constraint after scope specifier");
7827330f729Sjoerg
7837330f729Sjoerg // Consume the 'class' or 'typename' keyword.
784*e038c9c4Sjoerg TypenameKeyword = Tok.is(tok::kw_typename);
785*e038c9c4Sjoerg KeyLoc = ConsumeToken();
786*e038c9c4Sjoerg }
7877330f729Sjoerg
7887330f729Sjoerg // Grab the ellipsis (if given).
7897330f729Sjoerg SourceLocation EllipsisLoc;
7907330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
7917330f729Sjoerg Diag(EllipsisLoc,
7927330f729Sjoerg getLangOpts().CPlusPlus11
7937330f729Sjoerg ? diag::warn_cxx98_compat_variadic_templates
7947330f729Sjoerg : diag::ext_variadic_templates);
7957330f729Sjoerg }
7967330f729Sjoerg
7977330f729Sjoerg // Grab the template parameter name (if given)
7987330f729Sjoerg SourceLocation NameLoc = Tok.getLocation();
7997330f729Sjoerg IdentifierInfo *ParamName = nullptr;
8007330f729Sjoerg if (Tok.is(tok::identifier)) {
8017330f729Sjoerg ParamName = Tok.getIdentifierInfo();
8027330f729Sjoerg ConsumeToken();
8037330f729Sjoerg } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
8047330f729Sjoerg tok::greatergreater)) {
8057330f729Sjoerg // Unnamed template parameter. Don't have to do anything here, just
8067330f729Sjoerg // don't consume this token.
8077330f729Sjoerg } else {
8087330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
8097330f729Sjoerg return nullptr;
8107330f729Sjoerg }
8117330f729Sjoerg
8127330f729Sjoerg // Recover from misplaced ellipsis.
8137330f729Sjoerg bool AlreadyHasEllipsis = EllipsisLoc.isValid();
8147330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
8157330f729Sjoerg DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
8167330f729Sjoerg
8177330f729Sjoerg // Grab a default argument (if available).
8187330f729Sjoerg // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
8197330f729Sjoerg // we introduce the type parameter into the local scope.
8207330f729Sjoerg SourceLocation EqualLoc;
8217330f729Sjoerg ParsedType DefaultArg;
8227330f729Sjoerg if (TryConsumeToken(tok::equal, EqualLoc))
823*e038c9c4Sjoerg DefaultArg =
824*e038c9c4Sjoerg ParseTypeName(/*Range=*/nullptr, DeclaratorContext::TemplateTypeArg)
825*e038c9c4Sjoerg .get();
8267330f729Sjoerg
827*e038c9c4Sjoerg NamedDecl *NewDecl = Actions.ActOnTypeParameter(getCurScope(),
828*e038c9c4Sjoerg TypenameKeyword, EllipsisLoc,
829*e038c9c4Sjoerg KeyLoc, ParamName, NameLoc,
830*e038c9c4Sjoerg Depth, Position, EqualLoc,
831*e038c9c4Sjoerg DefaultArg,
832*e038c9c4Sjoerg TypeConstraint != nullptr);
833*e038c9c4Sjoerg
834*e038c9c4Sjoerg if (TypeConstraint) {
835*e038c9c4Sjoerg Actions.ActOnTypeConstraint(TypeConstraintSS, TypeConstraint,
836*e038c9c4Sjoerg cast<TemplateTypeParmDecl>(NewDecl),
837*e038c9c4Sjoerg EllipsisLoc);
838*e038c9c4Sjoerg }
839*e038c9c4Sjoerg
840*e038c9c4Sjoerg return NewDecl;
8417330f729Sjoerg }
8427330f729Sjoerg
8437330f729Sjoerg /// ParseTemplateTemplateParameter - Handle the parsing of template
8447330f729Sjoerg /// template parameters.
8457330f729Sjoerg ///
8467330f729Sjoerg /// type-parameter: [C++ temp.param]
8477330f729Sjoerg /// 'template' '<' template-parameter-list '>' type-parameter-key
8487330f729Sjoerg /// ...[opt] identifier[opt]
8497330f729Sjoerg /// 'template' '<' template-parameter-list '>' type-parameter-key
8507330f729Sjoerg /// identifier[opt] = id-expression
8517330f729Sjoerg /// type-parameter-key:
8527330f729Sjoerg /// 'class'
8537330f729Sjoerg /// 'typename' [C++1z]
8547330f729Sjoerg NamedDecl *
ParseTemplateTemplateParameter(unsigned Depth,unsigned Position)8557330f729Sjoerg Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) {
8567330f729Sjoerg assert(Tok.is(tok::kw_template) && "Expected 'template' keyword");
8577330f729Sjoerg
8587330f729Sjoerg // Handle the template <...> part.
8597330f729Sjoerg SourceLocation TemplateLoc = ConsumeToken();
8607330f729Sjoerg SmallVector<NamedDecl*,8> TemplateParams;
8617330f729Sjoerg SourceLocation LAngleLoc, RAngleLoc;
8627330f729Sjoerg {
863*e038c9c4Sjoerg MultiParseScope TemplateParmScope(*this);
864*e038c9c4Sjoerg if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams,
865*e038c9c4Sjoerg LAngleLoc, RAngleLoc)) {
8667330f729Sjoerg return nullptr;
8677330f729Sjoerg }
8687330f729Sjoerg }
8697330f729Sjoerg
8707330f729Sjoerg // Provide an ExtWarn if the C++1z feature of using 'typename' here is used.
8717330f729Sjoerg // Generate a meaningful error if the user forgot to put class before the
8727330f729Sjoerg // identifier, comma, or greater. Provide a fixit if the identifier, comma,
8737330f729Sjoerg // or greater appear immediately or after 'struct'. In the latter case,
8747330f729Sjoerg // replace the keyword with 'class'.
8757330f729Sjoerg if (!TryConsumeToken(tok::kw_class)) {
8767330f729Sjoerg bool Replace = Tok.isOneOf(tok::kw_typename, tok::kw_struct);
8777330f729Sjoerg const Token &Next = Tok.is(tok::kw_struct) ? NextToken() : Tok;
8787330f729Sjoerg if (Tok.is(tok::kw_typename)) {
8797330f729Sjoerg Diag(Tok.getLocation(),
8807330f729Sjoerg getLangOpts().CPlusPlus17
8817330f729Sjoerg ? diag::warn_cxx14_compat_template_template_param_typename
8827330f729Sjoerg : diag::ext_template_template_param_typename)
8837330f729Sjoerg << (!getLangOpts().CPlusPlus17
8847330f729Sjoerg ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
8857330f729Sjoerg : FixItHint());
8867330f729Sjoerg } else if (Next.isOneOf(tok::identifier, tok::comma, tok::greater,
8877330f729Sjoerg tok::greatergreater, tok::ellipsis)) {
8887330f729Sjoerg Diag(Tok.getLocation(), diag::err_class_on_template_template_param)
8897330f729Sjoerg << (Replace ? FixItHint::CreateReplacement(Tok.getLocation(), "class")
8907330f729Sjoerg : FixItHint::CreateInsertion(Tok.getLocation(), "class "));
8917330f729Sjoerg } else
8927330f729Sjoerg Diag(Tok.getLocation(), diag::err_class_on_template_template_param);
8937330f729Sjoerg
8947330f729Sjoerg if (Replace)
8957330f729Sjoerg ConsumeToken();
8967330f729Sjoerg }
8977330f729Sjoerg
8987330f729Sjoerg // Parse the ellipsis, if given.
8997330f729Sjoerg SourceLocation EllipsisLoc;
9007330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
9017330f729Sjoerg Diag(EllipsisLoc,
9027330f729Sjoerg getLangOpts().CPlusPlus11
9037330f729Sjoerg ? diag::warn_cxx98_compat_variadic_templates
9047330f729Sjoerg : diag::ext_variadic_templates);
9057330f729Sjoerg
9067330f729Sjoerg // Get the identifier, if given.
9077330f729Sjoerg SourceLocation NameLoc = Tok.getLocation();
9087330f729Sjoerg IdentifierInfo *ParamName = nullptr;
9097330f729Sjoerg if (Tok.is(tok::identifier)) {
9107330f729Sjoerg ParamName = Tok.getIdentifierInfo();
9117330f729Sjoerg ConsumeToken();
9127330f729Sjoerg } else if (Tok.isOneOf(tok::equal, tok::comma, tok::greater,
9137330f729Sjoerg tok::greatergreater)) {
9147330f729Sjoerg // Unnamed template parameter. Don't have to do anything here, just
9157330f729Sjoerg // don't consume this token.
9167330f729Sjoerg } else {
9177330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
9187330f729Sjoerg return nullptr;
9197330f729Sjoerg }
9207330f729Sjoerg
9217330f729Sjoerg // Recover from misplaced ellipsis.
9227330f729Sjoerg bool AlreadyHasEllipsis = EllipsisLoc.isValid();
9237330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
9247330f729Sjoerg DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true);
9257330f729Sjoerg
9267330f729Sjoerg TemplateParameterList *ParamList =
9277330f729Sjoerg Actions.ActOnTemplateParameterList(Depth, SourceLocation(),
9287330f729Sjoerg TemplateLoc, LAngleLoc,
9297330f729Sjoerg TemplateParams,
9307330f729Sjoerg RAngleLoc, nullptr);
9317330f729Sjoerg
9327330f729Sjoerg // Grab a default argument (if available).
9337330f729Sjoerg // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
9347330f729Sjoerg // we introduce the template parameter into the local scope.
9357330f729Sjoerg SourceLocation EqualLoc;
9367330f729Sjoerg ParsedTemplateArgument DefaultArg;
9377330f729Sjoerg if (TryConsumeToken(tok::equal, EqualLoc)) {
9387330f729Sjoerg DefaultArg = ParseTemplateTemplateArgument();
9397330f729Sjoerg if (DefaultArg.isInvalid()) {
9407330f729Sjoerg Diag(Tok.getLocation(),
9417330f729Sjoerg diag::err_default_template_template_parameter_not_template);
9427330f729Sjoerg SkipUntil(tok::comma, tok::greater, tok::greatergreater,
9437330f729Sjoerg StopAtSemi | StopBeforeMatch);
9447330f729Sjoerg }
9457330f729Sjoerg }
9467330f729Sjoerg
9477330f729Sjoerg return Actions.ActOnTemplateTemplateParameter(getCurScope(), TemplateLoc,
9487330f729Sjoerg ParamList, EllipsisLoc,
9497330f729Sjoerg ParamName, NameLoc, Depth,
9507330f729Sjoerg Position, EqualLoc, DefaultArg);
9517330f729Sjoerg }
9527330f729Sjoerg
9537330f729Sjoerg /// ParseNonTypeTemplateParameter - Handle the parsing of non-type
9547330f729Sjoerg /// template parameters (e.g., in "template<int Size> class array;").
9557330f729Sjoerg ///
9567330f729Sjoerg /// template-parameter:
9577330f729Sjoerg /// ...
9587330f729Sjoerg /// parameter-declaration
9597330f729Sjoerg NamedDecl *
ParseNonTypeTemplateParameter(unsigned Depth,unsigned Position)9607330f729Sjoerg Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
9617330f729Sjoerg // Parse the declaration-specifiers (i.e., the type).
9627330f729Sjoerg // FIXME: The type should probably be restricted in some way... Not all
9637330f729Sjoerg // declarators (parts of declarators?) are accepted for parameters.
9647330f729Sjoerg DeclSpec DS(AttrFactory);
9657330f729Sjoerg ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
9667330f729Sjoerg DeclSpecContext::DSC_template_param);
9677330f729Sjoerg
9687330f729Sjoerg // Parse this as a typename.
969*e038c9c4Sjoerg Declarator ParamDecl(DS, DeclaratorContext::TemplateParam);
9707330f729Sjoerg ParseDeclarator(ParamDecl);
9717330f729Sjoerg if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
9727330f729Sjoerg Diag(Tok.getLocation(), diag::err_expected_template_parameter);
9737330f729Sjoerg return nullptr;
9747330f729Sjoerg }
9757330f729Sjoerg
9767330f729Sjoerg // Recover from misplaced ellipsis.
9777330f729Sjoerg SourceLocation EllipsisLoc;
9787330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
9797330f729Sjoerg DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, ParamDecl);
9807330f729Sjoerg
9817330f729Sjoerg // If there is a default value, parse it.
9827330f729Sjoerg // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before
9837330f729Sjoerg // we introduce the template parameter into the local scope.
9847330f729Sjoerg SourceLocation EqualLoc;
9857330f729Sjoerg ExprResult DefaultArg;
9867330f729Sjoerg if (TryConsumeToken(tok::equal, EqualLoc)) {
9877330f729Sjoerg // C++ [temp.param]p15:
9887330f729Sjoerg // When parsing a default template-argument for a non-type
9897330f729Sjoerg // template-parameter, the first non-nested > is taken as the
9907330f729Sjoerg // end of the template-parameter-list rather than a greater-than
9917330f729Sjoerg // operator.
9927330f729Sjoerg GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
9937330f729Sjoerg EnterExpressionEvaluationContext ConstantEvaluated(
9947330f729Sjoerg Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
9957330f729Sjoerg
9967330f729Sjoerg DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
9977330f729Sjoerg if (DefaultArg.isInvalid())
9987330f729Sjoerg SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
9997330f729Sjoerg }
10007330f729Sjoerg
10017330f729Sjoerg // Create the parameter.
10027330f729Sjoerg return Actions.ActOnNonTypeTemplateParameter(getCurScope(), ParamDecl,
10037330f729Sjoerg Depth, Position, EqualLoc,
10047330f729Sjoerg DefaultArg.get());
10057330f729Sjoerg }
10067330f729Sjoerg
DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,SourceLocation CorrectLoc,bool AlreadyHasEllipsis,bool IdentifierHasName)10077330f729Sjoerg void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc,
10087330f729Sjoerg SourceLocation CorrectLoc,
10097330f729Sjoerg bool AlreadyHasEllipsis,
10107330f729Sjoerg bool IdentifierHasName) {
10117330f729Sjoerg FixItHint Insertion;
10127330f729Sjoerg if (!AlreadyHasEllipsis)
10137330f729Sjoerg Insertion = FixItHint::CreateInsertion(CorrectLoc, "...");
10147330f729Sjoerg Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
10157330f729Sjoerg << FixItHint::CreateRemoval(EllipsisLoc) << Insertion
10167330f729Sjoerg << !IdentifierHasName;
10177330f729Sjoerg }
10187330f729Sjoerg
DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,Declarator & D)10197330f729Sjoerg void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc,
10207330f729Sjoerg Declarator &D) {
10217330f729Sjoerg assert(EllipsisLoc.isValid());
10227330f729Sjoerg bool AlreadyHasEllipsis = D.getEllipsisLoc().isValid();
10237330f729Sjoerg if (!AlreadyHasEllipsis)
10247330f729Sjoerg D.setEllipsisLoc(EllipsisLoc);
10257330f729Sjoerg DiagnoseMisplacedEllipsis(EllipsisLoc, D.getIdentifierLoc(),
10267330f729Sjoerg AlreadyHasEllipsis, D.hasName());
10277330f729Sjoerg }
10287330f729Sjoerg
10297330f729Sjoerg /// Parses a '>' at the end of a template list.
10307330f729Sjoerg ///
10317330f729Sjoerg /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries
10327330f729Sjoerg /// to determine if these tokens were supposed to be a '>' followed by
10337330f729Sjoerg /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary.
10347330f729Sjoerg ///
10357330f729Sjoerg /// \param RAngleLoc the location of the consumed '>'.
10367330f729Sjoerg ///
10377330f729Sjoerg /// \param ConsumeLastToken if true, the '>' is consumed.
10387330f729Sjoerg ///
10397330f729Sjoerg /// \param ObjCGenericList if true, this is the '>' closing an Objective-C
10407330f729Sjoerg /// type parameter or type argument list, rather than a C++ template parameter
10417330f729Sjoerg /// or argument list.
10427330f729Sjoerg ///
10437330f729Sjoerg /// \returns true, if current token does not start with '>', false otherwise.
ParseGreaterThanInTemplateList(SourceLocation LAngleLoc,SourceLocation & RAngleLoc,bool ConsumeLastToken,bool ObjCGenericList)1044*e038c9c4Sjoerg bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc,
1045*e038c9c4Sjoerg SourceLocation &RAngleLoc,
10467330f729Sjoerg bool ConsumeLastToken,
10477330f729Sjoerg bool ObjCGenericList) {
10487330f729Sjoerg // What will be left once we've consumed the '>'.
10497330f729Sjoerg tok::TokenKind RemainingToken;
10507330f729Sjoerg const char *ReplacementStr = "> >";
10517330f729Sjoerg bool MergeWithNextToken = false;
10527330f729Sjoerg
10537330f729Sjoerg switch (Tok.getKind()) {
10547330f729Sjoerg default:
1055*e038c9c4Sjoerg Diag(getEndOfPreviousToken(), diag::err_expected) << tok::greater;
1056*e038c9c4Sjoerg Diag(LAngleLoc, diag::note_matching) << tok::less;
10577330f729Sjoerg return true;
10587330f729Sjoerg
10597330f729Sjoerg case tok::greater:
10607330f729Sjoerg // Determine the location of the '>' token. Only consume this token
10617330f729Sjoerg // if the caller asked us to.
10627330f729Sjoerg RAngleLoc = Tok.getLocation();
10637330f729Sjoerg if (ConsumeLastToken)
10647330f729Sjoerg ConsumeToken();
10657330f729Sjoerg return false;
10667330f729Sjoerg
10677330f729Sjoerg case tok::greatergreater:
10687330f729Sjoerg RemainingToken = tok::greater;
10697330f729Sjoerg break;
10707330f729Sjoerg
10717330f729Sjoerg case tok::greatergreatergreater:
10727330f729Sjoerg RemainingToken = tok::greatergreater;
10737330f729Sjoerg break;
10747330f729Sjoerg
10757330f729Sjoerg case tok::greaterequal:
10767330f729Sjoerg RemainingToken = tok::equal;
10777330f729Sjoerg ReplacementStr = "> =";
10787330f729Sjoerg
10797330f729Sjoerg // Join two adjacent '=' tokens into one, for cases like:
10807330f729Sjoerg // void (*p)() = f<int>;
10817330f729Sjoerg // return f<int>==p;
10827330f729Sjoerg if (NextToken().is(tok::equal) &&
10837330f729Sjoerg areTokensAdjacent(Tok, NextToken())) {
10847330f729Sjoerg RemainingToken = tok::equalequal;
10857330f729Sjoerg MergeWithNextToken = true;
10867330f729Sjoerg }
10877330f729Sjoerg break;
10887330f729Sjoerg
10897330f729Sjoerg case tok::greatergreaterequal:
10907330f729Sjoerg RemainingToken = tok::greaterequal;
10917330f729Sjoerg break;
10927330f729Sjoerg }
10937330f729Sjoerg
10947330f729Sjoerg // This template-id is terminated by a token that starts with a '>'.
10957330f729Sjoerg // Outside C++11 and Objective-C, this is now error recovery.
10967330f729Sjoerg //
10977330f729Sjoerg // C++11 allows this when the token is '>>', and in CUDA + C++11 mode, we
10987330f729Sjoerg // extend that treatment to also apply to the '>>>' token.
10997330f729Sjoerg //
11007330f729Sjoerg // Objective-C allows this in its type parameter / argument lists.
11017330f729Sjoerg
11027330f729Sjoerg SourceLocation TokBeforeGreaterLoc = PrevTokLocation;
11037330f729Sjoerg SourceLocation TokLoc = Tok.getLocation();
11047330f729Sjoerg Token Next = NextToken();
11057330f729Sjoerg
11067330f729Sjoerg // Whether splitting the current token after the '>' would undesirably result
11077330f729Sjoerg // in the remaining token pasting with the token after it. This excludes the
11087330f729Sjoerg // MergeWithNextToken cases, which we've already handled.
11097330f729Sjoerg bool PreventMergeWithNextToken =
11107330f729Sjoerg (RemainingToken == tok::greater ||
11117330f729Sjoerg RemainingToken == tok::greatergreater) &&
11127330f729Sjoerg (Next.isOneOf(tok::greater, tok::greatergreater,
11137330f729Sjoerg tok::greatergreatergreater, tok::equal, tok::greaterequal,
11147330f729Sjoerg tok::greatergreaterequal, tok::equalequal)) &&
11157330f729Sjoerg areTokensAdjacent(Tok, Next);
11167330f729Sjoerg
11177330f729Sjoerg // Diagnose this situation as appropriate.
11187330f729Sjoerg if (!ObjCGenericList) {
11197330f729Sjoerg // The source range of the replaced token(s).
11207330f729Sjoerg CharSourceRange ReplacementRange = CharSourceRange::getCharRange(
11217330f729Sjoerg TokLoc, Lexer::AdvanceToTokenCharacter(TokLoc, 2, PP.getSourceManager(),
11227330f729Sjoerg getLangOpts()));
11237330f729Sjoerg
11247330f729Sjoerg // A hint to put a space between the '>>'s. In order to make the hint as
11257330f729Sjoerg // clear as possible, we include the characters either side of the space in
11267330f729Sjoerg // the replacement, rather than just inserting a space at SecondCharLoc.
11277330f729Sjoerg FixItHint Hint1 = FixItHint::CreateReplacement(ReplacementRange,
11287330f729Sjoerg ReplacementStr);
11297330f729Sjoerg
11307330f729Sjoerg // A hint to put another space after the token, if it would otherwise be
11317330f729Sjoerg // lexed differently.
11327330f729Sjoerg FixItHint Hint2;
11337330f729Sjoerg if (PreventMergeWithNextToken)
11347330f729Sjoerg Hint2 = FixItHint::CreateInsertion(Next.getLocation(), " ");
11357330f729Sjoerg
11367330f729Sjoerg unsigned DiagId = diag::err_two_right_angle_brackets_need_space;
11377330f729Sjoerg if (getLangOpts().CPlusPlus11 &&
11387330f729Sjoerg (Tok.is(tok::greatergreater) || Tok.is(tok::greatergreatergreater)))
11397330f729Sjoerg DiagId = diag::warn_cxx98_compat_two_right_angle_brackets;
11407330f729Sjoerg else if (Tok.is(tok::greaterequal))
11417330f729Sjoerg DiagId = diag::err_right_angle_bracket_equal_needs_space;
11427330f729Sjoerg Diag(TokLoc, DiagId) << Hint1 << Hint2;
11437330f729Sjoerg }
11447330f729Sjoerg
11457330f729Sjoerg // Find the "length" of the resulting '>' token. This is not always 1, as it
11467330f729Sjoerg // can contain escaped newlines.
11477330f729Sjoerg unsigned GreaterLength = Lexer::getTokenPrefixLength(
11487330f729Sjoerg TokLoc, 1, PP.getSourceManager(), getLangOpts());
11497330f729Sjoerg
11507330f729Sjoerg // Annotate the source buffer to indicate that we split the token after the
11517330f729Sjoerg // '>'. This allows us to properly find the end of, and extract the spelling
11527330f729Sjoerg // of, the '>' token later.
11537330f729Sjoerg RAngleLoc = PP.SplitToken(TokLoc, GreaterLength);
11547330f729Sjoerg
11557330f729Sjoerg // Strip the initial '>' from the token.
11567330f729Sjoerg bool CachingTokens = PP.IsPreviousCachedToken(Tok);
11577330f729Sjoerg
11587330f729Sjoerg Token Greater = Tok;
11597330f729Sjoerg Greater.setLocation(RAngleLoc);
11607330f729Sjoerg Greater.setKind(tok::greater);
11617330f729Sjoerg Greater.setLength(GreaterLength);
11627330f729Sjoerg
11637330f729Sjoerg unsigned OldLength = Tok.getLength();
11647330f729Sjoerg if (MergeWithNextToken) {
11657330f729Sjoerg ConsumeToken();
11667330f729Sjoerg OldLength += Tok.getLength();
11677330f729Sjoerg }
11687330f729Sjoerg
11697330f729Sjoerg Tok.setKind(RemainingToken);
11707330f729Sjoerg Tok.setLength(OldLength - GreaterLength);
11717330f729Sjoerg
11727330f729Sjoerg // Split the second token if lexing it normally would lex a different token
11737330f729Sjoerg // (eg, the fifth token in 'A<B>>>' should re-lex as '>', not '>>').
11747330f729Sjoerg SourceLocation AfterGreaterLoc = TokLoc.getLocWithOffset(GreaterLength);
11757330f729Sjoerg if (PreventMergeWithNextToken)
11767330f729Sjoerg AfterGreaterLoc = PP.SplitToken(AfterGreaterLoc, Tok.getLength());
11777330f729Sjoerg Tok.setLocation(AfterGreaterLoc);
11787330f729Sjoerg
11797330f729Sjoerg // Update the token cache to match what we just did if necessary.
11807330f729Sjoerg if (CachingTokens) {
11817330f729Sjoerg // If the previous cached token is being merged, delete it.
11827330f729Sjoerg if (MergeWithNextToken)
11837330f729Sjoerg PP.ReplacePreviousCachedToken({});
11847330f729Sjoerg
11857330f729Sjoerg if (ConsumeLastToken)
11867330f729Sjoerg PP.ReplacePreviousCachedToken({Greater, Tok});
11877330f729Sjoerg else
11887330f729Sjoerg PP.ReplacePreviousCachedToken({Greater});
11897330f729Sjoerg }
11907330f729Sjoerg
11917330f729Sjoerg if (ConsumeLastToken) {
11927330f729Sjoerg PrevTokLocation = RAngleLoc;
11937330f729Sjoerg } else {
11947330f729Sjoerg PrevTokLocation = TokBeforeGreaterLoc;
11957330f729Sjoerg PP.EnterToken(Tok, /*IsReinject=*/true);
11967330f729Sjoerg Tok = Greater;
11977330f729Sjoerg }
11987330f729Sjoerg
11997330f729Sjoerg return false;
12007330f729Sjoerg }
12017330f729Sjoerg
12027330f729Sjoerg
12037330f729Sjoerg /// Parses a template-id that after the template name has
12047330f729Sjoerg /// already been parsed.
12057330f729Sjoerg ///
12067330f729Sjoerg /// This routine takes care of parsing the enclosed template argument
12077330f729Sjoerg /// list ('<' template-parameter-list [opt] '>') and placing the
12087330f729Sjoerg /// results into a form that can be transferred to semantic analysis.
12097330f729Sjoerg ///
12107330f729Sjoerg /// \param ConsumeLastToken if true, then we will consume the last
12117330f729Sjoerg /// token that forms the template-id. Otherwise, we will leave the
12127330f729Sjoerg /// last token in the stream (e.g., so that it can be replaced with an
12137330f729Sjoerg /// annotation token).
12147330f729Sjoerg bool
ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,SourceLocation & LAngleLoc,TemplateArgList & TemplateArgs,SourceLocation & RAngleLoc)12157330f729Sjoerg Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken,
12167330f729Sjoerg SourceLocation &LAngleLoc,
12177330f729Sjoerg TemplateArgList &TemplateArgs,
12187330f729Sjoerg SourceLocation &RAngleLoc) {
12197330f729Sjoerg assert(Tok.is(tok::less) && "Must have already parsed the template-name");
12207330f729Sjoerg
12217330f729Sjoerg // Consume the '<'.
12227330f729Sjoerg LAngleLoc = ConsumeToken();
12237330f729Sjoerg
12247330f729Sjoerg // Parse the optional template-argument-list.
12257330f729Sjoerg bool Invalid = false;
12267330f729Sjoerg {
12277330f729Sjoerg GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
12287330f729Sjoerg if (!Tok.isOneOf(tok::greater, tok::greatergreater,
12297330f729Sjoerg tok::greatergreatergreater, tok::greaterequal,
12307330f729Sjoerg tok::greatergreaterequal))
12317330f729Sjoerg Invalid = ParseTemplateArgumentList(TemplateArgs);
12327330f729Sjoerg
12337330f729Sjoerg if (Invalid) {
12347330f729Sjoerg // Try to find the closing '>'.
1235*e038c9c4Sjoerg if (getLangOpts().CPlusPlus11)
1236*e038c9c4Sjoerg SkipUntil(tok::greater, tok::greatergreater,
1237*e038c9c4Sjoerg tok::greatergreatergreater, StopAtSemi | StopBeforeMatch);
12387330f729Sjoerg else
12397330f729Sjoerg SkipUntil(tok::greater, StopAtSemi | StopBeforeMatch);
12407330f729Sjoerg }
12417330f729Sjoerg }
12427330f729Sjoerg
1243*e038c9c4Sjoerg return ParseGreaterThanInTemplateList(LAngleLoc, RAngleLoc, ConsumeLastToken,
1244*e038c9c4Sjoerg /*ObjCGenericList=*/false) ||
1245*e038c9c4Sjoerg Invalid;
12467330f729Sjoerg }
12477330f729Sjoerg
12487330f729Sjoerg /// Replace the tokens that form a simple-template-id with an
12497330f729Sjoerg /// annotation token containing the complete template-id.
12507330f729Sjoerg ///
12517330f729Sjoerg /// The first token in the stream must be the name of a template that
12527330f729Sjoerg /// is followed by a '<'. This routine will parse the complete
12537330f729Sjoerg /// simple-template-id and replace the tokens with a single annotation
12547330f729Sjoerg /// token with one of two different kinds: if the template-id names a
12557330f729Sjoerg /// type (and \p AllowTypeAnnotation is true), the annotation token is
12567330f729Sjoerg /// a type annotation that includes the optional nested-name-specifier
12577330f729Sjoerg /// (\p SS). Otherwise, the annotation token is a template-id
12587330f729Sjoerg /// annotation that does not include the optional
12597330f729Sjoerg /// nested-name-specifier.
12607330f729Sjoerg ///
12617330f729Sjoerg /// \param Template the declaration of the template named by the first
12627330f729Sjoerg /// token (an identifier), as returned from \c Action::isTemplateName().
12637330f729Sjoerg ///
12647330f729Sjoerg /// \param TNK the kind of template that \p Template
12657330f729Sjoerg /// refers to, as returned from \c Action::isTemplateName().
12667330f729Sjoerg ///
12677330f729Sjoerg /// \param SS if non-NULL, the nested-name-specifier that precedes
12687330f729Sjoerg /// this template name.
12697330f729Sjoerg ///
12707330f729Sjoerg /// \param TemplateKWLoc if valid, specifies that this template-id
12717330f729Sjoerg /// annotation was preceded by the 'template' keyword and gives the
12727330f729Sjoerg /// location of that keyword. If invalid (the default), then this
12737330f729Sjoerg /// template-id was not preceded by a 'template' keyword.
12747330f729Sjoerg ///
12757330f729Sjoerg /// \param AllowTypeAnnotation if true (the default), then a
12767330f729Sjoerg /// simple-template-id that refers to a class template, template
12777330f729Sjoerg /// template parameter, or other template that produces a type will be
12787330f729Sjoerg /// replaced with a type annotation token. Otherwise, the
12797330f729Sjoerg /// simple-template-id is always replaced with a template-id
12807330f729Sjoerg /// annotation token.
12817330f729Sjoerg ///
1282*e038c9c4Sjoerg /// \param TypeConstraint if true, then this is actually a type-constraint,
1283*e038c9c4Sjoerg /// meaning that the template argument list can be omitted (and the template in
1284*e038c9c4Sjoerg /// question must be a concept).
1285*e038c9c4Sjoerg ///
12867330f729Sjoerg /// If an unrecoverable parse error occurs and no annotation token can be
12877330f729Sjoerg /// formed, this function returns true.
12887330f729Sjoerg ///
AnnotateTemplateIdToken(TemplateTy Template,TemplateNameKind TNK,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,UnqualifiedId & TemplateName,bool AllowTypeAnnotation,bool TypeConstraint)12897330f729Sjoerg bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK,
12907330f729Sjoerg CXXScopeSpec &SS,
12917330f729Sjoerg SourceLocation TemplateKWLoc,
12927330f729Sjoerg UnqualifiedId &TemplateName,
1293*e038c9c4Sjoerg bool AllowTypeAnnotation,
1294*e038c9c4Sjoerg bool TypeConstraint) {
12957330f729Sjoerg assert(getLangOpts().CPlusPlus && "Can only annotate template-ids in C++");
1296*e038c9c4Sjoerg assert((Tok.is(tok::less) || TypeConstraint) &&
12977330f729Sjoerg "Parser isn't at the beginning of a template-id");
1298*e038c9c4Sjoerg assert(!(TypeConstraint && AllowTypeAnnotation) && "type-constraint can't be "
1299*e038c9c4Sjoerg "a type annotation");
1300*e038c9c4Sjoerg assert((!TypeConstraint || TNK == TNK_Concept_template) && "type-constraint "
1301*e038c9c4Sjoerg "must accompany a concept name");
1302*e038c9c4Sjoerg assert((Template || TNK == TNK_Non_template) && "missing template name");
13037330f729Sjoerg
13047330f729Sjoerg // Consume the template-name.
13057330f729Sjoerg SourceLocation TemplateNameLoc = TemplateName.getSourceRange().getBegin();
13067330f729Sjoerg
13077330f729Sjoerg // Parse the enclosed template argument list.
13087330f729Sjoerg SourceLocation LAngleLoc, RAngleLoc;
13097330f729Sjoerg TemplateArgList TemplateArgs;
1310*e038c9c4Sjoerg bool ArgsInvalid = false;
1311*e038c9c4Sjoerg if (!TypeConstraint || Tok.is(tok::less)) {
1312*e038c9c4Sjoerg ArgsInvalid = ParseTemplateIdAfterTemplateName(false, LAngleLoc,
1313*e038c9c4Sjoerg TemplateArgs, RAngleLoc);
1314*e038c9c4Sjoerg // If we couldn't recover from invalid arguments, don't form an annotation
1315*e038c9c4Sjoerg // token -- we don't know how much to annotate.
1316*e038c9c4Sjoerg // FIXME: This can lead to duplicate diagnostics if we retry parsing this
1317*e038c9c4Sjoerg // template-id in another context. Try to annotate anyway?
1318*e038c9c4Sjoerg if (RAngleLoc.isInvalid())
13197330f729Sjoerg return true;
13207330f729Sjoerg }
13217330f729Sjoerg
13227330f729Sjoerg ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs);
13237330f729Sjoerg
13247330f729Sjoerg // Build the annotation token.
13257330f729Sjoerg if (TNK == TNK_Type_template && AllowTypeAnnotation) {
1326*e038c9c4Sjoerg TypeResult Type = ArgsInvalid
1327*e038c9c4Sjoerg ? TypeError()
1328*e038c9c4Sjoerg : Actions.ActOnTemplateIdType(
1329*e038c9c4Sjoerg getCurScope(), SS, TemplateKWLoc, Template,
1330*e038c9c4Sjoerg TemplateName.Identifier, TemplateNameLoc,
1331*e038c9c4Sjoerg LAngleLoc, TemplateArgsPtr, RAngleLoc);
13327330f729Sjoerg
13337330f729Sjoerg Tok.setKind(tok::annot_typename);
1334*e038c9c4Sjoerg setTypeAnnotation(Tok, Type);
13357330f729Sjoerg if (SS.isNotEmpty())
13367330f729Sjoerg Tok.setLocation(SS.getBeginLoc());
13377330f729Sjoerg else if (TemplateKWLoc.isValid())
13387330f729Sjoerg Tok.setLocation(TemplateKWLoc);
13397330f729Sjoerg else
13407330f729Sjoerg Tok.setLocation(TemplateNameLoc);
13417330f729Sjoerg } else {
13427330f729Sjoerg // Build a template-id annotation token that can be processed
13437330f729Sjoerg // later.
13447330f729Sjoerg Tok.setKind(tok::annot_template_id);
13457330f729Sjoerg
13467330f729Sjoerg IdentifierInfo *TemplateII =
13477330f729Sjoerg TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
13487330f729Sjoerg ? TemplateName.Identifier
13497330f729Sjoerg : nullptr;
13507330f729Sjoerg
13517330f729Sjoerg OverloadedOperatorKind OpKind =
13527330f729Sjoerg TemplateName.getKind() == UnqualifiedIdKind::IK_Identifier
13537330f729Sjoerg ? OO_None
13547330f729Sjoerg : TemplateName.OperatorFunctionId.Operator;
13557330f729Sjoerg
13567330f729Sjoerg TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create(
1357*e038c9c4Sjoerg TemplateKWLoc, TemplateNameLoc, TemplateII, OpKind, Template, TNK,
1358*e038c9c4Sjoerg LAngleLoc, RAngleLoc, TemplateArgs, ArgsInvalid, TemplateIds);
13597330f729Sjoerg
13607330f729Sjoerg Tok.setAnnotationValue(TemplateId);
13617330f729Sjoerg if (TemplateKWLoc.isValid())
13627330f729Sjoerg Tok.setLocation(TemplateKWLoc);
13637330f729Sjoerg else
13647330f729Sjoerg Tok.setLocation(TemplateNameLoc);
13657330f729Sjoerg }
13667330f729Sjoerg
13677330f729Sjoerg // Common fields for the annotation token
13687330f729Sjoerg Tok.setAnnotationEndLoc(RAngleLoc);
13697330f729Sjoerg
13707330f729Sjoerg // In case the tokens were cached, have Preprocessor replace them with the
13717330f729Sjoerg // annotation token.
13727330f729Sjoerg PP.AnnotateCachedTokens(Tok);
13737330f729Sjoerg return false;
13747330f729Sjoerg }
13757330f729Sjoerg
13767330f729Sjoerg /// Replaces a template-id annotation token with a type
13777330f729Sjoerg /// annotation token.
13787330f729Sjoerg ///
13797330f729Sjoerg /// If there was a failure when forming the type from the template-id,
13807330f729Sjoerg /// a type annotation token will still be created, but will have a
13817330f729Sjoerg /// NULL type pointer to signify an error.
13827330f729Sjoerg ///
1383*e038c9c4Sjoerg /// \param SS The scope specifier appearing before the template-id, if any.
1384*e038c9c4Sjoerg ///
13857330f729Sjoerg /// \param IsClassName Is this template-id appearing in a context where we
13867330f729Sjoerg /// know it names a class, such as in an elaborated-type-specifier or
13877330f729Sjoerg /// base-specifier? ('typename' and 'template' are unneeded and disallowed
13887330f729Sjoerg /// in those contexts.)
AnnotateTemplateIdTokenAsType(CXXScopeSpec & SS,bool IsClassName)1389*e038c9c4Sjoerg void Parser::AnnotateTemplateIdTokenAsType(CXXScopeSpec &SS,
1390*e038c9c4Sjoerg bool IsClassName) {
13917330f729Sjoerg assert(Tok.is(tok::annot_template_id) && "Requires template-id tokens");
13927330f729Sjoerg
13937330f729Sjoerg TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1394*e038c9c4Sjoerg assert(TemplateId->mightBeType() &&
13957330f729Sjoerg "Only works for type and dependent templates");
13967330f729Sjoerg
13977330f729Sjoerg ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
13987330f729Sjoerg TemplateId->NumArgs);
13997330f729Sjoerg
1400*e038c9c4Sjoerg TypeResult Type =
1401*e038c9c4Sjoerg TemplateId->isInvalid()
1402*e038c9c4Sjoerg ? TypeError()
1403*e038c9c4Sjoerg : Actions.ActOnTemplateIdType(
1404*e038c9c4Sjoerg getCurScope(), SS, TemplateId->TemplateKWLoc,
1405*e038c9c4Sjoerg TemplateId->Template, TemplateId->Name,
1406*e038c9c4Sjoerg TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
1407*e038c9c4Sjoerg TemplateArgsPtr, TemplateId->RAngleLoc,
1408*e038c9c4Sjoerg /*IsCtorOrDtorName*/ false, IsClassName);
14097330f729Sjoerg // Create the new "type" annotation token.
14107330f729Sjoerg Tok.setKind(tok::annot_typename);
1411*e038c9c4Sjoerg setTypeAnnotation(Tok, Type);
1412*e038c9c4Sjoerg if (SS.isNotEmpty()) // it was a C++ qualified type name.
1413*e038c9c4Sjoerg Tok.setLocation(SS.getBeginLoc());
14147330f729Sjoerg // End location stays the same
14157330f729Sjoerg
14167330f729Sjoerg // Replace the template-id annotation token, and possible the scope-specifier
14177330f729Sjoerg // that precedes it, with the typename annotation token.
14187330f729Sjoerg PP.AnnotateCachedTokens(Tok);
14197330f729Sjoerg }
14207330f729Sjoerg
14217330f729Sjoerg /// Determine whether the given token can end a template argument.
isEndOfTemplateArgument(Token Tok)14227330f729Sjoerg static bool isEndOfTemplateArgument(Token Tok) {
1423*e038c9c4Sjoerg // FIXME: Handle '>>>'.
1424*e038c9c4Sjoerg return Tok.isOneOf(tok::comma, tok::greater, tok::greatergreater,
1425*e038c9c4Sjoerg tok::greatergreatergreater);
14267330f729Sjoerg }
14277330f729Sjoerg
14287330f729Sjoerg /// Parse a C++ template template argument.
ParseTemplateTemplateArgument()14297330f729Sjoerg ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() {
14307330f729Sjoerg if (!Tok.is(tok::identifier) && !Tok.is(tok::coloncolon) &&
14317330f729Sjoerg !Tok.is(tok::annot_cxxscope))
14327330f729Sjoerg return ParsedTemplateArgument();
14337330f729Sjoerg
14347330f729Sjoerg // C++0x [temp.arg.template]p1:
14357330f729Sjoerg // A template-argument for a template template-parameter shall be the name
14367330f729Sjoerg // of a class template or an alias template, expressed as id-expression.
14377330f729Sjoerg //
14387330f729Sjoerg // We parse an id-expression that refers to a class template or alias
14397330f729Sjoerg // template. The grammar we parse is:
14407330f729Sjoerg //
14417330f729Sjoerg // nested-name-specifier[opt] template[opt] identifier ...[opt]
14427330f729Sjoerg //
14437330f729Sjoerg // followed by a token that terminates a template argument, such as ',',
14447330f729Sjoerg // '>', or (in some cases) '>>'.
14457330f729Sjoerg CXXScopeSpec SS; // nested-name-specifier, if present
1446*e038c9c4Sjoerg ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1447*e038c9c4Sjoerg /*ObjectHadErrors=*/false,
14487330f729Sjoerg /*EnteringContext=*/false);
14497330f729Sjoerg
14507330f729Sjoerg ParsedTemplateArgument Result;
14517330f729Sjoerg SourceLocation EllipsisLoc;
14527330f729Sjoerg if (SS.isSet() && Tok.is(tok::kw_template)) {
14537330f729Sjoerg // Parse the optional 'template' keyword following the
14547330f729Sjoerg // nested-name-specifier.
14557330f729Sjoerg SourceLocation TemplateKWLoc = ConsumeToken();
14567330f729Sjoerg
14577330f729Sjoerg if (Tok.is(tok::identifier)) {
14587330f729Sjoerg // We appear to have a dependent template name.
14597330f729Sjoerg UnqualifiedId Name;
14607330f729Sjoerg Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
14617330f729Sjoerg ConsumeToken(); // the identifier
14627330f729Sjoerg
14637330f729Sjoerg TryConsumeToken(tok::ellipsis, EllipsisLoc);
14647330f729Sjoerg
1465*e038c9c4Sjoerg // If the next token signals the end of a template argument, then we have
1466*e038c9c4Sjoerg // a (possibly-dependent) template name that could be a template template
1467*e038c9c4Sjoerg // argument.
14687330f729Sjoerg TemplateTy Template;
14697330f729Sjoerg if (isEndOfTemplateArgument(Tok) &&
1470*e038c9c4Sjoerg Actions.ActOnTemplateName(getCurScope(), SS, TemplateKWLoc, Name,
14717330f729Sjoerg /*ObjectType=*/nullptr,
14727330f729Sjoerg /*EnteringContext=*/false, Template))
14737330f729Sjoerg Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
14747330f729Sjoerg }
14757330f729Sjoerg } else if (Tok.is(tok::identifier)) {
14767330f729Sjoerg // We may have a (non-dependent) template name.
14777330f729Sjoerg TemplateTy Template;
14787330f729Sjoerg UnqualifiedId Name;
14797330f729Sjoerg Name.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
14807330f729Sjoerg ConsumeToken(); // the identifier
14817330f729Sjoerg
14827330f729Sjoerg TryConsumeToken(tok::ellipsis, EllipsisLoc);
14837330f729Sjoerg
14847330f729Sjoerg if (isEndOfTemplateArgument(Tok)) {
14857330f729Sjoerg bool MemberOfUnknownSpecialization;
14867330f729Sjoerg TemplateNameKind TNK = Actions.isTemplateName(
14877330f729Sjoerg getCurScope(), SS,
14887330f729Sjoerg /*hasTemplateKeyword=*/false, Name,
14897330f729Sjoerg /*ObjectType=*/nullptr,
14907330f729Sjoerg /*EnteringContext=*/false, Template, MemberOfUnknownSpecialization);
14917330f729Sjoerg if (TNK == TNK_Dependent_template_name || TNK == TNK_Type_template) {
14927330f729Sjoerg // We have an id-expression that refers to a class template or
14937330f729Sjoerg // (C++0x) alias template.
14947330f729Sjoerg Result = ParsedTemplateArgument(SS, Template, Name.StartLocation);
14957330f729Sjoerg }
14967330f729Sjoerg }
14977330f729Sjoerg }
14987330f729Sjoerg
14997330f729Sjoerg // If this is a pack expansion, build it as such.
15007330f729Sjoerg if (EllipsisLoc.isValid() && !Result.isInvalid())
15017330f729Sjoerg Result = Actions.ActOnPackExpansion(Result, EllipsisLoc);
15027330f729Sjoerg
15037330f729Sjoerg return Result;
15047330f729Sjoerg }
15057330f729Sjoerg
15067330f729Sjoerg /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]).
15077330f729Sjoerg ///
15087330f729Sjoerg /// template-argument: [C++ 14.2]
15097330f729Sjoerg /// constant-expression
15107330f729Sjoerg /// type-id
15117330f729Sjoerg /// id-expression
ParseTemplateArgument()15127330f729Sjoerg ParsedTemplateArgument Parser::ParseTemplateArgument() {
15137330f729Sjoerg // C++ [temp.arg]p2:
15147330f729Sjoerg // In a template-argument, an ambiguity between a type-id and an
15157330f729Sjoerg // expression is resolved to a type-id, regardless of the form of
15167330f729Sjoerg // the corresponding template-parameter.
15177330f729Sjoerg //
15187330f729Sjoerg // Therefore, we initially try to parse a type-id - and isCXXTypeId might look
15197330f729Sjoerg // up and annotate an identifier as an id-expression during disambiguation,
15207330f729Sjoerg // so enter the appropriate context for a constant expression template
15217330f729Sjoerg // argument before trying to disambiguate.
15227330f729Sjoerg
15237330f729Sjoerg EnterExpressionEvaluationContext EnterConstantEvaluated(
15247330f729Sjoerg Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
15257330f729Sjoerg /*LambdaContextDecl=*/nullptr,
15267330f729Sjoerg /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_TemplateArgument);
15277330f729Sjoerg if (isCXXTypeId(TypeIdAsTemplateArgument)) {
15287330f729Sjoerg TypeResult TypeArg = ParseTypeName(
1529*e038c9c4Sjoerg /*Range=*/nullptr, DeclaratorContext::TemplateArg);
15307330f729Sjoerg return Actions.ActOnTemplateTypeArgument(TypeArg);
15317330f729Sjoerg }
15327330f729Sjoerg
15337330f729Sjoerg // Try to parse a template template argument.
15347330f729Sjoerg {
15357330f729Sjoerg TentativeParsingAction TPA(*this);
15367330f729Sjoerg
15377330f729Sjoerg ParsedTemplateArgument TemplateTemplateArgument
15387330f729Sjoerg = ParseTemplateTemplateArgument();
15397330f729Sjoerg if (!TemplateTemplateArgument.isInvalid()) {
15407330f729Sjoerg TPA.Commit();
15417330f729Sjoerg return TemplateTemplateArgument;
15427330f729Sjoerg }
15437330f729Sjoerg
15447330f729Sjoerg // Revert this tentative parse to parse a non-type template argument.
15457330f729Sjoerg TPA.Revert();
15467330f729Sjoerg }
15477330f729Sjoerg
15487330f729Sjoerg // Parse a non-type template argument.
15497330f729Sjoerg SourceLocation Loc = Tok.getLocation();
15507330f729Sjoerg ExprResult ExprArg = ParseConstantExpressionInExprEvalContext(MaybeTypeCast);
1551*e038c9c4Sjoerg if (ExprArg.isInvalid() || !ExprArg.get()) {
15527330f729Sjoerg return ParsedTemplateArgument();
1553*e038c9c4Sjoerg }
15547330f729Sjoerg
15557330f729Sjoerg return ParsedTemplateArgument(ParsedTemplateArgument::NonType,
15567330f729Sjoerg ExprArg.get(), Loc);
15577330f729Sjoerg }
15587330f729Sjoerg
15597330f729Sjoerg /// ParseTemplateArgumentList - Parse a C++ template-argument-list
15607330f729Sjoerg /// (C++ [temp.names]). Returns true if there was an error.
15617330f729Sjoerg ///
15627330f729Sjoerg /// template-argument-list: [C++ 14.2]
15637330f729Sjoerg /// template-argument
15647330f729Sjoerg /// template-argument-list ',' template-argument
15657330f729Sjoerg bool
ParseTemplateArgumentList(TemplateArgList & TemplateArgs)15667330f729Sjoerg Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs) {
15677330f729Sjoerg
15687330f729Sjoerg ColonProtectionRAIIObject ColonProtection(*this, false);
15697330f729Sjoerg
15707330f729Sjoerg do {
15717330f729Sjoerg ParsedTemplateArgument Arg = ParseTemplateArgument();
15727330f729Sjoerg SourceLocation EllipsisLoc;
15737330f729Sjoerg if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
15747330f729Sjoerg Arg = Actions.ActOnPackExpansion(Arg, EllipsisLoc);
15757330f729Sjoerg
1576*e038c9c4Sjoerg if (Arg.isInvalid())
15777330f729Sjoerg return true;
15787330f729Sjoerg
15797330f729Sjoerg // Save this template argument.
15807330f729Sjoerg TemplateArgs.push_back(Arg);
15817330f729Sjoerg
15827330f729Sjoerg // If the next token is a comma, consume it and keep reading
15837330f729Sjoerg // arguments.
15847330f729Sjoerg } while (TryConsumeToken(tok::comma));
15857330f729Sjoerg
15867330f729Sjoerg return false;
15877330f729Sjoerg }
15887330f729Sjoerg
15897330f729Sjoerg /// Parse a C++ explicit template instantiation
15907330f729Sjoerg /// (C++ [temp.explicit]).
15917330f729Sjoerg ///
15927330f729Sjoerg /// explicit-instantiation:
15937330f729Sjoerg /// 'extern' [opt] 'template' declaration
15947330f729Sjoerg ///
15957330f729Sjoerg /// Note that the 'extern' is a GNU extension and C++11 feature.
ParseExplicitInstantiation(DeclaratorContext Context,SourceLocation ExternLoc,SourceLocation TemplateLoc,SourceLocation & DeclEnd,ParsedAttributes & AccessAttrs,AccessSpecifier AS)15967330f729Sjoerg Decl *Parser::ParseExplicitInstantiation(DeclaratorContext Context,
15977330f729Sjoerg SourceLocation ExternLoc,
15987330f729Sjoerg SourceLocation TemplateLoc,
15997330f729Sjoerg SourceLocation &DeclEnd,
16007330f729Sjoerg ParsedAttributes &AccessAttrs,
16017330f729Sjoerg AccessSpecifier AS) {
16027330f729Sjoerg // This isn't really required here.
16037330f729Sjoerg ParsingDeclRAIIObject
16047330f729Sjoerg ParsingTemplateParams(*this, ParsingDeclRAIIObject::NoParent);
16057330f729Sjoerg
16067330f729Sjoerg return ParseSingleDeclarationAfterTemplate(
16077330f729Sjoerg Context, ParsedTemplateInfo(ExternLoc, TemplateLoc),
16087330f729Sjoerg ParsingTemplateParams, DeclEnd, AccessAttrs, AS);
16097330f729Sjoerg }
16107330f729Sjoerg
getSourceRange() const16117330f729Sjoerg SourceRange Parser::ParsedTemplateInfo::getSourceRange() const {
16127330f729Sjoerg if (TemplateParams)
16137330f729Sjoerg return getTemplateParamsRange(TemplateParams->data(),
16147330f729Sjoerg TemplateParams->size());
16157330f729Sjoerg
16167330f729Sjoerg SourceRange R(TemplateLoc);
16177330f729Sjoerg if (ExternLoc.isValid())
16187330f729Sjoerg R.setBegin(ExternLoc);
16197330f729Sjoerg return R;
16207330f729Sjoerg }
16217330f729Sjoerg
LateTemplateParserCallback(void * P,LateParsedTemplate & LPT)16227330f729Sjoerg void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) {
16237330f729Sjoerg ((Parser *)P)->ParseLateTemplatedFuncDef(LPT);
16247330f729Sjoerg }
16257330f729Sjoerg
16267330f729Sjoerg /// Late parse a C++ function template in Microsoft mode.
ParseLateTemplatedFuncDef(LateParsedTemplate & LPT)16277330f729Sjoerg void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
16287330f729Sjoerg if (!LPT.D)
16297330f729Sjoerg return;
16307330f729Sjoerg
1631*e038c9c4Sjoerg // Destroy TemplateIdAnnotations when we're done, if possible.
1632*e038c9c4Sjoerg DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
1633*e038c9c4Sjoerg
16347330f729Sjoerg // Get the FunctionDecl.
16357330f729Sjoerg FunctionDecl *FunD = LPT.D->getAsFunction();
16367330f729Sjoerg // Track template parameter depth.
16377330f729Sjoerg TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
16387330f729Sjoerg
16397330f729Sjoerg // To restore the context after late parsing.
16407330f729Sjoerg Sema::ContextRAII GlobalSavedContext(
16417330f729Sjoerg Actions, Actions.Context.getTranslationUnitDecl());
16427330f729Sjoerg
1643*e038c9c4Sjoerg MultiParseScope Scopes(*this);
16447330f729Sjoerg
1645*e038c9c4Sjoerg // Get the list of DeclContexts to reenter.
1646*e038c9c4Sjoerg SmallVector<DeclContext*, 4> DeclContextsToReenter;
1647*e038c9c4Sjoerg for (DeclContext *DC = FunD; DC && !DC->isTranslationUnit();
1648*e038c9c4Sjoerg DC = DC->getLexicalParent())
1649*e038c9c4Sjoerg DeclContextsToReenter.push_back(DC);
16507330f729Sjoerg
1651*e038c9c4Sjoerg // Reenter scopes from outermost to innermost.
1652*e038c9c4Sjoerg for (DeclContext *DC : reverse(DeclContextsToReenter)) {
1653*e038c9c4Sjoerg CurTemplateDepthTracker.addDepth(
1654*e038c9c4Sjoerg ReenterTemplateScopes(Scopes, cast<Decl>(DC)));
1655*e038c9c4Sjoerg Scopes.Enter(Scope::DeclScope);
1656*e038c9c4Sjoerg // We'll reenter the function context itself below.
1657*e038c9c4Sjoerg if (DC != FunD)
1658*e038c9c4Sjoerg Actions.PushDeclContext(Actions.getCurScope(), DC);
16597330f729Sjoerg }
16607330f729Sjoerg
16617330f729Sjoerg assert(!LPT.Toks.empty() && "Empty body!");
16627330f729Sjoerg
16637330f729Sjoerg // Append the current token at the end of the new token stream so that it
16647330f729Sjoerg // doesn't get lost.
16657330f729Sjoerg LPT.Toks.push_back(Tok);
16667330f729Sjoerg PP.EnterTokenStream(LPT.Toks, true, /*IsReinject*/true);
16677330f729Sjoerg
16687330f729Sjoerg // Consume the previously pushed token.
16697330f729Sjoerg ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
16707330f729Sjoerg assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) &&
16717330f729Sjoerg "Inline method not starting with '{', ':' or 'try'");
16727330f729Sjoerg
16737330f729Sjoerg // Parse the method body. Function body parsing code is similar enough
16747330f729Sjoerg // to be re-used for method bodies as well.
16757330f729Sjoerg ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
16767330f729Sjoerg Scope::CompoundStmtScope);
16777330f729Sjoerg
16787330f729Sjoerg // Recreate the containing function DeclContext.
1679*e038c9c4Sjoerg Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent());
16807330f729Sjoerg
16817330f729Sjoerg Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
16827330f729Sjoerg
16837330f729Sjoerg if (Tok.is(tok::kw_try)) {
16847330f729Sjoerg ParseFunctionTryBlock(LPT.D, FnScope);
16857330f729Sjoerg } else {
16867330f729Sjoerg if (Tok.is(tok::colon))
16877330f729Sjoerg ParseConstructorInitializer(LPT.D);
16887330f729Sjoerg else
16897330f729Sjoerg Actions.ActOnDefaultCtorInitializers(LPT.D);
16907330f729Sjoerg
16917330f729Sjoerg if (Tok.is(tok::l_brace)) {
16927330f729Sjoerg assert((!isa<FunctionTemplateDecl>(LPT.D) ||
16937330f729Sjoerg cast<FunctionTemplateDecl>(LPT.D)
16947330f729Sjoerg ->getTemplateParameters()
16957330f729Sjoerg ->getDepth() == TemplateParameterDepth - 1) &&
16967330f729Sjoerg "TemplateParameterDepth should be greater than the depth of "
16977330f729Sjoerg "current template being instantiated!");
16987330f729Sjoerg ParseFunctionStatementBody(LPT.D, FnScope);
16997330f729Sjoerg Actions.UnmarkAsLateParsedTemplate(FunD);
17007330f729Sjoerg } else
17017330f729Sjoerg Actions.ActOnFinishFunctionBody(LPT.D, nullptr);
17027330f729Sjoerg }
17037330f729Sjoerg }
17047330f729Sjoerg
17057330f729Sjoerg /// Lex a delayed template function for late parsing.
LexTemplateFunctionForLateParsing(CachedTokens & Toks)17067330f729Sjoerg void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) {
17077330f729Sjoerg tok::TokenKind kind = Tok.getKind();
17087330f729Sjoerg if (!ConsumeAndStoreFunctionPrologue(Toks)) {
17097330f729Sjoerg // Consume everything up to (and including) the matching right brace.
17107330f729Sjoerg ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
17117330f729Sjoerg }
17127330f729Sjoerg
17137330f729Sjoerg // If we're in a function-try-block, we need to store all the catch blocks.
17147330f729Sjoerg if (kind == tok::kw_try) {
17157330f729Sjoerg while (Tok.is(tok::kw_catch)) {
17167330f729Sjoerg ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
17177330f729Sjoerg ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
17187330f729Sjoerg }
17197330f729Sjoerg }
17207330f729Sjoerg }
17217330f729Sjoerg
17227330f729Sjoerg /// We've parsed something that could plausibly be intended to be a template
17237330f729Sjoerg /// name (\p LHS) followed by a '<' token, and the following code can't possibly
17247330f729Sjoerg /// be an expression. Determine if this is likely to be a template-id and if so,
17257330f729Sjoerg /// diagnose it.
diagnoseUnknownTemplateId(ExprResult LHS,SourceLocation Less)17267330f729Sjoerg bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) {
17277330f729Sjoerg TentativeParsingAction TPA(*this);
17287330f729Sjoerg // FIXME: We could look at the token sequence in a lot more detail here.
17297330f729Sjoerg if (SkipUntil(tok::greater, tok::greatergreater, tok::greatergreatergreater,
17307330f729Sjoerg StopAtSemi | StopBeforeMatch)) {
17317330f729Sjoerg TPA.Commit();
17327330f729Sjoerg
17337330f729Sjoerg SourceLocation Greater;
1734*e038c9c4Sjoerg ParseGreaterThanInTemplateList(Less, Greater, true, false);
17357330f729Sjoerg Actions.diagnoseExprIntendedAsTemplateName(getCurScope(), LHS,
17367330f729Sjoerg Less, Greater);
17377330f729Sjoerg return true;
17387330f729Sjoerg }
17397330f729Sjoerg
17407330f729Sjoerg // There's no matching '>' token, this probably isn't supposed to be
17417330f729Sjoerg // interpreted as a template-id. Parse it as an (ill-formed) comparison.
17427330f729Sjoerg TPA.Revert();
17437330f729Sjoerg return false;
17447330f729Sjoerg }
17457330f729Sjoerg
checkPotentialAngleBracket(ExprResult & PotentialTemplateName)17467330f729Sjoerg void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) {
17477330f729Sjoerg assert(Tok.is(tok::less) && "not at a potential angle bracket");
17487330f729Sjoerg
17497330f729Sjoerg bool DependentTemplateName = false;
17507330f729Sjoerg if (!Actions.mightBeIntendedToBeTemplateName(PotentialTemplateName,
17517330f729Sjoerg DependentTemplateName))
17527330f729Sjoerg return;
17537330f729Sjoerg
17547330f729Sjoerg // OK, this might be a name that the user intended to be parsed as a
17557330f729Sjoerg // template-name, followed by a '<' token. Check for some easy cases.
17567330f729Sjoerg
17577330f729Sjoerg // If we have potential_template<>, then it's supposed to be a template-name.
17587330f729Sjoerg if (NextToken().is(tok::greater) ||
17597330f729Sjoerg (getLangOpts().CPlusPlus11 &&
17607330f729Sjoerg NextToken().isOneOf(tok::greatergreater, tok::greatergreatergreater))) {
17617330f729Sjoerg SourceLocation Less = ConsumeToken();
17627330f729Sjoerg SourceLocation Greater;
1763*e038c9c4Sjoerg ParseGreaterThanInTemplateList(Less, Greater, true, false);
17647330f729Sjoerg Actions.diagnoseExprIntendedAsTemplateName(
17657330f729Sjoerg getCurScope(), PotentialTemplateName, Less, Greater);
17667330f729Sjoerg // FIXME: Perform error recovery.
17677330f729Sjoerg PotentialTemplateName = ExprError();
17687330f729Sjoerg return;
17697330f729Sjoerg }
17707330f729Sjoerg
17717330f729Sjoerg // If we have 'potential_template<type-id', assume it's supposed to be a
17727330f729Sjoerg // template-name if there's a matching '>' later on.
17737330f729Sjoerg {
17747330f729Sjoerg // FIXME: Avoid the tentative parse when NextToken() can't begin a type.
17757330f729Sjoerg TentativeParsingAction TPA(*this);
17767330f729Sjoerg SourceLocation Less = ConsumeToken();
17777330f729Sjoerg if (isTypeIdUnambiguously() &&
17787330f729Sjoerg diagnoseUnknownTemplateId(PotentialTemplateName, Less)) {
17797330f729Sjoerg TPA.Commit();
17807330f729Sjoerg // FIXME: Perform error recovery.
17817330f729Sjoerg PotentialTemplateName = ExprError();
17827330f729Sjoerg return;
17837330f729Sjoerg }
17847330f729Sjoerg TPA.Revert();
17857330f729Sjoerg }
17867330f729Sjoerg
17877330f729Sjoerg // Otherwise, remember that we saw this in case we see a potentially-matching
17887330f729Sjoerg // '>' token later on.
17897330f729Sjoerg AngleBracketTracker::Priority Priority =
17907330f729Sjoerg (DependentTemplateName ? AngleBracketTracker::DependentName
17917330f729Sjoerg : AngleBracketTracker::PotentialTypo) |
17927330f729Sjoerg (Tok.hasLeadingSpace() ? AngleBracketTracker::SpaceBeforeLess
17937330f729Sjoerg : AngleBracketTracker::NoSpaceBeforeLess);
17947330f729Sjoerg AngleBrackets.add(*this, PotentialTemplateName.get(), Tok.getLocation(),
17957330f729Sjoerg Priority);
17967330f729Sjoerg }
17977330f729Sjoerg
checkPotentialAngleBracketDelimiter(const AngleBracketTracker::Loc & LAngle,const Token & OpToken)17987330f729Sjoerg bool Parser::checkPotentialAngleBracketDelimiter(
17997330f729Sjoerg const AngleBracketTracker::Loc &LAngle, const Token &OpToken) {
18007330f729Sjoerg // If a comma in an expression context is followed by a type that can be a
18017330f729Sjoerg // template argument and cannot be an expression, then this is ill-formed,
18027330f729Sjoerg // but might be intended to be part of a template-id.
18037330f729Sjoerg if (OpToken.is(tok::comma) && isTypeIdUnambiguously() &&
18047330f729Sjoerg diagnoseUnknownTemplateId(LAngle.TemplateName, LAngle.LessLoc)) {
18057330f729Sjoerg AngleBrackets.clear(*this);
18067330f729Sjoerg return true;
18077330f729Sjoerg }
18087330f729Sjoerg
18097330f729Sjoerg // If a context that looks like a template-id is followed by '()', then
18107330f729Sjoerg // this is ill-formed, but might be intended to be a template-id
18117330f729Sjoerg // followed by '()'.
18127330f729Sjoerg if (OpToken.is(tok::greater) && Tok.is(tok::l_paren) &&
18137330f729Sjoerg NextToken().is(tok::r_paren)) {
18147330f729Sjoerg Actions.diagnoseExprIntendedAsTemplateName(
18157330f729Sjoerg getCurScope(), LAngle.TemplateName, LAngle.LessLoc,
18167330f729Sjoerg OpToken.getLocation());
18177330f729Sjoerg AngleBrackets.clear(*this);
18187330f729Sjoerg return true;
18197330f729Sjoerg }
18207330f729Sjoerg
18217330f729Sjoerg // After a '>' (etc), we're no longer potentially in a construct that's
18227330f729Sjoerg // intended to be treated as a template-id.
18237330f729Sjoerg if (OpToken.is(tok::greater) ||
18247330f729Sjoerg (getLangOpts().CPlusPlus11 &&
18257330f729Sjoerg OpToken.isOneOf(tok::greatergreater, tok::greatergreatergreater)))
18267330f729Sjoerg AngleBrackets.clear(*this);
18277330f729Sjoerg return false;
18287330f729Sjoerg }
1829