Skip to content

Commit

Permalink
Merge pull request #11 from maximusron/overload-dev
Browse files Browse the repository at this point in the history
ptr/reftype overload solution in CompareMethodArg
  • Loading branch information
wlav authored Sep 5, 2023
2 parents ccda3d2 + 9952c83 commit 9c07abd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
6 changes: 5 additions & 1 deletion cling/src/core/meta/inc/TInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,14 @@ class TInterpreter : public TNamed {
virtual Bool_t IsIntegerType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsSignedIntegerType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsUnsignedIntegerType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsIntegralType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsFloatingType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsPointerType(const void * /* QualTypePtr */) const {return 0;}
virtual Bool_t IsVoidPointerType(const void * /* QualTypePtr */) const {return 0;}

virtual TypeInfo_t *GetNonReferenceType(const void * /* QualTypePtr */) const {return 0;}
virtual TypeInfo_t *GetUnqualifiedType(const void * /* QualTypePtr */) const {return 0;}
virtual TypeInfo_t *GetPointerType(const void * /* QualTypePtr */) const {return 0;}

// FunctionDecl interface
virtual Bool_t FunctionDeclId_IsMethod(DeclId_t /* fdeclid */) const {return 0;}
};
Expand Down
36 changes: 36 additions & 0 deletions cling/src/core/metacling/src/TCling.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8479,6 +8479,15 @@ bool TCling::IsIntegerType(const void * QualTypePtr) const

////////////////////////////////////////////////////////////////////////////////

bool TCling::IsIntegralType(const void * QualTypePtr) const
{
clang::QualType QT = clang::QualType::getFromOpaquePtr(QualTypePtr);
const clang::ASTContext& astContext = fInterpreter->getCI()->getASTContext();
return QT->isIntegralType(astContext);
}

////////////////////////////////////////////////////////////////////////////////

bool TCling::IsSignedIntegerType(const void * QualTypePtr) const
{
clang::QualType QT = clang::QualType::getFromOpaquePtr(QualTypePtr);
Expand Down Expand Up @@ -8519,6 +8528,33 @@ bool TCling::IsVoidPointerType(const void * QualTypePtr) const

////////////////////////////////////////////////////////////////////////////////

TypeInfo_t* TCling::GetNonReferenceType(const void * QualTypePtr) const
{
clang::QualType QT = clang::QualType::getFromOpaquePtr(QualTypePtr);
clang::QualType QT1 = QT.getNonReferenceType();
return (TypeInfo_t*) new TClingTypeInfo(GetInterpreterImpl(), QT1);

}
////////////////////////////////////////////////////////////////////////////////

TypeInfo_t* TCling::GetUnqualifiedType(const void * QualTypePtr) const
{
clang::QualType QT = clang::QualType::getFromOpaquePtr(QualTypePtr);
clang::QualType QT1 = QT.getUnqualifiedType();
return (TypeInfo_t*) new TClingTypeInfo(GetInterpreterImpl(), QT1);

}
////////////////////////////////////////////////////////////////////////////////

TypeInfo_t* TCling::GetPointerType(const void * QualTypePtr) const
{
clang::QualType QT = clang::QualType::getFromOpaquePtr(QualTypePtr);
clang::QualType QT1 = QT->getPointeeType();
return (TypeInfo_t*) new TClingTypeInfo(GetInterpreterImpl(), QT1);

}
////////////////////////////////////////////////////////////////////////////////

bool TCling::FunctionDeclId_IsMethod(DeclId_t fdeclid) const
{
clang::FunctionDecl *FD = (clang::FunctionDecl *) fdeclid;
Expand Down
5 changes: 5 additions & 0 deletions cling/src/core/metacling/src/TCling.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,17 @@ class TCling final : public TInterpreter {
virtual bool IsIntegerType(const void * QualTypePtr) const;
virtual bool IsSignedIntegerType(const void * QualTypePtr) const;
virtual bool IsUnsignedIntegerType(const void * QualTypePtr) const;
virtual bool IsIntegralType(const void * QualTypePtr) const;
virtual bool IsFloatingType(const void * QualTypePtr) const;
virtual bool IsPointerType(const void * QualTypePtr) const;
virtual bool IsVoidPointerType(const void * QualTypePtr) const;

// FunctionDecl interface
bool FunctionDeclId_IsMethod(DeclId_t fdeclid) const;

virtual TypeInfo_t* GetNonReferenceType(const void * QualTypePtr) const;
virtual TypeInfo_t* GetUnqualifiedType(const void * QualTypePtr) const;
virtual TypeInfo_t* GetPointerType(const void * QualTypePtr) const;
};

} // namespace CppyyLegacy
Expand Down
53 changes: 37 additions & 16 deletions clingwrapper/src/clingwrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,28 @@ std::string Cppyy::ResolveEnum(const std::string& enum_type)
return restype; // should default to some int variant
}

static Cppyy::TCppIndex_t ArgSimilarityScore(void *argqtp, void *reqqtp)
{
// This scoring is not based on any particular rules
if (gInterpreter->IsSameType(argqtp, reqqtp))
return 0; // Best match
else if ((gInterpreter->IsSignedIntegerType(argqtp) && gInterpreter->IsSignedIntegerType(reqqtp)) ||
(gInterpreter->IsUnsignedIntegerType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)) ||
(gInterpreter->IsFloatingType(argqtp) && gInterpreter->IsFloatingType(reqqtp)))
return 1;
else if ((gInterpreter->IsSignedIntegerType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)) ||
(gInterpreter->IsFloatingType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)))
return 2;
else if ((gInterpreter->IsIntegerType(argqtp) && gInterpreter->IsIntegerType(reqqtp)))
return 3;
else if ((gInterpreter->IsIntegralType(argqtp) && gInterpreter->IsIntegralType(reqqtp)))
return 4;
else if ((gInterpreter->IsVoidPointerType(argqtp) && gInterpreter->IsPointerType(reqqtp)))
return 5;
else
return 10; // Penalize heavily for no possible match
}

Cppyy::TCppScope_t Cppyy::GetScope(const std::string& sname)
{
// First, try cache
Expand Down Expand Up @@ -1720,22 +1742,21 @@ Cppyy::TCppIndex_t Cppyy::CompareMethodArgType(TCppMethod_t method, TCppIndex_t
TypeInfo_t *reqti = gInterpreter->TypeInfo_Factory(req_type.c_str());
void *reqqtp = gInterpreter->TypeInfo_QualTypePtr(reqti);

// This scoring is not based on any particular rules
if (gInterpreter->IsSameType(argqtp, reqqtp))
return 0; // Best match
else if ((gInterpreter->IsSignedIntegerType(argqtp) && gInterpreter->IsSignedIntegerType(reqqtp)) ||
(gInterpreter->IsUnsignedIntegerType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)) ||
(gInterpreter->IsFloatingType(argqtp) && gInterpreter->IsFloatingType(reqqtp)))
return 1;
else if ((gInterpreter->IsSignedIntegerType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)) ||
(gInterpreter->IsFloatingType(argqtp) && gInterpreter->IsUnsignedIntegerType(reqqtp)))
return 2;
else if ((gInterpreter->IsIntegerType(argqtp) && gInterpreter->IsIntegerType(reqqtp)))
return 3;
else if ((gInterpreter->IsVoidPointerType(argqtp) && gInterpreter->IsPointerType(reqqtp)))
return 4;
else
return 10; // Penalize heavily for no possible match
if (ArgSimilarityScore(argqtp, reqqtp) < 10) {
return ArgSimilarityScore(argqtp, reqqtp);
}
else { // Match using underlying types
if(gInterpreter->IsPointerType(argqtp))
argqtp = gInterpreter->TypeInfo_QualTypePtr(gInterpreter->GetPointerType(argqtp));

// Handles reference types and strips qualifiers
TypeInfo_t *arg_ul = gInterpreter->GetNonReferenceType(argqtp);
TypeInfo_t *req_ul = gInterpreter->GetNonReferenceType(reqqtp);
argqtp = gInterpreter->TypeInfo_QualTypePtr(gInterpreter->GetUnqualifiedType(gInterpreter->TypeInfo_QualTypePtr(arg_ul)));
reqqtp = gInterpreter->TypeInfo_QualTypePtr(gInterpreter->GetUnqualifiedType(gInterpreter->TypeInfo_QualTypePtr(req_ul)));

return ArgSimilarityScore(argqtp, reqqtp);
}
}
return INT_MAX; // Method is not valid
}
Expand Down

0 comments on commit 9c07abd

Please sign in to comment.