-
Notifications
You must be signed in to change notification settings - Fork 31
Add type info interfaces motivated by numba #534
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 |
---|---|---|
|
@@ -1549,11 +1549,47 @@ namespace Cpp { | |
return QT.isPODType(getASTContext()); | ||
} | ||
|
||
bool IsIntegerType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->hasIntegerRepresentation(); | ||
} | ||
|
||
bool IsIntegralType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->isIntegralType(getASTContext()); | ||
} | ||
|
||
bool IsSignedIntegerType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->hasSignedIntegerRepresentation(); | ||
} | ||
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 = (Decl *) var;
^ |
||
|
||
bool IsUnsignedIntegerType(TCppType_t type) { | ||
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: redundant boolean literal in conditional return statement [readability-simplify-boolean-expr] lib/Interpreter/CppInterOp.cpp:1565: - if (llvm::isa_and_nonnull<VarDecl>(D)) {
- return true;
- }
-
- return false;
+ return llvm::isa_and_nonnull<VarDecl>(D); |
||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->hasUnsignedIntegerRepresentation(); | ||
} | ||
|
||
bool IsFloatingType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->hasFloatingRepresentation(); | ||
} | ||
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 *) var;
^ |
||
|
||
bool IsSameType(TCppType_t type_a, TCppType_t type_b) { | ||
clang::QualType QT1 = clang::QualType::getFromOpaquePtr(type_a); | ||
clang::QualType QT2 = clang::QualType::getFromOpaquePtr(type_b); | ||
return getASTContext().hasSameType(QT1, QT2); | ||
} | ||
|
||
bool IsPointerType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->isPointerType(); | ||
} | ||
|
||
bool IsVoidPointerType(TCppType_t type) { | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT->isVoidPointerType(); | ||
} | ||
|
||
TCppType_t GetPointeeType(TCppType_t type) { | ||
if (!IsPointerType(type)) | ||
return nullptr; | ||
|
@@ -1573,8 +1609,17 @@ namespace Cpp { | |
return QT.getNonReferenceType().getAsOpaquePtr(); | ||
} | ||
|
||
TCppType_t GetUnqualifiedType(TCppType_t type) { | ||
if (!type) | ||
return nullptr; | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
return QT.getUnqualifiedType().getAsOpaquePtr(); | ||
} | ||
|
||
TCppType_t GetUnderlyingType(TCppType_t type) | ||
{ | ||
if (!type) | ||
return nullptr; | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
QT = QT->getCanonicalTypeUnqualified(); | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -600,3 +600,127 @@ TEST(TypeReflectionTest, IsFunctionPointerType) { | |||||
EXPECT_FALSE( | ||||||
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i")))); | ||||||
} | ||||||
|
||||||
TEST(TypeReflectionTest, IntegerTypes) { | ||||||
Cpp::CreateInterpreter(); | ||||||
std::vector<Decl*> Decls; | ||||||
std::string code = R"( | ||||||
int a; | ||||||
int *b; | ||||||
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: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]
Suggested change
|
||||||
double c; | ||||||
enum A { x, y }; | ||||||
A evar = x; | ||||||
char k; | ||||||
long int l; | ||||||
unsigned int m; | ||||||
unsigned long n; | ||||||
)"; | ||||||
|
||||||
GetAllTopLevelDecls(code, Decls); | ||||||
|
||||||
EXPECT_TRUE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[0]))); | ||||||
EXPECT_FALSE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[1]))); | ||||||
EXPECT_FALSE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[2]))); | ||||||
EXPECT_TRUE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[4]))); | ||||||
EXPECT_TRUE(Cpp::IsIntegralType(Cpp::GetVariableType(Decls[5]))); | ||||||
EXPECT_TRUE(Cpp::IsIntegralType(Cpp::GetVariableType(Decls[6]))); | ||||||
EXPECT_TRUE(Cpp::IsUnsignedIntegerType(Cpp::GetVariableType(Decls[7]))); | ||||||
EXPECT_FALSE(Cpp::IsSignedIntegerType(Cpp::GetVariableType(Decls[8]))); | ||||||
} | ||||||
|
||||||
TEST(TypeReflectionTest, VoidPtrType) { | ||||||
Cpp::CreateInterpreter(); | ||||||
std::vector<Decl*> Decls; | ||||||
std::string code = R"( | ||||||
class A {}; | ||||||
using VoidPtrType = void*; | ||||||
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: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]
Suggested change
|
||||||
VoidPtrType a = nullptr; | ||||||
void * b = nullptr; | ||||||
A *pa = nullptr; | ||||||
)"; | ||||||
|
||||||
GetAllTopLevelDecls(code, Decls); | ||||||
|
||||||
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetVariableType(Decls[2])), "VoidPtrType"); | ||||||
EXPECT_TRUE(Cpp::IsVoidPointerType(Cpp::GetVariableType(Decls[2]))); | ||||||
EXPECT_TRUE(Cpp::IsVoidPointerType(Cpp::GetVariableType(Decls[3]))); | ||||||
EXPECT_FALSE(Cpp::IsVoidPointerType(Cpp::GetVariableType(Decls[4]))); | ||||||
} | ||||||
|
||||||
TEST(TypeReflectionTest, IsSameType) { | ||||||
Cpp::CreateInterpreter(); | ||||||
std::vector<Decl*> Decls; | ||||||
|
||||||
std::string code = R"( | ||||||
#include <cstdarg> | ||||||
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: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]
Suggested change
|
||||||
#include <iostream> | ||||||
|
||||||
typedef std::va_list VaListAlias; | ||||||
std::va_list va1; | ||||||
VaListAlias va2; | ||||||
|
||||||
const int ci = 0; | ||||||
int const ic = 0; | ||||||
|
||||||
signed int si1 = 0; | ||||||
int si2 = 0; | ||||||
|
||||||
void *x; | ||||||
)"; | ||||||
|
||||||
|
||||||
GetAllTopLevelDecls(code, Decls); | ||||||
|
||||||
#include <iostream> | ||||||
|
||||||
ASTContext &Ctxt = Interp->getCI()->getASTContext(); | ||||||
|
||||||
Decls.assign(Decls.end() - 8, Decls.end()); | ||||||
|
||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("bool"), Ctxt.BoolTy.getAsOpaquePtr())); | ||||||
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: included header iostream is not used directly [misc-include-cleaner]
Suggested change
|
||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("float"), Ctxt.FloatTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("long"), Ctxt.LongTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("long long"), Ctxt.LongLongTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("short"), Ctxt.ShortTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("char"), Ctxt.SignedCharTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("unsigned char"), Ctxt.UnsignedCharTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetType("unsigned int"), Ctxt.UnsignedIntTy.getAsOpaquePtr())); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetVariableType(Decls[7]), Ctxt.VoidPtrTy.getAsOpaquePtr())); | ||||||
|
||||||
// Expect the typedef to std::va_list to be the same type | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetVariableType(Decls[1]), Cpp::GetVariableType(Decls[2]))); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetVariableType(Decls[3]), Cpp::GetVariableType(Decls[4]))); | ||||||
EXPECT_TRUE(Cpp::IsSameType(Cpp::GetVariableType(Decls[5]), Cpp::GetVariableType(Decls[6]))); | ||||||
} | ||||||
|
||||||
TEST(VariableReflectionTest, Is_Get_Reference) { | ||||||
Cpp::CreateInterpreter(); | ||||||
std::vector<Decl*> Decls; | ||||||
std::string code = R"( | ||||||
class A {}; | ||||||
int a; | ||||||
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: function 'TEST' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]
Suggested change
|
||||||
int &b = a; | ||||||
double c; | ||||||
double &d = c; | ||||||
A e; | ||||||
A &f = e; | ||||||
)"; | ||||||
|
||||||
GetAllTopLevelDecls(code, Decls); | ||||||
|
||||||
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[1]))); | ||||||
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[2]))); | ||||||
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[3]))); | ||||||
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[4]))); | ||||||
EXPECT_FALSE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[5]))); | ||||||
EXPECT_TRUE(Cpp::IsReferenceType(Cpp::GetVariableType(Decls[6]))); | ||||||
|
||||||
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[2])), | ||||||
Cpp::GetVariableType(Decls[1])); | ||||||
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[4])), | ||||||
Cpp::GetVariableType(Decls[3])); | ||||||
EXPECT_EQ(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[6])), | ||||||
Cpp::GetVariableType(Decls[5])); | ||||||
|
||||||
EXPECT_FALSE(Cpp::GetNonReferenceType(Cpp::GetVariableType(Decls[5]))); | ||||||
} |
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.
Probably we should have an enum instead of many interfaces..