@@ -750,8 +750,52 @@ TEST(ScopeReflectionTest, InstantiateNNTPClassTemplate) {
750
750
ASTContext &C = Interp->getCI ()->getASTContext ();
751
751
Cpp::TCppType_t IntTy = C.IntTy .getAsOpaquePtr ();
752
752
std::vector<Cpp::TemplateArgInfo> args1 = {{IntTy, " 5" }};
753
- EXPECT_TRUE (Cpp::InstantiateClassTemplate (Decls[0 ], args1.data (),
754
- /* type_size*/ args1.size ()));
753
+ EXPECT_TRUE (Cpp::InstantiateTemplate (Decls[0 ], args1.data (),
754
+ /* type_size*/ args1.size ()));
755
+ }
756
+
757
+ TEST (ScopeReflectionTest, InstantiateVarTemplate) {
758
+ std::vector<Decl*> Decls;
759
+ std::string code = R"(
760
+ template<class T> constexpr T pi = T(3.1415926535897932385L);
761
+ )" ;
762
+
763
+ GetAllTopLevelDecls (code, Decls);
764
+ ASTContext& C = Interp->getCI ()->getASTContext ();
765
+
766
+ std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy .getAsOpaquePtr ()};
767
+ auto Instance1 = Cpp::InstantiateTemplate (Decls[0 ], args1.data (),
768
+ /* type_size*/ args1.size ());
769
+ EXPECT_TRUE (isa<VarDecl>((Decl*)Instance1));
770
+ auto * VD = cast<VarTemplateSpecializationDecl>((Decl*)Instance1);
771
+ VarTemplateDecl* VDTD1 = VD->getSpecializedTemplate ();
772
+ EXPECT_TRUE (VDTD1->isThisDeclarationADefinition ());
773
+ #if CLANG_VERSION_MAJOR > 13
774
+ TemplateArgument TA1 = (*VD->getTemplateArgsInfo ())[0 ].getArgument ();
775
+ #else
776
+ TemplateArgument TA1 = VD->getTemplateArgsInfo ()[0 ].getArgument ();
777
+ #endif // CLANG_VERSION_MAJOR
778
+ EXPECT_TRUE (TA1.getAsType ()->isIntegerType ());
779
+ }
780
+
781
+ TEST (ScopeReflectionTest, InstantiateFunctionTemplate) {
782
+ std::vector<Decl*> Decls;
783
+ std::string code = R"(
784
+ template<typename T> T TrivialFnTemplate() { return T(); }
785
+ )" ;
786
+
787
+ GetAllTopLevelDecls (code, Decls);
788
+ ASTContext& C = Interp->getCI ()->getASTContext ();
789
+
790
+ std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy .getAsOpaquePtr ()};
791
+ auto Instance1 = Cpp::InstantiateTemplate (Decls[0 ], args1.data (),
792
+ /* type_size*/ args1.size ());
793
+ EXPECT_TRUE (isa<FunctionDecl>((Decl*)Instance1));
794
+ FunctionDecl* FD = cast<FunctionDecl>((Decl*)Instance1);
795
+ FunctionDecl* FnTD1 = FD->getTemplateInstantiationPattern ();
796
+ EXPECT_TRUE (FnTD1->isThisDeclarationADefinition ());
797
+ TemplateArgument TA1 = FD->getTemplateSpecializationArgs ()->get (0 );
798
+ EXPECT_TRUE (TA1.getAsType ()->isIntegerType ());
755
799
}
756
800
757
801
TEST (ScopeReflectionTest, InstantiateTemplateFunctionFromString) {
@@ -807,29 +851,26 @@ TEST(ScopeReflectionTest, InstantiateClassTemplate) {
807
851
ASTContext &C = Interp->getCI ()->getASTContext ();
808
852
809
853
std::vector<Cpp::TemplateArgInfo> args1 = {C.IntTy .getAsOpaquePtr ()};
810
- auto Instance1 = Cpp::InstantiateClassTemplate (Decls[0 ],
811
- args1.data (),
812
- /* type_size*/ args1.size ());
854
+ auto Instance1 = Cpp::InstantiateTemplate (Decls[0 ], args1.data (),
855
+ /* type_size*/ args1.size ());
813
856
EXPECT_TRUE (isa<ClassTemplateSpecializationDecl>((Decl*)Instance1));
814
857
auto *CTSD1 = static_cast <ClassTemplateSpecializationDecl*>(Instance1);
815
858
EXPECT_TRUE (CTSD1->hasDefinition ());
816
859
TemplateArgument TA1 = CTSD1->getTemplateArgs ().get (0 );
817
860
EXPECT_TRUE (TA1.getAsType ()->isIntegerType ());
818
861
EXPECT_TRUE (CTSD1->hasDefinition ());
819
862
820
- auto Instance2 = Cpp::InstantiateClassTemplate (Decls[1 ],
821
- nullptr ,
822
- /* type_size*/ 0 );
863
+ auto Instance2 = Cpp::InstantiateTemplate (Decls[1 ], nullptr ,
864
+ /* type_size*/ 0 );
823
865
EXPECT_TRUE (isa<ClassTemplateSpecializationDecl>((Decl*)Instance2));
824
866
auto *CTSD2 = static_cast <ClassTemplateSpecializationDecl*>(Instance2);
825
867
EXPECT_TRUE (CTSD2->hasDefinition ());
826
868
TemplateArgument TA2 = CTSD2->getTemplateArgs ().get (0 );
827
869
EXPECT_TRUE (TA2.getAsType ()->isIntegerType ());
828
870
829
871
std::vector<Cpp::TemplateArgInfo> args3 = {C.IntTy .getAsOpaquePtr ()};
830
- auto Instance3 = Cpp::InstantiateClassTemplate (Decls[2 ],
831
- args3.data (),
832
- /* type_size*/ args3.size ());
872
+ auto Instance3 = Cpp::InstantiateTemplate (Decls[2 ], args3.data (),
873
+ /* type_size*/ args3.size ());
833
874
EXPECT_TRUE (isa<ClassTemplateSpecializationDecl>((Decl*)Instance3));
834
875
auto *CTSD3 = static_cast <ClassTemplateSpecializationDecl*>(Instance3);
835
876
EXPECT_TRUE (CTSD3->hasDefinition ());
@@ -844,9 +885,8 @@ TEST(ScopeReflectionTest, InstantiateClassTemplate) {
844
885
845
886
std::vector<Cpp::TemplateArgInfo> args4 = {C.IntTy .getAsOpaquePtr (),
846
887
{C.IntTy .getAsOpaquePtr (), " 3" }};
847
- auto Instance4 = Cpp::InstantiateClassTemplate (Decls[3 ],
848
- args4.data (),
849
- /* type_size*/ args4.size ());
888
+ auto Instance4 = Cpp::InstantiateTemplate (Decls[3 ], args4.data (),
889
+ /* type_size*/ args4.size ());
850
890
851
891
EXPECT_TRUE (isa<ClassTemplateSpecializationDecl>((Decl*)Instance4));
852
892
auto *CTSD4 = static_cast <ClassTemplateSpecializationDecl*>(Instance4);
@@ -901,4 +941,4 @@ TEST(ScopeReflectionTest, IncludeVector) {
901
941
#include <iostream>
902
942
)" ;
903
943
Interp->process (code);
904
- }
944
+ }
0 commit comments