diff --git a/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseTransparentFunctorsCheck.cpp @@ -96,8 +96,9 @@ unsigned ArgNum = 0; const auto *FunctorParentType = FunctorParentLoc.getType()->castAs(); - for (; ArgNum < FunctorParentType->getNumArgs(); ++ArgNum) { - const TemplateArgument &Arg = FunctorParentType->getArg(ArgNum); + for (; ArgNum < FunctorParentType->getSpecifiedArguments().size(); ++ArgNum) { + const TemplateArgument &Arg = + FunctorParentType->getSpecifiedArguments()[ArgNum]; if (Arg.getKind() != TemplateArgument::Type) continue; QualType ParentArgType = Arg.getAsType(); @@ -107,7 +108,7 @@ break; } // Functor is a default template argument. - if (ArgNum == FunctorParentType->getNumArgs()) + if (ArgNum == FunctorParentType->getSpecifiedArguments().size()) return; TemplateArgumentLoc FunctorLoc = FunctorParentLoc.getArgLoc(ArgNum); auto FunctorTypeLoc = getInnerTypeLocAs( diff --git a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp --- a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp +++ b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp @@ -183,8 +183,10 @@ if (Template->getAsCXXRecordDecl()->getName() != "complex") return true; - const auto *Builtin = - Template->getArg(0).getAsType().getTypePtr()->getAs(); + const auto *Builtin = Template->getSpecifiedArguments()[0] + .getAsType() + .getTypePtr() + ->getAs(); if (Builtin && !isMPITypeMatching(ComplexCXXMatches, Builtin->getKind(), MPIDatatype)) { diff --git a/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp b/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp @@ -128,7 +128,7 @@ auto UnlessFunctionType = unless(hasUnqualifiedDesugaredType(functionType())); auto IsAutoDeducedToPointer = [](const auto &...InnerMatchers) { return autoType(hasDeducedType( - hasUnqualifiedDesugaredType(pointerType(pointee(InnerMatchers...))))); + hasCanonicalType(pointerType(pointee(InnerMatchers...))))); }; Finder->addMatcher( diff --git a/clang-tools-extra/clangd/HeuristicResolver.cpp b/clang-tools-extra/clangd/HeuristicResolver.cpp --- a/clang-tools-extra/clangd/HeuristicResolver.cpp +++ b/clang-tools-extra/clangd/HeuristicResolver.cpp @@ -80,9 +80,9 @@ auto *TST = T->getAs(); if (!TST) return nullptr; - if (TST->getNumArgs() == 0) + if (TST->getSpecifiedArguments().size() == 0) return nullptr; - const TemplateArgument &FirstArg = TST->getArg(0); + const TemplateArgument &FirstArg = TST->getSpecifiedArguments()[0]; if (FirstArg.getKind() != TemplateArgument::Type) return nullptr; return FirstArg.getAsType().getTypePtrOrNull(); diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -1067,7 +1067,7 @@ HI.LocalScope = ""; HI.Kind = index::SymbolKind::TypeAlias; HI.Definition = "template using AA = A"; - HI.Type = {"A", "type-parameter-0-0"}; // FIXME: should be 'T' + HI.Type = {"A", "T"}; HI.TemplateParameters = {{{"typename"}, std::string("T"), llvm::None}}; }}, {// Constant array diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -1628,22 +1628,27 @@ bool ParameterPack, TemplateTypeParmDecl *ParmDecl = nullptr) const; - QualType getTemplateSpecializationType(TemplateName T, - ArrayRef Args, - QualType Canon = QualType()) const; - - QualType - getCanonicalTemplateSpecializationType(TemplateName T, - ArrayRef Args) const; - - QualType getTemplateSpecializationType(TemplateName T, - ArrayRef Args, - QualType Canon = QualType()) const; - - TypeSourceInfo * - getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, - const TemplateArgumentListInfo &Args, - QualType Canon = QualType()) const; + QualType getTemplateSpecializationType( + TemplateName T, ArrayRef SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Canon = QualType()) const; + + QualType getCanonicalTemplateSpecializationType( + TemplateName T, ArrayRef ConvertedArgs) const; + + QualType getTemplateSpecializationType( + TemplateName T, ArrayRef SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Canon = QualType()) const; + + TypeSourceInfo *getTemplateSpecializationTypeInfo( + TemplateName T, SourceLocation TLoc, + const TemplateArgumentListInfo &SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Canon = QualType()) const; QualType getParenType(QualType NamedType) const; @@ -1658,10 +1663,9 @@ const IdentifierInfo *Name, QualType Canon = QualType()) const; - QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, - NestedNameSpecifier *NNS, - const IdentifierInfo *Name, - const TemplateArgumentListInfo &Args) const; + QualType getDependentTemplateSpecializationType( + ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, + const IdentifierInfo *Name, ArrayRef Args) const; QualType getDependentTemplateSpecializationType( ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, const IdentifierInfo *Name, ArrayRef Args) const; @@ -2706,6 +2710,13 @@ TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) const; + /// Retrieve the "canonical" template argument list. + /// + /// Returns true if any arguments were non-canonical. + bool getCanonicalTemplateArguments( + ArrayRef OrigArgs, + SmallVectorImpl &CanonArgs) const; + /// Type Query functions. If the type is an instance of the specified class, /// return the Type pointer for the underlying maximally pretty type. This /// is a member of ASTContext because this may need to do some amount of diff --git a/clang/include/clang/AST/ASTImporter.h b/clang/include/clang/AST/ASTImporter.h --- a/clang/include/clang/AST/ASTImporter.h +++ b/clang/include/clang/AST/ASTImporter.h @@ -480,6 +480,13 @@ /// error. llvm::Expected Import(const APValue &FromValue); + /// Import the given C++ TemplateArgumentList from the "from" + /// context into the "to" context. + /// + /// \returns The equivalent initializer in the "to" context, or the import + /// error. + llvm::Expected Import(const TemplateArgumentList *); + /// Import the definition of the given declaration, including all of /// the declarations it contains. [[nodiscard]] llvm::Error ImportDefinition(Decl *From); diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h --- a/clang/include/clang/AST/ASTNodeTraverser.h +++ b/clang/include/clang/AST/ASTNodeTraverser.h @@ -395,7 +395,7 @@ Visit(T->getArgumentPack()); } void VisitTemplateSpecializationType(const TemplateSpecializationType *T) { - for (const auto &Arg : *T) + for (const auto &Arg : T->getSpecifiedArguments()) Visit(Arg); } void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -2141,11 +2141,9 @@ static ClassTemplatePartialSpecializationDecl * Create(ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, - TemplateParameterList *Params, - ClassTemplateDecl *SpecializedTemplate, + TemplateParameterList *Params, ClassTemplateDecl *SpecializedTemplate, ArrayRef Args, - const TemplateArgumentListInfo &ArgInfos, - QualType CanonInjectedType, + const TemplateArgumentListInfo &ArgInfos, QualType InjectedType, ClassTemplatePartialSpecializationDecl *PrevDecl); static ClassTemplatePartialSpecializationDecl * diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -3175,6 +3175,8 @@ /// In X.F, this is the decl referenced by F. ValueDecl *MemberDecl; + const TemplateArgumentList *Deduced; + /// MemberDNLoc - Provides source/type location info for the /// declaration name embedded in MemberDecl. DeclarationNameLoc MemberDNLoc; @@ -3200,21 +3202,20 @@ MemberExpr(Expr *Base, bool IsArrow, SourceLocation OperatorLoc, ValueDecl *MemberDecl, const DeclarationNameInfo &NameInfo, - QualType T, ExprValueKind VK, ExprObjectKind OK, - NonOdrUseReason NOUR); + const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK, + ExprObjectKind OK, NonOdrUseReason NOUR); MemberExpr(EmptyShell Empty) : Expr(MemberExprClass, Empty), Base(), MemberDecl() {} public: - static MemberExpr *Create(const ASTContext &C, Expr *Base, bool IsArrow, - SourceLocation OperatorLoc, - NestedNameSpecifierLoc QualifierLoc, - SourceLocation TemplateKWLoc, ValueDecl *MemberDecl, - DeclAccessPair FoundDecl, - DeclarationNameInfo MemberNameInfo, - const TemplateArgumentListInfo *TemplateArgs, - QualType T, ExprValueKind VK, ExprObjectKind OK, - NonOdrUseReason NOUR); + static MemberExpr * + Create(const ASTContext &C, Expr *Base, bool IsArrow, + SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc, + SourceLocation TemplateKWLoc, ValueDecl *MemberDecl, + DeclAccessPair FoundDecl, DeclarationNameInfo MemberNameInfo, + const TemplateArgumentListInfo *TemplateArgs, + const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK, + ExprObjectKind OK, NonOdrUseReason NOUR); /// Create an implicit MemberExpr, with no location, qualifier, template /// arguments, and so on. Suitable only for non-static member access. @@ -3225,7 +3226,8 @@ return Create(C, Base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(), SourceLocation(), MemberDecl, DeclAccessPair::make(MemberDecl, MemberDecl->getAccess()), - DeclarationNameInfo(), nullptr, T, VK, OK, NOUR_None); + DeclarationNameInfo(), nullptr, nullptr, T, VK, OK, + NOUR_None); } static MemberExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier, @@ -3251,6 +3253,8 @@ return getTrailingObjects()->FoundDecl; } + const TemplateArgumentList *getDeduced() const { return Deduced; } + /// Determines whether this member expression actually had /// a C++ nested-name-specifier prior to the name of the member, e.g., /// x->Base::foo. diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -276,8 +276,7 @@ /// /// \returns false if the visitation was terminated early, true otherwise. // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead. - bool TraverseTemplateArguments(const TemplateArgument *Args, - unsigned NumArgs); + bool TraverseTemplateArguments(ArrayRef Args); /// Recursively visit a base specifier. This can be overridden by a /// subclass. @@ -873,8 +872,7 @@ return getDerived().TraverseStmt(Arg.getAsExpr()); case TemplateArgument::Pack: - return getDerived().TraverseTemplateArguments(Arg.pack_begin(), - Arg.pack_size()); + return getDerived().TraverseTemplateArguments(Arg.pack_elements()); } return true; @@ -914,8 +912,7 @@ return getDerived().TraverseStmt(ArgLoc.getSourceExpression()); case TemplateArgument::Pack: - return getDerived().TraverseTemplateArguments(Arg.pack_begin(), - Arg.pack_size()); + return getDerived().TraverseTemplateArguments(Arg.pack_elements()); } return true; @@ -923,10 +920,9 @@ template bool RecursiveASTVisitor::TraverseTemplateArguments( - const TemplateArgument *Args, unsigned NumArgs) { - for (unsigned I = 0; I != NumArgs; ++I) { - TRY_TO(TraverseTemplateArgument(Args[I])); - } + ArrayRef Args) { + for (const TemplateArgument &Arg : Args) + TRY_TO(TraverseTemplateArgument(Arg)); return true; } @@ -1083,7 +1079,7 @@ DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); if (T->isConstrained()) { - TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); + TRY_TO(TraverseTemplateArguments(T->getTypeConstraintArguments())); } }) DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, { @@ -1103,7 +1099,10 @@ DEF_TRAVERSE_TYPE(TemplateSpecializationType, { TRY_TO(TraverseTemplateName(T->getTemplateName())); - TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); + if (T->isCanonicalUnqualified()) + TRY_TO(TraverseTemplateArguments(T->getConvertedArguments())); + else + TRY_TO(TraverseTemplateArguments(T->getSpecifiedArguments())); }) DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) @@ -1131,7 +1130,7 @@ DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); - TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); + TRY_TO(TraverseTemplateArguments(T->template_arguments())); }) DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); }) diff --git a/clang/include/clang/AST/TemplateBase.h b/clang/include/clang/AST/TemplateBase.h --- a/clang/include/clang/AST/TemplateBase.h +++ b/clang/include/clang/AST/TemplateBase.h @@ -572,6 +572,12 @@ SourceLocation RAngleLoc) : LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc) {} + TemplateArgumentListInfo(TemplateArgumentListInfo &&) = default; + TemplateArgumentListInfo &operator=(TemplateArgumentListInfo &&) = default; + TemplateArgumentListInfo(const TemplateArgumentListInfo &) = default; + TemplateArgumentListInfo & + operator=(const TemplateArgumentListInfo &) = default; + // This can leak if used in an AST node, use ASTTemplateArgumentListInfo // instead. void *operator new(size_t bytes, ASTContext &C) = delete; @@ -588,9 +594,8 @@ return Arguments.data(); } - llvm::ArrayRef arguments() const { - return Arguments; - } + llvm::ArrayRef arguments() const { return Arguments; } + llvm::MutableArrayRef arguments() { return Arguments; } const TemplateArgumentLoc &operator[](unsigned I) const { return Arguments[I]; @@ -698,22 +703,6 @@ const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, const TemplateArgument &Arg); -inline TemplateSpecializationType::iterator - TemplateSpecializationType::end() const { - return getArgs() + getNumArgs(); -} - -inline DependentTemplateSpecializationType::iterator - DependentTemplateSpecializationType::end() const { - return getArgs() + getNumArgs(); -} - -inline const TemplateArgument & - TemplateSpecializationType::getArg(unsigned Idx) const { - assert(Idx < getNumArgs() && "Template argument out of range"); - return getArgs()[Idx]; -} - inline const TemplateArgument & DependentTemplateSpecializationType::getArg(unsigned Idx) const { assert(Idx < getNumArgs() && "Template argument out of range"); diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h --- a/clang/include/clang/AST/Type.h +++ b/clang/include/clang/AST/Type.h @@ -1856,10 +1856,9 @@ /// specialization, which is expected to be able to hold at least 1024 /// according to [implimits]. However, as this limit is somewhat easy to /// hit with template metaprogramming we'd prefer to keep it as large - /// as possible. At the moment it has been left as a non-bitfield since - /// this type safely fits in 64 bits as an unsigned, so there is no reason - /// to introduce the performance impact of a bitfield. - unsigned NumArgs; + /// as possible. + unsigned NumSpecifiedArgs : 16; + unsigned NumConvertedArgs : 16; }; class DependentTemplateSpecializationTypeBitfields { @@ -2405,6 +2404,18 @@ /// immediately following this class. template const T *getAs() const; + /// Look through sugar for an instance of TemplateSpecializationType which + /// is not a type alias. + const TemplateSpecializationType * + getAsNonAliasTemplateSpecializationType() const; + + const TemplateSpecializationType * + castAsNonAliasTemplateSpecializationType() const { + auto TST = getAsNonAliasTemplateSpecializationType(); + assert(TST && "not a TemplateSpecializationType"); + return TST; + } + /// Member-template getAsAdjusted. Look through specific kinds /// of sugar (parens, attributes, etc) for an instance of \. /// This is used when you need to walk over sugar nodes that represent some @@ -5327,9 +5338,12 @@ TemplateName Template; TemplateSpecializationType(TemplateName T, - ArrayRef Args, - QualType Canon, - QualType Aliased); + ArrayRef SpecifiedArgs, + ArrayRef ConvertedArgs, + QualType Canon, QualType Aliased); + + TemplateSpecializationType(TemplateName T, + ArrayRef ConvertedArgs); public: /// Determine whether any of the given template arguments are dependent. @@ -5375,38 +5389,19 @@ /// \endcode bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } - /// Get the aliased type, if this is a specialization of a type alias - /// template. - QualType getAliasedType() const { - assert(isTypeAlias() && "not a type alias template specialization"); - return *reinterpret_cast(end()); - } - - using iterator = const TemplateArgument *; - - iterator begin() const { return getArgs(); } - iterator end() const; // defined inline in TemplateBase.h - /// Retrieve the name of the template that we are specializing. TemplateName getTemplateName() const { return Template; } - /// Retrieve the template arguments. - const TemplateArgument *getArgs() const { - return reinterpret_cast(this + 1); - } - - /// Retrieve the number of template arguments. - unsigned getNumArgs() const { - return TemplateSpecializationTypeBits.NumArgs; + ArrayRef getSpecifiedArguments() const { + return {reinterpret_cast(this + 1), + TemplateSpecializationTypeBits.NumSpecifiedArgs}; } - /// Retrieve a specific template argument as a type. - /// \pre \c isArgType(Arg) - const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h + ArrayRef getConvertedArguments() const; - ArrayRef template_arguments() const { - return {getArgs(), getNumArgs()}; - } + /// Get the aliased type, if this is a specialization of a type alias + /// template. + QualType getAliasedType() const; bool isSugared() const { return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); @@ -5417,13 +5412,14 @@ } void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { - Profile(ID, Template, template_arguments(), Ctx); - if (isTypeAlias()) - getAliasedType().Profile(ID); + assert(getSpecifiedArguments().size() == 0); + assert(!isTypeAlias()); + assert(isCanonicalUnqualified()); + Profile(ID, Template, getConvertedArguments(), Ctx); } static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, - ArrayRef Args, + ArrayRef ConvertedArgs, const ASTContext &Context); static bool classof(const Type *T) { diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h --- a/clang/include/clang/AST/TypeLoc.h +++ b/clang/include/clang/AST/TypeLoc.h @@ -901,7 +901,7 @@ } }; -struct BTFTagAttributedLocInfo {}; // Nothing. +struct alignas(4) BTFTagAttributedLocInfo {}; // Nothing. /// Type source information for an btf_tag attributed type. class BTFTagAttributedTypeLoc @@ -1634,7 +1634,7 @@ } unsigned getNumArgs() const { - return getTypePtr()->getNumArgs(); + return getTypePtr()->getSpecifiedArguments().size(); } void setArgLocInfo(unsigned i, TemplateArgumentLocInfo AI) { @@ -1646,7 +1646,8 @@ } TemplateArgumentLoc getArgLoc(unsigned i) const { - return TemplateArgumentLoc(getTypePtr()->getArg(i), getArgLocInfo(i)); + return TemplateArgumentLoc(getTypePtr()->getSpecifiedArguments()[i], + getArgLocInfo(i)); } SourceLocation getTemplateNameLoc() const { @@ -1681,12 +1682,12 @@ setTemplateNameLoc(Loc); setLAngleLoc(Loc); setRAngleLoc(Loc); - initializeArgLocs(Context, getNumArgs(), getTypePtr()->getArgs(), + initializeArgLocs(Context, getTypePtr()->getSpecifiedArguments(), getArgInfos(), Loc); } - static void initializeArgLocs(ASTContext &Context, unsigned NumArgs, - const TemplateArgument *Args, + static void initializeArgLocs(ASTContext &Context, + ArrayRef Args, TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc); diff --git a/clang/include/clang/AST/TypeProperties.td b/clang/include/clang/AST/TypeProperties.td --- a/clang/include/clang/AST/TypeProperties.td +++ b/clang/include/clang/AST/TypeProperties.td @@ -652,29 +652,27 @@ def : Property<"templateName", TemplateName> { let Read = [{ node->getTemplateName() }]; } - def : Property<"templateArguments", Array> { - let Read = [{ node->template_arguments() }]; + def : Property<"specifiedArguments", Array> { + let Read = [{ node->getSpecifiedArguments() }]; } - def : Property<"underlyingType", Optional> { + def : Property<"convertedArguments", Array> { + let Read = [{ node->getConvertedArguments() }]; + } + def : Property<"underlyingType", QualType> { let Read = [{ node->isTypeAlias() - ? llvm::Optional(node->getAliasedType()) + ? node->getAliasedType() : node->isCanonicalUnqualified() - ? llvm::None - : llvm::Optional(node->getCanonicalTypeInternal()) + ? QualType() : node->getCanonicalTypeInternal() }]; } def : Creator<[{ - QualType result; - if (!underlyingType) { - result = ctx.getCanonicalTemplateSpecializationType(templateName, - templateArguments); - } else { - result = ctx.getTemplateSpecializationType(templateName, - templateArguments, - *underlyingType); - } + QualType result = ctx.getTemplateSpecializationType(templateName, + specifiedArguments, + convertedArguments, + /*CanonicalConvertedArguments=*/{}, + underlyingType); if (dependent) const_cast(result.getTypePtr()) ->addDependence(TypeDependence::DependentInstantiation); diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -1942,7 +1942,7 @@ inline ArrayRef getTemplateSpecializationArgs(const TemplateSpecializationType &T) { - return llvm::makeArrayRef(T.getArgs(), T.getNumArgs()); + return T.getSpecifiedArguments(); } inline ArrayRef diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -472,6 +472,9 @@ /// forward slash (/) elsewhere. bool UseTargetPathSeparator = false; + /// Resugar template specializations. + bool Resugar = true; + LangOptions(); /// Set language defaults for the given input language and diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5829,6 +5829,9 @@ LangOpts<"RecoveryASTType">, DefaultTrue, NegFlag, PosFlag>; +def fno_resugar : Flag<["-"], "fno-resugar">, + HelpText<"Do not resugar template specializations">, + MarshallingInfoNegativeFlag>; let Group = Action_Group in { diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h --- a/clang/include/clang/Sema/Overload.h +++ b/clang/include/clang/Sema/Overload.h @@ -885,6 +885,9 @@ StandardConversionSequence FinalConversion; }; + /// Deduced Arguments for Function Templates. + const TemplateArgumentList *Deduced; + /// Get RewriteKind value in OverloadCandidateRewriteKind type (This /// function is to workaround the spurious GCC bitfield enum warning) OverloadCandidateRewriteKind getRewriteKind() const { diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -3882,7 +3882,8 @@ bool AllowExplicitConversion = false, ADLCallKind IsADLCandidate = ADLCallKind::NotADL, ConversionSequenceList EarlyConversions = None, - OverloadCandidateParamOrder PO = {}); + OverloadCandidateParamOrder PO = {}, + const TemplateArgumentList *Deduced = nullptr); void AddFunctionCandidates(const UnresolvedSetImpl &Functions, ArrayRef Args, OverloadCandidateSet &CandidateSet, @@ -3897,16 +3898,16 @@ OverloadCandidateSet& CandidateSet, bool SuppressUserConversion = false, OverloadCandidateParamOrder PO = {}); - void AddMethodCandidate(CXXMethodDecl *Method, - DeclAccessPair FoundDecl, + void AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext, QualType ObjectType, Expr::Classification ObjectClassification, ArrayRef Args, - OverloadCandidateSet& CandidateSet, + OverloadCandidateSet &CandidateSet, bool SuppressUserConversions = false, bool PartialOverloading = false, ConversionSequenceList EarlyConversions = None, - OverloadCandidateParamOrder PO = {}); + OverloadCandidateParamOrder PO = {}, + const TemplateArgumentList *Deduced = nullptr); void AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, DeclAccessPair FoundDecl, CXXRecordDecl *ActingContext, @@ -4042,10 +4043,9 @@ bool resolveAndFixAddressOfSingleOverloadCandidate( ExprResult &SrcExpr, bool DoFunctionPointerConversion = false); - FunctionDecl * - ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, - bool Complain = false, - DeclAccessPair *Found = nullptr); + FunctionDecl *ResolveSingleFunctionTemplateSpecialization( + OverloadExpr *ovl, TemplateArgumentListInfo &ExplicitTemplateArgs, + bool Complain = false, DeclAccessPair *Found = nullptr); bool ResolveAndFixSingleFunctionTemplateSpecialization( ExprResult &SrcExpr, bool DoFunctionPointerConversion = false, @@ -4053,12 +4053,13 @@ QualType DestTypeForComplaining = QualType(), unsigned DiagIDForComplaining = 0); - Expr *FixOverloadedFunctionReference(Expr *E, - DeclAccessPair FoundDecl, - FunctionDecl *Fn); - ExprResult FixOverloadedFunctionReference(ExprResult, - DeclAccessPair FoundDecl, - FunctionDecl *Fn); + Expr *FixOverloadedFunctionReference(Expr *E, DeclAccessPair FoundDecl, + FunctionDecl *Fn, + const TemplateArgumentList *Deduced); + ExprResult + FixOverloadedFunctionReference(ExprResult, DeclAccessPair FoundDecl, + FunctionDecl *Fn, + const TemplateArgumentList *Deduced); void AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE, ArrayRef Args, @@ -5478,13 +5479,11 @@ SourceLocation TemplateKWLoc = SourceLocation(), const TemplateArgumentListInfo *TemplateArgs = nullptr); - ExprResult - BuildAnonymousStructUnionMemberReference( - const CXXScopeSpec &SS, - SourceLocation nameLoc, + ExprResult BuildAnonymousStructUnionMemberReference( + const CXXScopeSpec &SS, SourceLocation nameLoc, IndirectFieldDecl *indirectField, DeclAccessPair FoundDecl = DeclAccessPair::make(nullptr, AS_none), - Expr *baseObjectExpr = nullptr, + Expr *baseObjectExpr = nullptr, const Type *BaseType = nullptr, SourceLocation opLoc = SourceLocation()); ExprResult BuildPossibleImplicitMemberExpr( @@ -5677,7 +5676,8 @@ ExprResult BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow, SourceLocation OpLoc, - const CXXScopeSpec &SS, FieldDecl *Field, + const NestedNameSpecifierLoc &NNS, + FieldDecl *Field, QualType FieldType, DeclAccessPair FoundDecl, const DeclarationNameInfo &MemberNameInfo); @@ -5703,22 +5703,20 @@ UnqualifiedId &Member, Decl *ObjCImpDecl); - MemberExpr * - BuildMemberExpr(Expr *Base, bool IsArrow, SourceLocation OpLoc, - const CXXScopeSpec *SS, SourceLocation TemplateKWLoc, - ValueDecl *Member, DeclAccessPair FoundDecl, - bool HadMultipleCandidates, - const DeclarationNameInfo &MemberNameInfo, QualType Ty, - ExprValueKind VK, ExprObjectKind OK, - const TemplateArgumentListInfo *TemplateArgs = nullptr); - MemberExpr * - BuildMemberExpr(Expr *Base, bool IsArrow, SourceLocation OpLoc, - NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, - ValueDecl *Member, DeclAccessPair FoundDecl, - bool HadMultipleCandidates, - const DeclarationNameInfo &MemberNameInfo, QualType Ty, - ExprValueKind VK, ExprObjectKind OK, - const TemplateArgumentListInfo *TemplateArgs = nullptr); + MemberExpr *BuildMemberExpr( + Expr *Base, bool IsArrow, SourceLocation OpLoc, const CXXScopeSpec *SS, + SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl, + bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo, + QualType Ty, ExprValueKind VK, ExprObjectKind OK, + const TemplateArgumentListInfo *TemplateArgs = nullptr, + const TemplateArgumentList *Deduced = nullptr); + MemberExpr *BuildMemberExpr( + Expr *Base, bool IsArrow, SourceLocation OpLoc, + NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, + ValueDecl *Member, DeclAccessPair FoundDecl, bool HadMultipleCandidates, + const DeclarationNameInfo &MemberNameInfo, QualType Ty, ExprValueKind VK, + ExprObjectKind OK, const TemplateArgumentListInfo *TemplateArgs = nullptr, + const TemplateArgumentList *Deduced = nullptr); void ActOnDefaultCtorInitializers(Decl *CDtorDecl); bool ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, @@ -7954,9 +7952,9 @@ void NoteAllFoundTemplates(TemplateName Name); - QualType CheckTemplateIdType(TemplateName Template, + QualType CheckTemplateIdType(const CXXScopeSpec &SS, TemplateName Template, SourceLocation TemplateLoc, - TemplateArgumentListInfo &TemplateArgs); + TemplateArgumentListInfo &TemplateArgs); TypeResult ActOnTemplateIdType(Scope *S, CXXScopeSpec &SS, SourceLocation TemplateKWLoc, @@ -9642,6 +9640,29 @@ } }; + QualType resugar(const CXXScopeSpec &SS, QualType T); + + QualType resugar(const CXXScopeSpec &SS, NamedDecl *ND, + ArrayRef Args, QualType T); + QualType resugar(const CXXScopeSpec &SS, NamedDecl *ND, + ArrayRef Args, QualType T); + + QualType resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + QualType T); + + QualType resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + NamedDecl *ND, ArrayRef Args, + QualType T); + QualType resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + NamedDecl *ND, ArrayRef Args, QualType T); + + QualType resugar(const Type *Base, QualType T); + + QualType resugar(const Type *Base, NamedDecl *ND, + ArrayRef Args, QualType T); + QualType resugar(const Type *Base, NamedDecl *ND, + ArrayRef Args, QualType T); + void PerformPendingInstantiations(bool LocalOnly = false); TypeSourceInfo *SubstType(TypeSourceInfo *T, diff --git a/clang/include/clang/Serialization/ASTRecordReader.h b/clang/include/clang/Serialization/ASTRecordReader.h --- a/clang/include/clang/Serialization/ASTRecordReader.h +++ b/clang/include/clang/Serialization/ASTRecordReader.h @@ -249,6 +249,10 @@ void readTemplateArgumentList(SmallVectorImpl &TemplArgs, bool Canonicalize = false); + /// Read a template argument list. + const TemplateArgumentList * + readTemplateArgumentList(bool Canonicalize = false); + /// Read a UnresolvedSet structure, advancing Idx. void readUnresolvedSet(LazyASTUnresolvedSet &Set); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -4806,6 +4806,7 @@ QualType ASTContext::getSubstTemplateTypeParmPackType(Decl *ReplacedDecl, unsigned Index, const TemplateArgument &ArgPack) { + // FIXME: Allow non-canonical argument pack. #ifndef NDEBUG for (const auto &P : ArgPack.pack_elements()) { assert(P.getKind() == TemplateArgument::Type && "Pack contains a non-type"); @@ -4867,41 +4868,46 @@ return QualType(TypeParm, 0); } -TypeSourceInfo * -ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, - SourceLocation NameLoc, - const TemplateArgumentListInfo &Args, - QualType Underlying) const { +TypeSourceInfo *ASTContext::getTemplateSpecializationTypeInfo( + TemplateName Name, SourceLocation NameLoc, + const TemplateArgumentListInfo &SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Underlying) const { assert(!Name.getAsDependentTemplateName() && "No dependent template names here!"); - QualType TST = - getTemplateSpecializationType(Name, Args.arguments(), Underlying); + QualType TST = getTemplateSpecializationType( + Name, SpecifiedArgs.arguments(), SugaredConvertedArgs, + CanonicalConvertedArgs, Underlying); TypeSourceInfo *DI = CreateTypeSourceInfo(TST); TemplateSpecializationTypeLoc TL = DI->getTypeLoc().castAs(); TL.setTemplateKeywordLoc(SourceLocation()); TL.setTemplateNameLoc(NameLoc); - TL.setLAngleLoc(Args.getLAngleLoc()); - TL.setRAngleLoc(Args.getRAngleLoc()); + TL.setLAngleLoc(SpecifiedArgs.getLAngleLoc()); + TL.setRAngleLoc(SpecifiedArgs.getRAngleLoc()); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) - TL.setArgLocInfo(i, Args[i].getLocInfo()); + TL.setArgLocInfo(i, SpecifiedArgs[i].getLocInfo()); return DI; } -QualType -ASTContext::getTemplateSpecializationType(TemplateName Template, - ArrayRef Args, - QualType Underlying) const { +QualType ASTContext::getTemplateSpecializationType( + TemplateName Template, ArrayRef SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Underlying) const { assert(!Template.getAsDependentTemplateName() && "No dependent template names here!"); - SmallVector ArgVec; - ArgVec.reserve(Args.size()); - for (const TemplateArgumentLoc &Arg : Args) - ArgVec.push_back(Arg.getArgument()); + SmallVector SpecifiedArgVec; + SpecifiedArgVec.reserve(SpecifiedArgs.size()); + for (const TemplateArgumentLoc &Arg : SpecifiedArgs) + SpecifiedArgVec.push_back(Arg.getArgument()); - return getTemplateSpecializationType(Template, ArgVec, Underlying); + return getTemplateSpecializationType(Template, SpecifiedArgVec, + SugaredConvertedArgs, + CanonicalConvertedArgs, Underlying); } #ifndef NDEBUG @@ -4914,12 +4920,30 @@ } #endif -QualType -ASTContext::getTemplateSpecializationType(TemplateName Template, - ArrayRef Args, - QualType Underlying) const { +bool ASTContext::getCanonicalTemplateArguments( + ArrayRef OrigArgs, + SmallVectorImpl &CanonArgs) const { + bool AnyNonCanonArgs = false; + unsigned NumArgs = OrigArgs.size(); + CanonArgs.resize(NumArgs); + for (unsigned I = 0; I != NumArgs; ++I) { + TemplateArgument OrigArg = OrigArgs[I]; + TemplateArgument &CanonArg = CanonArgs[I]; + CanonArg = getCanonicalTemplateArgument(OrigArg); + if (!CanonArg.structurallyEquals(OrigArg)) + AnyNonCanonArgs = true; + } + return AnyNonCanonArgs; +} + +QualType ASTContext::getTemplateSpecializationType( + TemplateName Template, ArrayRef SpecifiedArgs, + ArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Underlying) const { assert(!Template.getAsDependentTemplateName() && "No dependent template names here!"); + assert(SugaredConvertedArgs.size() != 0); // Look through qualified template names. if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) Template = QTN->getUnderlyingTemplate(); @@ -4932,48 +4956,44 @@ else { // We can get here with an alias template when the specialization contains // a pack expansion that does not match up with a parameter pack. - assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && + assert((!IsTypeAlias || hasAnyPackExpansions(SpecifiedArgs)) && "Caller must compute aliased type"); IsTypeAlias = false; - CanonType = getCanonicalTemplateSpecializationType(Template, Args); + SmallVector CanonArgsVec; + if (CanonicalConvertedArgs.size() == 0) { + getCanonicalTemplateArguments(SugaredConvertedArgs, CanonArgsVec); + CanonicalConvertedArgs = CanonArgsVec; + } + CanonType = getCanonicalTemplateSpecializationType(Template, + CanonicalConvertedArgs); } // Allocate the (non-canonical) template specialization type, but don't // try to unique it: these types typically have location information that // we don't unique and don't want to lose. - void *Mem = Allocate(sizeof(TemplateSpecializationType) + - sizeof(TemplateArgument) * Args.size() + - (IsTypeAlias? sizeof(QualType) : 0), - TypeAlignment); - auto *Spec - = new (Mem) TemplateSpecializationType(Template, Args, CanonType, - IsTypeAlias ? Underlying : QualType()); + void *Mem = + Allocate(sizeof(TemplateSpecializationType) + + sizeof(TemplateArgument) * + (SpecifiedArgs.size() + SugaredConvertedArgs.size()) + + (IsTypeAlias ? sizeof(QualType) : 0), + TypeAlignment); + auto *Spec = new (Mem) TemplateSpecializationType( + Template, SpecifiedArgs, SugaredConvertedArgs, CanonType, + IsTypeAlias ? Underlying : QualType()); Types.push_back(Spec); return QualType(Spec, 0); } -static bool -getCanonicalTemplateArguments(const ASTContext &C, - ArrayRef OrigArgs, - SmallVectorImpl &CanonArgs) { - bool AnyNonCanonArgs = false; - unsigned NumArgs = OrigArgs.size(); - CanonArgs.resize(NumArgs); - for (unsigned I = 0; I != NumArgs; ++I) { - const TemplateArgument &OrigArg = OrigArgs[I]; - TemplateArgument &CanonArg = CanonArgs[I]; - CanonArg = C.getCanonicalTemplateArgument(OrigArg); - if (!CanonArg.structurallyEquals(OrigArg)) - AnyNonCanonArgs = true; - } - return AnyNonCanonArgs; -} - QualType ASTContext::getCanonicalTemplateSpecializationType( - TemplateName Template, ArrayRef Args) const { + TemplateName Template, ArrayRef ConvertedArgs) const { assert(!Template.getAsDependentTemplateName() && "No dependent template names here!"); + assert(ConvertedArgs.size() != 0); +#ifndef NDEBUG + for (const TemplateArgument &A : ConvertedArgs) + assert(A.structurallyEquals(getCanonicalTemplateArgument(A))); +#endif // Look through qualified template names. if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) @@ -4981,27 +5001,22 @@ // Build the canonical template specialization type. TemplateName CanonTemplate = getCanonicalTemplateName(Template); - SmallVector CanonArgs; - ::getCanonicalTemplateArguments(*this, Args, CanonArgs); // Determine whether this canonical template specialization type already // exists. llvm::FoldingSetNodeID ID; - TemplateSpecializationType::Profile(ID, CanonTemplate, - CanonArgs, *this); + TemplateSpecializationType::Profile(ID, CanonTemplate, ConvertedArgs, *this); void *InsertPos = nullptr; - TemplateSpecializationType *Spec - = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); + TemplateSpecializationType *Spec = + TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); if (!Spec) { // Allocate a new canonical template specialization type. void *Mem = Allocate((sizeof(TemplateSpecializationType) + - sizeof(TemplateArgument) * CanonArgs.size()), + sizeof(TemplateArgument) * ConvertedArgs.size()), TypeAlignment); - Spec = new (Mem) TemplateSpecializationType(CanonTemplate, - CanonArgs, - QualType(), QualType()); + Spec = new (Mem) TemplateSpecializationType(CanonTemplate, ConvertedArgs); Types.push_back(Spec); TemplateSpecializationTypes.InsertNode(Spec, InsertPos); } @@ -5102,12 +5117,9 @@ return QualType(T, 0); } -QualType -ASTContext::getDependentTemplateSpecializationType( - ElaboratedTypeKeyword Keyword, - NestedNameSpecifier *NNS, - const IdentifierInfo *Name, - const TemplateArgumentListInfo &Args) const { +QualType ASTContext::getDependentTemplateSpecializationType( + ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, + const IdentifierInfo *Name, ArrayRef Args) const { // TODO: avoid this copy SmallVector ArgCopy; for (unsigned I = 0, E = Args.size(); I != E; ++I) @@ -5140,8 +5152,7 @@ if (Keyword == ETK_None) CanonKeyword = ETK_Typename; SmallVector CanonArgs; - bool AnyNonCanonArgs = - ::getCanonicalTemplateArguments(*this, Args, CanonArgs); + bool AnyNonCanonArgs = getCanonicalTemplateArguments(Args, CanonArgs); QualType Canon; if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { @@ -5762,7 +5773,7 @@ if (DeducedType.isNull()) { SmallVector CanonArgs; bool AnyNonCanonArgs = - ::getCanonicalTemplateArguments(*this, TypeConstraintArgs, CanonArgs); + getCanonicalTemplateArguments(TypeConstraintArgs, CanonArgs); if (AnyNonCanonArgs) { Canon = getAutoTypeInternal(QualType(), Keyword, IsDependent, IsPack, TypeConstraintConcept, CanonArgs, true); @@ -7318,8 +7329,7 @@ void ASTContext::setCFConstantStringType(QualType T) { const auto *TD = T->castAs(); CFConstantStringTypeDecl = cast(TD->getDecl()); - const auto *TagType = - CFConstantStringTypeDecl->getUnderlyingType()->castAs(); + const auto *TagType = TD->castAs(); CFConstantStringTagDecl = TagType->getDecl(); } @@ -12700,12 +12710,15 @@ case Type::TemplateSpecialization: { const auto *TX = cast(X), *TY = cast(Y); - auto As = getCommonTemplateArguments(Ctx, TX->template_arguments(), - TY->template_arguments()); + auto SpecAs = getCommonTemplateArguments(Ctx, TX->getSpecifiedArguments(), + TY->getSpecifiedArguments()); + auto ConvAs = getCommonTemplateArguments(Ctx, TX->getConvertedArguments(), + TY->getConvertedArguments()); return Ctx.getTemplateSpecializationType( ::getCommonTemplateNameChecked(Ctx, TX->getTemplateName(), TY->getTemplateName()), - As, X->getCanonicalTypeInternal()); + SpecAs, ConvAs, /*CanonicalConvertedArgs=*/{}, + X->getCanonicalTypeInternal()); } case Type::DependentName: { const auto *NX = cast(X), @@ -12914,11 +12927,14 @@ TY->getTemplateName()); if (!CTN.getAsVoidPointer()) return QualType(); - SmallVector Args; - if (getCommonTemplateArguments(Ctx, Args, TX->template_arguments(), - TY->template_arguments())) + SmallVector SpecAs; + if (getCommonTemplateArguments(Ctx, SpecAs, TX->getSpecifiedArguments(), + TY->getSpecifiedArguments())) return QualType(); - return Ctx.getTemplateSpecializationType(CTN, Args, + auto ConvAs = getCommonTemplateArguments(Ctx, TX->getConvertedArguments(), + TY->getConvertedArguments()); + return Ctx.getTemplateSpecializationType(CTN, SpecAs, ConvAs, + /*CanonicalConvertedArgs=*/{}, Ctx.getQualifiedType(Underlying)); } case Type::Typedef: { diff --git a/clang/lib/AST/ASTDiagnostic.cpp b/clang/lib/AST/ASTDiagnostic.cpp --- a/clang/lib/AST/ASTDiagnostic.cpp +++ b/clang/lib/AST/ASTDiagnostic.cpp @@ -118,8 +118,7 @@ if (!TST->isTypeAlias()) { bool DesugarArgument = false; SmallVector Args; - for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) { - const TemplateArgument &Arg = TST->getArg(I); + for (const TemplateArgument &Arg : TST->getSpecifiedArguments()) { if (Arg.getKind() == TemplateArgument::Type) Args.push_back(desugarForDiagnostic(Context, Arg.getAsType(), DesugarArgument)); @@ -130,7 +129,8 @@ if (DesugarArgument) { ShouldAKA = true; QT = Context.getTemplateSpecializationType( - TST->getTemplateName(), Args, QT); + TST->getTemplateName(), Args, TST->getConvertedArguments(), + /*CanonicalConvertedArguments=*/{}, QT); } break; } @@ -986,7 +986,7 @@ if (isEnd()) return; // Set to first template argument. If not a parameter pack, done. - TemplateArgument TA = TST->getArg(0); + TemplateArgument TA = TST->getSpecifiedArguments()[0]; if (TA.getKind() != TemplateArgument::Pack) return; // Start looking into the parameter pack. @@ -1007,7 +1007,7 @@ /// isEnd - Returns true if the iterator is one past the end. bool isEnd() const { assert(TST && "InternalIterator is invalid with a null TST."); - return Index >= TST->getNumArgs(); + return Index >= TST->getSpecifiedArguments().size(); } /// &operator++ - Increment the iterator to the next template argument. @@ -1027,11 +1027,11 @@ // Loop until a template argument is found, or the end is reached. while (true) { // Advance to the next template argument. Break if reached the end. - if (++Index == TST->getNumArgs()) + if (++Index == TST->getSpecifiedArguments().size()) break; // If the TemplateArgument is not a parameter pack, done. - TemplateArgument TA = TST->getArg(Index); + TemplateArgument TA = TST->getSpecifiedArguments()[Index]; if (TA.getKind() != TemplateArgument::Pack) break; @@ -1051,7 +1051,7 @@ assert(TST && "InternalIterator is invalid with a null TST."); assert(!isEnd() && "Index exceeds number of arguments."); if (CurrentTA == EndTA) - return TST->getArg(Index); + return TST->getSpecifiedArguments()[Index]; else return *CurrentTA; } @@ -1135,9 +1135,11 @@ return nullptr; Ty = Context.getTemplateSpecializationType( - TemplateName(CTSD->getSpecializedTemplate()), - CTSD->getTemplateArgs().asArray(), - Ty.getLocalUnqualifiedType().getCanonicalType()); + TemplateName(CTSD->getSpecializedTemplate()), + /*SpecifiedArgs=*/CTSD->getTemplateArgs().asArray(), + /*SugaredConvertedArgs=*/CTSD->getTemplateArgs().asArray(), + /*CanonicalConvertedArgs=*/{}, + Ty.getLocalUnqualifiedType().getCanonicalType()); return Ty->getAs(); } diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -471,9 +471,8 @@ Error ImportDefinition( ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind = IDK_Default); - Error ImportTemplateArguments( - const TemplateArgument *FromArgs, unsigned NumFromArgs, - SmallVectorImpl &ToArgs); + Error ImportTemplateArguments(ArrayRef FromArgs, + SmallVectorImpl &ToArgs); Expected ImportTemplateArgument(const TemplateArgument &From); @@ -791,9 +790,8 @@ return std::move(Err); // Import template arguments. - auto TemplArgs = FTSInfo->TemplateArguments->asArray(); - if (Error Err = ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(), - std::get<1>(Result))) + if (Error Err = ImportTemplateArguments(FTSInfo->TemplateArguments->asArray(), + std::get<1>(Result))) return std::move(Err); return Result; @@ -894,8 +892,7 @@ case TemplateArgument::Pack: { SmallVector ToPack; ToPack.reserve(From.pack_size()); - if (Error Err = ImportTemplateArguments( - From.pack_begin(), From.pack_size(), ToPack)) + if (Error Err = ImportTemplateArguments(From.pack_elements(), ToPack)) return std::move(Err); return TemplateArgument( @@ -1436,9 +1433,7 @@ return ToTypeConstraintConcept.takeError(); SmallVector ToTemplateArgs; - ArrayRef FromTemplateArgs = T->getTypeConstraintArguments(); - if (Error Err = ImportTemplateArguments(FromTemplateArgs.data(), - FromTemplateArgs.size(), + if (Error Err = ImportTemplateArguments(T->getTypeConstraintArguments(), ToTemplateArgs)) return std::move(Err); @@ -1557,9 +1552,14 @@ if (!ToTemplateOrErr) return ToTemplateOrErr.takeError(); - SmallVector ToTemplateArgs; - if (Error Err = ImportTemplateArguments( - T->getArgs(), T->getNumArgs(), ToTemplateArgs)) + SmallVector ToSpecifiedArgs; + if (Error Err = + ImportTemplateArguments(T->getSpecifiedArguments(), ToSpecifiedArgs)) + return std::move(Err); + + SmallVector ToConvertedArgs; + if (Error Err = + ImportTemplateArguments(T->getConvertedArguments(), ToConvertedArgs)) return std::move(Err); QualType ToCanonType; @@ -1571,9 +1571,9 @@ else return TyOrErr.takeError(); } - return Importer.getToContext().getTemplateSpecializationType(*ToTemplateOrErr, - ToTemplateArgs, - ToCanonType); + return Importer.getToContext().getTemplateSpecializationType( + *ToTemplateOrErr, ToSpecifiedArgs, ToConvertedArgs, + /*CanonicalConvertedArgs=*/{}, ToCanonType); } ExpectedType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { @@ -1617,8 +1617,7 @@ SmallVector ToPack; ToPack.reserve(T->getNumArgs()); - if (Error Err = ImportTemplateArguments( - T->getArgs(), T->getNumArgs(), ToPack)) + if (Error Err = ImportTemplateArguments(T->template_arguments(), ToPack)) return std::move(Err); return Importer.getToContext().getDependentTemplateSpecializationType( @@ -2189,10 +2188,10 @@ } Error ASTNodeImporter::ImportTemplateArguments( - const TemplateArgument *FromArgs, unsigned NumFromArgs, + ArrayRef FromArgs, SmallVectorImpl &ToArgs) { - for (unsigned I = 0; I != NumFromArgs; ++I) { - if (auto ToOrErr = import(FromArgs[I])) + for (const TemplateArgument &FromArg : FromArgs) { + if (auto ToOrErr = import(FromArg)) ToArgs.push_back(*ToOrErr); else return ToOrErr.takeError(); @@ -3331,7 +3330,7 @@ Optional VisitTemplateSpecializationType(const TemplateSpecializationType *T) { - for (const auto &Arg : T->template_arguments()) + for (const auto &Arg : T->getSpecifiedArguments()) if (checkTemplateArgument(Arg)) return true; // This type is a "sugar" to a record type, it can have a desugared type. @@ -5834,8 +5833,8 @@ // Import template arguments. SmallVector TemplateArgs; - if (Error Err = ImportTemplateArguments( - D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs)) + if (Error Err = + ImportTemplateArguments(D->getTemplateArgs().asArray(), TemplateArgs)) return std::move(Err); // Try to find an existing specialization with these template arguments and // template parameter list. @@ -6156,8 +6155,8 @@ // Import template arguments. SmallVector TemplateArgs; - if (Error Err = ImportTemplateArguments( - D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs)) + if (Error Err = + ImportTemplateArguments(D->getTemplateArgs().asArray(), TemplateArgs)) return std::move(Err); // Try to find an existing specialization with these template arguments. @@ -7813,10 +7812,8 @@ SmallVector ToPartialArguments; if (E->isPartiallySubstituted()) { - if (Error Err = ImportTemplateArguments( - E->getPartialArguments().data(), - E->getPartialArguments().size(), - ToPartialArguments)) + if (Error Err = ImportTemplateArguments(E->getPartialArguments(), + ToPartialArguments)) return std::move(Err); } @@ -7954,6 +7951,7 @@ auto ToQualifierLoc = importChecked(Err, E->getQualifierLoc()); auto ToTemplateKeywordLoc = importChecked(Err, E->getTemplateKeywordLoc()); auto ToMemberDecl = importChecked(Err, E->getMemberDecl()); + auto ToDeduced = importChecked(Err, E->getDeduced()); auto ToType = importChecked(Err, E->getType()); auto ToDecl = importChecked(Err, E->getFoundDecl().getDecl()); auto ToName = importChecked(Err, E->getMemberNameInfo().getName()); @@ -7978,7 +7976,7 @@ return MemberExpr::Create(Importer.getToContext(), ToBase, E->isArrow(), ToOperatorLoc, ToQualifierLoc, ToTemplateKeywordLoc, ToMemberDecl, ToFoundDecl, ToMemberNameInfo, - ResInfo, ToType, E->getValueKind(), + ResInfo, ToDeduced, ToType, E->getValueKind(), E->getObjectKind(), E->isNonOdrUse()); } @@ -9658,6 +9656,17 @@ return Importer.ImportAPValue(FromValue); } +llvm::Expected +ASTImporter::Import(const TemplateArgumentList *ArgList) { + ASTNodeImporter Importer(*this); + if (!ArgList) + return nullptr; + SmallVector ToArgs(ArgList->size()); + if (auto Res = Importer.ImportTemplateArguments(ArgList->asArray(), ToArgs)) + return std::move(Res); + return TemplateArgumentList::CreateCopy(ToContext, ToArgs); +} + Error ASTImporter::ImportDefinition(Decl *From) { ExpectedDecl ToOrErr = Import(From); if (!ToOrErr) diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp --- a/clang/lib/AST/ASTStructuralEquivalence.cpp +++ b/clang/lib/AST/ASTStructuralEquivalence.cpp @@ -526,6 +526,10 @@ return true; } +static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, + ArrayRef Args1, + ArrayRef Args2); + /// Determine whether two template arguments are equivalent. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, const TemplateArgument &Arg1, @@ -568,20 +572,26 @@ Arg2.getAsExpr()); case TemplateArgument::Pack: - if (Arg1.pack_size() != Arg2.pack_size()) - return false; - - for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) - if (!IsStructurallyEquivalent(Context, Arg1.pack_begin()[I], - Arg2.pack_begin()[I])) - return false; - - return true; + return IsStructurallyEquivalent(Context, Arg1.pack_elements(), + Arg2.pack_elements()); } llvm_unreachable("Invalid template argument kind"); } +/// Determine whether two template argument lists are equivalent. +static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, + ArrayRef Args1, + ArrayRef Args2) { + if (Args1.size() != Args2.size()) + return false; + + for (unsigned I = 0, N = Args1.size(); I != N; ++I) + if (!IsStructurallyEquivalent(Context, Args1[I], Args2[I])) + return false; + return true; +} + /// Determine structural equivalence for the common part of array /// types. static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, @@ -1011,16 +1021,10 @@ if (Auto1->getTypeConstraintConcept() != Auto2->getTypeConstraintConcept()) return false; - ArrayRef Auto1Args = - Auto1->getTypeConstraintArguments(); - ArrayRef Auto2Args = - Auto2->getTypeConstraintArguments(); - if (Auto1Args.size() != Auto2Args.size()) + if (!IsStructurallyEquivalent(Context, + Auto1->getTypeConstraintArguments(), + Auto2->getTypeConstraintArguments())) return false; - for (unsigned I = 0, N = Auto1Args.size(); I != N; ++I) { - if (!IsStructurallyEquivalent(Context, Auto1Args[I], Auto2Args[I])) - return false; - } } break; } @@ -1094,13 +1098,12 @@ if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(), Spec2->getTemplateName())) return false; - if (Spec1->getNumArgs() != Spec2->getNumArgs()) + if (!IsStructurallyEquivalent(Context, Spec1->getSpecifiedArguments(), + Spec2->getSpecifiedArguments())) + return false; + if (!IsStructurallyEquivalent(Context, Spec1->getConvertedArguments(), + Spec2->getConvertedArguments())) return false; - for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { - if (!IsStructurallyEquivalent(Context, Spec1->getArg(I), - Spec2->getArg(I))) - return false; - } break; } @@ -1151,13 +1154,9 @@ if (!IsStructurallyEquivalent(Spec1->getIdentifier(), Spec2->getIdentifier())) return false; - if (Spec1->getNumArgs() != Spec2->getNumArgs()) + if (!IsStructurallyEquivalent(Context, Spec1->template_arguments(), + Spec2->template_arguments())) return false; - for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { - if (!IsStructurallyEquivalent(Context, Spec1->getArg(I), - Spec2->getArg(I))) - return false; - } break; } diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp --- a/clang/lib/AST/DeclPrinter.cpp +++ b/clang/lib/AST/DeclPrinter.cpp @@ -1001,7 +1001,7 @@ if (const auto* TSI = S->getTypeAsWritten()) if (const auto *TST = dyn_cast(TSI->getType())) - Args = TST->template_arguments(); + Args = TST->getSpecifiedArguments(); printTemplateArguments( Args, S->getSpecializedTemplate()->getTemplateParameters()); } diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp --- a/clang/lib/AST/DeclTemplate.cpp +++ b/clang/lib/AST/DeclTemplate.cpp @@ -624,9 +624,10 @@ TemplateParameterList *Params = getTemplateParameters(); SmallVector TemplateArgs; Context.getInjectedTemplateArgs(Params, TemplateArgs); - CommonPtr->InjectedClassNameType - = Context.getTemplateSpecializationType(TemplateName(this), - TemplateArgs); + CommonPtr->InjectedClassNameType = Context.getTemplateSpecializationType( + TemplateName(this), + /*SpecifiedArgs=*/TemplateArgs, /*SugaredConvertedArgs=*/TemplateArgs, + /*CanonicalConvertedArgs=*/{}); return CommonPtr->InjectedClassNameType; } @@ -1075,15 +1076,12 @@ } ClassTemplatePartialSpecializationDecl * -ClassTemplatePartialSpecializationDecl:: -Create(ASTContext &Context, TagKind TK,DeclContext *DC, - SourceLocation StartLoc, SourceLocation IdLoc, - TemplateParameterList *Params, - ClassTemplateDecl *SpecializedTemplate, - ArrayRef Args, - const TemplateArgumentListInfo &ArgInfos, - QualType CanonInjectedType, - ClassTemplatePartialSpecializationDecl *PrevDecl) { +ClassTemplatePartialSpecializationDecl::Create( + ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, + SourceLocation IdLoc, TemplateParameterList *Params, + ClassTemplateDecl *SpecializedTemplate, ArrayRef Args, + const TemplateArgumentListInfo &ArgInfos, QualType InjectedType, + ClassTemplatePartialSpecializationDecl *PrevDecl) { const ASTTemplateArgumentListInfo *ASTArgInfos = ASTTemplateArgumentListInfo::Create(Context, ArgInfos); @@ -1094,7 +1092,7 @@ Result->setSpecializationKind(TSK_ExplicitSpecialization); Result->setMayHaveOutOfDateDef(false); - Context.getInjectedClassNameType(Result, CanonInjectedType); + Context.getInjectedClassNameType(Result, InjectedType); return Result; } diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -1612,11 +1612,13 @@ MemberExpr::MemberExpr(Expr *Base, bool IsArrow, SourceLocation OperatorLoc, ValueDecl *MemberDecl, - const DeclarationNameInfo &NameInfo, QualType T, + const DeclarationNameInfo &NameInfo, + const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK, ExprObjectKind OK, NonOdrUseReason NOUR) : Expr(MemberExprClass, T, VK, OK), Base(Base), MemberDecl(MemberDecl), - MemberDNLoc(NameInfo.getInfo()), MemberLoc(NameInfo.getLoc()) { + Deduced(Deduced), MemberDNLoc(NameInfo.getInfo()), + MemberLoc(NameInfo.getLoc()) { assert(!NameInfo.getName() || MemberDecl->getDeclName() == NameInfo.getName()); MemberExprBits.IsArrow = IsArrow; @@ -1633,7 +1635,8 @@ NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *MemberDecl, DeclAccessPair FoundDecl, DeclarationNameInfo NameInfo, const TemplateArgumentListInfo *TemplateArgs, - QualType T, ExprValueKind VK, ExprObjectKind OK, NonOdrUseReason NOUR) { + const TemplateArgumentList *Deduced, QualType T, ExprValueKind VK, + ExprObjectKind OK, NonOdrUseReason NOUR) { bool HasQualOrFound = QualifierLoc || FoundDecl.getDecl() != MemberDecl || FoundDecl.getAccess() != MemberDecl->getAccess(); bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid(); @@ -1645,7 +1648,7 @@ void *Mem = C.Allocate(Size, alignof(MemberExpr)); MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl, - NameInfo, T, VK, OK, NOUR); + NameInfo, Deduced, T, VK, OK, NOUR); // FIXME: remove remaining dependence computation to computeDependence(). auto Deps = E->getDependence(); diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -1244,8 +1244,9 @@ // FIXME: GCC does not appear to mangle the template arguments when // the template in question is a dependent template name. Should we // emulate that badness? - mangleTemplateArgs(TST->getTemplateName(), TST->getArgs(), - TST->getNumArgs()); + auto Args = TST->isCanonicalUnqualified() ? TST->getConvertedArguments() + : TST->getSpecifiedArguments(); + mangleTemplateArgs(TST->getTemplateName(), Args.begin(), Args.size()); addSubstitution(QualType(TST, 0)); } } else if (const auto *DTST = @@ -2410,13 +2411,14 @@ break; } } - + auto Args = TST->isCanonicalUnqualified() ? TST->getConvertedArguments() + : TST->getSpecifiedArguments(); // Note: we don't pass in the template name here. We are mangling the // original source-level template arguments, so we shouldn't consider // conversions to the corresponding template parameter. // FIXME: Other compilers mangle partially-resolved template arguments in // unresolved-qualifier-levels. - mangleTemplateArgs(TemplateName(), TST->getArgs(), TST->getNumArgs()); + mangleTemplateArgs(TemplateName(), Args.begin(), Args.size()); break; } @@ -3873,8 +3875,10 @@ } void CXXNameMangler::mangleType(const TemplateSpecializationType *T) { + auto Args = T->isCanonicalUnqualified() ? T->getConvertedArguments() + : T->getSpecifiedArguments(); if (TemplateDecl *TD = T->getTemplateName().getAsTemplateDecl()) { - mangleTemplateName(TD, T->getArgs(), T->getNumArgs()); + mangleTemplateName(TD, Args.begin(), Args.size()); } else { if (mangleSubstitution(QualType(T, 0))) return; @@ -3884,7 +3888,7 @@ // FIXME: GCC does not appear to mangle the template arguments when // the template in question is a dependent template name. Should we // emulate that badness? - mangleTemplateArgs(T->getTemplateName(), T->getArgs(), T->getNumArgs()); + mangleTemplateArgs(T->getTemplateName(), Args.begin(), Args.size()); addSubstitution(QualType(T, 0)); } } diff --git a/clang/lib/AST/NestedNameSpecifier.cpp b/clang/lib/AST/NestedNameSpecifier.cpp --- a/clang/lib/AST/NestedNameSpecifier.cpp +++ b/clang/lib/AST/NestedNameSpecifier.cpp @@ -315,7 +315,7 @@ TemplateName::Qualified::None); // Print the template argument list. - printTemplateArgumentList(OS, SpecType->template_arguments(), + printTemplateArgumentList(OS, SpecType->getSpecifiedArguments(), InnerPolicy); } else if (const auto *DepSpecType = dyn_cast(T)) { diff --git a/clang/lib/AST/ODRHash.cpp b/clang/lib/AST/ODRHash.cpp --- a/clang/lib/AST/ODRHash.cpp +++ b/clang/lib/AST/ODRHash.cpp @@ -1015,8 +1015,8 @@ void VisitEnumType(const EnumType *T) { VisitTagType(T); } void VisitTemplateSpecializationType(const TemplateSpecializationType *T) { - ID.AddInteger(T->getNumArgs()); - for (const auto &TA : T->template_arguments()) { + ID.AddInteger(T->getSpecifiedArguments().size()); + for (const auto &TA : T->getSpecifiedArguments()) { Hash.AddTemplateArgument(TA); } Hash.AddTemplateName(T->getTemplateName()); diff --git a/clang/lib/AST/QualTypeNames.cpp b/clang/lib/AST/QualTypeNames.cpp --- a/clang/lib/AST/QualTypeNames.cpp +++ b/clang/lib/AST/QualTypeNames.cpp @@ -129,11 +129,9 @@ if (const auto *TST = dyn_cast(TypePtr)) { bool MightHaveChanged = false; SmallVector FQArgs; - for (TemplateSpecializationType::iterator I = TST->begin(), E = TST->end(); - I != E; ++I) { - // Cheap to copy and potentially modified by - // getFullyQualifedTemplateArgument. - TemplateArgument Arg(*I); + // Cheap to copy and potentially modified by + // getFullyQualifedTemplateArgument. + for (TemplateArgument Arg : TST->getSpecifiedArguments()) { MightHaveChanged |= getFullyQualifiedTemplateArgument( Ctx, Arg, WithGlobalNsPrefix); FQArgs.push_back(Arg); @@ -143,8 +141,8 @@ // allocate new type in the AST. if (MightHaveChanged) { QualType QT = Ctx.getTemplateSpecializationType( - TST->getTemplateName(), FQArgs, - TST->getCanonicalTypeInternal()); + TST->getTemplateName(), FQArgs, TST->getConvertedArguments(), + /*CanonicalConvertedArgs=*/{}, TST->getCanonicalTypeInternal()); // getTemplateSpecializationType returns a fully qualified // version of the specialization itself, so no need to qualify // it. @@ -175,7 +173,8 @@ if (MightHaveChanged) { TemplateName TN(TSTDecl->getSpecializedTemplate()); QualType QT = Ctx.getTemplateSpecializationType( - TN, FQArgs, + TN, FQArgs, FQArgs, + /*CanonicalConvertedArgs=*/{}, TSTRecord->getCanonicalTypeInternal()); // getTemplateSpecializationType returns a fully qualified // version of the specialization itself, so no need to qualify diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp --- a/clang/lib/AST/Type.cpp +++ b/clang/lib/AST/Type.cpp @@ -1774,6 +1774,17 @@ return nullptr; } +const TemplateSpecializationType * +Type::getAsNonAliasTemplateSpecializationType() const { + for (auto T = this; /**/; /**/) { + const TemplateSpecializationType *TST = + T->getAs(); + if (!TST || !TST->isTypeAlias()) + return TST; + T = TST->desugar().getTypePtr(); + } +} + bool Type::hasAttr(attr::Kind AK) const { const Type *Cur = this; while (const auto *AT = Cur->getAs()) { @@ -3808,16 +3819,56 @@ } TemplateSpecializationType::TemplateSpecializationType( - TemplateName T, ArrayRef Args, QualType Canon, + TemplateName T, ArrayRef ConvertedArgs) + : Type(TemplateSpecialization, QualType(this, 0), + TypeDependence::DependentInstantiation | + (toTypeDependence(T.getDependence()) & + TypeDependence::UnexpandedPack)), + Template(T) { + TemplateSpecializationTypeBits.NumSpecifiedArgs = 0; + TemplateSpecializationTypeBits.NumConvertedArgs = ConvertedArgs.size(); + TemplateSpecializationTypeBits.TypeAlias = false; + + assert(!T.getAsDependentTemplateName() && + "Use DependentTemplateSpecializationType for dependent template-name"); + assert((T.getKind() == TemplateName::Template || + T.getKind() == TemplateName::SubstTemplateTemplateParm || + T.getKind() == TemplateName::SubstTemplateTemplateParmPack || + T.getKind() == TemplateName::UsingTemplate) && + "Unexpected template name for TemplateSpecializationType"); + + auto *ConvArgs = + const_cast(getConvertedArguments().data()); + for (const TemplateArgument &Arg : ConvertedArgs) { + // Update instantiation-dependent, variably-modified, and error bits. + // If the canonical type exists and is non-dependent, the template + // specialization type can be non-dependent even if one of the type + // arguments is. Given: + // template using U = int; + // U is always non-dependent, irrespective of the type T. + // However, U contains an unexpanded parameter pack, even though + // its expansion (and thus its desugared type) doesn't. + addDependence(toTypeDependence(Arg.getDependence()) & + ~TypeDependence::Dependent); + if (Arg.getKind() == TemplateArgument::Type) + addDependence(Arg.getAsType()->getDependence() & + TypeDependence::VariablyModified); + new (ConvArgs++) TemplateArgument(Arg); + } +} + +TemplateSpecializationType::TemplateSpecializationType( + TemplateName T, ArrayRef SpecifiedArgs, + ArrayRef ConvertedArgs, QualType Canon, QualType AliasedType) : Type(TemplateSpecialization, Canon.isNull() ? QualType(this, 0) : Canon, - (Canon.isNull() - ? TypeDependence::DependentInstantiation - : toSemanticDependence(Canon->getDependence())) | + (Canon.isNull() ? TypeDependence::DependentInstantiation + : toSemanticDependence(Canon->getDependence())) | (toTypeDependence(T.getDependence()) & TypeDependence::UnexpandedPack)), Template(T) { - TemplateSpecializationTypeBits.NumArgs = Args.size(); + TemplateSpecializationTypeBits.NumSpecifiedArgs = SpecifiedArgs.size(); + TemplateSpecializationTypeBits.NumConvertedArgs = ConvertedArgs.size(); TemplateSpecializationTypeBits.TypeAlias = !AliasedType.isNull(); assert(!T.getAsDependentTemplateName() && @@ -3828,8 +3879,9 @@ T.getKind() == TemplateName::UsingTemplate) && "Unexpected template name for TemplateSpecializationType"); - auto *TemplateArgs = reinterpret_cast(this + 1); - for (const TemplateArgument &Arg : Args) { + auto *SpecArgs = + const_cast(getSpecifiedArguments().data()); + for (const TemplateArgument &Arg : SpecifiedArgs) { // Update instantiation-dependent, variably-modified, and error bits. // If the canonical type exists and is non-dependent, the template // specialization type can be non-dependent even if one of the type @@ -3843,23 +3895,39 @@ if (Arg.getKind() == TemplateArgument::Type) addDependence(Arg.getAsType()->getDependence() & TypeDependence::VariablyModified); - new (TemplateArgs++) TemplateArgument(Arg); + new (SpecArgs++) TemplateArgument(Arg); } + // For a non-canonical TST, the converted args don't figure into + // the dependence. + auto *ConvArgs = + const_cast(getConvertedArguments().data()); + for (const TemplateArgument &Arg : ConvertedArgs) + new (ConvArgs++) TemplateArgument(Arg); + // Store the aliased type if this is a type alias template specialization. - if (isTypeAlias()) { - auto *Begin = reinterpret_cast(this + 1); - *reinterpret_cast(Begin + getNumArgs()) = AliasedType; - } + if (isTypeAlias()) + *reinterpret_cast(const_cast( + getConvertedArguments().end())) = AliasedType; } -void -TemplateSpecializationType::Profile(llvm::FoldingSetNodeID &ID, - TemplateName T, - ArrayRef Args, - const ASTContext &Context) { +ArrayRef +TemplateSpecializationType::getConvertedArguments() const { + return {getSpecifiedArguments().end(), + TemplateSpecializationTypeBits.NumConvertedArgs}; +} + +QualType TemplateSpecializationType::getAliasedType() const { + assert(isTypeAlias() && "not a type alias template specialization"); + return *reinterpret_cast(getConvertedArguments().end()); +} + +void TemplateSpecializationType::Profile( + llvm::FoldingSetNodeID &ID, TemplateName T, + ArrayRef ConvertedArgs, const ASTContext &Context) { T.Profile(ID); - for (const TemplateArgument &Arg : Args) + ID.AddInteger(ConvertedArgs.size()); + for (const TemplateArgument &Arg : ConvertedArgs) Arg.Profile(ID, Context); } diff --git a/clang/lib/AST/TypeLoc.cpp b/clang/lib/AST/TypeLoc.cpp --- a/clang/lib/AST/TypeLoc.cpp +++ b/clang/lib/AST/TypeLoc.cpp @@ -568,17 +568,16 @@ setTemplateNameLoc(Loc); setLAngleLoc(Loc); setRAngleLoc(Loc); - TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(), - getTypePtr()->getArgs(), - getArgInfos(), Loc); + TemplateSpecializationTypeLoc::initializeArgLocs( + Context, + ArrayRef(getTypePtr()->getArgs(), getNumArgs()), + getArgInfos(), Loc); } -void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context, - unsigned NumArgs, - const TemplateArgument *Args, - TemplateArgumentLocInfo *ArgInfos, - SourceLocation Loc) { - for (unsigned i = 0, e = NumArgs; i != e; ++i) { +void TemplateSpecializationTypeLoc::initializeArgLocs( + ASTContext &Context, ArrayRef Args, + TemplateArgumentLocInfo *ArgInfos, SourceLocation Loc) { + for (unsigned i = 0, e = Args.size(); i != e; ++i) { switch (Args[i].getKind()) { case TemplateArgument::Null: llvm_unreachable("Impossible TemplateArgument"); @@ -635,9 +634,10 @@ setRAngleLoc(Loc); setLAngleLoc(Loc); setRParenLoc(Loc); - TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(), - getTypePtr()->getArgs(), - getArgInfos(), Loc); + TemplateSpecializationTypeLoc::initializeArgLocs( + Context, + ArrayRef(getTypePtr()->getArgs(), getNumArgs()), + getArgInfos(), Loc); setNameLoc(Loc); } diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1379,7 +1379,7 @@ if (!Policy.PrintCanonicalTypes && TAW) { const TemplateSpecializationType *TST = cast(TAW->getType()); - Args = TST->template_arguments(); + Args = TST->getSpecifiedArguments(); } else { const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); Args = TemplateArgs.asArray(); @@ -1502,7 +1502,10 @@ DefaultTemplateArgsPolicyRAII TemplateArgs(Policy); const TemplateParameterList *TPL = TD ? TD->getTemplateParameters() : nullptr; - printTemplateArgumentList(OS, T->template_arguments(), Policy, TPL); + ArrayRef Args = T->isCanonicalUnqualified() + ? T->getConvertedArguments() + : T->getSpecifiedArguments(); + printTemplateArgumentList(OS, Args, Policy, TPL); spaceBeforePlaceHolder(OS); } @@ -1985,7 +1988,8 @@ ArrayRef TemplateArgs; if (auto *TTST = T->getAs()) { Template = TTST->getTemplateName(); - TemplateArgs = TTST->template_arguments(); + assert(!TTST->isCanonicalUnqualified()); + TemplateArgs = TTST->getSpecifiedArguments(); } else if (auto *CTSD = dyn_cast_or_null( T->getAsCXXRecordDecl())) { Template = TemplateName(CTSD->getSpecializedTemplate()); @@ -1997,11 +2001,12 @@ if (!isSubstitutedTemplateArgument(Ctx, Template, PTST->getTemplateName(), Args, Depth)) return false; - if (TemplateArgs.size() != PTST->getNumArgs()) + ArrayRef PArgs = PTST->getConvertedArguments(); + if (TemplateArgs.size() != PArgs.size()) return false; for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) - if (!isSubstitutedTemplateArgument(Ctx, TemplateArgs[I], PTST->getArg(I), - Args, Depth)) + if (!isSubstitutedTemplateArgument(Ctx, TemplateArgs[I], PArgs[I], Args, + Depth)) return false; return true; } diff --git a/clang/lib/Analysis/BodyFarm.cpp b/clang/lib/Analysis/BodyFarm.cpp --- a/clang/lib/Analysis/BodyFarm.cpp +++ b/clang/lib/Analysis/BodyFarm.cpp @@ -230,8 +230,8 @@ C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(), SourceLocation(), MemberDecl, FoundDecl, DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()), - /* TemplateArgumentListInfo=*/ nullptr, MemberDecl->getType(), ValueKind, - OK_Ordinary, NOUR_None); + /* TemplateArgumentListInfo=*/nullptr, /*Deduced=*/nullptr, + MemberDecl->getType(), ValueKind, OK_Ordinary, NOUR_None); } ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) { diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -1275,7 +1275,8 @@ llvm::raw_svector_ostream OS(NS); Ty->getTemplateName().print(OS, getPrintingPolicy(), TemplateName::Qualified::None); - printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy()); + printTemplateArgumentList(OS, Ty->getSpecifiedArguments(), + getPrintingPolicy()); SourceLocation Loc = AliasDecl->getLocation(); return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc), diff --git a/clang/lib/Index/USRGeneration.cpp b/clang/lib/Index/USRGeneration.cpp --- a/clang/lib/Index/USRGeneration.cpp +++ b/clang/lib/Index/USRGeneration.cpp @@ -855,9 +855,9 @@ = T->getAs()) { Out << '>'; VisitTemplateName(Spec->getTemplateName()); - Out << Spec->getNumArgs(); - for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) - VisitTemplateArgument(Spec->getArg(I)); + Out << Spec->getConvertedArguments().size(); + for (const TemplateArgument &Arg : Spec->getConvertedArguments()) + VisitTemplateArgument(Arg); return; } if (const DependentNameType *DNT = T->getAs()) { diff --git a/clang/lib/Sema/SemaCXXScopeSpec.cpp b/clang/lib/Sema/SemaCXXScopeSpec.cpp --- a/clang/lib/Sema/SemaCXXScopeSpec.cpp +++ b/clang/lib/Sema/SemaCXXScopeSpec.cpp @@ -99,8 +99,7 @@ if (ClassTemplateDecl *ClassTemplate = dyn_cast_or_null( SpecType->getTemplateName().getAsTemplateDecl())) { - QualType ContextType - = Context.getCanonicalType(QualType(SpecType, 0)); + auto ContextType = QualType(SpecType, 0); // If the type of the nested name specifier is the same as the // injected class name of the named class template, we're entering @@ -734,8 +733,8 @@ return false; } - QualType T = - Context.getTypeDeclType(cast(SD->getUnderlyingDecl())); + QualType T = resugar( + SS, Context.getTypeDeclType(cast(SD->getUnderlyingDecl()))); if (T->isEnumeralType()) Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); @@ -875,17 +874,19 @@ QualType T = BuildDecltypeType(DS.getRepAsExpr()); if (T.isNull()) return true; + T = resugar(SS, T); + + TypeLocBuilder TLB; + DecltypeTypeLoc DecltypeTL = TLB.push(T); + DecltypeTL.setDecltypeLoc(DS.getTypeSpecTypeLoc()); + DecltypeTL.setRParenLoc(DS.getTypeofParensRange().getEnd()); if (!T->isDependentType() && !T->getAs()) { Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace) - << T << getLangOpts().CPlusPlus; + << T << getLangOpts().CPlusPlus; return true; } - TypeLocBuilder TLB; - DecltypeTypeLoc DecltypeTL = TLB.push(T); - DecltypeTL.setDecltypeLoc(DS.getTypeSpecTypeLoc()); - DecltypeTL.setRParenLoc(DS.getTypeofParensRange().getEnd()); SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), ColonColonLoc); return false; @@ -931,10 +932,9 @@ // Handle a dependent template specialization for which we cannot resolve // the template name. assert(DTN->getQualifier() == SS.getScopeRep()); - QualType T = Context.getDependentTemplateSpecializationType(ETK_None, - DTN->getQualifier(), - DTN->getIdentifier(), - TemplateArgs); + QualType T = Context.getDependentTemplateSpecializationType( + ETK_None, DTN->getQualifier(), DTN->getIdentifier(), + TemplateArgs.arguments()); // Create source-location information for this type. TypeLocBuilder Builder; @@ -975,18 +975,10 @@ // We were able to resolve the template name to an actual template. // Build an appropriate nested-name-specifier. - QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); + QualType T = CheckTemplateIdType(SS, Template, TemplateNameLoc, TemplateArgs); if (T.isNull()) return true; - // Alias template specializations can produce types which are not valid - // nested name specifiers. - if (!T->isDependentType() && !T->getAs()) { - Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; - NoteAllFoundTemplates(Template); - return true; - } - // Provide source-location information for the template specialization type. TypeLocBuilder Builder; TemplateSpecializationTypeLoc SpecTL @@ -998,6 +990,13 @@ for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); + // Alias template specializations can produce types which are not valid + // nested name specifiers. + if (!T->isDependentType() && !T->getAs()) { + Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; + NoteAllFoundTemplates(Template); + return true; + } SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), CCLoc); diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -1808,7 +1808,9 @@ return TC_Failed; } - SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn); + // FIXME: resugar + SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr, FoundOverload, Fn, + nullptr); if (!SrcExpr.isUsable()) { msg = 0; return TC_Failed; @@ -2885,7 +2887,9 @@ DeclAccessPair DAP; if (FunctionDecl *FD = Self.ResolveAddressOfOverloadedFunction( SrcExpr.get(), DestType, /*Complain=*/true, DAP)) - SrcExpr = Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD); + // FIXME: resugar + SrcExpr = + Self.FixOverloadedFunctionReference(SrcExpr.get(), DAP, FD, nullptr); else return; assert(SrcExpr.isUsable()); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -587,9 +587,11 @@ if (!FD || FD->isUnnamedBitfield() || FD->isAnonymousStructOrUnion()) continue; + QualType FieldType = FD->getType(); + llvm::SmallString<20> Format = llvm::StringRef("%s%s %s "); llvm::SmallVector Args = {FieldIndentArg, - getTypeString(FD->getType()), + getTypeString(FieldType), getStringLiteral(FD->getName())}; if (FD->isBitField()) { @@ -605,15 +607,16 @@ ExprResult Field = IFD ? S.BuildAnonymousStructUnionMemberReference( CXXScopeSpec(), Loc, IFD, - DeclAccessPair::make(IFD, AS_public), RecordArg, Loc) + DeclAccessPair::make(IFD, AS_public), RecordArg, nullptr, + Loc) : S.BuildFieldReferenceExpr( - RecordArg, RecordArgIsPtr, Loc, CXXScopeSpec(), FD, - DeclAccessPair::make(FD, AS_public), + RecordArg, RecordArgIsPtr, Loc, NestedNameSpecifierLoc(), + FD, FieldType, DeclAccessPair::make(FD, AS_public), DeclarationNameInfo(FD->getDeclName(), Loc)); if (Field.isInvalid()) return true; - auto *InnerRD = FD->getType()->getAsRecordDecl(); + auto *InnerRD = FieldType->getAsRecordDecl(); auto *InnerCXXRD = dyn_cast_or_null(InnerRD); if (InnerRD && (!InnerCXXRD || InnerCXXRD->isAggregate())) { // Recursively print the values of members of aggregate record type. @@ -622,7 +625,7 @@ return true; } else { Format += " "; - if (appendFormatSpecifier(FD->getType(), Format)) { + if (appendFormatSpecifier(FieldType, Format)) { // We know how to print this field. Args.push_back(Field.get()); } else { diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -4659,9 +4659,10 @@ // Note we only handle the sugared types, they closely match what users wrote. // We explicitly choose to not handle ClassTemplateSpecializationDecl. if (auto *Specialization = T->getAs()) { - if (Specialization->getNumArgs() != 1) + if (Specialization->getSpecifiedArguments().size() != 1) return nullptr; - const TemplateArgument &Argument = Specialization->getArg(0); + const TemplateArgument &Argument = + Specialization->getSpecifiedArguments()[0]; if (Argument.getKind() != TemplateArgument::Type) return nullptr; return Argument.getAsType()->getAs(); diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp --- a/clang/lib/Sema/SemaCoroutine.cpp +++ b/clang/lib/Sema/SemaCoroutine.cpp @@ -91,8 +91,8 @@ AddArg(T); // Build the template-id. - QualType CoroTrait = - S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args); + QualType CoroTrait = S.CheckTemplateIdType( + CXXScopeSpec(), TemplateName(CoroTraits), KwLoc, Args); if (CoroTrait.isNull()) return QualType(); if (S.RequireCompleteType(KwLoc, CoroTrait, @@ -169,8 +169,8 @@ S.Context.getTrivialTypeSourceInfo(PromiseType, Loc))); // Build the template-id. - QualType CoroHandleType = - S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args); + QualType CoroHandleType = S.CheckTemplateIdType( + CXXScopeSpec(), TemplateName(CoroHandle), Loc, Args); if (CoroHandleType.isNull()) return QualType(); if (S.RequireCompleteType(Loc, CoroHandleType, diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -531,6 +531,8 @@ DiagnoseUseOfDecl(IIDecl, NameLoc); T = Context.getTypeDeclType(TD); + if (SS && SS->isNotEmpty()) + T = resugar(*SS, T); MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); } else if (ObjCInterfaceDecl *IDecl = dyn_cast(IIDecl)) { (void)DiagnoseUseOfDecl(IDecl, NameLoc); @@ -12410,6 +12412,7 @@ return true; } + // FIXME: Set TypeSourceInfo? VDecl->setType(DeducedType); assert(VDecl->isLinkageValid()); diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -3599,9 +3599,13 @@ return; } } else if (auto *ULE = dyn_cast(E)) { - if (ULE->hasExplicitTemplateArgs()) + if (ULE->hasExplicitTemplateArgs()) { S.Diag(Loc, diag::warn_cleanup_ext); - FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); + TemplateArgumentListInfo ExplicitTemplateArgs; + ULE->copyTemplateArgumentsInto(ExplicitTemplateArgs); + FD = S.ResolveSingleFunctionTemplateSpecialization( + ULE, ExplicitTemplateArgs, /*Complain=*/true); + } NI = ULE->getNameInfo(); if (!FD) { S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 @@ -3623,6 +3627,7 @@ // We're currently more strict than GCC about what function types we accept. // If this ever proves to be a problem it should be easy to fix. + // FIXME: resugar QualType Ty = S.Context.getPointerType(cast(D)->getType()); QualType ParamTy = FD->getParamDecl(0)->getType(); if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -1032,7 +1032,8 @@ } // Build the template-id. - QualType TraitTy = S.CheckTemplateIdType(TemplateName(TraitTD), Loc, Args); + QualType TraitTy = + S.CheckTemplateIdType(CXXScopeSpec(), TemplateName(TraitTD), Loc, Args); if (TraitTy.isNull()) return true; if (!S.isCompleteType(Loc, TraitTy)) { @@ -1402,6 +1403,9 @@ if (FD->isUnnamedBitfield()) continue; + // FIXME: Avoid having to recreate the naming context for every field. + QualType FieldType = S.resugar(DecompType.getTypePtr(), FD->getType()); + // All the non-static data members are required to be nameable, so they // must all have names. if (!FD->getDeclName()) { @@ -1413,7 +1417,7 @@ if (FD->isAnonymousStructOrUnion()) { S.Diag(Src->getLocation(), diag::err_decomp_decl_anon_union_member) - << DecompType << FD->getType()->isUnionType(); + << DecompType << FieldType->isUnionType(); S.Diag(FD->getLocation(), diag::note_declared_at); return true; } @@ -1445,7 +1449,7 @@ if (E.isInvalid()) return true; E = S.BuildFieldReferenceExpr(E.get(), /*IsArrow*/ false, Loc, - CXXScopeSpec(), FD, + NestedNameSpecifierLoc(), FD, FieldType, DeclAccessPair::make(FD, FD->getAccess()), DeclarationNameInfo(FD->getDeclName(), Loc)); if (E.isInvalid()) @@ -1459,7 +1463,7 @@ Qualifiers Q = DecompType.getQualifiers(); if (FD->isMutable()) Q.removeConst(); - B->setBinding(S.BuildQualifiedType(FD->getType(), Loc, Q), E.get()); + B->setBinding(S.BuildQualifiedType(FieldType, Loc, Q), E.get()); } if (I != Bindings.size()) @@ -8270,10 +8274,13 @@ DeclAccessPair Found = DeclAccessPair::make(Field, Field->getAccess()); DeclarationNameInfo NameInfo(Field->getDeclName(), Loc); + QualType FieldType = Field->getType(); return {S.BuildFieldReferenceExpr(Obj.first.get(), /*IsArrow=*/false, Loc, - CXXScopeSpec(), Field, Found, NameInfo), + NestedNameSpecifierLoc(), Field, + FieldType, Found, NameInfo), S.BuildFieldReferenceExpr(Obj.second.get(), /*IsArrow=*/false, Loc, - CXXScopeSpec(), Field, Found, NameInfo)}; + NestedNameSpecifierLoc(), Field, + FieldType, Found, NameInfo)}; } // FIXME: When expanding a subobject, register a note in the code synthesis @@ -11503,7 +11510,7 @@ Ty->getAs()) { Template = dyn_cast_or_null( TST->getTemplateName().getAsTemplateDecl()); - Arguments = TST->getArgs(); + Arguments = TST->getSpecifiedArguments().begin(); } if (!Template) return false; @@ -11585,7 +11592,8 @@ return Context.getElaboratedType( ElaboratedTypeKeyword::ETK_None, NestedNameSpecifier::Create(Context, nullptr, getStdNamespace()), - CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); + CheckTemplateIdType(CXXScopeSpec(), TemplateName(StdInitializerList), Loc, + Args)); } bool Sema::isInitListConstructor(const FunctionDecl *Ctor) { diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -3307,6 +3307,8 @@ QualType type = VD->getType(); if (type.isNull()) return ExprError(); + type = TemplateArgs ? resugar(SS, VD, TemplateArgs->arguments(), type) + : resugar(SS, type); ExprValueKind valueKind = VK_PRValue; // In 'T ...V;', the type of the declaration 'V' is 'T...', but the type of @@ -9967,7 +9969,8 @@ DeclAccessPair DAP; if (FunctionDecl *FD = ResolveAddressOfOverloadedFunction( RHS.get(), LHSType, /*Complain=*/false, DAP)) - RHS = FixOverloadedFunctionReference(RHS.get(), DAP, FD); + // FIXME: resugar + RHS = FixOverloadedFunctionReference(RHS.get(), DAP, FD, nullptr); else return Incompatible; } @@ -14258,14 +14261,18 @@ } OverloadExpr *Ovl = cast(E); - if (isa(Ovl)) - if (!ResolveSingleFunctionTemplateSpecialization(Ovl)) { - Diag(OpLoc, diag::err_invalid_form_pointer_member_function) - << OrigOp.get()->getSourceRange(); - return QualType(); - } - - return Context.OverloadTy; + if (!isa(Ovl)) + return Context.OverloadTy; + if (Ovl->hasExplicitTemplateArgs()) { + TemplateArgumentListInfo ExplicitTemplateArgs; + Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); + if (ResolveSingleFunctionTemplateSpecialization(Ovl, + ExplicitTemplateArgs)) + return Context.OverloadTy; + } + Diag(OpLoc, diag::err_invalid_form_pointer_member_function) + << OrigOp.get()->getSourceRange(); + return QualType(); } if (PTy->getKind() == BuiltinType::UnknownAny) @@ -14371,8 +14378,24 @@ if (isa(MD)) Diag(OpLoc, diag::err_typecheck_addrof_dtor) << op->getSourceRange(); - QualType MPTy = Context.getMemberPointerType( - op->getType(), Context.getTypeDeclType(MD->getParent()).getTypePtr()); + const CXXRecordDecl *Cls = MD->getParent(); + const Type *ClsType = nullptr; + if (const NestedNameSpecifier *NNS = DRE->getQualifier()) { + const Type *Type = NNS->getAsType(); + const CXXRecordDecl *ClsAsWritten = + Type ? Type->getAsCXXRecordDecl() : NNS->getAsRecordDecl(); + assert(ClsAsWritten != nullptr); + if (declaresSameEntity(Cls, ClsAsWritten)) + ClsType = + Type ? Type : Context.getTypeDeclType(ClsAsWritten).getTypePtr(); + else + // FIXME: Can we do better here? + assert(ClsAsWritten->isDerivedFrom(Cls)); + } + if (!ClsType) + ClsType = Context.getTypeDeclType(Cls).getTypePtr(); + + QualType MPTy = Context.getMemberPointerType(op->getType(), ClsType); // Under the MS ABI, lock down the inheritance model now. if (Context.getTargetInfo().getCXXABI().isMicrosoft()) (void)isCompleteType(OpLoc, MPTy); @@ -19183,8 +19206,8 @@ S.Context, Base.get(), ME->isArrow(), ME->getOperatorLoc(), ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), ME->getMemberDecl(), ME->getFoundDecl(), ME->getMemberNameInfo(), - CopiedTemplateArgs(ME), ME->getType(), ME->getValueKind(), - ME->getObjectKind(), ME->isNonOdrUse()); + CopiedTemplateArgs(ME), ME->getDeduced(), ME->getType(), + ME->getValueKind(), ME->getObjectKind(), ME->isNonOdrUse()); } if (ME->getMemberDecl()->isCXXInstanceMember()) @@ -19201,7 +19224,8 @@ S.Context, ME->getBase(), ME->isArrow(), ME->getOperatorLoc(), ME->getQualifierLoc(), ME->getTemplateKeywordLoc(), ME->getMemberDecl(), ME->getFoundDecl(), ME->getMemberNameInfo(), CopiedTemplateArgs(ME), - ME->getType(), ME->getValueKind(), ME->getObjectKind(), NOUR); + ME->getDeduced(), ME->getType(), ME->getValueKind(), + ME->getObjectKind(), NOUR); } case Expr::BinaryOperatorClass: { @@ -19503,10 +19527,18 @@ // Re-set the member to trigger a recomputation of the dependence bits // for the expression. - if (auto *DRE = dyn_cast_or_null(E)) + CXXScopeSpec SS; + if (auto *DRE = dyn_cast_or_null(E)) { DRE->setDecl(DRE->getDecl()); - else if (auto *ME = dyn_cast_or_null(E)) + SS.Adopt(DRE->getQualifierLoc()); + DRE->setType(SemaRef.resugar( + SS, DRE->getDecl(), DRE->template_arguments(), DRE->getType())); + } else if (auto *ME = dyn_cast_or_null(E)) { ME->setMemberDecl(ME->getMemberDecl()); + SS.Adopt(ME->getQualifierLoc()); + ME->setType(SemaRef.resugar(SS, ME->getMemberDecl(), + ME->template_arguments(), ME->getType())); + } } else if (FirstInstantiation || isa(Var)) { // FIXME: For a specialization of a variable template, we don't @@ -20741,6 +20773,7 @@ SS.Adopt(DRE->getQualifierLoc()); TemplateArgumentListInfo TemplateArgs; DRE->copyTemplateArgumentsInto(TemplateArgs); + // FIXME: resugar return BuildDeclRefExpr( FD, FD->getType(), VK_LValue, DRE->getNameInfo(), DRE->hasQualifier() ? &SS : nullptr, DRE->getFoundDecl(), diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -4250,7 +4250,8 @@ if (DiagnoseUseOfDecl(Fn, From->getBeginLoc())) return ExprError(); - From = FixOverloadedFunctionReference(From, Found, Fn); + // FIXME: resugar + From = FixOverloadedFunctionReference(From, Found, Fn, nullptr); // We might get back another placeholder expression if we resolved to a // builtin. diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp --- a/clang/lib/Sema/SemaExprMember.cpp +++ b/clang/lib/Sema/SemaExprMember.cpp @@ -798,18 +798,14 @@ false, ExtraArgs); } -ExprResult -Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, - SourceLocation loc, - IndirectFieldDecl *indirectField, - DeclAccessPair foundDecl, - Expr *baseObjectExpr, - SourceLocation opLoc) { +ExprResult Sema::BuildAnonymousStructUnionMemberReference( + const CXXScopeSpec &SS, SourceLocation loc, + IndirectFieldDecl *indirectField, DeclAccessPair foundDecl, + Expr *baseObjectExpr, const Type *BaseType, SourceLocation opLoc) { // First, build the expression that refers to the base object. // Case 1: the base of the indirect field is not a field. VarDecl *baseVariable = indirectField->getVarDecl(); - CXXScopeSpec EmptySS; if (baseVariable) { assert(baseVariable->getType()->isRecordType()); @@ -822,6 +818,7 @@ DeclarationNameInfo baseNameInfo(DeclarationName(), loc); + CXXScopeSpec EmptySS; ExprResult result = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); if (result.isInvalid()) return ExprError(); @@ -839,6 +836,8 @@ IndirectFieldDecl::chain_iterator FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + // Case 2: the base of the indirect field is a field and the user // wrote a member expression. if (!baseVariable) { @@ -849,11 +848,15 @@ // Make a nameInfo that properly uses the anonymous name. DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); + // FIXME: Avoid redundant setting of the naming scope with the loop below. + QualType FieldType = + BaseType ? resugar(BaseType, NNS, field->getType()) : field->getType(); + // Build the first member access in the chain with full information. - result = - BuildFieldReferenceExpr(result, baseObjectIsPointer, SourceLocation(), - SS, field, foundDecl, memberNameInfo) - .get(); + result = BuildFieldReferenceExpr(result, baseObjectIsPointer, + SourceLocation(), NNS, field, FieldType, + foundDecl, memberNameInfo) + .get(); if (!result) return ExprError(); } @@ -869,10 +872,14 @@ DeclAccessPair fakeFoundDecl = DeclAccessPair::make(field, field->getAccess()); + QualType FieldType = BaseType && FI == FEnd + ? resugar(BaseType, NNS, field->getType()) + : field->getType(); + result = BuildFieldReferenceExpr(result, /*isarrow*/ false, SourceLocation(), - (FI == FEnd ? SS : EmptySS), field, - fakeFoundDecl, memberNameInfo) + (FI == FEnd ? NNS : NestedNameSpecifierLoc()), + field, FieldType, fakeFoundDecl, memberNameInfo) .get(); } @@ -897,12 +904,13 @@ SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl, bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo, QualType Ty, ExprValueKind VK, ExprObjectKind OK, - const TemplateArgumentListInfo *TemplateArgs) { + const TemplateArgumentListInfo *TemplateArgs, + const TemplateArgumentList *Deduced) { NestedNameSpecifierLoc NNS = SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(); return BuildMemberExpr(Base, IsArrow, OpLoc, NNS, TemplateKWLoc, Member, FoundDecl, HadMultipleCandidates, MemberNameInfo, Ty, - VK, OK, TemplateArgs); + VK, OK, TemplateArgs, Deduced); } MemberExpr *Sema::BuildMemberExpr( @@ -910,13 +918,14 @@ SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl, bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo, QualType Ty, ExprValueKind VK, ExprObjectKind OK, - const TemplateArgumentListInfo *TemplateArgs) { + const TemplateArgumentListInfo *TemplateArgs, + const TemplateArgumentList *Deduced) { assert((!IsArrow || Base->isPRValue()) && "-> base must be a pointer prvalue"); - MemberExpr *E = - MemberExpr::Create(Context, Base, IsArrow, OpLoc, NNS, TemplateKWLoc, - Member, FoundDecl, MemberNameInfo, TemplateArgs, Ty, - VK, OK, getNonOdrUseReasonInCurrentContext(Member)); + MemberExpr *E = MemberExpr::Create( + Context, Base, IsArrow, OpLoc, NNS, TemplateKWLoc, Member, FoundDecl, + MemberNameInfo, TemplateArgs, Deduced, Ty, VK, OK, + getNonOdrUseReasonInCurrentContext(Member)); E->setHadMultipleCandidates(HadMultipleCandidates); MarkMemberReferenced(E); @@ -1094,29 +1103,41 @@ if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) return ExprError(); - if (FieldDecl *FD = dyn_cast(MemberDecl)) - return BuildFieldReferenceExpr(BaseExpr, IsArrow, OpLoc, SS, FD, FoundDecl, - MemberNameInfo); + if (FieldDecl *FD = dyn_cast(MemberDecl)) { + assert(!TemplateArgs); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + QualType FieldType = resugar(BaseType.getTypePtr(), NNS, FD->getType()); + // null + // in case of implicit access. + return BuildFieldReferenceExpr(BaseExpr, IsArrow, OpLoc, NNS, FD, FieldType, + FoundDecl, MemberNameInfo); + } if (MSPropertyDecl *PD = dyn_cast(MemberDecl)) + // FIXME: resugar these. return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD, MemberNameInfo); if (IndirectFieldDecl *FD = dyn_cast(MemberDecl)) // We may have found a field within an anonymous union or struct // (C++ [class.union]). - return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, - FoundDecl, BaseExpr, - OpLoc); + return BuildAnonymousStructUnionMemberReference( + SS, MemberLoc, FD, FoundDecl, BaseExpr, BaseType.getTypePtr(), OpLoc); if (VarDecl *Var = dyn_cast(MemberDecl)) { - return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var, + assert(!TemplateArgs); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + QualType VarType = resugar(BaseType.getTypePtr(), NNS, Var->getType()); + return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, NNS, TemplateKWLoc, Var, FoundDecl, /*HadMultipleCandidates=*/false, - MemberNameInfo, Var->getType().getNonReferenceType(), + MemberNameInfo, VarType.getNonReferenceType(), VK_LValue, OK_Ordinary); } if (CXXMethodDecl *MemberFn = dyn_cast(MemberDecl)) { + assert(!TemplateArgs); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + ExprValueKind valueKind; QualType type; if (MemberFn->isInstance()) { @@ -1124,20 +1145,23 @@ type = Context.BoundMemberTy; } else { valueKind = VK_LValue; - type = MemberFn->getType(); + type = resugar(BaseType.getTypePtr(), NNS, MemberFn->getType()); } - return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, + return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, NNS, TemplateKWLoc, MemberFn, FoundDecl, /*HadMultipleCandidates=*/false, MemberNameInfo, type, valueKind, OK_Ordinary); } assert(!isa(MemberDecl) && "member function not C++ method?"); if (EnumConstantDecl *Enum = dyn_cast(MemberDecl)) { - return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Enum, + assert(!TemplateArgs); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + // FIXME: EnumType resugaring not implemented. + QualType EnumType = resugar(BaseType.getTypePtr(), NNS, Enum->getType()); + return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, NNS, TemplateKWLoc, Enum, FoundDecl, /*HadMultipleCandidates=*/false, - MemberNameInfo, Enum->getType(), VK_PRValue, - OK_Ordinary); + MemberNameInfo, EnumType, VK_PRValue, OK_Ordinary); } if (VarTemplateDecl *VarTempl = dyn_cast(MemberDecl)) { @@ -1161,10 +1185,13 @@ if (!Var->getTemplateSpecializationKind()) Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, MemberLoc); + NestedNameSpecifierLoc NNS = SS.getWithLocInContext(Context); + QualType VarType = resugar(BaseType.getTypePtr(), NNS, Var, + TemplateArgs->arguments(), Var->getType()); return BuildMemberExpr( - BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var, FoundDecl, + BaseExpr, IsArrow, OpLoc, NNS, TemplateKWLoc, Var, FoundDecl, /*HadMultipleCandidates=*/false, MemberNameInfo, - Var->getType().getNonReferenceType(), VK_LValue, OK_Ordinary); + VarType.getNonReferenceType(), VK_LValue, OK_Ordinary); } // We found something that we didn't expect. Complain. @@ -1796,11 +1823,10 @@ } } -ExprResult -Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow, - SourceLocation OpLoc, const CXXScopeSpec &SS, - FieldDecl *Field, DeclAccessPair FoundDecl, - const DeclarationNameInfo &MemberNameInfo) { +ExprResult Sema::BuildFieldReferenceExpr( + Expr *BaseExpr, bool IsArrow, SourceLocation OpLoc, + const NestedNameSpecifierLoc &NNS, FieldDecl *Field, QualType FieldType, + DeclAccessPair FoundDecl, const DeclarationNameInfo &MemberNameInfo) { // x.a is an l-value if 'a' has a reference type. Otherwise: // x.a is an l-value/x-value/pr-value if the base is (and note // that *x is always an l-value), except that if the base isn't @@ -1817,9 +1843,8 @@ OK = OK_BitField; // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] - QualType MemberType = Field->getType(); - if (const ReferenceType *Ref = MemberType->getAs()) { - MemberType = Ref->getPointeeType(); + if (const ReferenceType *Ref = FieldType->getAs()) { + FieldType = Ref->getPointeeType(); VK = VK_LValue; } else { QualType BaseType = BaseExpr->getType(); @@ -1835,29 +1860,29 @@ if (Field->isMutable()) BaseQuals.removeConst(); Qualifiers MemberQuals = - Context.getCanonicalType(MemberType).getQualifiers(); + Context.getCanonicalType(FieldType).getQualifiers(); assert(!MemberQuals.hasAddressSpace()); Qualifiers Combined = BaseQuals + MemberQuals; if (Combined != MemberQuals) - MemberType = Context.getQualifiedType(MemberType, Combined); + FieldType = Context.getQualifiedType(FieldType, Combined); // Pick up NoDeref from the base in case we end up using AddrOf on the // result. E.g. the expression // &someNoDerefPtr->pointerMember // should be a noderef pointer again. if (BaseType->hasAttr(attr::NoDeref)) - MemberType = - Context.getAttributedType(attr::NoDeref, MemberType, MemberType); + FieldType = + Context.getAttributedType(attr::NoDeref, FieldType, FieldType); } auto *CurMethod = dyn_cast(CurContext); if (!(CurMethod && CurMethod->isDefaulted())) UnusedPrivateFields.remove(Field); - ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), - FoundDecl, Field); + ExprResult Base = PerformObjectMemberConversion( + BaseExpr, NNS.getNestedNameSpecifier(), FoundDecl, Field); if (Base.isInvalid()) return ExprError(); @@ -1872,10 +1897,10 @@ } } - return BuildMemberExpr(Base.get(), IsArrow, OpLoc, &SS, + return BuildMemberExpr(Base.get(), IsArrow, OpLoc, NNS, /*TemplateKWLoc=*/SourceLocation(), Field, FoundDecl, /*HadMultipleCandidates=*/false, MemberNameInfo, - MemberType, VK, OK); + FieldType, VK, OK); } /// Builds an implicit member access expression. The current context diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -8261,9 +8261,9 @@ S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) return ExprError(); - CurInit = S.FixOverloadedFunctionReference(CurInit, - Step->Function.FoundDecl, - Step->Function.Function); + // FIXME: resugar + CurInit = S.FixOverloadedFunctionReference( + CurInit, Step->Function.FoundDecl, Step->Function.Function, nullptr); // We might get back another placeholder expression if we resolved to a // builtin. if (!CurInit.isInvalid()) @@ -10289,12 +10289,13 @@ MarkFunctionReferenced(Kind.getLocation(), Best->Function); break; } + QualType RT = Best->Function->getReturnType(); + // FIXME: resugar here // C++ [dcl.type.class.deduct]p1: // The placeholder is replaced by the return type of the function selected // by overload resolution for class template deduction. - QualType DeducedType = - SubstAutoType(TSInfo->getType(), Best->Function->getReturnType()); + QualType DeducedType = SubstAutoType(TSInfo->getType(), RT); Diag(TSInfo->getTypeLoc().getBeginLoc(), diag::warn_cxx14_compat_class_template_argument_deduction) << TSInfo->getTypeLoc().getSourceRange() << 1 << DeducedType; diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -5691,9 +5691,6 @@ Sema::CCEKind CCE, bool RequireInt, NamedDecl *Dest) { - assert(S.getLangOpts().CPlusPlus11 && - "converted constant expression outside C++11"); - if (checkPlaceholderForOverload(S, From)) return ExprError(); @@ -6332,7 +6329,7 @@ OverloadCandidateSet &CandidateSet, bool SuppressUserConversions, bool PartialOverloading, bool AllowExplicit, bool AllowExplicitConversions, ADLCallKind IsADLCandidate, ConversionSequenceList EarlyConversions, - OverloadCandidateParamOrder PO) { + OverloadCandidateParamOrder PO, const TemplateArgumentList *Deduced) { const FunctionProtoType *Proto = dyn_cast(Function->getType()->getAs()); assert(Proto && "Functions without a prototype cannot be overloaded"); @@ -6351,7 +6348,7 @@ AddMethodCandidate(Method, FoundDecl, Method->getParent(), QualType(), Expr::Classification::makeSimpleLValue(), Args, CandidateSet, SuppressUserConversions, - PartialOverloading, EarlyConversions, PO); + PartialOverloading, EarlyConversions, PO, Deduced); return; } // We treat a constructor like a non-member function, since its object @@ -6396,6 +6393,7 @@ Candidate.IsADLCandidate = IsADLCandidate; Candidate.IgnoreObjectArgument = false; Candidate.ExplicitCallArguments = Args.size(); + Candidate.Deduced = Deduced; // Explicit functions are not actually candidates at all if we're not // allowing them in this context, but keep them around so we can point @@ -6930,16 +6928,13 @@ /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't /// allow user-defined conversions via constructors or conversion /// operators. -void -Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl, - CXXRecordDecl *ActingContext, QualType ObjectType, - Expr::Classification ObjectClassification, - ArrayRef Args, - OverloadCandidateSet &CandidateSet, - bool SuppressUserConversions, - bool PartialOverloading, - ConversionSequenceList EarlyConversions, - OverloadCandidateParamOrder PO) { +void Sema::AddMethodCandidate( + CXXMethodDecl *Method, DeclAccessPair FoundDecl, + CXXRecordDecl *ActingContext, QualType ObjectType, + Expr::Classification ObjectClassification, ArrayRef Args, + OverloadCandidateSet &CandidateSet, bool SuppressUserConversions, + bool PartialOverloading, ConversionSequenceList EarlyConversions, + OverloadCandidateParamOrder PO, const TemplateArgumentList *Deduced) { const FunctionProtoType *Proto = dyn_cast(Method->getType()->getAs()); assert(Proto && "Methods without a prototype cannot be overloaded"); @@ -6970,6 +6965,7 @@ Candidate.IsSurrogate = false; Candidate.IgnoreObjectArgument = false; Candidate.ExplicitCallArguments = Args.size(); + Candidate.Deduced = Deduced; unsigned NumParams = Proto->getNumParams(); @@ -7146,7 +7142,7 @@ AddMethodCandidate(cast(Specialization), FoundDecl, ActingContext, ObjectType, ObjectClassification, Args, CandidateSet, SuppressUserConversions, PartialOverloading, - Conversions, PO); + Conversions, PO, Info.takeSugared()); } /// Determine whether a given function template has a simple explicit specifier @@ -7226,10 +7222,11 @@ // Add the function template specialization produced by template argument // deduction as a candidate. assert(Specialization && "Missing function template specialization?"); - AddOverloadCandidate( - Specialization, FoundDecl, Args, CandidateSet, SuppressUserConversions, - PartialOverloading, AllowExplicit, - /*AllowExplicitConversions*/ false, IsADLCandidate, Conversions, PO); + AddOverloadCandidate(Specialization, FoundDecl, Args, CandidateSet, + SuppressUserConversions, PartialOverloading, + AllowExplicit, + /*AllowExplicitConversions*/ false, IsADLCandidate, + Conversions, PO, Info.takeSugared()); } /// Check that implicit conversion sequences can be formed for each argument @@ -12075,15 +12072,20 @@ FailedCandidates(OvlExpr->getNameLoc(), /*ForTakingAddress=*/true) { ExtractUnqualifiedFunctionTypeFromTargetType(); + if (OvlExpr->hasExplicitTemplateArgs()) + OvlExpr->copyTemplateArgumentsInto(OvlExplicitTemplateArgs); + if (TargetFunctionType->isFunctionType()) { if (UnresolvedMemberExpr *UME = dyn_cast(OvlExpr)) if (!UME->isImplicitAccess() && - !S.ResolveSingleFunctionTemplateSpecialization(UME)) + (!OvlExpr->hasExplicitTemplateArgs() || + !S.ResolveSingleFunctionTemplateSpecialization( + UME, OvlExplicitTemplateArgs))) StaticMemberFunctionFromBoundPointer = true; } else if (OvlExpr->hasExplicitTemplateArgs()) { DeclAccessPair dap; if (FunctionDecl *Fn = S.ResolveSingleFunctionTemplateSpecialization( - OvlExpr, false, &dap)) { + OvlExpr, OvlExplicitTemplateArgs, /*Complain=*/false, &dap)) { if (CXXMethodDecl *Method = dyn_cast(Fn)) if (!Method->isStatic()) { // If the target type is a non-function type and the function found @@ -12102,9 +12104,6 @@ return; } - if (OvlExpr->hasExplicitTemplateArgs()) - OvlExpr->copyTemplateArgumentsInto(OvlExplicitTemplateArgs); - if (FindAllFunctionsThatMatchTargetTypeExactly()) { // C++ [over.over]p4: // If more than one function is selected, [...] @@ -12590,7 +12589,8 @@ // for both. DiagnoseUseOfDecl(Found, E->getExprLoc()); CheckAddressOfMemberAccess(E, DAP); - Expr *Fixed = FixOverloadedFunctionReference(E, DAP, Found); + // FIXME: resugar + Expr *Fixed = FixOverloadedFunctionReference(E, DAP, Found, nullptr); if (DoFunctionPointerConversion && Fixed->getType()->isFunctionType()) SrcExpr = DefaultFunctionArrayConversion(Fixed, /*Diagnose=*/false); else @@ -12608,10 +12608,9 @@ /// /// If no template-ids are found, no diagnostics are emitted and NULL is /// returned. -FunctionDecl * -Sema::ResolveSingleFunctionTemplateSpecialization(OverloadExpr *ovl, - bool Complain, - DeclAccessPair *FoundResult) { +FunctionDecl *Sema::ResolveSingleFunctionTemplateSpecialization( + OverloadExpr *ovl, TemplateArgumentListInfo &ExplicitTemplateArgs, + bool Complain, DeclAccessPair *FoundResult) { // C++ [over.over]p1: // [...] [Note: any redundant set of parentheses surrounding the // overloaded function name is ignored (5.1). ] @@ -12619,12 +12618,9 @@ // [...] The overloaded function name can be preceded by the & // operator. - // If we didn't actually find any template-ids, we're done. - if (!ovl->hasExplicitTemplateArgs()) - return nullptr; + // Specializations must have template args. + assert(ovl->hasExplicitTemplateArgs()); - TemplateArgumentListInfo ExplicitTemplateArgs; - ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); TemplateSpecCandidateSet FailedCandidates(ovl->getNameLoc()); // Look through all of the overloaded functions, searching for one @@ -12699,50 +12695,54 @@ assert(SrcExpr.get()->getType() == Context.OverloadTy); OverloadExpr::FindResult ovl = OverloadExpr::find(SrcExpr.get()); - - DeclAccessPair found; ExprResult SingleFunctionExpression; - if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( - ovl.Expression, /*complain*/ false, &found)) { - if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getBeginLoc())) { - SrcExpr = ExprError(); - return true; - } + if (ovl.Expression->hasExplicitTemplateArgs()) { + TemplateArgumentListInfo ExplicitTemplateArgs; + ovl.Expression->copyTemplateArgumentsInto(ExplicitTemplateArgs); + + DeclAccessPair found; + if (FunctionDecl *fn = ResolveSingleFunctionTemplateSpecialization( + ovl.Expression, ExplicitTemplateArgs, /*Complain=*/false, &found)) { + if (DiagnoseUseOfDecl(fn, SrcExpr.get()->getBeginLoc())) { + SrcExpr = ExprError(); + return true; + } - // It is only correct to resolve to an instance method if we're - // resolving a form that's permitted to be a pointer to member. - // Otherwise we'll end up making a bound member expression, which - // is illegal in all the contexts we resolve like this. - if (!ovl.HasFormOfMemberPointer && - isa(fn) && - cast(fn)->isInstance()) { - if (!complain) return false; - - Diag(ovl.Expression->getExprLoc(), - diag::err_bound_member_function) - << 0 << ovl.Expression->getSourceRange(); - - // TODO: I believe we only end up here if there's a mix of - // static and non-static candidates (otherwise the expression - // would have 'bound member' type, not 'overload' type). - // Ideally we would note which candidate was chosen and why - // the static candidates were rejected. - SrcExpr = ExprError(); - return true; - } + // It is only correct to resolve to an instance method if we're + // resolving a form that's permitted to be a pointer to member. + // Otherwise we'll end up making a bound member expression, which + // is illegal in all the contexts we resolve like this. + if (!ovl.HasFormOfMemberPointer && isa(fn) && + cast(fn)->isInstance()) { + if (!complain) + return false; - // Fix the expression to refer to 'fn'. - SingleFunctionExpression = - FixOverloadedFunctionReference(SrcExpr.get(), found, fn); + Diag(ovl.Expression->getExprLoc(), diag::err_bound_member_function) + << 0 << ovl.Expression->getSourceRange(); - // If desired, do function-to-pointer decay. - if (doFunctionPointerConversion) { - SingleFunctionExpression = - DefaultFunctionArrayLvalueConversion(SingleFunctionExpression.get()); - if (SingleFunctionExpression.isInvalid()) { + // TODO: I believe we only end up here if there's a mix of + // static and non-static candidates (otherwise the expression + // would have 'bound member' type, not 'overload' type). + // Ideally we would note which candidate was chosen and why + // the static candidates were rejected. SrcExpr = ExprError(); return true; } + + // Fix the expression to refer to 'fn'. + // FIXME: resugar + SingleFunctionExpression = + FixOverloadedFunctionReference(SrcExpr.get(), found, fn, nullptr); + + // If desired, do function-to-pointer decay. + if (doFunctionPointerConversion) { + SingleFunctionExpression = DefaultFunctionArrayLvalueConversion( + SingleFunctionExpression.get()); + if (SingleFunctionExpression.isInvalid()) { + SrcExpr = ExprError(); + return true; + } + } } } @@ -13255,7 +13255,8 @@ SemaRef.CheckUnresolvedLookupAccess(ULE, (*Best)->FoundDecl); if (SemaRef.DiagnoseUseOfDecl(FDecl, ULE->getNameLoc())) return ExprError(); - Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); + Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl, + (*Best)->Deduced); return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, ExecConfig, /*IsExecConfig=*/false, (*Best)->IsADLCandidate); @@ -13313,7 +13314,8 @@ // We emitted an error for the unavailable/deleted function call but keep // the call in the AST. FunctionDecl *FDecl = (*Best)->Function; - Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl); + Fn = SemaRef.FixOverloadedFunctionReference(Fn, (*Best)->FoundDecl, FDecl, + (*Best)->Deduced); return SemaRef.BuildResolvedCallExpr(Fn, FDecl, LParenLoc, Args, RParenLoc, ExecConfig, /*IsExecConfig=*/false, (*Best)->IsADLCandidate); @@ -14581,7 +14583,8 @@ if (!Succeeded) return BuildRecoveryExpr(chooseRecoveryType(CandidateSet, &Best)); - MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method); + MemExprE = FixOverloadedFunctionReference(MemExprE, FoundDecl, Method, + Best->Deduced); // If overload resolution picked a static member, build a // non-member call based on that function. @@ -14592,20 +14595,34 @@ MemExpr = cast(MemExprE->IgnoreParens()); } + assert(Method && "Member call to something that isn't a method?"); - QualType ResultType = Method->getReturnType(); - ExprValueKind VK = Expr::getValueKindForType(ResultType); - ResultType = ResultType.getNonLValueExprType(Context); + QualType MethodType; + { + QualType BaseType = MemExpr->getBase()->getType(); + if (MemExpr->isArrow()) + BaseType = BaseType->castAs()->getPointeeType(); + NestedNameSpecifierLoc NNS = MemExpr->getQualifierLoc(); + // FIXME: Should we resugar the explicit template arguments as well? + const TemplateArgumentList *Deduced = MemExpr->getDeduced(); + MethodType = Deduced + ? resugar(BaseType.getTypePtr(), NNS, Method, + Deduced->asArray(), Method->getType()) + : resugar(BaseType.getTypePtr(), NNS, Method->getType()); + } + + const auto *Proto = MethodType->castAs(); + QualType ReturnType = Proto->getReturnType(); + + ExprValueKind VK = Expr::getValueKindForType(ReturnType); + QualType ResultType = ReturnType.getNonLValueExprType(Context); - assert(Method && "Member call to something that isn't a method?"); - const auto *Proto = Method->getType()->castAs(); CXXMemberCallExpr *TheCall = CXXMemberCallExpr::Create( Context, MemExprE, Args, ResultType, VK, RParenLoc, CurFPFeatureOverrides(), Proto->getNumParams()); // Check for a valid return type. - if (CheckCallReturnType(Method->getReturnType(), MemExpr->getMemberLoc(), - TheCall, Method)) + if (CheckCallReturnType(ReturnType, MemExpr->getMemberLoc(), TheCall, Method)) return BuildRecoveryExpr(ResultType); // Convert the object argument (for a non-static member function call). @@ -15196,11 +15213,13 @@ /// perhaps a '&' around it). We have resolved the overloaded function /// to the function declaration Fn, so patch up the expression E to /// refer (possibly indirectly) to Fn. Returns the new expr. -Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, - FunctionDecl *Fn) { +Expr * +Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, + FunctionDecl *Fn, + const TemplateArgumentList *Deduced) { if (ParenExpr *PE = dyn_cast(E)) { - Expr *SubExpr = FixOverloadedFunctionReference(PE->getSubExpr(), - Found, Fn); + Expr *SubExpr = + FixOverloadedFunctionReference(PE->getSubExpr(), Found, Fn, Deduced); if (SubExpr == PE->getSubExpr()) return PE; @@ -15208,8 +15227,8 @@ } if (ImplicitCastExpr *ICE = dyn_cast(E)) { - Expr *SubExpr = FixOverloadedFunctionReference(ICE->getSubExpr(), - Found, Fn); + Expr *SubExpr = + FixOverloadedFunctionReference(ICE->getSubExpr(), Found, Fn, Deduced); assert(Context.hasSameType(ICE->getSubExpr()->getType(), SubExpr->getType()) && "Implicit cast type cannot be determined from overload"); @@ -15224,8 +15243,8 @@ if (auto *GSE = dyn_cast(E)) { if (!GSE->isResultDependent()) { - Expr *SubExpr = - FixOverloadedFunctionReference(GSE->getResultExpr(), Found, Fn); + Expr *SubExpr = FixOverloadedFunctionReference(GSE->getResultExpr(), + Found, Fn, Deduced); if (SubExpr == GSE->getResultExpr()) return GSE; @@ -15259,7 +15278,7 @@ // UnresolvedLookupExpr holding an overloaded member function // or template. Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), - Found, Fn); + Found, Fn, Deduced); if (SubExpr == UnOp->getSubExpr()) return UnOp; @@ -15271,10 +15290,14 @@ // We have taken the address of a pointer to member // function. Perform the computation here so that we get the // appropriate pointer to member type. + // FIXME: get sugared class type QualType ClassType = Context.getTypeDeclType(cast(Method->getDeclContext())); - QualType MemPtrType - = Context.getMemberPointerType(Fn->getType(), ClassType.getTypePtr()); + QualType Type = Deduced ? resugar(CXXScopeSpec(), Fn, + Deduced->asArray(), Fn->getType()) + : resugar(CXXScopeSpec(), Fn->getType()); + QualType MemPtrType = + Context.getMemberPointerType(Type, ClassType.getTypePtr()); // Under the MS ABI, lock down the inheritance model now. if (Context.getTargetInfo().getCXXABI().isMicrosoft()) (void)isCompleteType(UnOp->getOperatorLoc(), MemPtrType); @@ -15284,8 +15307,8 @@ UnOp->getOperatorLoc(), false, CurFPFeatureOverrides()); } } - Expr *SubExpr = FixOverloadedFunctionReference(UnOp->getSubExpr(), - Found, Fn); + Expr *SubExpr = + FixOverloadedFunctionReference(UnOp->getSubExpr(), Found, Fn, Deduced); if (SubExpr == UnOp->getSubExpr()) return UnOp; @@ -15301,21 +15324,28 @@ ULE->copyTemplateArgumentsInto(TemplateArgsBuffer); TemplateArgs = &TemplateArgsBuffer; } + NestedNameSpecifierLoc NNS = ULE->getQualifierLoc(); - QualType Type = Fn->getType(); ExprValueKind ValueKind = getLangOpts().CPlusPlus ? VK_LValue : VK_PRValue; // FIXME: Duplicated from BuildDeclarationNameExpr. - if (unsigned BID = Fn->getBuiltinID()) { - if (!Context.BuiltinInfo.isDirectlyAddressable(BID)) { + QualType Type; + { + unsigned BID = Fn->getBuiltinID(); + if (BID && !Context.BuiltinInfo.isDirectlyAddressable(BID)) { Type = Context.BuiltinFnTy; ValueKind = VK_PRValue; + } else { + CXXScopeSpec SS; + SS.Adopt(NNS); + Type = Deduced ? resugar(SS, Fn, Deduced->asArray(), Fn->getType()) + : resugar(SS, Fn->getType()); } } DeclRefExpr *DRE = BuildDeclRefExpr( - Fn, Type, ValueKind, ULE->getNameInfo(), ULE->getQualifierLoc(), - Found.getDecl(), ULE->getTemplateKeywordLoc(), TemplateArgs); + Fn, Type, ValueKind, ULE->getNameInfo(), NNS, Found.getDecl(), + ULE->getTemplateKeywordLoc(), TemplateArgs); DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); return DRE; } @@ -15327,6 +15357,10 @@ MemExpr->copyTemplateArgumentsInto(TemplateArgsBuffer); TemplateArgs = &TemplateArgsBuffer; } + QualType BaseType = MemExpr->getBaseType(); + const Type *BasePointeeType = BaseType->getPointeeType().getTypePtrOrNull(); + if (!BasePointeeType) + BasePointeeType = BaseType.getTypePtr(); Expr *Base; @@ -15334,46 +15368,51 @@ // implicit member access, rewrite to a simple decl ref. if (MemExpr->isImplicitAccess()) { if (cast(Fn)->isStatic()) { - DeclRefExpr *DRE = BuildDeclRefExpr( - Fn, Fn->getType(), VK_LValue, MemExpr->getNameInfo(), - MemExpr->getQualifierLoc(), Found.getDecl(), - MemExpr->getTemplateKeywordLoc(), TemplateArgs); + QualType Type = Deduced ? resugar(BasePointeeType, Fn, + Deduced->asArray(), Fn->getType()) + : resugar(BasePointeeType, Fn->getType()); + DeclRefExpr *DRE = + BuildDeclRefExpr(Fn, Type, VK_LValue, MemExpr->getNameInfo(), + MemExpr->getQualifierLoc(), Found.getDecl(), + MemExpr->getTemplateKeywordLoc(), TemplateArgs); DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); return DRE; } else { SourceLocation Loc = MemExpr->getMemberLoc(); if (MemExpr->getQualifier()) Loc = MemExpr->getQualifierLoc().getBeginLoc(); - Base = - BuildCXXThisExpr(Loc, MemExpr->getBaseType(), /*IsImplicit=*/true); + Base = BuildCXXThisExpr(Loc, BaseType, /*IsImplicit=*/true); } } else Base = MemExpr->getBase(); ExprValueKind valueKind; - QualType type; + QualType Type; if (cast(Fn)->isStatic()) { valueKind = VK_LValue; - type = Fn->getType(); + Type = Deduced ? resugar(BasePointeeType, Fn, Deduced->asArray(), + Fn->getType()) + : resugar(BasePointeeType, Fn->getType()); } else { valueKind = VK_PRValue; - type = Context.BoundMemberTy; + Type = Context.BoundMemberTy; } return BuildMemberExpr( Base, MemExpr->isArrow(), MemExpr->getOperatorLoc(), MemExpr->getQualifierLoc(), MemExpr->getTemplateKeywordLoc(), Fn, Found, - /*HadMultipleCandidates=*/true, MemExpr->getMemberNameInfo(), - type, valueKind, OK_Ordinary, TemplateArgs); + /*HadMultipleCandidates=*/true, MemExpr->getMemberNameInfo(), Type, + valueKind, OK_Ordinary, TemplateArgs, Deduced); } llvm_unreachable("Invalid reference to overloaded function"); } -ExprResult Sema::FixOverloadedFunctionReference(ExprResult E, - DeclAccessPair Found, - FunctionDecl *Fn) { - return FixOverloadedFunctionReference(E.get(), Found, Fn); +ExprResult +Sema::FixOverloadedFunctionReference(ExprResult E, DeclAccessPair Found, + FunctionDecl *Fn, + const TemplateArgumentList *Deduced) { + return FixOverloadedFunctionReference(E.get(), Found, Fn, Deduced); } bool clang::shouldEnforceArgLimit(bool PartialOverloading, diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -11,6 +11,7 @@ #include "TreeTransform.h" #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ASTLambda.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclFriend.h" #include "clang/AST/DeclTemplate.h" @@ -91,6 +92,1270 @@ return Depth; } +TemplateDecl *getTemplateDecl(NamedDecl *D) { + switch (D->getKind()) { + case Decl::Kind::Var: + case Decl::Kind::ParmVar: + case Decl::Kind::Field: + case Decl::Kind::IndirectField: + case Decl::Kind::EnumConstant: + case Decl::Kind::Binding: + case Decl::Kind::ImplicitParam: + case Decl::Kind::MSGuid: + case Decl::Kind::MSProperty: + case Decl::Kind::NonTypeTemplateParm: + case Decl::Kind::TemplateParamObject: + return nullptr; + case Decl::Kind::FunctionTemplate: + case Decl::Kind::ClassTemplate: + case Decl::Kind::TypeAliasTemplate: + case Decl::Kind::BuiltinTemplate: + return cast(D); + case Decl::Kind::VarTemplateSpecialization: + return cast(D)->getSpecializedTemplate(); + case Decl::Kind::CXXDeductionGuide: + return cast(D)->getDeducedTemplate(); + case Decl::Kind::CXXConversion: + case Decl::Kind::CXXConstructor: + case Decl::Kind::CXXDestructor: + case Decl::Kind::CXXMethod: + case Decl::Kind::Function: { + const auto *FD = cast(D); + switch (FD->getTemplatedKind()) { + case FunctionDecl::TK_NonTemplate: + case FunctionDecl::TK_DependentNonTemplate: + return nullptr; + case FunctionDecl::TK_FunctionTemplate: + return FD->getDescribedFunctionTemplate(); + case FunctionDecl::TK_MemberSpecialization: + return nullptr; + case FunctionDecl::TK_FunctionTemplateSpecialization: + return FD->getPrimaryTemplate(); + case FunctionDecl::TK_DependentFunctionTemplateSpecialization: + return nullptr; + } + llvm_unreachable("Unhandled function template kind"); + } + case Decl::Kind::CXXRecord: + return cast(D)->getDescribedClassTemplate(); + case Decl::Kind::TemplateTemplateParm: + return cast(D); + default: + // Maybe fall back to Decl::getDescribedTemplate. + D->dumpColor(); + llvm_unreachable("Unhandled decl kind"); + } +} + +namespace { +class Resugarer : public TreeTransform { + using inherited = TreeTransform; + + struct Inner { + const Decl *ReplacedDecl; + ArrayRef Args; + }; + Inner CurInner; + + using TemplateToArgs = + llvm::DenseMap>; + TemplateToArgs CurTemplateToArgs; + + SmallVector, 4> ArgsBuf; + + const TemplateArgument *getArgument(const Decl *ReplacedDecl, unsigned Index, + Optional PackIndex) const { + ArrayRef Args; + if (!CurInner.Args.empty() && CurInner.ReplacedDecl == ReplacedDecl) { + Args = CurInner.Args; + } else { + auto It = CurTemplateToArgs.find(ReplacedDecl); + if (It == CurTemplateToArgs.end()) + return nullptr; + Args = It->second; + } + // FIXME: This should never happen, but some arguments might be missing + // from argument deduction of function templates + if (Index >= Args.size()) + return nullptr; + const TemplateArgument &Arg = Args[Index]; + if (!PackIndex) + return &Arg; + ArrayRef PackArgs = Arg.getPackAsArray(); + // FIXME: As above, arguments might be missing. + if (*PackIndex >= PackArgs.size()) + return nullptr; + return &PackArgs[PackArgs.size() - 1 - *PackIndex]; + } + + struct BaseSemanticContextRAII { + BaseSemanticContextRAII(Resugarer &R) + : R(&R), OldInner(std::move(R.CurInner)) {} + ~BaseSemanticContextRAII() { R->CurInner = std::move(OldInner); } + + protected: + Resugarer *R; + + private: + Inner OldInner; + }; + +public: + struct NonTemplateSemanticContextRAII { + NonTemplateSemanticContextRAII(Resugarer &R) + : R(&R), OldInner(std::move(R.CurInner)) {} + ~NonTemplateSemanticContextRAII() { R->CurInner = std::move(OldInner); } + + protected: + Resugarer *R; + + private: + Inner OldInner; + }; + + struct SemanticContextRAII : private NonTemplateSemanticContextRAII { + SemanticContextRAII(Resugarer &R, NamedDecl *ND, + ArrayRef Args) + : NonTemplateSemanticContextRAII(R), OldArgsBufSize(R.ArgsBuf.size()) { + assert(ND != nullptr); + assert(Args.size() != 0); + TemplateDecl *TD = getTemplateDecl(ND); + assert(TD != nullptr); + R.CurInner = {TD->getCanonicalDecl(), Args}; + } + + SemanticContextRAII(Resugarer &R, NamedDecl *ND, + ArrayRef Args) + : NonTemplateSemanticContextRAII(R), OldArgsBufSize(R.ArgsBuf.size()) { + assert(ND != nullptr); + if (Args.size() == 0) + return; + TemplateDecl *TD = getTemplateDecl(ND); + assert(TD != nullptr); + R.CurInner.ReplacedDecl = TD->getCanonicalDecl(); + + Sema::SFINAETrap Trap(R.SemaRef); + + Qualifiers ThisTypeQuals; + CXXRecordDecl *ThisContext = nullptr; + if (auto *Method = dyn_cast(ND)) { + ThisContext = Method->getParent(); + ThisTypeQuals = Method->getMethodQualifiers(); + } + Sema::CXXThisScopeRAII ThisScope(R.SemaRef, ThisContext, ThisTypeQuals, + R.SemaRef.getLangOpts().CPlusPlus17); + + TemplateArgumentListInfo TAL; + for (const auto &ArgLoc : Args) + TAL.addArgument(ArgLoc); + SmallVector ConvertedArgs, CanonicalConverted; + if (R.SemaRef.CheckTemplateArgumentList( + TD, SourceLocation(), TAL, false, ConvertedArgs, + CanonicalConverted, + /*UpdateArgsWithConversions=*/false)) { + // FIXME: This can fail sometimes because we only keep + // the as-written template arguments around for + // functions, and we don't have the information + // to try to deduce them again. + assert(TD->getKind() == Decl::Kind::FunctionTemplate); + return; + } + switch (ND->getKind()) { + case Decl::Kind::TypeAliasTemplate: + case Decl::Kind::BuiltinTemplate: + case Decl::Kind::CXXMethod: + case Decl::Kind::Function: + break; + case Decl::Kind::VarTemplateSpecialization: { + auto *VD = cast(ND); + auto *VTPSD = VD->getSpecializedTemplateOrPartial() + .dyn_cast(); + if (!VTPSD) + break; + TemplateParameterList *TPL = VTPSD->getTemplateParameters(); + TemplateDeductionInfo Info(SourceLocation{}, TPL->getDepth()); + // FIXME: We can't deal very well with non-canonical template + // arguments so this will fail sometimes. + Sema::TemplateDeductionResult Res = R.SemaRef.DeduceTemplateArguments( + VTPSD, + TemplateArgumentList(TemplateArgumentList::OnStackType{}, + ConvertedArgs), + Info); + if (Res != Sema::TDK_Success) + break; + R.CurInner.Args = Info.takeSugared()->asArray(); + return; + } + default: + ND->dump(); + llvm_unreachable("Unhandled Template Kind"); + } + R.CurInner.Args = R.ArgsBuf.emplace_back(std::move(ConvertedArgs)); + } + + ~SemanticContextRAII() { R->ArgsBuf.resize(OldArgsBufSize); } + + size_t OldArgsBufSize; + }; + + struct NamingContextBase { + NamingContextBase(Resugarer &R) + : R(&R), OldTemplateToArgs(std::move(R.CurTemplateToArgs)), + OldArgsBufSize(R.ArgsBuf.size()) {} + ~NamingContextBase() { + R->CurTemplateToArgs = std::move(OldTemplateToArgs); + R->ArgsBuf.resize(OldArgsBufSize); + } + + protected: + void insertTemplateToMap(bool Reverse, const Decl *Template, + ArrayRef Args) { + Template = Template->getCanonicalDecl(); + assert(!Args.empty()); + if (Reverse) + R->CurTemplateToArgs.try_emplace(Template, Args); + else + R->CurTemplateToArgs[Template] = Args; + } + + void addTypeToMap(bool Reverse, const Type *T) { + struct { + NamedDecl *ND; + const TemplateSpecializationType *TS; + } Entity; + { + switch (T->getCanonicalTypeInternal()->getTypeClass()) { + case Type::Record: { + const auto *TS = T->getAsNonAliasTemplateSpecializationType(); + if (!TS) + return; + Entity = {TS->getAsRecordDecl(), TS}; + break; + } + case Type::InjectedClassName: { + const auto *ICN = T->castAs(); + Entity = {ICN->getDecl(), ICN->getInjectedTST()}; + break; + } + case Type::TemplateSpecialization: + case Type::DependentTemplateSpecialization: + case Type::TemplateTypeParm: + case Type::Decltype: + case Type::DependentName: + case Type::Enum: + case Type::Builtin: + case Type::UnaryTransform: + return; + default: + T->dump(); + llvm_unreachable(""); + } + } + switch (Entity.ND->getKind()) { + case Decl::CXXRecord: + case Decl::Record: + return; + case Decl::ClassTemplateSpecialization: + case Decl::ClassTemplatePartialSpecialization: { + auto *CTSD = cast(Entity.ND); + if (auto *CTPSD = + CTSD->getSpecializedTemplateOrPartial() + .dyn_cast()) { + TemplateParameterList *TPL = CTPSD->getTemplateParameters(); + TemplateDeductionInfo Info(SourceLocation{}, TPL->getDepth()); + [[maybe_unused]] Sema::TemplateDeductionResult Res = + R->SemaRef.DeduceTemplateArguments( + CTPSD, + TemplateArgumentList(TemplateArgumentList::OnStackType{}, + Entity.TS->getConvertedArguments()), + Info); + // FIXME: Workaround for CE ASTNodeAPI.json generation failure. + // assert(Res == Sema::TDK_Success); + if (Res != Sema::TDK_Success) + return; + return insertTemplateToMap(Reverse, CTSD, + Info.takeSugared()->asArray()); + } + return insertTemplateToMap(Reverse, CTSD, + Entity.TS->getConvertedArguments()); + } + default: + break; + } + Entity.ND->dumpColor(); + llvm_unreachable("Unhandled Decl Kind"); + } + + Resugarer *R; + TemplateToArgs OldTemplateToArgs; + + private: + size_t OldArgsBufSize; + }; + + struct NamingContextRAII : NamingContextBase { + NamingContextRAII(Resugarer &R, const NestedNameSpecifier *NNS, + const Type *BaseType = nullptr) + : NamingContextBase(R) { + if (BaseType) { + assert(!BaseType->getAs()); + addTypeToMap(/*Reverse=*/false, BaseType); + } + for (/**/; NNS; NNS = NNS->getPrefix()) { + switch (NNS->getKind()) { + case NestedNameSpecifier::Global: + case NestedNameSpecifier::Namespace: + case NestedNameSpecifier::NamespaceAlias: + return; + case NestedNameSpecifier::Identifier: + case NestedNameSpecifier::Super: + continue; + case NestedNameSpecifier::TypeSpec: + case NestedNameSpecifier::TypeSpecWithTemplate: + addTypeToMap(/*Reverse=*/false, NNS->getAsType()); + continue; + } + llvm_unreachable("Unknown NestedNameSpecifier Kind"); + } + for (auto &&I : OldTemplateToArgs) { + auto &P = R.CurTemplateToArgs[I.first]; + if (P.empty()) + P = I.second; + } + } + }; + + struct NamingContextTransformRAII : NamingContextBase { + NamingContextTransformRAII(Resugarer &R, NestedNameSpecifierLoc &NNS) + : NamingContextBase(R) { + SmallVector Qs; + bool InheritOldMap = true; + for (/**/; NNS; NNS = NNS.getPrefix()) { + NestedNameSpecifier::SpecifierKind K = + NNS.getNestedNameSpecifier()->getKind(); + InheritOldMap &= K != NestedNameSpecifier::Global && + K != NestedNameSpecifier::Namespace && + K != NestedNameSpecifier::NamespaceAlias; + Qs.push_back(NNS); + } + if (InheritOldMap) + R.CurTemplateToArgs.copyFrom(OldTemplateToArgs); + + CXXScopeSpec SS; + for (const NestedNameSpecifierLoc &Q : llvm::reverse(Qs)) { + const auto *Qnns = Q.getNestedNameSpecifier(); + switch (Qnns->getKind()) { + case NestedNameSpecifier::Global: + SS.MakeGlobal(R.SemaRef.Context, Q.getBeginLoc()); + continue; + case NestedNameSpecifier::Namespace: + SS.Extend(R.SemaRef.Context, Qnns->getAsNamespace(), + Q.getLocalBeginLoc(), Q.getLocalEndLoc()); + continue; + case NestedNameSpecifier::NamespaceAlias: + SS.Extend(R.SemaRef.Context, Qnns->getAsNamespaceAlias(), + Q.getLocalBeginLoc(), Q.getLocalEndLoc()); + continue; + case NestedNameSpecifier::Identifier: + SS.Extend(R.SemaRef.Context, Qnns->getAsIdentifier(), + Q.getLocalBeginLoc(), Q.getLocalEndLoc()); + continue; + case NestedNameSpecifier::Super: + SS.MakeSuper(R.SemaRef.Context, Qnns->getAsRecordDecl(), + Q.getBeginLoc(), Q.getEndLoc()); + continue; + case NestedNameSpecifier::TypeSpec: + case NestedNameSpecifier::TypeSpecWithTemplate: { + TypeLocBuilder TLB; + TypeLoc TL = Q.getTypeLoc(); + TLB.reserve(TL.getFullDataSize()); + QualType T = R.TransformType(TLB, TL); + addTypeToMap(/*Reverse=*/true, T.getTypePtr()); + SS.Extend(R.SemaRef.Context, /*FIXME:*/ SourceLocation(), + TLB.getTypeSourceInfo(R.SemaRef.Context, T)->getTypeLoc(), + Q.getLocalEndLoc()); + continue; + } + } + llvm_unreachable("Unknown NestedNameSpecifier Kind"); + } + if (SS.getScopeRep() == NNS.getNestedNameSpecifier()) + return; + if (llvm::equal( + ArrayRef(SS.location_data(), SS.location_size()), + ArrayRef((char *)NNS.getOpaqueData(), NNS.getDataLength()))) + NNS = NestedNameSpecifierLoc(SS.getScopeRep(), NNS.getOpaqueData()); + else + NNS = SS.getWithLocInContext(R.SemaRef.Context); + } + }; + + Resugarer(Sema &SemaRef) : inherited(SemaRef) {} + + bool AlwaysRebuild() { return false; } + bool ReplacingOriginal() { return false; } + + QualType TransformQualifiedType(TypeLocBuilder &TLB, QualifiedTypeLoc TL) { + QualType NewUnqual = TransformType(TLB, TL.getUnqualifiedLoc()); + QualType Result = TL.getType(); + if (NewUnqual != TL.getUnqualifiedLoc().getType()) + Result = SemaRef.Context.getQualifiedType( + NewUnqual, TL.getType().getLocalQualifiers()); + TLB.TypeWasModifiedSafely(Result); + return Result; + } + + QualType TransformAdjustedType(TypeLocBuilder &TLB, AdjustedTypeLoc TL) { + const AdjustedType *T = TL.getTypePtr(); + QualType NewOrig = TransformType(TLB, TL.getOriginalLoc()); + + QualType Result = TL.getType(); + if (NewOrig != T->getOriginalType()) + Result = SemaRef.Context.getAdjustedType(NewOrig, T->getAdjustedType()); + TLB.push(Result); + return Result; + } + + QualType TransformElaboratedType(TypeLocBuilder &TLB, ElaboratedTypeLoc TL) { + NestedNameSpecifierLoc QualifierLoc = TL.getQualifierLoc(); + NamingContextTransformRAII NamingScope(*this, QualifierLoc); + QualType NamedT = TransformType(TLB, TL.getNamedTypeLoc()); + + const ElaboratedType *T = TL.getTypePtr(); + QualType Result = TL.getType(); + if (QualifierLoc != TL.getQualifierLoc() || NamedT != T->getNamedType()) + Result = SemaRef.Context.getElaboratedType( + T->getKeyword(), QualifierLoc.getNestedNameSpecifier(), NamedT); + + auto NewTL = TLB.push(Result); + NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); + NewTL.setQualifierLoc(QualifierLoc); + return Result; + } + + Decl *TransformDecl(SourceLocation Loc, Decl *D) const { return D; } + + QualType TransformSubstTemplateTypeParmType(TypeLocBuilder &TLB, + SubstTemplateTypeParmTypeLoc TL) { + QualType QT = TL.getType(); + const SubstTemplateTypeParmType *T = TL.getTypePtr(); + Decl *ReplacedDecl = T->getReplacedDecl(); + + Optional PackIndex = T->getPackIndex(); + if (const TemplateArgument *Arg = + getArgument(ReplacedDecl, T->getIndex(), PackIndex)) { + QualType Replacement = Arg->getAsType().getNonPackExpansionType(); + if (!SemaRef.Context.hasSameType(Replacement, T->getReplacementType())) { + Replacement.dump(); + T->getReplacementType().dump(); + assert(false); + } + if (Replacement != T->getReplacementType()) + QT = SemaRef.Context.getSubstTemplateTypeParmType( + Replacement, ReplacedDecl, T->getIndex(), PackIndex); + } + auto NewTL = TLB.push(QT); + NewTL.setNameLoc(TL.getNameLoc()); + return QT; + } + + using inherited::TransformTemplateSpecializationType; + + QualType TransformTemplateSpecializationType(TypeLocBuilder &TLB, + TemplateSpecializationTypeLoc TL, + TemplateName N) { + const TemplateSpecializationType *T = TL.getTypePtr(); + + SmallVector SpecArgs(TL.getNumArgs()); + for (unsigned I = 0, E = TL.getNumArgs(); I < E; ++I) + SpecArgs[I] = TL.getArgLoc(I); + TransformTemplateArguments(SpecArgs); + + auto ConvertedArgs = SmallVector( + llvm::iterator_range( + T->getConvertedArguments())); + TransformTemplateArguments(ConvertedArgs); + + QualType Underlying = T->desugar(); + if (T->isTypeAlias()) { + SemanticContextRAII SemanticScope(*this, N.getAsTemplateDecl(), + ConvertedArgs); + Underlying = TransformType(Underlying); + } + + // FIXME: Rebuild only if changed. + QualType Result = SemaRef.Context.getTemplateSpecializationType( + N, SpecArgs, ConvertedArgs, /*CanonicalConvertedArgs=*/{}, Underlying); + + auto NewTL = TLB.push(Result); + NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); + NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); + NewTL.setLAngleLoc(TL.getLAngleLoc()); + NewTL.setRAngleLoc(TL.getRAngleLoc()); + for (unsigned I = 0, E = SpecArgs.size(); I < E; ++I) + NewTL.setArgLocInfo(I, SpecArgs[I].getLocInfo()); + return Result; + } + + QualType TransformDependentTemplateSpecializationType( + TypeLocBuilder &TLB, DependentTemplateSpecializationTypeLoc TL) { + const DependentTemplateSpecializationType *T = TL.getTypePtr(); + + SmallVector Args(TL.getNumArgs()); + for (unsigned I = 0, E = TL.getNumArgs(); I < E; ++I) + Args[I] = TL.getArgLoc(I); + + NestedNameSpecifierLoc NNS = TL.getQualifierLoc(); + NamingContextTransformRAII NamingScope(*this, NNS); + TransformTemplateArguments(Args); + // FIXME: Don't rebuild if nothing changed. + QualType Result = SemaRef.Context.getDependentTemplateSpecializationType( + T->getKeyword(), NNS.getNestedNameSpecifier(), T->getIdentifier(), + Args); + + auto NewTL = TLB.push(Result); + NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); + NewTL.setQualifierLoc(NNS); + NewTL.setTemplateKeywordLoc(TL.getTemplateKeywordLoc()); + NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); + NewTL.setLAngleLoc(TL.getLAngleLoc()); + NewTL.setRAngleLoc(TL.getRAngleLoc()); + for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) + NewTL.setArgLocInfo(I, Args[I].getLocInfo()); + return Result; + } + + TypeSourceInfo *TransformTSIInObjectScope(TypeLoc TL, QualType, NamedDecl *, + CXXScopeSpec &) = delete; + + QualType TransformDependentTemplateSpecializationType( + TypeLocBuilder &, DependentTemplateSpecializationTypeLoc, TemplateName, + CXXScopeSpec &) = delete; + + TemplateName TransformTemplateName(CXXScopeSpec &SS, TemplateName Name, + SourceLocation NameLoc, + QualType ObjectType = QualType(), + NamedDecl *FirstQualifierInScope = nullptr, + bool AllowInjectedClassName = false) { + if (Name.getKind() == TemplateName::NameKind::SubstTemplateTemplateParm) + return Name; // FIXME: resugar these + return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, + FirstQualifierInScope, + AllowInjectedClassName); + } + + QualType TransformDependentNameType(TypeLocBuilder &TLB, + DependentNameTypeLoc TL, + bool DeducedTSTContext = false) { + const DependentNameType *T = TL.getTypePtr(); + + NestedNameSpecifierLoc QualifierLoc = TL.getQualifierLoc(); + NamingContextTransformRAII NamingContext(*this, QualifierLoc); + assert(QualifierLoc); + assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); + QualType Result = SemaRef.Context.getDependentNameType( + T->getKeyword(), QualifierLoc.getNestedNameSpecifier(), + T->getIdentifier()); + auto NewTL = TLB.push(Result); + NewTL.setElaboratedKeywordLoc(TL.getElaboratedKeywordLoc()); + NewTL.setQualifierLoc(QualifierLoc); + NewTL.setNameLoc(TL.getNameLoc()); + return Result; + } + + QualType TransformTypedefType(TypeLocBuilder &TLB, TypedefTypeLoc TL) { + const TypedefType *T = TL.getTypePtr(); + const TypedefNameDecl *D = T->getDecl(); + QualType OldUnderlying = T->desugar(); + NonTemplateSemanticContextRAII SemanticScope(*this); + QualType NewUnderlying = TransformType(OldUnderlying); + QualType Result = TL.getType(); + if (NewUnderlying != OldUnderlying) + Result = SemaRef.Context.getTypedefType(D, NewUnderlying); + auto NewTL = TLB.push(Result); + NewTL.setNameLoc(TL.getNameLoc()); + return Result; + } + + QualType TransformUsingType(TypeLocBuilder &TLB, UsingTypeLoc TL) { + const UsingType *T = TL.getTypePtr(); + const UsingShadowDecl *D = T->getFoundDecl(); + QualType OldUnderlying = T->desugar(); + NonTemplateSemanticContextRAII SemanticScope(*this); + QualType NewUnderlying = TransformType(OldUnderlying); + QualType Result = TL.getType(); + if (NewUnderlying != OldUnderlying) + Result = SemaRef.Context.getUsingType(D, NewUnderlying); + TLB.pushTypeSpec(Result).setNameLoc(TL.getNameLoc()); + return Result; + } + + ExprResult TransformExpr(Expr *E) { return E; } + + bool TransformTemplateArgument(const TemplateArgumentLoc &Input, + TemplateArgumentLoc &Output, bool) = delete; + + bool TransformTemplateArgument(TemplateArgument &Arg) { + switch (Arg.getKind()) { + case TemplateArgument::Null: + llvm_unreachable("Unexpected Null TemplateArgument"); + case TemplateArgument::Pack: { + ArrayRef PackArray = Arg.getPackAsArray(); + if (PackArray.empty()) + return false; + auto NewPack = PackArray.copy(SemaRef.Context); + Arg = TemplateArgument(NewPack); + return TransformTemplateArguments(NewPack); + } + case TemplateArgument::Integral: + case TemplateArgument::NullPtr: + case TemplateArgument::Declaration: { + QualType T = Arg.getNonTypeTemplateArgumentType(); + QualType NewT = TransformType(T); + if (NewT == T) + return false; + switch (Arg.getKind()) { + case TemplateArgument::Integral: + Arg = TemplateArgument(SemaRef.Context, Arg.getAsIntegral(), NewT); + return true; + case TemplateArgument::NullPtr: + Arg = TemplateArgument(NewT, /*IsNullPtr=*/true); + return true; + case TemplateArgument::Declaration: + Arg = TemplateArgument(Arg.getAsDecl(), NewT); + return true; + default: + break; + } + llvm_unreachable(""); + } + case TemplateArgument::Type: { + QualType T = TransformType(Arg.getAsType()); + if (T == Arg.getAsType()) + return false; + Arg = TemplateArgument(T); + return true; + } + case TemplateArgument::Template: + case TemplateArgument::TemplateExpansion: { + // FIXME: Transform these. + return false; + } + case TemplateArgument::Expression: + // FIXME: convert the type of these. + return false; + } + llvm_unreachable("Unexpected TemplateArgument kind"); + } + + bool TransformTemplateArgument(TemplateArgumentLoc &AL) { + const TemplateArgument &Arg = AL.getArgument(); + switch (Arg.getKind()) { + case TemplateArgument::Null: + llvm_unreachable("Unexpected Null TemplateArgument"); + case TemplateArgument::Pack: { + ArrayRef PackArray = Arg.getPackAsArray(); + SmallVector Pack(PackArray.size()); + for (unsigned I = 0; I < PackArray.size(); ++I) + Pack[I] = SemaRef.getTrivialTemplateArgumentLoc( + PackArray[I], QualType(), SourceLocation()); + bool Changed = TransformTemplateArguments(Pack); + SmallVector ROuts(Pack.size()); + for (unsigned I = 0; I < Pack.size(); ++I) + ROuts[I] = Pack[I].getArgument(); + AL = TemplateArgumentLoc( + TemplateArgument::CreatePackCopy(SemaRef.Context, ROuts), + AL.getLocInfo()); + return Changed; + } + case TemplateArgument::Integral: + case TemplateArgument::NullPtr: + case TemplateArgument::Declaration: { + QualType T = Arg.getNonTypeTemplateArgumentType(); + QualType NewT = TransformType(T); + if (NewT == T) + return false; + switch (Arg.getKind()) { + case TemplateArgument::Integral: + AL = TemplateArgumentLoc( + TemplateArgument(SemaRef.Context, Arg.getAsIntegral(), NewT), + AL.getLocInfo()); + return true; + case TemplateArgument::NullPtr: + AL = TemplateArgumentLoc(TemplateArgument(NewT, /*IsNullPtr=*/true), + AL.getLocInfo()); + return true; + default: + AL = TemplateArgumentLoc(TemplateArgument(Arg.getAsDecl(), NewT), + AL.getLocInfo()); + return true; + } + llvm_unreachable(""); + } + case TemplateArgument::Type: { + if (Arg.isPackExpansion()) { + QualType T = TransformType(Arg.getAsType()); + if (T == Arg.getAsType()) + return false; + AL = TemplateArgumentLoc(TemplateArgument(T), AL.getLocInfo()); + return true; + } + TypeSourceInfo *DI = AL.getTypeSourceInfo(); + if (!DI) + DI = InventTypeSourceInfo(Arg.getAsType()); + TypeSourceInfo *NewDI = TransformType(DI); + if (DI == NewDI) + return false; + AL = TemplateArgumentLoc(TemplateArgument(NewDI->getType()), NewDI); + return true; + } + case TemplateArgument::Template: { + NestedNameSpecifierLoc QualifierLoc = AL.getTemplateQualifierLoc(); + NamingContextTransformRAII NamingContext(*this, QualifierLoc); + + CXXScopeSpec SS; + SS.Adopt(QualifierLoc); + TemplateName Template = TransformTemplateName(SS, Arg.getAsTemplate(), + AL.getTemplateNameLoc()); + if (Template.getAsVoidPointer() == Arg.getAsTemplate().getAsVoidPointer()) + return false; + + AL = TemplateArgumentLoc(SemaRef.Context, TemplateArgument(Template), + QualifierLoc, AL.getTemplateNameLoc()); + return true; + } + case TemplateArgument::TemplateExpansion: { + AL = TemplateArgumentLoc( + TemplateArgument(Arg.getAsTemplateOrTemplatePattern(), + Arg.getNumTemplateExpansions()), + AL.getLocInfo()); + return false; + } + case TemplateArgument::Expression: + // FIXME: convert the type of these. + return false; + } + llvm_unreachable("Unexpected TemplateArgument kind"); + } + + template + bool TransformTemplateArguments(InputIterator First, InputIterator Last, + TemplateArgumentListInfo &Outputs, + bool Uneval = true) = delete; + + bool TransformTemplateArguments(MutableArrayRef Args) { + bool Changed = false; + for (auto &Arg : Args) + Changed |= TransformTemplateArgument(Arg); + return Changed; + } + bool TransformTemplateArguments(MutableArrayRef Args) { + bool Changed = false; + for (auto &Arg : Args) + Changed |= TransformTemplateArgument(Arg); + return Changed; + } + + QualType TransformPackExpansionType(TypeLocBuilder &TLB, + PackExpansionTypeLoc TL) { + QualType Pattern = TransformType(TLB, TL.getPatternLoc()); + assert(!Pattern.isNull()); + + QualType Result = TL.getType(); + if (Pattern != TL.getPatternLoc().getType()) { + Result = SemaRef.Context.getPackExpansionType( + Pattern, TL.getTypePtr()->getNumExpansions(), + /*ExpectPackInType=*/false); + } + + auto NewT = TLB.push(Result); + NewT.setEllipsisLoc(TL.getEllipsisLoc()); + return Result; + } + + bool TransformTypes(llvm::ArrayRef In, + SmallVectorImpl &Out) { + bool Changed = false; + for (unsigned i = 0; i != In.size(); ++i) { + QualType OldType = In[i]; + QualType NewType = TransformType(OldType); + assert(!NewType.isNull()); + Changed |= NewType != OldType; + Out[i] = NewType; + } + return Changed; + } + + QualType TransformFunctionProtoType(TypeLocBuilder &TLB, + FunctionProtoTypeLoc TL) { + const FunctionProtoType *T = TL.getTypePtr(); + + QualType ResultType = TransformType(TLB, TL.getReturnLoc()); + assert(!ResultType.isNull()); + + bool Changed = ResultType != TL.getReturnLoc().getType(); + + SmallVector ParamTypes(T->getNumParams()); + Changed |= TransformTypes(llvm::ArrayRef(T->param_types().begin(), + T->param_types().end()), + ParamTypes); + + FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo(); + + SmallVector ExceptionStorage( + EPI.ExceptionSpec.Exceptions.size()); + Changed |= TransformTypes(EPI.ExceptionSpec.Exceptions, ExceptionStorage); + EPI.ExceptionSpec.Exceptions = ExceptionStorage; + + QualType Result = + Changed ? SemaRef.Context.getFunctionType(ResultType, ParamTypes, EPI) + : TL.getType(); + + auto NewTL = TLB.push(Result); + NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); + NewTL.setLParenLoc(TL.getLParenLoc()); + NewTL.setRParenLoc(TL.getRParenLoc()); + NewTL.setExceptionSpecRange(TL.getExceptionSpecRange()); + NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); + for (unsigned i = 0, e = NewTL.getNumParams(); i != e; ++i) + NewTL.setParam(i, TL.getParam(i)); + return Result; + } + + bool TransformFunctionTypeParams(ArrayRef ParamTypes, + SmallVectorImpl &OutParamTypes) { + for (unsigned i = 0; i != ParamTypes.size(); ++i) { + QualType NewType = TransformType(ParamTypes[i]); + assert(!NewType.isNull()); + OutParamTypes.push_back(NewType); + } + return false; + } + + QualType TransformAttributedType(TypeLocBuilder &TLB, AttributedTypeLoc TL) { + const AttributedType *T = TL.getTypePtr(); + QualType MT = TransformType(TLB, TL.getModifiedLoc()); + assert(!MT.isNull()); + + const Attr *OldAttr = TL.getAttr(); + const Attr *NewAttr = OldAttr ? TransformAttr(OldAttr) : nullptr; + assert(!OldAttr == !NewAttr); + + // FIXME: Rebuild if Attr changes? + QualType Result = TL.getType(); + if (MT != T->getModifiedType()) { + Result = SemaRef.Context.getAttributedType(TL.getAttrKind(), MT, + T->getEquivalentType()); + } + + auto newTL = TLB.push(Result); + newTL.setAttr(NewAttr); + return Result; + } + + QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { + NestedNameSpecifierLoc NNS; + const AutoType *T = TL.getTypePtr(); + + SmallVector Args(TL.getNumArgs()); + for (unsigned I = 0, E = TL.getNumArgs(); I < E; ++I) + Args[I] = TL.getArgLoc(I); + bool ChangedArgs = TransformTemplateArguments(Args); + + QualType Deduced = !T->getDeducedType().isNull() + ? TransformType(T->getDeducedType()) + : QualType(); + + QualType Result = TL.getType(); + if (Deduced != T->getDeducedType() || ChangedArgs) { + NNS = TL.getNestedNameSpecifierLoc(); + NamingContextTransformRAII NamingContext(*this, NNS); + // FIXME: Maybe don't rebuild if all template arguments are the same. + llvm::SmallVector TypeConstraintArgs(Args.size()); + for (unsigned I = 0; I < Args.size(); ++I) + TypeConstraintArgs[I] = Args[I].getArgument(); + Result = SemaRef.Context.getAutoType( + Deduced, T->getKeyword(), Deduced.isNull(), + T->containsUnexpandedParameterPack(), T->getTypeConstraintConcept(), + TypeConstraintArgs); + } + + auto NewTL = TLB.push(Result); + NewTL.setNameLoc(TL.getNameLoc()); + NewTL.setNestedNameSpecifierLoc(NNS); + NewTL.setTemplateKWLoc(TL.getTemplateKWLoc()); + NewTL.setConceptNameLoc(TL.getConceptNameLoc()); + NewTL.setFoundDecl(TL.getFoundDecl()); + NewTL.setLAngleLoc(TL.getLAngleLoc()); + NewTL.setRAngleLoc(TL.getRAngleLoc()); + NewTL.setRParenLoc(TL.getRParenLoc()); + for (unsigned I = 0; I < NewTL.getNumArgs(); ++I) + NewTL.setArgLocInfo(I, Args[I].getLocInfo()); + return Result; + } + + QualType TransformConstantArrayType(TypeLocBuilder &TLB, + ConstantArrayTypeLoc TL) { + const ConstantArrayType *T = TL.getTypePtr(); + QualType ElementType = TransformType(TLB, TL.getElementLoc()); + assert(!ElementType.isNull()); + + // Prefer the expression from the TypeLoc; the other may have been uniqued. + Expr *OldSize = TL.getSizeExpr(); + if (!OldSize) + OldSize = const_cast(T->getSizeExpr()); + Expr *NewSize = nullptr; + if (OldSize) { + NewSize = TransformExpr(OldSize).template getAs(); + } + + QualType Result = TL.getType(); + if (ElementType != T->getElementType() || + (T->getSizeExpr() && NewSize != OldSize)) { + Result = SemaRef.Context.getConstantArrayType( + ElementType, T->getSize(), NewSize, T->getSizeModifier(), + T->getIndexTypeCVRQualifiers()); + } + + auto NewTL = TLB.push(Result); + NewTL.setLBracketLoc(TL.getLBracketLoc()); + NewTL.setRBracketLoc(TL.getRBracketLoc()); + NewTL.setSizeExpr(NewSize); + return Result; + } + + QualType TransformIncompleteArrayType(TypeLocBuilder &TLB, + IncompleteArrayTypeLoc TL) { + const IncompleteArrayType *T = TL.getTypePtr(); + QualType ElementType = TransformType(TLB, TL.getElementLoc()); + assert(!ElementType.isNull()); + + QualType Result = TL.getType(); + if (ElementType != T->getElementType()) { + Result = SemaRef.Context.getIncompleteArrayType( + ElementType, T->getSizeModifier(), T->getIndexTypeCVRQualifiers()); + } + + auto NewTL = TLB.push(Result); + NewTL.setLBracketLoc(TL.getLBracketLoc()); + NewTL.setRBracketLoc(TL.getRBracketLoc()); + NewTL.setSizeExpr(nullptr); + return Result; + } + + QualType TransformVariableArrayType(TypeLocBuilder &TLB, + VariableArrayTypeLoc TL) { + const VariableArrayType *T = TL.getTypePtr(); + QualType ElementType = TransformType(TLB, TL.getElementLoc()); + assert(!ElementType.isNull()); + + Expr *NewSize = TransformExpr(T->getSizeExpr()).template getAs(); + + QualType Result = TL.getType(); + if (ElementType != T->getElementType() || NewSize != T->getSizeExpr()) { + Result = SemaRef.Context.getVariableArrayType( + ElementType, NewSize, T->getSizeModifier(), + T->getIndexTypeCVRQualifiers(), TL.getBracketsRange()); + } + + auto NewTL = TLB.push(Result); + NewTL.setLBracketLoc(TL.getLBracketLoc()); + NewTL.setRBracketLoc(TL.getRBracketLoc()); + NewTL.setSizeExpr(NewSize); + return Result; + } + + QualType TransformDependentSizedArrayType(TypeLocBuilder &TLB, + DependentSizedArrayTypeLoc TL) { + const DependentSizedArrayType *T = TL.getTypePtr(); + QualType ElementType = TransformType(TLB, TL.getElementLoc()); + assert(!ElementType.isNull()); + + // Prefer the expression from the TypeLoc; the other may have been uniqued. + Expr *OldSize = TL.getSizeExpr(); + if (!OldSize) + OldSize = const_cast(T->getSizeExpr()); + Expr *NewSize = nullptr; + if (OldSize) { + NewSize = TransformExpr(OldSize).template getAs(); + } + + QualType Result = TL.getType(); + if (ElementType != T->getElementType() || NewSize != OldSize) { + Result = SemaRef.Context.getDependentSizedArrayType( + ElementType, NewSize, T->getSizeModifier(), + T->getIndexTypeCVRQualifiers(), TL.getBracketsRange()); + } + + auto NewTL = TLB.push(Result); + NewTL.setLBracketLoc(TL.getLBracketLoc()); + NewTL.setRBracketLoc(TL.getRBracketLoc()); + NewTL.setSizeExpr(NewSize); + return Result; + } + + QualType TransformBTFTagAttributedType(TypeLocBuilder &TLB, + BTFTagAttributedTypeLoc TL) { + // The BTFTagAttributedType is available for C only. + const BTFTagAttributedType *T = TL.getTypePtr(); + + QualType NewWrappedType = TransformType(TLB, TL.getWrappedLoc()); + assert(!NewWrappedType.isNull()); + + const Attr *NewAttr = TransformAttr(T->getAttr()); + + QualType Result = TL.getType(); + if (NewWrappedType != T->getWrappedType() || NewAttr != T->getAttr()) { + Result = + SemaRef.Context.getBTFTagAttributedType(T->getAttr(), NewWrappedType); + } + TLB.push(Result); + return Result; + } + + QualType TransformDependentAddressSpaceType(TypeLocBuilder &TLB, + DependentAddressSpaceTypeLoc TL) { + const DependentAddressSpaceType *T = TL.getTypePtr(); + + QualType pointeeType = TransformType(TLB, TL.getPointeeTypeLoc()); + assert(!pointeeType.isNull()); + + Expr *AddrSpace = + TransformExpr(T->getAddrSpaceExpr()).template getAs(); + + QualType Result = TL.getType(); + if (pointeeType != T->getPointeeType() || + AddrSpace != T->getAddrSpaceExpr()) { + Result = SemaRef.Context.getDependentAddressSpaceType( + pointeeType, AddrSpace, T->getAttributeLoc()); + } + + auto NewTL = TLB.push(Result); + NewTL.setAttrOperandParensRange(TL.getAttrOperandParensRange()); + NewTL.setAttrExprOperand(AddrSpace); + NewTL.setAttrNameLoc(TL.getAttrNameLoc()); + return Result; + } + + QualType TransformDeducedTemplateSpecializationType( + TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { + const DeducedTemplateSpecializationType *T = TL.getTypePtr(); + + CXXScopeSpec SS; + TemplateName Template = TransformTemplateName(SS, T->getTemplateName(), + TL.getTemplateNameLoc()); + assert(!Template.isNull()); + + QualType NewDeduced; + if (!T->getDeducedType().isNull()) { + NewDeduced = TransformType(T->getDeducedType()); + assert(!NewDeduced.isNull()); + } + + QualType Result = TL.getType(); + if (Template.getAsVoidPointer() != + T->getTemplateName().getAsVoidPointer() || + NewDeduced != T->getDeducedType()) + Result = SemaRef.Context.getDeducedTemplateSpecializationType( + Template, NewDeduced, T->isDependentType()); + + auto NewTL = TLB.push(Result); + NewTL.setTemplateNameLoc(TL.getTemplateNameLoc()); + return Result; + } + + QualType TransformTypeOfExprType(TypeLocBuilder &TLB, TypeOfExprTypeLoc TL) { + ExprResult E = TransformExpr(TL.getUnderlyingExpr()); + // FIXME: resugar + QualType Result = TL.getType(); + if (E.get() != TL.getUnderlyingExpr()) { + Result = SemaRef.BuildTypeofExprType(E.get()); + } + TypeOfExprTypeLoc NewTL = TLB.push(Result); + NewTL.setTypeofLoc(TL.getTypeofLoc()); + NewTL.setLParenLoc(TL.getLParenLoc()); + NewTL.setRParenLoc(TL.getRParenLoc()); + return Result; + } + + QualType TransformTypeOfType(TypeLocBuilder &TLB, TypeOfTypeLoc TL) { + TypeSourceInfo *Old = TL.getUnderlyingTInfo(); + TypeSourceInfo *New = TransformType(Old); + + QualType Result = TL.getType(); + if (New != Old) + Result = SemaRef.Context.getTypeOfType(New->getType()); + + TypeOfTypeLoc NewTL = TLB.push(Result); + NewTL.setTypeofLoc(TL.getTypeofLoc()); + NewTL.setLParenLoc(TL.getLParenLoc()); + NewTL.setRParenLoc(TL.getRParenLoc()); + NewTL.setUnderlyingTInfo(New); + return Result; + } + + QualType TransformDecltypeType(TypeLocBuilder &TLB, DecltypeTypeLoc TL) { + const DecltypeType *T = TL.getTypePtr(); + QualType NewT = TransformType(T->getUnderlyingType()); + QualType Result = TL.getType(); + if (NewT != T->getUnderlyingType()) + Result = SemaRef.Context.getDecltypeType(T->getUnderlyingExpr(), NewT); + + DecltypeTypeLoc NewTL = TLB.push(Result); + NewTL.setDecltypeLoc(TL.getDecltypeLoc()); + NewTL.setRParenLoc(TL.getRParenLoc()); + return Result; + } + + QualType TransformUnaryTransformType(TypeLocBuilder &TLB, + UnaryTransformTypeLoc TL) { + QualType Result = TL.getType(); + if (Result->isDependentType()) { + TypeSourceInfo *NewBase = TransformType(TL.getUnderlyingTInfo()); + if (NewBase->getType() != TL.getUnderlyingTInfo()->getType()) + Result = SemaRef.BuildUnaryTransformType( + NewBase->getType(), TL.getTypePtr()->getUTTKind(), TL.getKWLoc()); + } + UnaryTransformTypeLoc NewTL = TLB.push(Result); + NewTL.setKWLoc(TL.getKWLoc()); + NewTL.setParensRange(TL.getParensRange()); + NewTL.setUnderlyingTInfo(TL.getUnderlyingTInfo()); + return Result; + } + // FIXME: missing resugar of enums. +}; +} // namespace + +QualType Sema::resugar(const CXXScopeSpec &SS, QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + Resugarer::NamingContextRAII NamingScope(R, SS.getScopeRep()); + return R.TransformType(T); +} + +QualType Sema::resugar(const CXXScopeSpec &SS, NamedDecl *ND, + ArrayRef Args, QualType T) { + assert(ND != nullptr); + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + Resugarer::NamingContextRAII NamingScope(R, SS.getScopeRep()); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} +QualType Sema::resugar(const CXXScopeSpec &SS, NamedDecl *ND, + ArrayRef Args, QualType T) { + assert(ND != nullptr); + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + Resugarer::NamingContextRAII NamingScope(R, SS.getScopeRep()); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} + +static const NestedNameSpecifier *decomposeBaseType(const Type *&Base) { + if (const auto *ElTy = Base->getAs()) { + Base = ElTy->getNamedType().getTypePtr(); + return ElTy->getQualifier(); + } + return nullptr; +} + +QualType Sema::resugar(const Type *Base, QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + return R.TransformType(T); +} + +QualType Sema::resugar(const Type *Base, NamedDecl *ND, + ArrayRef Args, QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} +QualType Sema::resugar(const Type *Base, NamedDecl *ND, + ArrayRef Args, QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} + +QualType Sema::resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + Resugarer::NamingContextTransformRAII FieldScope(R, FieldNNS); + return R.TransformType(T); +} + +QualType Sema::resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + NamedDecl *ND, ArrayRef Args, + QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + Resugarer::NamingContextTransformRAII FieldScope(R, FieldNNS); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} +QualType Sema::resugar(const Type *Base, NestedNameSpecifierLoc &FieldNNS, + NamedDecl *ND, ArrayRef Args, + QualType T) { + if (!getLangOpts().Resugar) + return T; + Resugarer R(*this); + const NestedNameSpecifier *BaseNNS = decomposeBaseType(Base); + Resugarer::NamingContextRAII BaseScope(R, BaseNNS, Base); + Resugarer::NamingContextTransformRAII FieldScope(R, FieldNNS); + Resugarer::SemanticContextRAII SemanticScope(R, ND, Args); + return R.TransformType(T); +} + +static QualType getResugaredTemplateSpecializationType( + Sema &S, const CXXScopeSpec &SS, TemplateName Template, + MutableArrayRef SpecArgs, + MutableArrayRef SugaredConvertedArgs, + ArrayRef CanonicalConvertedArgs, + QualType Underlying = QualType()) { + if (!S.getLangOpts().Resugar) + return S.Context.getTemplateSpecializationType( + Template, SpecArgs, SugaredConvertedArgs, CanonicalConvertedArgs, + Underlying); + + auto *TD = Template.getAsTemplateDecl(); + bool IsTypeAlias = !Underlying.isNull() && TD && TD->isTypeAlias(); + + Resugarer R(S); + Resugarer::NamingContextRAII NamingScope(R, SS.getScopeRep()); + R.TransformTemplateArguments(SpecArgs); + R.TransformTemplateArguments(SugaredConvertedArgs); + if (IsTypeAlias) { + Resugarer::SemanticContextRAII SemanticScope(R, TD, SugaredConvertedArgs); + Underlying = R.TransformType(Underlying); + } + return S.Context.getTemplateSpecializationType( + Template, SpecArgs, SugaredConvertedArgs, CanonicalConvertedArgs, + Underlying); +} + /// \brief Determine whether the declaration found is acceptable as the name /// of a template and, if so, return that template declaration. Otherwise, /// returns null. @@ -3497,8 +4762,10 @@ } static QualType -checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, - const SmallVectorImpl &Converted, +checkBuiltinTemplateIdType(Sema &SemaRef, const CXXScopeSpec &SS, + TemplateName Name, BuiltinTemplateDecl *BTD, + MutableArrayRef SugaredConverted, + MutableArrayRef CanonicalConverted, SourceLocation TemplateLoc, TemplateArgumentListInfo &TemplateArgs) { ASTContext &Context = SemaRef.getASTContext(); @@ -3517,7 +4784,7 @@ // Specializations of __make_integer_seq are treated like // S. - QualType OrigType = Converted[1].getAsType(); + QualType OrigType = SugaredConverted[1].getAsType(); // C++14 [inteseq.intseq]p1: // T shall be an integer type. if (!OrigType->isDependentType() && !OrigType->isIntegralType(Context)) { @@ -3526,10 +4793,11 @@ return QualType(); } - TemplateArgument NumArgsArg = Converted[2]; + TemplateArgument NumArgsArg = SugaredConverted[2]; if (NumArgsArg.isDependent()) - return Context.getCanonicalTemplateSpecializationType(TemplateName(BTD), - Converted); + return SemaRef.Context.getTemplateSpecializationType( + TemplateName(BTD), TemplateArgs.arguments(), SugaredConverted, + CanonicalConverted); TemplateArgumentListInfo SyntheticTemplateArgs; // The type argument, wrapped in substitution sugar, gets reused as the @@ -3559,24 +4827,29 @@ // Wrap the template in substitution sugar. TemplateName TN = SemaRef.Context.getSubstTemplateTemplateParm( cast(TPL->getParam(0)), - Converted[0].getAsTemplate()); + SugaredConverted[0].getAsTemplate()); // The first template argument will be reused as the template decl that // our synthetic template arguments will be applied to. - return SemaRef.CheckTemplateIdType(TN, TemplateLoc, SyntheticTemplateArgs); + QualType Result = + SemaRef.CheckTemplateIdType(SS, TN, TemplateLoc, SyntheticTemplateArgs); + return SemaRef.Context.getTemplateSpecializationType( + Name, TemplateArgs.arguments(), SugaredConverted, + /*CanonicalConverted=*/{}, Result); } case BTK__type_pack_element: // Specializations of // __type_pack_element // are treated like T_Index. - assert(Converted.size() == 2 && - "__type_pack_element should be given an index and a parameter pack"); + assert(SugaredConverted.size() == 2 && + "__type_pack_element should be given an index and a parameter pack"); - TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; + TemplateArgument IndexArg = SugaredConverted[0], Ts = SugaredConverted[1]; if (IndexArg.isDependent() || Ts.isDependent()) - return Context.getCanonicalTemplateSpecializationType(TemplateName(BTD), - Converted); + return SemaRef.Context.getTemplateSpecializationType( + TemplateName(BTD), TemplateArgs.arguments(), SugaredConverted, + CanonicalConverted); llvm::APSInt Index = IndexArg.getAsIntegral(); assert(Index >= 0 && "the index used with __type_pack_element should be of " @@ -3590,8 +4863,12 @@ // We simply return the type at index `Index`. int64_t N = Index.getExtValue(); - return getSubstType(Ts.getPackAsArray()[N].getAsType(), 1, - Ts.pack_size() - 1 - N); + // FIXME: resugaring here is actually unnecessary. + return getResugaredTemplateSpecializationType( + SemaRef, SS, Name, TemplateArgs.arguments(), SugaredConverted, + /*CanonicalConverted=*/{}, + getSubstType(Ts.getPackAsArray()[N].getAsType(), 1, + Ts.pack_size() - 1 - N)); } llvm_unreachable("unexpected BuiltinTemplateDecl!"); } @@ -3734,7 +5011,8 @@ return { FailedCond, Description }; } -QualType Sema::CheckTemplateIdType(TemplateName Name, +// FIXME: We should get the context we are in from the TemplateDeclInstantiator. +QualType Sema::CheckTemplateIdType(const CXXScopeSpec &SS, TemplateName Name, SourceLocation TemplateLoc, TemplateArgumentListInfo &TemplateArgs) { DependentTemplateName *DTN @@ -3744,28 +5022,26 @@ // assume the template is a type template. Either our assumption is // correct, or the code is ill-formed and will be diagnosed when the // dependent name is substituted. - return Context.getDependentTemplateSpecializationType(ETK_None, - DTN->getQualifier(), - DTN->getIdentifier(), - TemplateArgs); + return Context.getDependentTemplateSpecializationType( + ETK_None, DTN->getQualifier(), DTN->getIdentifier(), + TemplateArgs.arguments()); if (Name.getAsAssumedTemplateName() && resolveAssumedTemplateNameAsType(/*Scope*/nullptr, Name, TemplateLoc)) return QualType(); - TemplateDecl *Template = Name.getAsTemplateDecl(); - if (!Template || isa(Template) || - isa(Template) || isa(Template)) { - // We might have a substituted template template parameter pack. If so, - // build a template specialization type for it. - if (Name.getAsSubstTemplateTemplateParmPack()) - return Context.getTemplateSpecializationType(Name, - TemplateArgs.arguments()); - - Diag(TemplateLoc, diag::err_template_id_not_a_type) - << Name; - NoteAllFoundTemplates(Name); - return QualType(); + TemplateDecl *Template; + if (const SubstTemplateTemplateParmPackStorage *S = + Name.getAsSubstTemplateTemplateParmPack()) { + Template = S->getParameterPack(); + } else { + Template = Name.getAsTemplateDecl(); + if (!Template || isa(Template) || + isa(Template) || isa(Template)) { + Diag(TemplateLoc, diag::err_template_id_not_a_type) << Name; + NoteAllFoundTemplates(Name); + return QualType(); + } } // Check that the template argument list is well-formed for this @@ -3778,14 +5054,18 @@ QualType CanonType; - if (TypeAliasTemplateDecl *AliasTemplate = - dyn_cast(Template)) { + if (isa(Template)) { + // We might have a substituted template template parameter pack. If so, + // build a template specialization type for it. + } else if (TypeAliasTemplateDecl *AliasTemplate = + dyn_cast(Template)) { // Find the canonical type for this type alias template specialization. TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); if (Pattern->isInvalidDecl()) return QualType(); + // FIXME: Just use SugaredConverted instead. TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, CanonicalConverted); @@ -3840,8 +5120,9 @@ return QualType(); } } else if (auto *BTD = dyn_cast(Template)) { - CanonType = checkBuiltinTemplateIdType(*this, BTD, SugaredConverted, - TemplateLoc, TemplateArgs); + return checkBuiltinTemplateIdType(*this, SS, Name, BTD, SugaredConverted, + CanonicalConverted, TemplateLoc, + TemplateArgs); } else if (Name.isDependent() || TemplateSpecializationType::anyDependentTemplateArguments( TemplateArgs, CanonicalConverted)) { @@ -3939,8 +5220,9 @@ // Build the fully-sugared type for this class template // specialization, which refers back to the class template // specialization we created or found. - return Context.getTemplateSpecializationType(Name, TemplateArgs.arguments(), - CanonType); + return getResugaredTemplateSpecializationType( + *this, SS, Name, TemplateArgs.arguments(), SugaredConverted, + CanonicalConverted, CanonType); } void Sema::ActOnUndeclaredTypeTemplateName(Scope *S, TemplateTy &ParsedName, @@ -4051,11 +5333,10 @@ translateTemplateArguments(TemplateArgsIn, TemplateArgs); if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { - QualType T - = Context.getDependentTemplateSpecializationType(ETK_None, - DTN->getQualifier(), - DTN->getIdentifier(), - TemplateArgs); + assert(SS.getScopeRep() == DTN->getQualifier()); + QualType T = Context.getDependentTemplateSpecializationType( + ETK_None, DTN->getQualifier(), DTN->getIdentifier(), + TemplateArgs.arguments()); // Build type-source information. TypeLocBuilder TLB; DependentTemplateSpecializationTypeLoc SpecTL @@ -4071,7 +5352,8 @@ return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); } - QualType SpecTy = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); + QualType SpecTy = + CheckTemplateIdType(SS, Template, TemplateIILoc, TemplateArgs); if (SpecTy.isNull()) return true; @@ -4121,10 +5403,10 @@ = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { - QualType T = Context.getDependentTemplateSpecializationType(Keyword, - DTN->getQualifier(), - DTN->getIdentifier(), - TemplateArgs); + assert(SS.getScopeRep() == DTN->getQualifier()); + QualType T = Context.getDependentTemplateSpecializationType( + Keyword, DTN->getQualifier(), DTN->getIdentifier(), + TemplateArgs.arguments()); // Build type-source information. TypeLocBuilder TLB; @@ -4152,30 +5434,14 @@ Diag(TAT->getLocation(), diag::note_declared_at); } - QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); - if (Result.isNull()) + QualType T = CheckTemplateIdType(SS, Template, TemplateLoc, TemplateArgs); + if (T.isNull()) return TypeResult(true); - // Check the tag kind - if (const RecordType *RT = Result->getAs()) { - RecordDecl *D = RT->getDecl(); - - IdentifierInfo *Id = D->getIdentifier(); - assert(Id && "templated class must have an identifier"); - - if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, - TagLoc, Id)) { - Diag(TagLoc, diag::err_use_with_wrong_tag) - << Result - << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); - Diag(D->getLocation(), diag::note_previous_use); - } - } - // Provide source-location information for the template specialization. TypeLocBuilder TLB; - TemplateSpecializationTypeLoc SpecTL - = TLB.push(Result); + TemplateSpecializationTypeLoc SpecTL = + TLB.push(T); SpecTL.setTemplateKeywordLoc(TemplateKWLoc); SpecTL.setTemplateNameLoc(TemplateLoc); SpecTL.setLAngleLoc(LAngleLoc); @@ -4185,11 +5451,29 @@ // Construct an elaborated type containing the nested-name-specifier (if any) // and tag keyword. - Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); - ElaboratedTypeLoc ElabTL = TLB.push(Result); + T = Context.getElaboratedType(Keyword, SS.getScopeRep(), T); + ElaboratedTypeLoc ElabTL = TLB.push(T); ElabTL.setElaboratedKeywordLoc(TagLoc); ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); - return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); + + // Check the tag kind + if (const RecordType *RT = T->getAs()) { + RecordDecl *D = RT->getDecl(); + + IdentifierInfo *Id = D->getIdentifier(); + assert(Id && "templated class must have an identifier"); + + if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, TagLoc, + Id)) { + Diag(TagLoc, diag::err_use_with_wrong_tag) + << T + << FixItHint::CreateReplacement(SourceRange(TagLoc), + D->getKindName()); + Diag(D->getLocation(), diag::note_previous_use); + } + } + + return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); } static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, @@ -7469,7 +8753,8 @@ if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) return ExprError(); - Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); + // FIXME: resugar + Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn, nullptr); ArgType = Arg->getType(); } else return ExprError(); @@ -7521,7 +8806,8 @@ if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) return ExprError(); - Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); + // FIXME: resugar + Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn, nullptr); ArgType = Arg->getType(); } else return ExprError(); @@ -8598,17 +9884,15 @@ isPartialSpecialization)) return true; - // The canonical type - QualType CanonType; + QualType T; if (isPartialSpecialization) { - // Build the canonical type that describes the converted template + // Build the sugared type that describes the converted template // arguments of the class template partial specialization. - TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); - CanonType = Context.getTemplateSpecializationType(CanonTemplate, - CanonicalConverted); + T = Context.getTemplateSpecializationType( + Name, TemplateArgs.arguments(), SugaredConverted, CanonicalConverted); - if (Context.hasSameType(CanonType, - ClassTemplate->getInjectedClassNameSpecialization()) && + if (Context.hasSameType( + T, ClassTemplate->getInjectedClassNameSpecialization()) && (!Context.getLangOpts().CPlusPlus20 || !TemplateParams->hasAssociatedConstraints())) { // C++ [temp.class.spec]p9b3: @@ -8635,11 +9919,12 @@ // Create a new class template partial specialization declaration node. ClassTemplatePartialSpecializationDecl *PrevPartial = cast_or_null(PrevDecl); + // FIXME: Use SugaredConverted here. ClassTemplatePartialSpecializationDecl *Partial = ClassTemplatePartialSpecializationDecl::Create( Context, Kind, ClassTemplate->getDeclContext(), KWLoc, TemplateNameLoc, TemplateParams, ClassTemplate, CanonicalConverted, - TemplateArgs, CanonType, PrevPartial); + TemplateArgs, T, PrevPartial); SetNestedNameSpecifier(*this, Partial, SS); if (TemplateParameterLists.size() > 1 && SS.isSet()) { Partial->setTemplateParameterListsInfo( @@ -8672,11 +9957,10 @@ ClassTemplate->AddSpecialization(Specialization, InsertPos); if (CurContext->isDependentContext()) { - TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); - CanonType = Context.getTemplateSpecializationType(CanonTemplate, - CanonicalConverted); + T = Context.getCanonicalTemplateSpecializationType(Name, + CanonicalConverted); } else { - CanonType = Context.getTypeDeclType(Specialization); + T = Context.getTypeDeclType(Specialization); } } @@ -8709,6 +9993,17 @@ } } + // Build the fully-sugared type for this class template + // specialization as the user wrote in the specialization + // itself. This means that we'll pretty-print the type retrieved + // from the specialization's declaration the way that the user + // actually wrote the specialization, rather than formatting the + // name based on the "canonical" representation used to store the + // template arguments in the specialization. + TypeSourceInfo *WrittenTy = Context.getTemplateSpecializationTypeInfo( + Name, TemplateNameLoc, TemplateArgs, SugaredConverted, + /*CanonicalConverted=*/{}, T); + // If this is not a friend, note that this is an explicit specialization. if (TUK != TUK_Friend) Specialization->setSpecializationKind(TSK_ExplicitSpecialization); @@ -8744,16 +10039,6 @@ << (isPartialSpecialization? 1 : 0) << FixItHint::CreateRemoval(ModulePrivateLoc); - // Build the fully-sugared type for this class template - // specialization as the user wrote in the specialization - // itself. This means that we'll pretty-print the type retrieved - // from the specialization's declaration the way that the user - // actually wrote the specialization, rather than formatting the - // name based on the "canonical" representation used to store the - // template arguments in the specialization. - TypeSourceInfo *WrittenTy - = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, - TemplateArgs, CanonType); if (TUK != TUK_Friend) { Specialization->setTypeAsWritten(WrittenTy); Specialization->setTemplateKeywordLoc(TemplateKWLoc); @@ -9920,10 +11205,9 @@ // the explicit instantiation, rather than formatting the name based // on the "canonical" representation used to store the template // arguments in the specialization. - TypeSourceInfo *WrittenTy - = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, - TemplateArgs, - Context.getTypeDeclType(Specialization)); + TypeSourceInfo *WrittenTy = Context.getTemplateSpecializationTypeInfo( + Name, TemplateNameLoc, TemplateArgs, SugaredConverted, CanonicalConverted, + Context.getTypeDeclType(Specialization)); Specialization->setTypeAsWritten(WrittenTy); // Set source locations for keywords. @@ -10644,7 +11928,7 @@ // Strangely, non-type results are not ignored by this lookup, so the // program is ill-formed if it finds an injected-class-name. if (TypenameLoc.isValid()) { - auto *LookupRD = + const auto *LookupRD = dyn_cast_or_null(computeDeclContext(SS, false)); if (LookupRD && LookupRD->getIdentifier() == TemplateII) { Diag(TemplateIILoc, @@ -10663,10 +11947,9 @@ // Construct a dependent template specialization type. assert(DTN && "dependent template has non-dependent name?"); assert(DTN->getQualifier() == SS.getScopeRep()); - QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, - DTN->getQualifier(), - DTN->getIdentifier(), - TemplateArgs); + QualType T = Context.getDependentTemplateSpecializationType( + ETK_Typename, DTN->getQualifier(), DTN->getIdentifier(), + TemplateArgs.arguments()); // Create source-location information for this type. TypeLocBuilder Builder; @@ -10683,7 +11966,7 @@ return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); } - QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); + QualType T = CheckTemplateIdType(SS, Template, TemplateIILoc, TemplateArgs); if (T.isNull()) return true; @@ -10703,8 +11986,7 @@ TL.setElaboratedKeywordLoc(TypenameLoc); TL.setQualifierLoc(SS.getWithLocInContext(Context)); - TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); - return CreateParsedType(T, TSI); + return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); } @@ -10910,9 +12192,9 @@ // We found a type. Build an ElaboratedType, since the // typename-specifier was just sugar. MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false); - return Context.getElaboratedType(Keyword, - QualifierLoc.getNestedNameSpecifier(), - Context.getTypeDeclType(Type)); + return Context.getElaboratedType( + Keyword, QualifierLoc.getNestedNameSpecifier(), + resugar(SS, Context.getTypeDeclType(Type))); } // C++ [dcl.type.simple]p2: diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -560,16 +560,16 @@ QualType UP = P; if (const auto *IP = P->getAs()) UP = IP->getInjectedSpecializationType(); - // FIXME: Try to preserve type sugar here, which is hard - // because of the unresolved template arguments. - const auto *TP = UP.getCanonicalType()->castAs(); + assert(isa(UP.getCanonicalType())); + const TemplateSpecializationType *TP = + UP->castAsNonAliasTemplateSpecializationType(); TemplateName TNP = TP->getTemplateName(); // If the parameter is an alias template, there is nothing to deduce. if (const auto *TD = TNP.getAsTemplateDecl(); TD && TD->isTypeAlias()) return Sema::TDK_Success; - ArrayRef PResolved = TP->template_arguments(); + ArrayRef PResolved = TP->getConvertedArguments(); QualType UA = A; // Treat an injected-class-name as its underlying template-id. @@ -577,9 +577,7 @@ UA = Injected->getInjectedSpecializationType(); // Check whether the template argument is a dependent template-id. - // FIXME: Should not lose sugar here. - if (const auto *SA = - dyn_cast(UA.getCanonicalType())) { + if (const auto *SA = UA->getAsNonAliasTemplateSpecializationType()) { TemplateName TNA = SA->getTemplateName(); // If the argument is an alias template, there is nothing to deduce. @@ -594,7 +592,7 @@ // argument. Ignore any missing/extra arguments, since they could be // filled in by default arguments. return DeduceTemplateArguments(S, TemplateParams, PResolved, - SA->template_arguments(), Info, Deduced, + SA->getConvertedArguments(), Info, Deduced, /*NumberOfArgumentsMustMatch=*/false); } @@ -3701,12 +3699,18 @@ /// Gets the type of a function for template-argument-deducton /// purposes when it's considered as part of an overload set. static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, - FunctionDecl *Fn) { + FunctionDecl *Fn, + const TemplateArgumentListInfo *Args) { // We may need to deduce the return type of the function now. if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) return {}; + // FIXME: get SS. + QualType FT = Fn->getType(); + if (Args) + FT = S.resugar(CXXScopeSpec(), Fn, Args->arguments(), FT); + if (CXXMethodDecl *Method = dyn_cast(Fn)) if (Method->isInstance()) { // An instance method that's referenced in a form that doesn't @@ -3714,12 +3718,14 @@ if (!R.HasFormOfMemberPointer) return {}; - return S.Context.getMemberPointerType(Fn->getType(), - S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); + // FIXME: resugar the class type here. + return S.Context.getMemberPointerType( + FT, S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); } - if (!R.IsAddressOfOperand) return Fn->getType(); - return S.Context.getPointerType(Fn->getType()); + if (!R.IsAddressOfOperand) + return FT; + return S.Context.getPointerType(FT); } /// Apply the deduction rules for overload sets. @@ -3742,6 +3748,11 @@ if (R.IsAddressOfOperand) TDF |= TDF_IgnoreQualifiers; + // Gather the explicit template arguments, if any. + TemplateArgumentListInfo ExplicitTemplateArgs; + if (Ovl->hasExplicitTemplateArgs()) + Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); + // C++0x [temp.deduct.call]p6: // When P is a function type, pointer to function type, or pointer // to member function type: @@ -3751,23 +3762,20 @@ !ParamType->isMemberFunctionPointerType()) { if (Ovl->hasExplicitTemplateArgs()) { // But we can still look for an explicit specialization. - if (FunctionDecl *ExplicitSpec - = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) - return GetTypeOfFunction(S, R, ExplicitSpec); + if (FunctionDecl *ExplicitSpec = + S.ResolveSingleFunctionTemplateSpecialization( + Ovl, ExplicitTemplateArgs)) + return GetTypeOfFunction(S, R, ExplicitSpec, &ExplicitTemplateArgs); } DeclAccessPair DAP; if (FunctionDecl *Viable = S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) - return GetTypeOfFunction(S, R, Viable); + return GetTypeOfFunction(S, R, Viable, nullptr); return {}; } - // Gather the explicit template arguments, if any. - TemplateArgumentListInfo ExplicitTemplateArgs; - if (Ovl->hasExplicitTemplateArgs()) - Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); QualType Match; for (UnresolvedSetIterator I = Ovl->decls_begin(), E = Ovl->decls_end(); I != E; ++I) { @@ -3791,7 +3799,7 @@ } FunctionDecl *Fn = cast(D); - QualType ArgType = GetTypeOfFunction(S, R, Fn); + QualType ArgType = GetTypeOfFunction(S, R, Fn, &ExplicitTemplateArgs); if (ArgType.isNull()) continue; // Function-to-pointer conversion. @@ -5412,7 +5420,7 @@ AtLeastAsSpecialized = !FinishTemplateArgumentDeduction( S, P2, /*IsPartialOrdering=*/true, TemplateArgumentList(TemplateArgumentList::OnStack, - TST1->template_arguments()), + TST1->getConvertedArguments()), Deduced, Info); }); return AtLeastAsSpecialized; @@ -5482,11 +5490,12 @@ // Consider this a fix for CWG1432. Similar to the fix for CWG1395. auto *TST1 = T1->castAs(); auto *TST2 = T2->castAs(); - if (TST1->getNumArgs()) { - const TemplateArgument &TA1 = TST1->template_arguments().back(); + if (TST1->getConvertedArguments().size()) { + const TemplateArgument &TA1 = TST1->getConvertedArguments().back(); if (TA1.getKind() == TemplateArgument::Pack) { - assert(TST1->getNumArgs() == TST2->getNumArgs()); - const TemplateArgument &TA2 = TST2->template_arguments().back(); + assert(TST1->getConvertedArguments().size() == + TST2->getConvertedArguments().size()); + const TemplateArgument &TA2 = TST2->getConvertedArguments().back(); assert(TA2.getKind() == TemplateArgument::Pack); unsigned PackSize1 = TA1.pack_size(); unsigned PackSize2 = TA2.pack_size(); @@ -5567,9 +5576,9 @@ " the same template."); TemplateName Name(PS1->getSpecializedTemplate()); TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); - QualType PT1 = Context.getTemplateSpecializationType( + QualType PT1 = Context.getCanonicalTemplateSpecializationType( CanonTemplate, PS1->getTemplateArgs().asArray()); - QualType PT2 = Context.getTemplateSpecializationType( + QualType PT2 = Context.getCanonicalTemplateSpecializationType( CanonTemplate, PS2->getTemplateArgs().asArray()); TemplateDeductionInfo Info(Loc); @@ -5584,13 +5593,18 @@ SmallVector PrimaryArgs; Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), PrimaryArgs); + Context.getCanonicalTemplateArguments(PrimaryArgs, PrimaryArgs); + + SmallVector SpecArgs; + Context.getCanonicalTemplateArguments(Spec->getTemplateArgs().asArray(), + SpecArgs); TemplateName CanonTemplate = Context.getCanonicalTemplateName(TemplateName(Primary)); - QualType PrimaryT = Context.getTemplateSpecializationType( + QualType PrimaryT = Context.getCanonicalTemplateSpecializationType( CanonTemplate, PrimaryArgs); - QualType PartialT = Context.getTemplateSpecializationType( - CanonTemplate, Spec->getTemplateArgs().asArray()); + QualType PartialT = + Context.getCanonicalTemplateSpecializationType(CanonTemplate, SpecArgs); VarTemplatePartialSpecializationDecl *MaybeSpec = getMoreSpecialized(*this, PartialT, PrimaryT, Spec, Primary, Info); @@ -5620,6 +5634,7 @@ // template parameters from the respective function template SmallVector AArgs; Context.getInjectedTemplateArgs(A, AArgs); + Context.getCanonicalTemplateArguments(AArgs, AArgs); // Check P's arguments against A's parameter list. This will fill in default // template arguments as needed. AArgs are already correct by construction. @@ -5630,6 +5645,7 @@ SFINAETrap Trap(*this); Context.getInjectedTemplateArgs(P, PArgs); + Context.getCanonicalTemplateArguments(PArgs, PArgs); TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); for (unsigned I = 0, N = P->size(); I != N; ++I) { @@ -5949,13 +5965,11 @@ // If the template argument list of P contains a pack expansion that is // not the last template argument, the entire template argument list is a // non-deduced context. - if (OnlyDeduced && - hasPackExpansionBeforeEnd(Spec->template_arguments())) + if (OnlyDeduced && hasPackExpansionBeforeEnd(Spec->getConvertedArguments())) break; - for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) - MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, - Used); + for (const TemplateArgument &Arg : Spec->getConvertedArguments()) + MarkUsedTemplateParameters(Ctx, Arg, OnlyDeduced, Depth, Used); break; } diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3762,8 +3762,9 @@ // Build the canonical type that describes the converted template // arguments of the class template explicit specialization. QualType CanonType = SemaRef.Context.getTemplateSpecializationType( - TemplateName(InstClassTemplate), CanonicalConverted, - SemaRef.Context.getRecordType(InstD)); + TemplateName(InstClassTemplate), + /*SpecifiedArgs=*/ArrayRef{}, CanonicalConverted, + /*CanonicalConverted=*/{}, SemaRef.Context.getRecordType(InstD)); // Build the fully-sugared type for this class template // specialization as the user wrote in the specialization @@ -3774,7 +3775,7 @@ // template arguments in the specialization. TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, - CanonType); + SugaredConverted, /*CanonicalConverted=*/{}, CanonType); InstD->setAccess(D->getAccess()); InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); @@ -4113,11 +4114,6 @@ ClassTemplate->findPartialSpecialization(CanonicalConverted, InstParams, InsertPos); - // Build the canonical type that describes the converted template - // arguments of the class template partial specialization. - QualType CanonType = SemaRef.Context.getTemplateSpecializationType( - TemplateName(ClassTemplate), CanonicalConverted); - // Build the fully-sugared type for this class template // specialization as the user wrote in the specialization // itself. This means that we'll pretty-print the type retrieved @@ -4125,12 +4121,9 @@ // actually wrote the specialization, rather than formatting the // name based on the "canonical" representation used to store the // template arguments in the specialization. - TypeSourceInfo *WrittenTy - = SemaRef.Context.getTemplateSpecializationTypeInfo( - TemplateName(ClassTemplate), - PartialSpec->getLocation(), - InstTemplateArgs, - CanonType); + TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( + TemplateName(ClassTemplate), PartialSpec->getLocation(), InstTemplateArgs, + SugaredConverted, CanonicalConverted); if (PrevDecl) { // We've already seen a partial specialization with the same template @@ -4161,8 +4154,8 @@ ClassTemplatePartialSpecializationDecl::Create( SemaRef.Context, PartialSpec->getTagKind(), Owner, PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, - ClassTemplate, CanonicalConverted, InstTemplateArgs, CanonType, - nullptr); + ClassTemplate, CanonicalConverted, InstTemplateArgs, + WrittenTy->getType(), nullptr); // Substitute the nested name specifier, if any. if (SubstQualifier(PartialSpec, InstPartialSpec)) return nullptr; @@ -4238,11 +4231,6 @@ VarTemplate->findPartialSpecialization(CanonicalConverted, InstParams, InsertPos); - // Build the canonical type that describes the converted template - // arguments of the variable template partial specialization. - QualType CanonType = SemaRef.Context.getTemplateSpecializationType( - TemplateName(VarTemplate), CanonicalConverted); - // Build the fully-sugared type for this variable template // specialization as the user wrote in the specialization // itself. This means that we'll pretty-print the type retrieved @@ -4252,7 +4240,7 @@ // template arguments in the specialization. TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, - CanonType); + SugaredConverted, CanonicalConverted); if (PrevDecl) { // We've already seen a partial specialization with the same template @@ -6175,7 +6163,8 @@ Args.addArgument( getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); } - QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); + QualType T = + CheckTemplateIdType(CXXScopeSpec(), TemplateName(TD), Loc, Args); if (T.isNull()) return nullptr; auto *SubstRecord = T->getAsCXXRecordDecl(); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -9138,6 +9138,56 @@ return Context.getTypeOfExprType(E); } +static QualType getSameReferencedType(ASTContext &Context, QualType VT, + QualType ET) { + assert(!ET->isReferenceType()); + if (const auto *VTL = VT->getAs()) + ET = Context.getLValueReferenceType(ET, VTL->isSpelledAsLValue()); + else if (VT->isRValueReferenceType()) + ET = Context.getRValueReferenceType(ET); + + if (!Context.hasSameUnqualifiedType(ET, VT)) { + ET.dump(); + VT.dump(); + assert(false && "!hasSameUnqualifiedType"); + } + + Qualifiers ToAdd = VT.getQualifiers(), ToRemove = ET.getQualifiers(); + (void)Qualifiers::removeCommonQualifiers(ToAdd, ToRemove); + + SplitQualType Split = ET.split(); + while (!ToRemove.empty()) { + (void)Qualifiers::removeCommonQualifiers(Split.Quals, ToRemove); + if (ToRemove.empty()) + break; + QualType Next; + switch (ET->getTypeClass()) { +#define ABSTRACT_TYPE(Class, Parent) +#define TYPE(Class, Parent) \ + case Type::Class: { \ + const auto *ty = cast(ET); \ + if (!ty->isSugared()) \ + goto done; \ + Next = ty->desugar(); \ + break; \ + } +#include "clang/AST/TypeNodes.inc" + } + Split = Next.split(); + } +done: + assert(ToRemove.empty()); + Split.Quals += ToAdd; + ET = Context.getQualifiedType(Split); + + if (!Context.hasSameType(ET, VT)) { + ET.dump(); + VT.dump(); + assert(false && "!hasSameType"); + } + return ET; +} + /// getDecltypeForExpr - Given an expr, will return the decltype for /// that expression, according to the rules in C++11 /// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18. @@ -9170,18 +9220,20 @@ // We apply the same rules for Objective-C ivar and property references. if (const auto *DRE = dyn_cast(IDExpr)) { const ValueDecl *VD = DRE->getDecl(); - QualType T = VD->getType(); + QualType T = getSameReferencedType(Context, VD->getType(), DRE->getType()); return isa(VD) ? T.getUnqualifiedType() : T; } if (const auto *ME = dyn_cast(IDExpr)) { if (const auto *VD = ME->getMemberDecl()) if (isa(VD) || isa(VD)) - return VD->getType(); + return getSameReferencedType(Context, VD->getType(), ME->getType()); } else if (const auto *IR = dyn_cast(IDExpr)) { + // FIXME: Sugar these. Breaks Modules/odr_hash.mm. return IR->getDecl()->getType(); } else if (const auto *PR = dyn_cast(IDExpr)) { if (PR->isExplicitProperty()) - return PR->getExplicitProperty()->getType(); + return getSameReferencedType( + Context, PR->getExplicitProperty()->getType(), PR->getType()); } else if (const auto *PE = dyn_cast(IDExpr)) { return PE->getType(); } diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -1009,7 +1009,8 @@ /// By default, performs semantic analysis when building the template /// specialization type. Subclasses may override this routine to provide /// different behavior. - QualType RebuildTemplateSpecializationType(TemplateName Template, + QualType RebuildTemplateSpecializationType(const CXXScopeSpec &SS, + TemplateName Template, SourceLocation TemplateLoc, TemplateArgumentListInfo &Args); @@ -1061,15 +1062,14 @@ // If it's still dependent, make a dependent specialization. if (InstName.getAsDependentTemplateName()) - return SemaRef.Context.getDependentTemplateSpecializationType(Keyword, - QualifierLoc.getNestedNameSpecifier(), - Name, - Args); + return SemaRef.Context.getDependentTemplateSpecializationType( + Keyword, QualifierLoc.getNestedNameSpecifier(), Name, + Args.arguments()); // Otherwise, make an elaborated type wrapping a non-dependent // specialization. - QualType T = - getDerived().RebuildTemplateSpecializationType(InstName, NameLoc, Args); + QualType T = getDerived().RebuildTemplateSpecializationType(SS, InstName, + NameLoc, Args); if (T.isNull()) return QualType(); return SemaRef.Context.getElaboratedType( @@ -2672,6 +2672,9 @@ NamedDecl *FirstQualifierInScope) { ExprResult BaseResult = getSema().PerformMemberExprBaseConversion(Base, isArrow); + CXXScopeSpec SS; + SS.Adopt(QualifierLoc); + if (!Member->getDeclName()) { // We have a reference to an unnamed field. This is always the // base of an anonymous struct/union member access, i.e. the @@ -2687,15 +2690,14 @@ return ExprError(); Base = BaseResult.get(); - CXXScopeSpec EmptySS; + // FIXME: resugar. return getSema().BuildFieldReferenceExpr( - Base, isArrow, OpLoc, EmptySS, cast(Member), - DeclAccessPair::make(FoundDecl, FoundDecl->getAccess()), MemberNameInfo); + Base, isArrow, OpLoc, NestedNameSpecifierLoc(), + cast(Member), Member->getType(), + DeclAccessPair::make(FoundDecl, FoundDecl->getAccess()), + MemberNameInfo); } - CXXScopeSpec SS; - SS.Adopt(QualifierLoc); - Base = BaseResult.get(); QualType BaseType = Base->getType(); @@ -4889,7 +4891,7 @@ return TL; TypeSourceInfo *TSI = - TransformTSIInObjectScope(TL, ObjectType, UnqualLookup, SS); + getDerived().TransformTSIInObjectScope(TL, ObjectType, UnqualLookup, SS); if (TSI) return TSI->getTypeLoc(); return TypeLoc(); @@ -4904,8 +4906,8 @@ if (getDerived().AlreadyTransformed(TSInfo->getType())) return TSInfo; - return TransformTSIInObjectScope(TSInfo->getTypeLoc(), ObjectType, - UnqualLookup, SS); + return getDerived().TransformTSIInObjectScope(TSInfo->getTypeLoc(), + ObjectType, UnqualLookup, SS); } template @@ -5509,7 +5511,8 @@ TypeLocBuilder &TLB, DependentAddressSpaceTypeLoc TL) { const DependentAddressSpaceType *T = TL.getTypePtr(); - QualType pointeeType = getDerived().TransformType(T->getPointeeType()); + QualType pointeeType = + getDerived().TransformType(TLB, TL.getPointeeTypeLoc()); if (pointeeType.isNull()) return QualType(); @@ -5542,9 +5545,8 @@ NewTL.setAttrNameLoc(TL.getAttrNameLoc()); } else { - TypeSourceInfo *DI = getSema().Context.getTrivialTypeSourceInfo( - Result, getDerived().getBaseLocation()); - TransformType(TLB, DI->getTypeLoc()); + // Result is just the pointee type with an extended qualifier added. + TLB.TypeWasModifiedSafely(Result); } return Result; @@ -6683,19 +6685,29 @@ TemplateArgumentListInfo NewTemplateArgs; NewTemplateArgs.setLAngleLoc(TL.getLAngleLoc()); NewTemplateArgs.setRAngleLoc(TL.getRAngleLoc()); - typedef TemplateArgumentLocContainerIterator - ArgIterator; - if (getDerived().TransformTemplateArguments(ArgIterator(TL, 0), - ArgIterator(TL, TL.getNumArgs()), - NewTemplateArgs)) - return QualType(); + + const auto *T = cast(TL.getType()); + if (T->isCanonicalUnqualified()) { + ArrayRef ConvertedArgs = T->getConvertedArguments(); + using ArgIterator = + TemplateArgumentLocInventIterator; + if (getDerived().TransformTemplateArguments( + ArgIterator(*this, ConvertedArgs.begin()), + ArgIterator(*this, ConvertedArgs.end()), NewTemplateArgs)) + return QualType(); + } else { + using ArgIterator = + TemplateArgumentLocContainerIterator; + if (getDerived().TransformTemplateArguments( + ArgIterator(TL, 0), ArgIterator(TL, TL.getNumArgs()), + NewTemplateArgs)) + return QualType(); + } // FIXME: maybe don't rebuild if all the template arguments are the same. - QualType Result = - getDerived().RebuildTemplateSpecializationType(Template, - TL.getTemplateNameLoc(), - NewTemplateArgs); + QualType Result = getDerived().RebuildTemplateSpecializationType( + CXXScopeSpec(), Template, TL.getTemplateNameLoc(), NewTemplateArgs); if (!Result.isNull()) { // Specializations of template template parameters are represented as @@ -6748,12 +6760,9 @@ // FIXME: maybe don't rebuild if all the template arguments are the same. if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { - QualType Result - = getSema().Context.getDependentTemplateSpecializationType( - TL.getTypePtr()->getKeyword(), - DTN->getQualifier(), - DTN->getIdentifier(), - NewTemplateArgs); + QualType Result = getSema().Context.getDependentTemplateSpecializationType( + TL.getTypePtr()->getKeyword(), DTN->getQualifier(), + DTN->getIdentifier(), NewTemplateArgs.arguments()); DependentTemplateSpecializationTypeLoc NewTL = TLB.push(Result); @@ -6768,10 +6777,8 @@ return Result; } - QualType Result - = getDerived().RebuildTemplateSpecializationType(Template, - TL.getTemplateNameLoc(), - NewTemplateArgs); + QualType Result = getDerived().RebuildTemplateSpecializationType( + SS, Template, TL.getTemplateNameLoc(), NewTemplateArgs); if (!Result.isNull()) { /// FIXME: Wrap this in an elaborated-type-specifier? @@ -11073,8 +11080,7 @@ getSema().FpPragmaStack.CurrentValue = NewOverrides; } - return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc, - Args, + return getDerived().RebuildCallExpr(Callee.get(), FakeLParenLoc, Args, E->getRParenLoc()); } @@ -14753,12 +14759,12 @@ return SemaRef.BuildUnaryTransformType(BaseType, UKind, Loc); } -template +template QualType TreeTransform::RebuildTemplateSpecializationType( - TemplateName Template, - SourceLocation TemplateNameLoc, - TemplateArgumentListInfo &TemplateArgs) { - return SemaRef.CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); + const CXXScopeSpec &SS, TemplateName Template, + SourceLocation TemplateNameLoc, TemplateArgumentListInfo &TemplateArgs) { + return SemaRef.CheckTemplateIdType(SS, Template, TemplateNameLoc, + TemplateArgs); } template diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -6740,9 +6740,8 @@ TL.setRAngleLoc(readSourceLocation()); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) TL.setArgLocInfo( - i, - Reader.readTemplateArgumentLocInfo( - TL.getTypePtr()->getArg(i).getKind())); + i, Reader.readTemplateArgumentLocInfo( + TL.getTypePtr()->getSpecifiedArguments()[i].getKind())); } void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { @@ -8848,6 +8847,15 @@ TemplArgs.push_back(readTemplateArgument(Canonicalize)); } +const TemplateArgumentList * +ASTRecordReader::readTemplateArgumentList(bool Canonicalize) { + SmallVector Args; + readTemplateArgumentList(Args, Canonicalize); + if (Args.size() == 0) + return nullptr; + return TemplateArgumentList::CreateCopy(getContext(), Args); +} + /// Read a UnresolvedSet structure. void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { unsigned NumDecls = readInt(); diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -1027,6 +1027,7 @@ E->MemberExprBits.HadMultipleCandidates = Record.readInt(); E->MemberExprBits.NonOdrUseReason = Record.readInt(); E->MemberExprBits.OperatorLoc = Record.readSourceLocation(); + E->Deduced = Record.readTemplateArgumentList(); if (HasQualifier || HasFoundDecl) { DeclAccessPair FoundDecl; diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -911,6 +911,10 @@ Record.push_back(E->hadMultipleCandidates()); Record.push_back(E->isNonOdrUse()); Record.AddSourceLocation(E->getOperatorLoc()); + if (E->Deduced) + Record.AddTemplateArgumentList(E->Deduced); + else + Record.push_back(0); if (HasFoundDecl) { DeclAccessPair FoundDecl = E->getFoundDecl(); diff --git a/clang/lib/StaticAnalyzer/Checkers/PointerSortingChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PointerSortingChecker.cpp --- a/clang/lib/StaticAnalyzer/Checkers/PointerSortingChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/PointerSortingChecker.cpp @@ -79,12 +79,9 @@ ); // Match only if the container has pointer-type elements. - auto IteratesPointerEltsM = hasArgument(0, - hasType(cxxRecordDecl(has( - fieldDecl(hasType(hasCanonicalType( - pointsTo(hasCanonicalType(pointerType())) - ))) - )))); + auto IteratesPointerEltsM = hasArgument( + 0, hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(has(fieldDecl( + hasType(hasCanonicalType(pointsTo(pointerType())))))))))); auto PointerSortM = traverse( TK_AsIs, diff --git a/clang/test/Analysis/cast-value-notes.cpp b/clang/test/Analysis/cast-value-notes.cpp --- a/clang/test/Analysis/cast-value-notes.cpp +++ b/clang/test/Analysis/cast-value-notes.cpp @@ -73,7 +73,7 @@ #if defined(X86) void evalReferences(const Shape &S) { const auto &C = dyn_cast(S); - // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}} + // expected-note@-1 {{Assuming 'S' is not a 'const Circle &'}} // expected-note@-2 {{Dereference of null pointer}} // expected-warning@-3 {{Dereference of null pointer}} clang_analyzer_printState(); @@ -86,26 +86,26 @@ const auto &C = dyn_cast(S); clang_analyzer_printState(); // X86-CHECK-SUPPRESSED: "dynamic_types": [ - // X86-CHECK-SUPPRESSED-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true } + // X86-CHECK-SUPPRESSED-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "DEVICE Circle const &", "sub_classable": true } (void)C; } #endif #if defined(NOT_SUPPRESSED) void evalReferences_addrspace(const Shape &S) { const auto &C = dyn_cast(S); - // expected-note@-1 {{Assuming 'S' is not a 'const __attribute__((address_space(3))) class clang::Circle &'}} + // expected-note@-1 {{Assuming 'S' is not a 'DEVICE Circle const &'}} // expected-note@-2 {{Dereference of null pointer}} // expected-warning@-3 {{Dereference of null pointer}} clang_analyzer_printState(); // X86-CHECK: "dynamic_types": [ - // X86-CHECK-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "const __attribute__((address_space(3))) class clang::Circle &", "sub_classable": true } + // X86-CHECK-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "DEVICE Circle const &", "sub_classable": true } (void)C; } #endif #elif defined(MIPS) void evalReferences(const Shape &S) { const auto &C = dyn_cast(S); - // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle &'}} + // expected-note@-1 {{Assuming 'S' is not a 'const Circle &'}} // expected-note@-2 {{Dereference of null pointer}} // expected-warning@-3 {{Dereference of null pointer}} } @@ -122,25 +122,25 @@ // expected-note@-1 {{'C' initialized here}} if (!dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}} + // expected-note@-1 {{Assuming 'C' is a 'const Circle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Rectangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Hexagon *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -176,29 +176,29 @@ void evalNonNullParamNonNullReturn(const Shape *S) { const auto *C = cast(S); - // expected-note@-1 {{'S' is a 'const class clang::Circle *'}} + // expected-note@-1 {{'S' is a 'const Circle *'}} // expected-note@-2 {{'C' initialized here}} if (!dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is a 'const class clang::Circle *'}} + // expected-note@-1 {{Assuming 'C' is a 'const Circle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Triangle *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Rectangle *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Rectangle *'}} // expected-note@-2 {{Taking false branch}} return; } if (dyn_cast_or_null(C)) { - // expected-note@-1 {{Assuming 'C' is not a 'const class clang::Hexagon *'}} + // expected-note@-1 {{Assuming 'C' is not a 'const Hexagon *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -234,10 +234,10 @@ void evalNonNullParamNullReturn(const Shape *S) { const auto *C = dyn_cast_or_null(S); - // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}} + // expected-note@-1 {{Assuming 'S' is not a 'const Circle *'}} if (const auto *T = dyn_cast_or_null(S)) { - // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}} + // expected-note@-1 {{Assuming 'S' is a 'const Triangle *'}} // expected-note@-2 {{'T' initialized here}} // expected-note@-3 {{'T' is non-null}} // expected-note@-4 {{Taking true branch}} @@ -261,7 +261,7 @@ void evalZeroParamNonNullReturnPointer(const Shape *S) { const auto *C = S->castAs(); - // expected-note@-1 {{'S' is a 'const class clang::Circle *'}} + // expected-note@-1 {{'S' is a 'const Circle *'}} // expected-note@-2 {{'C' initialized here}} (void)(1 / !C); @@ -282,12 +282,12 @@ void evalZeroParamNullReturn(const Shape *S) { const auto &C = S->getAs(); - // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Circle *'}} + // expected-note@-1 {{Assuming 'S' is not a 'const Circle *'}} // expected-note@-2 {{Storing null pointer value}} // expected-note@-3 {{'C' initialized here}} if (!dyn_cast_or_null(S)) { - // expected-note@-1 {{Assuming 'S' is a 'const class clang::Triangle *'}} + // expected-note@-1 {{Assuming 'S' is a 'const Triangle *'}} // expected-note@-2 {{Taking false branch}} return; } diff --git a/clang/test/Analysis/cast-value-state-dump.cpp b/clang/test/Analysis/cast-value-state-dump.cpp --- a/clang/test/Analysis/cast-value-state-dump.cpp +++ b/clang/test/Analysis/cast-value-state-dump.cpp @@ -18,12 +18,12 @@ void evalNonNullParamNonNullReturn(const Shape *S) { const auto *C = dyn_cast_or_null(S); - // expected-note@-1 {{Assuming 'S' is a 'const class clang::Circle *'}} + // expected-note@-1 {{Assuming 'S' is a 'const Circle *'}} // expected-note@-2 {{'C' initialized here}} // FIXME: We assumed that 'S' is a 'Circle' therefore it is not a 'Square'. if (dyn_cast_or_null(S)) { - // expected-note@-1 {{Assuming 'S' is not a 'const class clang::Square *'}} + // expected-note@-1 {{Assuming 'S' is not a 'const Square *'}} // expected-note@-2 {{Taking false branch}} return; } @@ -31,7 +31,7 @@ clang_analyzer_printState(); // CHECK: "dynamic_types": [ - // CHECK-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "const class clang::Circle", "sub_classable": true } + // CHECK-NEXT: { "region": "SymRegion{reg_$0}", "dyn_type": "const Circle", "sub_classable": true } // CHECK-NEXT: ], // CHECK-NEXT: "dynamic_casts": [ // CHECK: { "region": "SymRegion{reg_$0}", "casts": [ diff --git a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp --- a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp +++ b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp @@ -38,8 +38,8 @@ void b1(struct B); void b2(class B); -void b3(union B); // expected-error {{use of 'B' with tag type that does not match previous declaration}} -//void b4(enum B); // this just doesn't parse; you can't template an enum directly +void b3(union B); // expected-error {{use of 'union B' with tag type that does not match previous declaration}} +// void b4(enum B); // this just doesn't parse; you can't template an enum directly void c1(struct B::Member); void c2(class B::Member); diff --git a/clang/test/CXX/temp/temp.decls/temp.class.spec/p6.cpp b/clang/test/CXX/temp/temp.decls/temp.class.spec/p6.cpp --- a/clang/test/CXX/temp/temp.decls/temp.class.spec/p6.cpp +++ b/clang/test/CXX/temp/temp.decls/temp.class.spec/p6.cpp @@ -38,7 +38,7 @@ template struct Outer { template struct Inner; - template struct Inner {}; // expected-note{{previous declaration of class template partial specialization 'Inner' is here}} + template struct Inner {}; // expected-note{{previous declaration of class template partial specialization 'Inner' is here}} template struct Inner {}; // expected-error{{cannot be redeclared}} }; @@ -80,7 +80,7 @@ template struct Foo { template struct Bar; template struct Bar<0, T, Y> {}; - // expected-note-re@-1 {{previous declaration {{.*}} 'Bar<0, int, type-parameter-0-0>' is here}} + // expected-note-re@-1 {{previous declaration {{.*}} 'Bar<0, int, Y>' is here}} template struct Bar<0, U, Y> {}; // expected-error@-1 {{partial specialization 'Bar<0, int, Y>' cannot be redeclared}} }; diff --git a/clang/test/CXX/temp/temp.deduct.guide/p3.cpp b/clang/test/CXX/temp/temp.deduct.guide/p3.cpp --- a/clang/test/CXX/temp/temp.deduct.guide/p3.cpp +++ b/clang/test/CXX/temp/temp.deduct.guide/p3.cpp @@ -33,7 +33,7 @@ }; A(int) -> int; // expected-error {{deduced type 'int' of deduction guide is not a specialization of template 'A'}} -template A(T) -> B; // expected-error {{deduced type 'B' (aka 'A') of deduction guide is not written as a specialization of template 'A'}} +template A(T)->B; // expected-error {{deduced type 'B' (aka 'A') of deduction guide is not written as a specialization of template 'A'}} template A(T*) -> const A; // expected-error {{deduced type 'const A' of deduction guide is not a specialization of template 'A'}} // A deduction-guide shall be declared in the same scope as the corresponding diff --git a/clang/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp b/clang/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp --- a/clang/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp +++ b/clang/test/CodeGenCXX/mangle-ms-back-references-pr13207.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 | FileCheck %s +// RUN: %clang_cc1 -fno-resugar -emit-llvm %s -o - -triple=i386-pc-win32 | FileCheck %s template class A {}; @@ -220,6 +220,7 @@ class T20> struct Food {}; +// FIXME: Should implement resugaring cache for this. using B0 = Food; using B1 = Food; using B2 = Food; diff --git a/clang/test/CodeGenCXX/pr29160.cpp b/clang/test/CodeGenCXX/pr29160.cpp --- a/clang/test/CodeGenCXX/pr29160.cpp +++ b/clang/test/CodeGenCXX/pr29160.cpp @@ -1,8 +1,9 @@ -// RUN: %clang_cc1 -std=c++11 -triple i686-linux-gnu %s -o /dev/null -S -emit-llvm +// RUN: %clang_cc1 -std=c++11 -fno-resugar -triple i686-linux-gnu %s -o /dev/null -S -emit-llvm // // This test's failure mode is running ~forever. (For some value of "forever" // that's greater than 25 minutes on my machine) +// FIXME: Should implement resugaring cache for this. template struct Foo { template diff --git a/clang/test/Misc/diag-template-diffing.cpp b/clang/test/Misc/diag-template-diffing.cpp --- a/clang/test/Misc/diag-template-diffing.cpp +++ b/clang/test/Misc/diag-template-diffing.cpp @@ -257,24 +257,21 @@ int k9 = f9(V9()); // CHECK-ELIDE-NOTREE: no matching function for call to 'f9' -// CHECK-ELIDE-NOTREE: candidate function not viable: no known conversion from 'S9<[2 * ...], S9<[2 * ...], double>>' to 'S9<[2 * ...], S9<[2 * ...], const double>>' for 1st argument +// CHECK-ELIDE-NOTREE: candidate function not viable: no known conversion from 'S9<[2 * ...], U9>' to 'S9<[2 * ...], U9>' for 1st argument // CHECK-NOELIDE-NOTREE: no matching function for call to 'f9' -// CHECK-NOELIDE-NOTREE: candidate function not viable: no known conversion from 'S9>' to 'S9>' for 1st argument +// CHECK-NOELIDE-NOTREE: candidate function not viable: no known conversion from 'S9>' to 'S9>' for 1st argument // CHECK-ELIDE-TREE: no matching function for call to 'f9' // CHECK-ELIDE-TREE: candidate function not viable: no known conversion from argument type to parameter type for 1st argument // CHECK-ELIDE-TREE: S9< -// CHECK-ELIDE-TREE: [2 * ...], -// CHECK-ELIDE-TREE: S9< -// CHECK-ELIDE-TREE: [2 * ...], +// CHECK-ELIDE-TREE: [2 * ...], +// CHECK-ELIDE-TREE: U9< // CHECK-ELIDE-TREE: [double != const double]>> // CHECK-NOELIDE-TREE: no matching function for call to 'f9' // CHECK-NOELIDE-TREE: candidate function not viable: no known conversion from argument type to parameter type for 1st argument // CHECK-NOELIDE-TREE: S9< -// CHECK-NOELIDE-TREE: int, -// CHECK-NOELIDE-TREE: char, -// CHECK-NOELIDE-TREE: S9< -// CHECK-NOELIDE-TREE: int, -// CHECK-NOELIDE-TREE: char, +// CHECK-NOELIDE-TREE: int, +// CHECK-NOELIDE-TREE: char, +// CHECK-NOELIDE-TREE: U9< // CHECK-NOELIDE-TREE: [double != const double]>> template class class_types {}; @@ -1172,7 +1169,7 @@ // CHECK-ELIDE-NOTREE: no viable conversion from 'Wrapper>' to 'Wrapper>' Wrapper> W13 = MakeWrapper>(); -// CHECK-ELIDE-NOTREE: no viable conversion from 'Wrapper>' to 'Wrapper>' +// CHECK-ELIDE-NOTREE: no viable conversion from 'Wrapper>' to 'Wrapper>' Wrapper> W14 = MakeWrapper>(); // CHECK-ELIDE-NOTREE: no viable conversion from 'Wrapper>' to 'Wrapper>' } @@ -1384,8 +1381,8 @@ template struct A {}; template > R bar(); A<> &foo() { return bar(); } -// CHECK-ELIDE-NOTREE: error: non-const lvalue reference to type 'A<...>' cannot bind to a temporary of type 'A<...>' -// CHECK-NOELIDE-NOTREE: error: non-const lvalue reference to type 'A' cannot bind to a temporary of type 'A' +// CHECK-ELIDE-NOTREE: error: non-const lvalue reference to type 'A<>' cannot bind to a temporary of type 'A<>' +// CHECK-NOELIDE-NOTREE: error: non-const lvalue reference to type 'A<>' cannot bind to a temporary of type 'A<>' } namespace PR24587 { diff --git a/clang/test/Sema/Resugar/resugar-expr.cpp b/clang/test/Sema/Resugar/resugar-expr.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Sema/Resugar/resugar-expr.cpp @@ -0,0 +1,244 @@ +// RUN: %clang_cc1 -std=c++2b -fsyntax-only -verify=Y %s +// RUN: %clang_cc1 -std=c++2b -fsyntax-only -fno-resugar -verify=N %s + +enum class Z; + +struct bar {}; + +using Int = int; +using Float = float; +using Bar = bar; + +namespace t1 { +template struct A { + static constexpr A1 a = {}; +}; + +Z x1 = A::a; +// Y-error@-1 {{with an lvalue of type 'const Int' (aka 'const int')}} +// N-error@-2 {{with an lvalue of type 'const int'}} +} // namespace t1 + +namespace t2 { +template struct A { + static constexpr A1 A2::*a = {}; +}; + +Z x1 = A::a; +// Y-error@-1 {{with an lvalue of type 'Int Bar::*const'}} +// N-error@-2 {{with an lvalue of type 'int bar::*const'}} +} // namespace t2 + +namespace t3 { +template struct A { + template struct B { + static constexpr A1 B1::*a = {}; + }; +}; + +Z x1 = A::B::a; +// Y-error@-1 {{with an lvalue of type 'Float Bar::*const'}} +// N-error@-2 {{with an lvalue of type 'float bar::*const'}} +} // namespace t3 + +namespace t4 { +template A1 (*a) +(); + +Z x1 = decltype(a){}(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t4 + +namespace t5 { +template struct A { + A1(*a) + (); +}; + +Z x1 = decltype(A().a){}(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t5 + +namespace t6 { +template struct A { A2 A1::*f(); }; + +using M = int; +using N = int; + +struct B {}; +using X = B; +using Y = B; + +auto a = &A::f; +Z x1 = a; +// Y-error@-1 {{with an lvalue of type 'M X::*(A::*)()'}} +// N-error@-2 {{with an lvalue of type 'int t6::B::*(A::*)()'}} + +A b; +Z x2 = (b.*a)(); +// Y-error@-1 {{with an rvalue of type 'M X::*'}} +// N-error@-2 {{with an rvalue of type 'int t6::B::*'}} + +Z x3 = decltype((b.*a)()){}; +// Y-error@-1 {{with an rvalue of type 'decltype((b .* a)())' (aka 'M X::*')}} +// N-error@-2 {{with an rvalue of type 'decltype((b .* a)())' (aka 'int t6::B::*')}} +} // namespace t6 + +namespace t7 { +template struct A { A1 a; }; +auto [a] = A{}; + +Z x1 = a; +// Y-error@-1 {{with an lvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an lvalue of type 'int'}} +} // namespace t7 + +namespace t8 { +template struct A { + template static constexpr B1 (*b)(A1) = nullptr; +}; + +Z x1 = A::b; +// Y-error@-1 {{with an lvalue of type 'Int (*const)(Float)' (aka 'int (*const)(float)')}} +// N-error@-2 {{with an lvalue of type 'int (*const)(float)'}} +} // namespace t8 + +namespace t9 { +template struct A { + template static constexpr auto b = (B1(*)(A1)){}; +}; + +Z x1 = A::b; +// Y-error@-1 {{with an lvalue of type 'Int (*const)(Float)' (aka 'int (*const)(float)'}} +// N-error@-2 {{with an lvalue of type 'int (*const)(float)'}} +} // namespace t9 + +namespace t10 { +template struct A { + template static constexpr A1 (*m)(B1) = nullptr; +}; + +Z x1 = A().template m; +// Y-error@-1 {{with an lvalue of type 'Int (*const)(Float)' (aka 'int (*const)(float)'}} +// N-error@-2 {{with an lvalue of type 'int (*const)(float)'}} +} // namespace t10 + +namespace t11 { +template A1 a; +template A2 a; + +Z x1 = a; +// Y-error@-1 {{with an lvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an lvalue of type 'int'}} + +Z x2 = a; +// Y-error@-1 {{with an lvalue of type 'Float' (aka 'float'}} +// N-error@-2 {{with an lvalue of type 'float'}} +} // namespace t11 + +namespace t12 { +template struct A { A1 foo(); }; + +Z x1 = A().foo(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t12 + +namespace t13 { +template struct A { + auto foo() { return A1(); }; +}; + +Z x1 = A().foo(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t13 + +namespace t14 { +template struct A { + template auto foo1() -> A1 (*)(B1); + template auto foo2(B1) -> A1 (*)(B1); +}; + +Z x1 = A().foo1(); +// Y-error@-1 {{with an rvalue of type 'Int (*)(Float)' (aka 'int (*)(float)'}} +// N-error@-2 {{with an rvalue of type 'int (*)(float)'}} + +Z x2 = A().foo2(Float()); +// Y-error@-1 {{with an rvalue of type 'Int (*)(Float)' (aka 'int (*)(float)'}} +// N-error@-2 {{with an rvalue of type 'int (*)(float)'}} +} // namespace t14 + +namespace t15 { +template struct A { + static auto foo() -> A1; +}; + +Z x1 = A().foo(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t15 + +namespace t16 { +template static auto foo() -> A1; + +Z x1 = foo(); +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t16 + +namespace t17 { +template static auto foo(A1) -> A1*; + +Z x1 = foo(Int()); +// Y-error@-1 {{with an rvalue of type 'Int *' (aka 'int *')}} +// N-error@-2 {{with an rvalue of type 'int *'}} +} // namespace t16 + +namespace t18 { +template struct A { + template static auto foo() -> A1 (*)(B1); +}; + +Z x1 = A().template foo(); +// Y-error@-1 {{with an rvalue of type 'Int (*)(Float)' (aka 'int (*)(float)'}} +// N-error@-2 {{with an rvalue of type 'int (*)(float)'}} + +Z x2 = A::template foo(); +// Y-error@-1 {{with an rvalue of type 'Int (*)(Float)' (aka 'int (*)(float)'}} +// N-error@-2 {{with an rvalue of type 'int (*)(float)'}} +} // namespace t18 + +namespace t19 { +template struct A { + A1 m; +}; + +Z x1 = A().m; +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t19 + +namespace t20 { +template struct A { + static A1 m; +}; + +Z x1 = A().m; +// Y-error@-1 {{with an lvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an lvalue of type 'int'}} +} // namespace t20 + +namespace t21 { +template struct A { + struct { + A1 m; + }; +}; + +Z x1 = A().m; +// Y-error@-1 {{with an rvalue of type 'Int' (aka 'int')}} +// N-error@-2 {{with an rvalue of type 'int'}} +} // namespace t21 diff --git a/clang/test/Sema/Resugar/resugar-types.cpp b/clang/test/Sema/Resugar/resugar-types.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Sema/Resugar/resugar-types.cpp @@ -0,0 +1,203 @@ +// RUN: %clang_cc1 -std=c++2b -verify %s +// expected-no-diagnostics + +static constexpr int alignment = 64; // Suitable large alignment. + +struct Baz {}; +using Bar [[gnu::aligned(alignment)]] = Baz; +using Int [[gnu::aligned(alignment)]] = int; + +#define TEST(X) static_assert(alignof(X) == alignment) +#define TEST_NOT(X) static_assert(alignof(X) != alignment) + +// Sanity checks. +TEST_NOT(Baz); +TEST(Bar); + +namespace t1 { +template struct foo { using type = T; }; +template struct foo { using type = U; }; + +TEST(typename foo::type); +TEST(typename foo::type); +} // namespace t1 + +namespace t2 { +template struct foo1 { using type = T; }; +template struct foo2 { using type = typename foo1<1, T>::type; }; +TEST(typename foo2::type); +} // namespace t2 + +namespace t3 { +template struct foo1 { + template struct foo2 { using type1 = T; }; + using type2 = typename foo2<1, int>::type1; +}; +TEST(typename foo1::type2); +} // namespace t3 + +namespace t4 { +template struct foo { + template using type1 = T; + using type2 = type1; +}; +TEST(typename foo::type2); +} // namespace t4 + +namespace t5 { +template struct foo { + template using type1 = U; + using type2 = type1<1, T>; +}; +TEST(typename foo::type2); +} // namespace t5 + +namespace t6 { +template struct foo1 { + template struct foo2 { using type = U; }; + using type2 = typename foo2<1, T>::type; +}; +TEST(typename foo1::type2); +}; // namespace t6 + +namespace t7 { +template struct foo { + template using type1 = U; +}; +using type2 = typename foo::template type1<1, Bar>; +TEST(type2); +} // namespace t7 + +namespace t8 { +template struct foo { + using type1 = T; +}; +template using type2 = T; +using type3 = typename type2, int>::type1; +TEST(type3); +} // namespace t8 + +namespace t9 { +template struct Y { + using type1 = A; + using type2 = B; +}; +template using Z = Y; +template struct foo { + template using apply = Z; +}; +using T1 = foo::apply; +TEST_NOT(T1::type1); +TEST(T1::type2); + +using T2 = foo::apply; +TEST(T2::type1); +TEST_NOT(T2::type2); +} // namespace t9 + +namespace t10 { +template struct Y { + using type1 = A1; + using type2 = A2; +}; +template using Z = Y; +template struct foo { + template using bind = Z; +}; +using T1 = foo::bind; +TEST_NOT(T1::type1); +TEST(T1::type2); + +using T2 = foo::bind; +TEST(T2::type1); +TEST_NOT(T2::type2); +} // namespace t10 + +namespace t11 { +template struct A { using type1 = A2; }; +TEST_NOT(A::type1); // FIXME +} // namespace t11 + +namespace t12 { +template struct W { + template class TT> + struct X { + using type1 = TT; + }; +}; + +template struct Y { + using type2 = Y2; + using type3 = Y3; +}; + +using T1 = typename W::X::type1; +TEST_NOT(typename T1::type2); // FIXME +TEST(typename T1::type3); +} // namespace t12 + +namespace t13 { +template