Skip to content

Commit 3ce31ae

Browse files
committed
Add a capability of class template instantiation arguments.
1 parent 4f01613 commit 3ce31ae

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ namespace Cpp {
459459
std::string ObjToString(const char *type, void *obj);
460460

461461
struct TemplateArgInfo {
462-
TCppScope_t m_Type;
462+
TCppType_t m_Type;
463463
const char* m_IntegralValue;
464464
TemplateArgInfo(TCppScope_t type, const char* integral_value = nullptr)
465465
: m_Type(type), m_IntegralValue(integral_value) {}
@@ -468,6 +468,9 @@ namespace Cpp {
468468
TemplateArgInfo* template_args,
469469
size_t template_args_size);
470470

471+
/// Returns the class template instantiation arguments of \c templ_instance.
472+
void GetClassTemplateInstantiationArgs(TCppScope_t templ_instance,
473+
std::vector<TemplateArgInfo> &args);
471474
std::vector<std::string> GetAllCppNames(TCppScope_t scope);
472475

473476
void DumpScope(TCppScope_t scope);

lib/Interpreter/CppInterOp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,30 @@ namespace Cpp {
26962696
return GetScopeFromType(Instance);
26972697
}
26982698

2699+
void GetClassTemplateInstantiationArgs(TCppScope_t templ_instance,
2700+
std::vector<TemplateArgInfo> &args) {
2701+
auto* CTSD = static_cast<ClassTemplateSpecializationDecl*>(templ_instance);
2702+
for(const auto& TA : CTSD->getTemplateInstantiationArgs().asArray()) {
2703+
switch (TA.getKind()) {
2704+
default:
2705+
assert(0 && "Not yet supported!");
2706+
break;
2707+
case TemplateArgument::Pack:
2708+
for (auto SubTA : TA.pack_elements())
2709+
args.push_back({SubTA.getAsType().getAsOpaquePtr()});
2710+
break;
2711+
case TemplateArgument::Integral:
2712+
// FIXME: Support this case where the problem is where we provide the
2713+
// storage for the m_IntegralValue.
2714+
//llvm::APSInt Val = TA.getAsIntegral();
2715+
//args.push_back({TA.getIntegralType(), TA.getAsIntegral()})
2716+
//break;
2717+
case TemplateArgument::Type:
2718+
args.push_back({TA.getAsType().getAsOpaquePtr()});
2719+
}
2720+
}
2721+
}
2722+
26992723
std::vector<std::string> GetAllCppNames(TCppScope_t scope) {
27002724
auto *D = (clang::Decl *)scope;
27012725
clang::DeclContext *DC;

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,43 @@ TEST(ScopeReflectionTest, InstantiateClassTemplate) {
797797
EXPECT_TRUE(TA4_1.getAsIntegral() == 3);
798798
}
799799

800+
TEST(ScopeReflectionTest, GetClassTemplateInstantiationArgs) {
801+
std::vector<Decl *> Decls;
802+
std::string code = R"(
803+
template<typename ...T> struct __Cppyy_AppendTypesSlow {};
804+
__Cppyy_AppendTypesSlow<int, float, double> v1;
805+
__Cppyy_AppendTypesSlow<int> v2;
806+
__Cppyy_AppendTypesSlow<> v3;
807+
)";
808+
809+
GetAllTopLevelDecls(code, Decls);
810+
811+
auto *v1 = Cpp::GetNamed("v1");
812+
auto *v2 = Cpp::GetNamed("v2");
813+
auto *v3 = Cpp::GetNamed("v3");
814+
EXPECT_TRUE(v1 && v2 && v3);
815+
816+
auto *v1_class = Cpp::GetScopeFromType(Cpp::GetVariableType(v1));
817+
auto *v2_class = Cpp::GetScopeFromType(Cpp::GetVariableType(v2));
818+
auto *v3_class = Cpp::GetScopeFromType(Cpp::GetVariableType(v3));
819+
EXPECT_TRUE(v1_class && v2_class && v3_class);
820+
821+
std::vector<Cpp::TemplateArgInfo> instance_types;
822+
823+
Cpp::GetClassTemplateInstantiationArgs(v1_class, instance_types);
824+
EXPECT_TRUE(instance_types.size() == 3);
825+
826+
instance_types.clear();
827+
828+
Cpp::GetClassTemplateInstantiationArgs(v2_class, instance_types);
829+
EXPECT_TRUE(instance_types.size() == 1);
830+
831+
instance_types.clear();
832+
833+
Cpp::GetClassTemplateInstantiationArgs(v3_class, instance_types);
834+
EXPECT_TRUE(instance_types.size() == 0);
835+
}
836+
800837
TEST(ScopeReflectionTest, IncludeVector) {
801838
std::string code = R"(
802839
#include <vector>

0 commit comments

Comments
 (0)