Skip to content

Commit

Permalink
Refactor and optimise code
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronj0 committed Apr 8, 2024
1 parent 9f1b696 commit c22d764
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 53 deletions.
28 changes: 19 additions & 9 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ namespace Cpp {
CPPINTEROP_API int64_t GetBaseClassOffset(TCppScope_t derived,
TCppScope_t base);

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

Expand Down Expand Up @@ -342,6 +343,10 @@ namespace Cpp {

/// Sets a list of all the Templated Methods that are in the Class that is
/// supplied as a parameter.
///\param[in] name - method name
///\param[in] parent - Pointer to the scope/class under which the methods have
/// to be retrieved
///\param[out] funcs - vector of function pointers matching the name
CPPINTEROP_API void
GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
std::vector<TCppFunction_t>& funcs);
Expand Down Expand Up @@ -552,17 +557,22 @@ namespace Cpp {
/// Builds a template instantiation for a given templated declaration.
/// Offers a single interface for instantiation of class, function and
/// variable templates
///
///\param[in] tmpl - Uninstantiated template class/function
///\param[in] template_args - Pointer to vector of template arguments stored
/// in the \c TemplateArgInfo struct \param[in] template_args_size - Size of
/// the vector of template arguments passed as \c template_args
/// in the \c TemplateArgInfo struct
///\param[in] template_args_size - Size of the vector of template arguments
/// passed as \c template_args
///
///\returns Instantiated templated class/function/variable pointer
CPPINTEROP_API TCppScope_t
InstantiateTemplate(TCppScope_t tmpl, const TemplateArgInfo* template_args,
size_t template_args_size);

/// Returns the class template instantiation arguments of \c templ_instance.
/// Sets the class template instantiation arguments of \c templ_instance.
///
///\param[in] templ_instance - Pointer to the template instance
///\param[out] args - Vector of instantiation arguments
CPPINTEROP_API void
GetClassTemplateInstantiationArgs(TCppScope_t templ_instance,
std::vector<TemplateArgInfo>& args);
Expand All @@ -577,10 +587,10 @@ namespace Cpp {
/// argument types
///
///\param[in] candidates - Vector of suitable candidates that come under the
/// parent scope and have the same name (obtained using
/// GetClassTemplatedMethods) \param[in] explicit_types - set of expicitly
/// instantiated template types, if any \param[in] arg_types - set of argument
/// types
/// parent scope and have the same name (obtained using
/// GetClassTemplatedMethods)
///\param[in] explicit_types - set of expicitly instantiated template types
///\param[in] arg_types - set of argument types
///\returns Instantiated function pointer
CPPINTEROP_API TCppFunction_t
BestTemplateFunctionMatch(const std::vector<TCppFunction_t>& candidates,
Expand Down
73 changes: 29 additions & 44 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,8 @@ namespace Cpp {
bool HasDefaultConstructor(TCppScope_t scope) {
auto *D = (clang::Decl *) scope;

if (auto *CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) {
if (auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D))
return CXXRD->hasDefaultConstructor();
}

return false;
}
Expand Down Expand Up @@ -826,13 +825,11 @@ namespace Cpp {
TCppType_t GetFunctionReturnType(TCppFunction_t func)
{
auto *D = (clang::Decl *) func;
if (auto *FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D)) {
return FD->getReturnType().getAsOpaquePtr();
}
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
return FD->getReturnType().getAsOpaquePtr();

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D)) {
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
return (FD->getTemplatedDecl())->getReturnType().getAsOpaquePtr();
}

return 0;
}
Expand All @@ -852,9 +849,8 @@ namespace Cpp {
TCppIndex_t GetFunctionRequiredArgs(TCppConstFunction_t func)
{
auto *D = (const clang::Decl *) func;
if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl> (D)) {
if (auto* FD = llvm::dyn_cast_or_null<FunctionDecl>(D))
return FD->getMinRequiredArguments();
}

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
return (FD->getTemplatedDecl())->getMinRequiredArguments();
Expand Down Expand Up @@ -990,37 +986,27 @@ namespace Cpp {
if (tpl->size() < explicit_types.size())
continue;

else {
// right now uninstantiated functions give template typenames instead of
// actual types. We make this match solely based on count

const FunctionDecl* func = TFD->getTemplatedDecl();
if (func->getNumParams() != arg_types.size())
continue;

else {
// TODO : first score based on the type similarity before forcing
// instantiation try instantiating
TCppFunction_t instantiated = InstantiateTemplate(
candidate, arg_types.data(), arg_types.size());
if (instantiated)
return instantiated;

// Force the instantiation with template params in case of no args
// maybe steer instantiation better with arg set returned from
// TemplateProxy?
else {
instantiated = InstantiateTemplate(candidate, explicit_types.data(),
explicit_types.size());
if (instantiated)
return instantiated;

else {
continue;
}
}
}
}
// right now uninstantiated functions give template typenames instead of
// actual types. We make this match solely based on count

const FunctionDecl* func = TFD->getTemplatedDecl();
if (func->getNumParams() != arg_types.size())
continue;

// TODO : first score based on the type similarity before forcing
// instantiation try instantiating
TCppFunction_t instantiated = InstantiateTemplate(
candidate, arg_types.data(), arg_types.size());
if (instantiated)
return instantiated;

// Force the instantiation with template params in case of no args
// maybe steer instantiation better with arg set returned from
// TemplateProxy?
instantiated = InstantiateTemplate(candidate, explicit_types.data(),
explicit_types.size());
if (instantiated)
return instantiated;
}
return nullptr;
}
Expand Down Expand Up @@ -3139,12 +3125,11 @@ namespace Cpp {
auto *D = (clang::Decl *)func;
clang::ParmVarDecl* PI;

if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D)) {
if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D))
PI = FD->getParamDecl(param_index);
} else if (auto* FD =
llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D)) {

else if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(D))
PI = (FD->getTemplatedDecl())->getParamDecl(param_index);
}

return PI->getNameAsString();
}
Expand Down

0 comments on commit c22d764

Please sign in to comment.