-
Notifications
You must be signed in to change notification settings - Fork 35
[WIP] Things required to get Python Type-Hints to work with cppyy #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||
| #include "Compatibility.h" | ||||||
|
|
||||||
| #include "clang/AST/Attrs.inc" | ||||||
| #include "clang/AST/Comment.h" | ||||||
| #include "clang/AST/CXXInheritance.h" | ||||||
| #include "clang/AST/Decl.h" | ||||||
| #include "clang/AST/DeclAccessPair.h" | ||||||
|
|
@@ -380,6 +381,11 @@ | |||||
| return llvm::StringRef(Ty.getAsString()).contains("complex"); | ||||||
| } | ||||||
|
|
||||||
| bool IsTemplateClass(TCppScope_t handle) { | ||||||
| auto* D = (clang::Decl*)handle; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast] auto* D = (clang::Decl*)handle;
^ |
||||||
| return llvm::isa_and_nonnull<clang::ClassTemplateDecl>(D); | ||||||
| } | ||||||
|
|
||||||
| bool IsTemplate(TCppScope_t handle) { | ||||||
| auto* D = (clang::Decl*)handle; | ||||||
| return llvm::isa_and_nonnull<clang::TemplateDecl>(D); | ||||||
|
|
@@ -546,6 +552,15 @@ | |||||
| return llvm::isa_and_nonnull<clang::VarDecl>(D); | ||||||
| } | ||||||
|
|
||||||
| std::string GetDocString(TCppScope_t scope) { | ||||||
| auto *D = static_cast<Decl*>(scope); | ||||||
| auto &AST = getASTContext(); | ||||||
| const clang::RawComment *Comment = AST.getRawCommentForAnyRedecl(D); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clang::RawComment" is directly included [misc-include-cleaner] lib/CppInterOp/CppInterOp.cpp:42: - #if CLANG_VERSION_MAJOR >= 19
+ #include <clang/AST/RawCommentList.h>
+ #if CLANG_VERSION_MAJOR >= 19 |
||||||
| if (!Comment) | ||||||
| return ""; | ||||||
| return Comment->getFormattedText(AST.getSourceManager(), AST.getDiagnostics()); | ||||||
| } | ||||||
|
|
||||||
| std::string GetName(TCppType_t klass) { | ||||||
| auto* D = (clang::NamedDecl*)klass; | ||||||
|
|
||||||
|
|
@@ -877,6 +892,51 @@ | |||||
| return ComputeBaseOffset(getSema().getASTContext(), DCXXRD, Paths.front()); | ||||||
| } | ||||||
|
|
||||||
| template <typename DeclType> | ||||||
| static void GetNamespaceDecls(TCppScope_t ns, | ||||||
| std::vector<TCppScope_t>& members) { | ||||||
| if (!ns) | ||||||
| return; | ||||||
|
|
||||||
| auto* D = (clang::Decl*)ns; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast] auto* D = (clang::Decl*)ns;
^ |
||||||
|
|
||||||
| if (!D || !isa<NamespaceDecl>(D)) | ||||||
| return; | ||||||
|
|
||||||
| auto* NSD = dyn_cast<NamespaceDecl>(D)->getMostRecentDecl(); | ||||||
| while (NSD) { | ||||||
| for (Decl* DI : NSD->decls()) { | ||||||
| if (auto* MD = dyn_cast<DeclType>(DI)) | ||||||
| members.push_back(MD); | ||||||
| else if (auto* USD = dyn_cast<UsingShadowDecl>(DI)) { | ||||||
| if (auto *MD = dyn_cast<DeclType>(USD->getTargetDecl())) | ||||||
| members.push_back(MD); | ||||||
| } | ||||||
| } | ||||||
| NSD = NSD->getPreviousDecl(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void GetDatamembersInNamespace(TCppScope_t ns, std::vector<TCppScope_t>& members) { | ||||||
| GetNamespaceDecls<VarDecl>(ns, members); | ||||||
| } | ||||||
|
|
||||||
| void GetFunctionsInNamespace(TCppScope_t ns, std::vector<TCppScope_t>& members) { | ||||||
| GetNamespaceDecls<FunctionDecl>(ns, members); | ||||||
| } | ||||||
|
|
||||||
| void GetClassInNamespace(TCppScope_t ns, std::vector<TCppScope_t>& members) { | ||||||
| GetNamespaceDecls<RecordDecl>(ns, members); | ||||||
| } | ||||||
|
|
||||||
| void GetTemplatedClassInNamespace(TCppScope_t ns, std::vector<TCppScope_t>& members) { | ||||||
| GetNamespaceDecls<ClassTemplateDecl>(ns, members); | ||||||
| } | ||||||
|
|
||||||
| void GetTemplatedFunctionsInNamespace(TCppScope_t ns, std::vector<TCppScope_t>& members) { | ||||||
| GetNamespaceDecls<FunctionTemplateDecl>(ns, members); | ||||||
| } | ||||||
|
|
||||||
| template <typename DeclType> | ||||||
| static void GetClassDecls(TCppScope_t klass, | ||||||
| std::vector<TCppFunction_t>& methods) { | ||||||
|
|
@@ -888,6 +948,10 @@ | |||||
| if (auto* TD = dyn_cast<TypedefNameDecl>(D)) | ||||||
| D = GetScopeFromType(TD->getUnderlyingType()); | ||||||
|
|
||||||
| if (auto *CTD = llvm::dyn_cast_or_null<ClassTemplateDecl>(D)) { | ||||||
| D = CTD->getTemplatedDecl(); | ||||||
| } | ||||||
|
|
||||||
| if (!D || !isa<CXXRecordDecl>(D)) | ||||||
| return; | ||||||
|
|
||||||
|
|
@@ -1011,7 +1075,7 @@ | |||||
| QualType Type = FD->getReturnType(); | ||||||
| if (Type->isUndeducedAutoType()) { | ||||||
| bool needInstantiation = false; | ||||||
| if (IsTemplatedFunction(FD) && !FD->isDefined()) | ||||||
| if (IsTemplateInstantiationOrSpecialization(FD) && !FD->isDefined()) | ||||||
| needInstantiation = true; | ||||||
| if (auto* MD = llvm::dyn_cast<clang::CXXMethodDecl>(FD)) { | ||||||
| if (IsTemplateSpecialization(MD->getParent())) | ||||||
|
|
@@ -1055,8 +1119,10 @@ | |||||
| } | ||||||
|
|
||||||
| TCppType_t GetFunctionArgType(TCppFunction_t func, TCppIndex_t iarg) { | ||||||
| auto* D = (clang::Decl*)func; | ||||||
|
|
||||||
| auto* D = static_cast<clang::Decl*>(func); | ||||||
| if (auto *FTD = llvm::dyn_cast_or_null<FunctionTemplateDecl>(D)) { | ||||||
| D = FTD->getTemplatedDecl(); | ||||||
| } | ||||||
| if (auto* FD = llvm::dyn_cast_or_null<clang::FunctionDecl>(D)) { | ||||||
| if (iarg < FD->getNumParams()) { | ||||||
| auto* PVD = FD->getParamDecl(iarg); | ||||||
|
|
@@ -1122,7 +1188,12 @@ | |||||
|
|
||||||
| bool IsTemplatedFunction(TCppFunction_t func) { | ||||||
| auto* D = (Decl*)func; | ||||||
| return IsTemplatedFunction(D) || IsTemplateInstantiationOrSpecialization(D); | ||||||
| return IsTemplatedFunction(D) /*|| IsTemplateInstantiationOrSpecialization(D)*/; | ||||||
| } | ||||||
|
|
||||||
| bool IsTemplateInstantiationOrSpecialization(TCppScope_t scope) { | ||||||
| auto *D = static_cast<Decl*>(scope); | ||||||
| return IsTemplateInstantiationOrSpecialization(D); | ||||||
| } | ||||||
|
|
||||||
| // FIXME: This lookup is broken, and should no longer be used in favour of | ||||||
|
|
@@ -1334,6 +1405,9 @@ | |||||
|
|
||||||
| bool IsStaticMethod(TCppConstFunction_t method) { | ||||||
| const auto* D = static_cast<const Decl*>(method); | ||||||
| if (auto *FTD = llvm::dyn_cast_or_null<FunctionTemplateDecl>(D)) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: 'auto *FTD' can be declared as 'const auto *FTD' [readability-qualified-auto]
Suggested change
|
||||||
| D = FTD->getTemplatedDecl(); | ||||||
| } | ||||||
| if (auto* CXXMD = llvm::dyn_cast_or_null<CXXMethodDecl>(D)) { | ||||||
| return CXXMD->isStatic(); | ||||||
| } | ||||||
|
|
@@ -1404,6 +1478,9 @@ | |||||
|
|
||||||
| void GetDatamembers(TCppScope_t scope, std::vector<TCppScope_t>& datamembers) { | ||||||
| auto* D = (Decl*)scope; | ||||||
| if (auto *CTD = llvm::dyn_cast_or_null<ClassTemplateDecl>(D)) { | ||||||
| D = CTD->getTemplatedDecl(); | ||||||
| } | ||||||
|
|
||||||
| if (auto* CXXRD = llvm::dyn_cast_or_null<CXXRecordDecl>(D)) { | ||||||
| getSema().ForceDeclarationOfImplicitMembers(CXXRD); | ||||||
|
|
@@ -1497,7 +1574,7 @@ | |||||
| QualType QT = DD->getType(); | ||||||
|
|
||||||
| // Check if the type is a typedef type | ||||||
| if (QT->isTypedefNameType()) { | ||||||
| if (QT->isTypedefNameType() || QT->getAs<clang::TemplateTypeParmType>()) { | ||||||
| return QT.getAsOpaquePtr(); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -1758,6 +1835,8 @@ | |||||
| if (!type) | ||||||
| return 0; | ||||||
| QualType QT = QualType::getFromOpaquePtr(type); | ||||||
| if (QT->getAs<clang::TemplateTypeParmType>()) | ||||||
| return type; | ||||||
| return QT.getCanonicalType().getAsOpaquePtr(); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -3841,6 +3920,25 @@ | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| TCppIndex_t GetTemplateNumArgs(TCppScope_t scope) { | ||||||
| auto *D = static_cast<Decl*>(scope); | ||||||
| if (auto *TD = llvm::dyn_cast<TemplateDecl>(D)) { | ||||||
| auto *TPL = TD->getTemplateParameters(); | ||||||
| return TPL->size(); | ||||||
| } | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| std::string GetTemplateArgName(TCppScope_t scope, TCppIndex_t param_index) { | ||||||
| auto *D = static_cast<Decl*>(scope); | ||||||
| if (auto *TD = llvm::dyn_cast<TemplateDecl>(D)) { | ||||||
| auto *TPL = TD->getTemplateParameters(); | ||||||
| NamedDecl *ND = TPL->getParam(param_index); | ||||||
| return ND->getNameAsString(); | ||||||
| } | ||||||
| return ""; | ||||||
| } | ||||||
|
|
||||||
| std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index) { | ||||||
| auto* D = (clang::Decl*)func; | ||||||
| clang::ParmVarDecl* PI = nullptr; | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: #includes are not sorted properly [llvm-include-order]