//===--- ParseTemplate.cpp - Template Parsing -----------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements parsing of C++ templates. // //===----------------------------------------------------------------------===// #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/ExprCXX.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" #include "clang/Parse/RAIIObjectsForParser.h" #include "clang/Sema/DeclSpec.h" #include "clang/Sema/EnterExpressionEvaluationContext.h" #include "clang/Sema/ParsedTemplate.h" #include "clang/Sema/Scope.h" #include "clang/Sema/SemaDiagnostic.h" #include "llvm/Support/TimeProfiler.h" usingnamespaceclang; /// Re-enter a possible template scope, creating as many template parameter /// scopes as necessary. /// \return The number of template parameter scopes entered. unsigned Parser::ReenterTemplateScopes(MultiParseScope &S, Decl *D) { … } /// Parse a template declaration, explicit instantiation, or /// explicit specialization. Parser::DeclGroupPtrTy Parser::ParseDeclarationStartingWithTemplate(DeclaratorContext Context, SourceLocation &DeclEnd, ParsedAttributes &AccessAttrs) { … } /// Parse a template declaration or an explicit specialization. /// /// Template declarations include one or more template parameter lists /// and either the function or class template declaration. Explicit /// specializations contain one or more 'template < >' prefixes /// followed by a (possibly templated) declaration. Since the /// syntactic form of both features is nearly identical, we parse all /// of the template headers together and let semantic analysis sort /// the declarations from the explicit specializations. /// /// template-declaration: [C++ temp] /// 'export'[opt] 'template' '<' template-parameter-list '>' declaration /// /// template-declaration: [C++2a] /// template-head declaration /// template-head concept-definition /// /// TODO: requires-clause /// template-head: [C++2a] /// 'template' '<' template-parameter-list '>' /// requires-clause[opt] /// /// explicit-specialization: [ C++ temp.expl.spec] /// 'template' '<' '>' declaration Parser::DeclGroupPtrTy Parser::ParseTemplateDeclarationOrSpecialization( DeclaratorContext Context, SourceLocation &DeclEnd, ParsedAttributes &AccessAttrs, AccessSpecifier AS) { … } /// Parse a single declaration that declares a template, /// template specialization, or explicit instantiation of a template. /// /// \param DeclEnd will receive the source location of the last token /// within this declaration. /// /// \param AS the access specifier associated with this /// declaration. Will be AS_none for namespace-scope declarations. /// /// \returns the new declaration. Parser::DeclGroupPtrTy Parser::ParseDeclarationAfterTemplate( DeclaratorContext Context, ParsedTemplateInfo &TemplateInfo, ParsingDeclRAIIObject &DiagsFromTParams, SourceLocation &DeclEnd, ParsedAttributes &AccessAttrs, AccessSpecifier AS) { … } /// \brief Parse a single declaration that declares a concept. /// /// \param DeclEnd will receive the source location of the last token /// within this declaration. /// /// \returns the new declaration. Decl * Parser::ParseConceptDefinition(const ParsedTemplateInfo &TemplateInfo, SourceLocation &DeclEnd) { … } /// ParseTemplateParameters - Parses a template-parameter-list enclosed in /// angle brackets. Depth is the depth of this template-parameter-list, which /// is the number of template headers directly enclosing this template header. /// TemplateParams is the current list of template parameters we're building. /// The template parameter we parse will be added to this list. LAngleLoc and /// RAngleLoc will receive the positions of the '<' and '>', respectively, /// that enclose this template parameter list. /// /// \returns true if an error occurred, false otherwise. bool Parser::ParseTemplateParameters( MultiParseScope &TemplateScopes, unsigned Depth, SmallVectorImpl<NamedDecl *> &TemplateParams, SourceLocation &LAngleLoc, SourceLocation &RAngleLoc) { … } /// ParseTemplateParameterList - Parse a template parameter list. If /// the parsing fails badly (i.e., closing bracket was left out), this /// will try to put the token stream in a reasonable position (closing /// a statement, etc.) and return false. /// /// template-parameter-list: [C++ temp] /// template-parameter /// template-parameter-list ',' template-parameter bool Parser::ParseTemplateParameterList(const unsigned Depth, SmallVectorImpl<NamedDecl*> &TemplateParams) { … } /// Determine whether the parser is at the start of a template /// type parameter. Parser::TPResult Parser::isStartOfTemplateTypeParameter() { … } /// ParseTemplateParameter - Parse a template-parameter (C++ [temp.param]). /// /// template-parameter: [C++ temp.param] /// type-parameter /// parameter-declaration /// /// type-parameter: (See below) /// type-parameter-key ...[opt] identifier[opt] /// type-parameter-key identifier[opt] = type-id /// (C++2a) type-constraint ...[opt] identifier[opt] /// (C++2a) type-constraint identifier[opt] = type-id /// 'template' '<' template-parameter-list '>' type-parameter-key /// ...[opt] identifier[opt] /// 'template' '<' template-parameter-list '>' type-parameter-key /// identifier[opt] '=' id-expression /// /// type-parameter-key: /// class /// typename /// NamedDecl *Parser::ParseTemplateParameter(unsigned Depth, unsigned Position) { … } /// Check whether the current token is a template-id annotation denoting a /// type-constraint. bool Parser::isTypeConstraintAnnotation() { … } /// Try parsing a type-constraint at the current location. /// /// type-constraint: /// nested-name-specifier[opt] concept-name /// nested-name-specifier[opt] concept-name /// '<' template-argument-list[opt] '>'[opt] /// /// \returns true if an error occurred, and false otherwise. bool Parser::TryAnnotateTypeConstraint() { … } /// ParseTypeParameter - Parse a template type parameter (C++ [temp.param]). /// Other kinds of template parameters are parsed in /// ParseTemplateTemplateParameter and ParseNonTypeTemplateParameter. /// /// type-parameter: [C++ temp.param] /// 'class' ...[opt][C++0x] identifier[opt] /// 'class' identifier[opt] '=' type-id /// 'typename' ...[opt][C++0x] identifier[opt] /// 'typename' identifier[opt] '=' type-id NamedDecl *Parser::ParseTypeParameter(unsigned Depth, unsigned Position) { … } /// ParseTemplateTemplateParameter - Handle the parsing of template /// template parameters. /// /// type-parameter: [C++ temp.param] /// template-head type-parameter-key ...[opt] identifier[opt] /// template-head type-parameter-key identifier[opt] = id-expression /// type-parameter-key: /// 'class' /// 'typename' [C++1z] /// template-head: [C++2a] /// 'template' '<' template-parameter-list '>' /// requires-clause[opt] NamedDecl *Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { … } /// ParseNonTypeTemplateParameter - Handle the parsing of non-type /// template parameters (e.g., in "template<int Size> class array;"). /// /// template-parameter: /// ... /// parameter-declaration NamedDecl * Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) { … } void Parser::DiagnoseMisplacedEllipsis(SourceLocation EllipsisLoc, SourceLocation CorrectLoc, bool AlreadyHasEllipsis, bool IdentifierHasName) { … } void Parser::DiagnoseMisplacedEllipsisInDeclarator(SourceLocation EllipsisLoc, Declarator &D) { … } /// Parses a '>' at the end of a template list. /// /// If this function encounters '>>', '>>>', '>=', or '>>=', it tries /// to determine if these tokens were supposed to be a '>' followed by /// '>', '>>', '>=', or '>='. It emits an appropriate diagnostic if necessary. /// /// \param RAngleLoc the location of the consumed '>'. /// /// \param ConsumeLastToken if true, the '>' is consumed. /// /// \param ObjCGenericList if true, this is the '>' closing an Objective-C /// type parameter or type argument list, rather than a C++ template parameter /// or argument list. /// /// \returns true, if current token does not start with '>', false otherwise. bool Parser::ParseGreaterThanInTemplateList(SourceLocation LAngleLoc, SourceLocation &RAngleLoc, bool ConsumeLastToken, bool ObjCGenericList) { … } /// Parses a template-id that after the template name has /// already been parsed. /// /// This routine takes care of parsing the enclosed template argument /// list ('<' template-parameter-list [opt] '>') and placing the /// results into a form that can be transferred to semantic analysis. /// /// \param ConsumeLastToken if true, then we will consume the last /// token that forms the template-id. Otherwise, we will leave the /// last token in the stream (e.g., so that it can be replaced with an /// annotation token). bool Parser::ParseTemplateIdAfterTemplateName(bool ConsumeLastToken, SourceLocation &LAngleLoc, TemplateArgList &TemplateArgs, SourceLocation &RAngleLoc, TemplateTy Template) { … } /// Replace the tokens that form a simple-template-id with an /// annotation token containing the complete template-id. /// /// The first token in the stream must be the name of a template that /// is followed by a '<'. This routine will parse the complete /// simple-template-id and replace the tokens with a single annotation /// token with one of two different kinds: if the template-id names a /// type (and \p AllowTypeAnnotation is true), the annotation token is /// a type annotation that includes the optional nested-name-specifier /// (\p SS). Otherwise, the annotation token is a template-id /// annotation that does not include the optional /// nested-name-specifier. /// /// \param Template the declaration of the template named by the first /// token (an identifier), as returned from \c Action::isTemplateName(). /// /// \param TNK the kind of template that \p Template /// refers to, as returned from \c Action::isTemplateName(). /// /// \param SS if non-NULL, the nested-name-specifier that precedes /// this template name. /// /// \param TemplateKWLoc if valid, specifies that this template-id /// annotation was preceded by the 'template' keyword and gives the /// location of that keyword. If invalid (the default), then this /// template-id was not preceded by a 'template' keyword. /// /// \param AllowTypeAnnotation if true (the default), then a /// simple-template-id that refers to a class template, template /// template parameter, or other template that produces a type will be /// replaced with a type annotation token. Otherwise, the /// simple-template-id is always replaced with a template-id /// annotation token. /// /// \param TypeConstraint if true, then this is actually a type-constraint, /// meaning that the template argument list can be omitted (and the template in /// question must be a concept). /// /// If an unrecoverable parse error occurs and no annotation token can be /// formed, this function returns true. /// bool Parser::AnnotateTemplateIdToken(TemplateTy Template, TemplateNameKind TNK, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, UnqualifiedId &TemplateName, bool AllowTypeAnnotation, bool TypeConstraint) { … } /// Replaces a template-id annotation token with a type /// annotation token. /// /// If there was a failure when forming the type from the template-id, /// a type annotation token will still be created, but will have a /// NULL type pointer to signify an error. /// /// \param SS The scope specifier appearing before the template-id, if any. /// /// \param AllowImplicitTypename whether this is a context where T::type /// denotes a dependent type. /// \param IsClassName Is this template-id appearing in a context where we /// know it names a class, such as in an elaborated-type-specifier or /// base-specifier? ('typename' and 'template' are unneeded and disallowed /// in those contexts.) void Parser::AnnotateTemplateIdTokenAsType( CXXScopeSpec &SS, ImplicitTypenameContext AllowImplicitTypename, bool IsClassName) { … } /// Determine whether the given token can end a template argument. static bool isEndOfTemplateArgument(Token Tok) { … } /// Parse a C++ template template argument. ParsedTemplateArgument Parser::ParseTemplateTemplateArgument() { … } /// ParseTemplateArgument - Parse a C++ template argument (C++ [temp.names]). /// /// template-argument: [C++ 14.2] /// constant-expression /// type-id /// id-expression /// braced-init-list [C++26, DR] /// ParsedTemplateArgument Parser::ParseTemplateArgument() { … } /// ParseTemplateArgumentList - Parse a C++ template-argument-list /// (C++ [temp.names]). Returns true if there was an error. /// /// template-argument-list: [C++ 14.2] /// template-argument /// template-argument-list ',' template-argument /// /// \param Template is only used for code completion, and may be null. bool Parser::ParseTemplateArgumentList(TemplateArgList &TemplateArgs, TemplateTy Template, SourceLocation OpenLoc) { … } /// Parse a C++ explicit template instantiation /// (C++ [temp.explicit]). /// /// explicit-instantiation: /// 'extern' [opt] 'template' declaration /// /// Note that the 'extern' is a GNU extension and C++11 feature. Parser::DeclGroupPtrTy Parser::ParseExplicitInstantiation( DeclaratorContext Context, SourceLocation ExternLoc, SourceLocation TemplateLoc, SourceLocation &DeclEnd, ParsedAttributes &AccessAttrs, AccessSpecifier AS) { … } SourceRange Parser::ParsedTemplateInfo::getSourceRange() const { … } void Parser::LateTemplateParserCallback(void *P, LateParsedTemplate &LPT) { … } /// Late parse a C++ function template in Microsoft mode. void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) { … } /// Lex a delayed template function for late parsing. void Parser::LexTemplateFunctionForLateParsing(CachedTokens &Toks) { … } /// We've parsed something that could plausibly be intended to be a template /// name (\p LHS) followed by a '<' token, and the following code can't possibly /// be an expression. Determine if this is likely to be a template-id and if so, /// diagnose it. bool Parser::diagnoseUnknownTemplateId(ExprResult LHS, SourceLocation Less) { … } void Parser::checkPotentialAngleBracket(ExprResult &PotentialTemplateName) { … } bool Parser::checkPotentialAngleBracketDelimiter( const AngleBracketTracker::Loc &LAngle, const Token &OpToken) { … }