Skip to content

Commit 636775e

Browse files
committed
Refactoring, move common code to static function
1 parent 37fabc1 commit 636775e

File tree

2 files changed

+56
-68
lines changed

2 files changed

+56
-68
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ namespace Cpp {
286286

287287
/// Gets a list of all the Methods that are in the Class that is
288288
/// supplied as a parameter.
289+
///\param[in] klass - Pointer to the scope/class under which the methods have
290+
/// to be retrieved
289291
CPPINTEROP_API void GetClassMethods(TCppScope_t klass, std::vector<TCppFunction_t> &methods);
290292

291293
///\returns Template function pointer list to add proxies for
@@ -335,8 +337,11 @@ namespace Cpp {
335337
CPPINTEROP_API bool ExistsFunctionTemplate(const std::string& name,
336338
TCppScope_t parent = nullptr);
337339

338-
CPPINTEROP_API std::vector<TCppFunction_t>
339-
GetTemplatedMethods(const std::string& name, TCppScope_t parent = nullptr);
340+
/// Sets a list of all the Templated Methods that are in the Class that is
341+
/// supplied as a parameter.
342+
CPPINTEROP_API void
343+
GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
344+
std::vector<TCppFunction_t>& funcs);
340345

341346
/// Checks if the provided parameter is a method.
342347
CPPINTEROP_API bool IsMethod(TCppConstFunction_t method);
@@ -542,6 +547,14 @@ namespace Cpp {
542547
: m_Type(type), m_IntegralValue(integral_value) {}
543548
};
544549
/// Builds a template instantiation for a given templated declaration.
550+
/// Offers a single interface for instantiation of class, function and
551+
/// variable templates
552+
///\param[in] tmpl - Uninstantiated template class/function
553+
///\param[in] template_args - Pointer to vector of template arguments stored
554+
/// in the \c TemplateArgInfo struct \param[in] template_args_size - Size of
555+
/// the vector of template arguments passed as \c template_args
556+
///
557+
///\returns Instantiated templated class/function/variable pointer
545558
CPPINTEROP_API TCppScope_t InstantiateTemplate(TCppScope_t tmpl,
546559
const TemplateArgInfo* template_args,
547560
size_t template_args_size);
@@ -557,8 +570,15 @@ namespace Cpp {
557570
CPPINTEROP_API TCppFunction_t
558571
InstantiateTemplateFunctionFromString(const char* function_template);
559572

560-
// Find best template match based on explicit template parameters and arg
561-
// types
573+
/// Finds best template match based on explicit template parameters and
574+
/// argument types
575+
///
576+
///\param[in] candidates - Vector of suitable candidates that come under the
577+
/// parent scope and have the same name (obtained using
578+
/// GetClassTemplatedMethods) \param[in] explicit_types - set of expicitly
579+
/// instantiated template types, if any \param[in] arg_types - set of argument
580+
/// types
581+
///\returns Instantiated function pointer
562582
CPPINTEROP_API TCppFunction_t
563583
BestTemplateFunctionMatch(const std::vector<TCppFunction_t>& candidates,
564584
const std::vector<TemplateArgInfo>& explicit_types,

lib/Interpreter/CppInterOp.cpp

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -719,48 +719,41 @@ namespace Cpp {
719719
return ComputeBaseOffset(getSema().getASTContext(), DCXXRD, Paths.front());
720720
}
721721

722-
void GetClassMethods(TCppScope_t klass, std::vector<TCppFunction_t> &methods) {
723-
if (!klass) return;
724-
725-
auto *D = (clang::Decl *) klass;
726-
727-
if (auto *TD = dyn_cast<TypedefNameDecl>(D))
728-
D = GetScopeFromType(TD->getUnderlyingType());
729-
730-
if (D && isa<CXXRecordDecl>(D)) {
731-
auto* CXXRD = dyn_cast<CXXRecordDecl>(D);
732-
getSema().ForceDeclarationOfImplicitMembers(CXXRD);
733-
for (Decl* DI : CXXRD->decls()) {
734-
if (auto* MD = dyn_cast<CXXMethodDecl>(DI))
735-
methods.push_back(MD);
736-
else if (auto* USD = dyn_cast<UsingShadowDecl>(DI))
737-
if (auto* MD = dyn_cast<CXXMethodDecl>(USD->getTargetDecl()))
738-
methods.push_back(MD);
739-
}
740-
}
741-
}
742-
743-
void GetFunctionTemplatedDecls(TCppScope_t klass, std::vector<TCppFunction_t> &methods) {
744-
if (!klass) return;
722+
template <typename DeclType>
723+
static void GetClassDecls(TCppScope_t klass,
724+
std::vector<TCppFunction_t>& methods) {
725+
if (!klass)
726+
return;
745727

746728
auto* D = (clang::Decl*)klass;
747729

748730
if (auto* TD = dyn_cast<TypedefNameDecl>(D))
749731
D = GetScopeFromType(TD->getUnderlyingType());
750732

751-
if (D && isa<CXXRecordDecl>(D)) {
752-
auto* CXXRD = dyn_cast<CXXRecordDecl>(D);
753-
getSema().ForceDeclarationOfImplicitMembers(CXXRD);
754-
for (Decl* DI : CXXRD->decls()) {
755-
if (auto* MD = dyn_cast<FunctionTemplateDecl>(DI))
733+
if (!D || !isa<CXXRecordDecl>(D))
734+
return;
735+
736+
auto* CXXRD = dyn_cast<CXXRecordDecl>(D);
737+
getSema().ForceDeclarationOfImplicitMembers(CXXRD);
738+
for (Decl* DI : CXXRD->decls()) {
739+
if (auto* MD = dyn_cast<DeclType>(DI))
740+
methods.push_back(MD);
741+
else if (auto* USD = dyn_cast<UsingShadowDecl>(DI))
742+
if (auto* MD = dyn_cast<DeclType>(USD->getTargetDecl()))
756743
methods.push_back(MD);
757-
else if (auto* USD = dyn_cast<UsingShadowDecl>(DI))
758-
if (auto* MD = dyn_cast<FunctionTemplateDecl>(USD->getTargetDecl()))
759-
methods.push_back(MD);
760-
}
761744
}
762745
}
763746

747+
void GetClassMethods(TCppScope_t klass,
748+
std::vector<TCppFunction_t>& methods) {
749+
GetClassDecls<CXXMethodDecl>(klass, methods);
750+
}
751+
752+
void GetFunctionTemplatedDecls(TCppScope_t klass,
753+
std::vector<TCppFunction_t>& methods) {
754+
GetClassDecls<FunctionTemplateDecl>(klass, methods);
755+
}
756+
764757
bool HasDefaultConstructor(TCppScope_t scope) {
765758
auto *D = (clang::Decl *) scope;
766759

@@ -883,9 +876,7 @@ namespace Cpp {
883876
return 0;
884877
}
885878

886-
std::string GetFunctionSignature(
887-
TCppFunction_t func) { // FIXME : Doesn't work for template functions as
888-
// it does in cling
879+
std::string GetFunctionSignature(TCppFunction_t func) {
889880
if (!func)
890881
return "<unknown>";
891882

@@ -903,18 +894,6 @@ namespace Cpp {
903894
return Signature;
904895
}
905896

906-
// FIXME: else if (auto *FD = llvm::dyn_cast<FunctionTemplateDecl>(D)) {
907-
// std::string Signature;
908-
// raw_string_ostream SS(Signature);
909-
// PrintingPolicy Policy = getASTContext().getPrintingPolicy();
910-
// // Skip printing the body
911-
// Policy.TerseOutput = true;
912-
// Policy.FullyQualifiedName = true;
913-
// Policy.SuppressDefaultTemplateArgs = false;
914-
// FD->print(SS, Policy);
915-
// SS.flush();
916-
// return Signature;
917-
// }
918897
return "<unknown>";
919898
}
920899

@@ -954,7 +933,7 @@ namespace Cpp {
954933
{
955934
DeclContext *Within = 0;
956935
if (parent) {
957-
auto *D = (Decl *)parent;
936+
auto* D = (Decl*)parent;
958937
Within = llvm::dyn_cast<DeclContext>(D);
959938
}
960939

@@ -970,17 +949,16 @@ namespace Cpp {
970949
return true;
971950
}
972951

973-
std::vector<TCppFunction_t> GetTemplatedMethods(const std::string& name,
974-
TCppScope_t parent) {
952+
void GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
953+
std::vector<TCppFunction_t>& funcs) {
975954

976955
auto* D = (Decl*)parent;
977956

978957
if (!parent || name.empty())
979-
return {};
958+
return;
980959

981960
D = GetUnderlyingScope(D);
982961

983-
std::vector<TCppFunction_t> funcs;
984962
llvm::StringRef Name(name);
985963
auto& S = getSema();
986964
DeclarationName DName = &getASTContext().Idents.get(name);
@@ -990,34 +968,26 @@ namespace Cpp {
990968
Cpp_utils::Lookup::Named(&S, R, Decl::castToDeclContext(D));
991969

992970
if (R.empty())
993-
return {};
971+
return;
994972

995973
R.resolveKind();
996974

997-
for (auto* Found : R) {
975+
for (auto* Found : R)
998976
if (llvm::isa<FunctionTemplateDecl>(Found))
999977
funcs.push_back(Found);
1000-
}
1001-
1002-
if (!funcs.empty())
1003-
return funcs;
1004-
1005-
return {};
1006978
}
1007979

1008980
TCppFunction_t
1009981
BestTemplateFunctionMatch(const std::vector<TCppFunction_t>& candidates,
1010982
const std::vector<TemplateArgInfo>& explicit_types,
1011983
const std::vector<TemplateArgInfo>& arg_types) {
1012984

1013-
int k = 1;
1014985
for (const auto& candidate : candidates) {
1015986
auto* TFD = (FunctionTemplateDecl*)candidate;
1016987
clang::TemplateParameterList* tpl = TFD->getTemplateParameters();
1017988

1018989
if (tpl->size() < explicit_types.size()) {
1019990
// template parameter size does not match
1020-
++k;
1021991
continue;
1022992
}
1023993

@@ -1029,7 +999,6 @@ namespace Cpp {
1029999

10301000
// Check if number of function parameters match
10311001
if (func->getNumParams() != arg_types.size()) {
1032-
++k;
10331002
continue;
10341003
}
10351004

@@ -1051,7 +1020,6 @@ namespace Cpp {
10511020
return instantiated;
10521021

10531022
else {
1054-
k++;
10551023
continue;
10561024
}
10571025
}

0 commit comments

Comments
 (0)