37
37
#include " clang/Sema/SemaInternal.h"
38
38
#include " clang/Sema/Template.h"
39
39
#include " clang/Sema/TemplateDeduction.h"
40
+ #include " llvm/ADT/BitVector.h"
40
41
#include " llvm/ADT/SmallBitVector.h"
41
42
#include " llvm/ADT/SmallString.h"
42
43
#include " llvm/ADT/StringExtras.h"
@@ -2751,6 +2752,26 @@ struct ConvertConstructorToDeductionGuideTransform {
2751
2752
}
2752
2753
};
2753
2754
2755
+ unsigned getTemplateParameterDepth (NamedDecl *TemplateParam) {
2756
+ if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
2757
+ return TTP->getDepth ();
2758
+ if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
2759
+ return TTP->getDepth ();
2760
+ if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
2761
+ return NTTP->getDepth ();
2762
+ llvm_unreachable (" Unhandled template parameter types" );
2763
+ }
2764
+
2765
+ unsigned getTemplateParameterIndex (NamedDecl *TemplateParam) {
2766
+ if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
2767
+ return TTP->getIndex ();
2768
+ if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
2769
+ return TTP->getIndex ();
2770
+ if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
2771
+ return NTTP->getIndex ();
2772
+ llvm_unreachable (" Unhandled template parameter types" );
2773
+ }
2774
+
2754
2775
// Find all template parameters that appear in the given DeducedArgs.
2755
2776
// Return the indices of the template parameters in the TemplateParams.
2756
2777
SmallVector<unsigned > TemplateParamsReferencedInTemplateArgumentList (
@@ -2759,26 +2780,17 @@ SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
2759
2780
struct TemplateParamsReferencedFinder
2760
2781
: public RecursiveASTVisitor<TemplateParamsReferencedFinder> {
2761
2782
const TemplateParameterList* TemplateParamList;
2762
- llvm::DenseSet<NamedDecl *> TemplateParams;
2763
- llvm::DenseSet<const NamedDecl *> ReferencedTemplateParams;
2783
+ llvm::BitVector ReferencedTemplateParams;
2764
2784
2765
2785
TemplateParamsReferencedFinder (
2766
2786
const TemplateParameterList *TemplateParamList)
2767
2787
: TemplateParamList(TemplateParamList),
2768
- TemplateParams (TemplateParamList->begin (), TemplateParamList->end()) {
2769
- }
2788
+ ReferencedTemplateParams (TemplateParamList->size ()) {}
2770
2789
2771
2790
bool VisitTemplateTypeParmType (TemplateTypeParmType *TTP) {
2772
2791
// We use the index and depth to retrieve the corresponding template
2773
- // parameter from the parameter list.
2774
- // Note that Clang may not preserve type sugar during template argument
2775
- // deduction. In such cases, the TTP is a canonical TemplateTypeParamType,
2776
- // which only retains its index and depth information.
2777
- if (TTP->getDepth () == TemplateParamList->getDepth () &&
2778
- TTP->getIndex () < TemplateParamList->size ()) {
2779
- ReferencedTemplateParams.insert (
2780
- TemplateParamList->getParam (TTP->getIndex ()));
2781
- }
2792
+ // parameter from the parameter list, which is more robost.
2793
+ Mark (TTP->getDepth (), TTP->getIndex ());
2782
2794
return true ;
2783
2795
}
2784
2796
@@ -2794,17 +2806,22 @@ SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
2794
2806
}
2795
2807
2796
2808
void MarkAppeared (NamedDecl *ND) {
2797
- if (TemplateParams.contains (ND))
2798
- ReferencedTemplateParams.insert (ND);
2809
+ if (llvm::isa<NonTypeTemplateParmDecl, TemplateTypeParmDecl,
2810
+ TemplateTemplateParmDecl>(ND))
2811
+ Mark (getTemplateParameterDepth (ND), getTemplateParameterIndex (ND));
2812
+ }
2813
+ void Mark (unsigned Depth, unsigned Index) {
2814
+ if (Index < TemplateParamList->size () &&
2815
+ TemplateParamList->getParam (Index)->getTemplateDepth () == Depth)
2816
+ ReferencedTemplateParams.set (Index);
2799
2817
}
2800
2818
};
2801
2819
TemplateParamsReferencedFinder Finder (TemplateParamsList);
2802
2820
Finder.TraverseTemplateArguments(DeducedArgs);
2803
2821
2804
2822
SmallVector<unsigned > Results;
2805
2823
for (unsigned Index = 0 ; Index < TemplateParamsList->size (); ++Index) {
2806
- if (Finder.ReferencedTemplateParams .contains (
2807
- TemplateParamsList->getParam (Index)))
2824
+ if (Finder.ReferencedTemplateParams [Index])
2808
2825
Results.push_back (Index);
2809
2826
}
2810
2827
return Results;
@@ -2823,16 +2840,6 @@ bool hasDeclaredDeductionGuides(DeclarationName Name, DeclContext *DC) {
2823
2840
return false ;
2824
2841
}
2825
2842
2826
- unsigned getTemplateParameterDepth (NamedDecl *TemplateParam) {
2827
- if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam))
2828
- return TTP->getDepth ();
2829
- if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam))
2830
- return TTP->getDepth ();
2831
- if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(TemplateParam))
2832
- return NTTP->getDepth ();
2833
- llvm_unreachable (" Unhandled template parameter types" );
2834
- }
2835
-
2836
2843
NamedDecl *transformTemplateParameter (Sema &SemaRef, DeclContext *DC,
2837
2844
NamedDecl *TemplateParam,
2838
2845
MultiLevelTemplateArgumentList &Args,
0 commit comments