-
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 |
---|---|---|
|
@@ -1597,11 +1597,44 @@ | |
return QT.isPODType(getASTContext()); | ||
} | ||
|
||
bool IsIntegerType(TCppType_t type, Signedness s) { | ||
if (!type) | ||
return false; | ||
QualType QT = QualType::getFromOpaquePtr(type); | ||
switch (s) { | ||
case Signedness::kAny: | ||
return QT->hasIntegerRepresentation(); | ||
|
||
case Signedness::kSigned: | ||
return QT->hasSignedIntegerRepresentation(); | ||
|
||
case Signedness::kUnsigned: | ||
return QT->hasUnsignedIntegerRepresentation(); | ||
} | ||
return false; | ||
} | ||
|
||
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; | ||
|
@@ -1621,8 +1654,17 @@ | |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -605,3 +605,111 @@ TEST(TypeReflectionTest, IsFunctionPointerType) { | |||||
EXPECT_FALSE( | ||||||
Cpp::IsFunctionPointerType(Cpp::GetVariableType(Cpp::GetNamed("i")))); | ||||||
} | ||||||
|
||||||
TEST(TypeReflectionTest, IntegerTypes) { | ||||||
aaronj0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Cpp::CreateInterpreter(); | ||||||
std::vector<Decl*> Decls; | ||||||
std::string code = R"( | ||||||
int a; | ||||||
int *b; | ||||||
aaronj0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
double c; | ||||||
enum A { x, y }; | ||||||
A evar = x; | ||||||
char k; | ||||||
long int l; | ||||||
unsigned int m; | ||||||
unsigned long n; | ||||||
)"; | ||||||
|
||||||
GetAllTopLevelDecls(code, Decls); | ||||||
|
||||||
// Signedness defaults to Any and returns true for both signed and unsigned | ||||||
// types. | ||||||
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::IsIntegerType(Cpp::GetVariableType(Decls[5]))); | ||||||
EXPECT_TRUE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[6]))); | ||||||
EXPECT_TRUE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[7]), | ||||||
Cpp::Signedness::kUnsigned)); | ||||||
EXPECT_FALSE(Cpp::IsIntegerType(Cpp::GetVariableType(Decls[8]), | ||||||
Cpp::Signedness::kSigned)); | ||||||
} | ||||||
|
||||||
TEST(TypeReflectionTest, VoidPtrType) { | ||||||
aaronj0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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) { | ||||||
aaronj0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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> | ||||||
|
||||||
aaronj0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ASTContext& Ctxt = Interp->getCI()->getASTContext(); | ||||||
|
||||||
Decls.assign(Decls.end() - 8, Decls.end()); | ||||||
|
||||||
EXPECT_TRUE( | ||||||
Cpp::IsSameType(Cpp::GetType("bool"), Ctxt.BoolTy.getAsOpaquePtr())); | ||||||
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]))); | ||||||
} |
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: enum 'Signedness' uses a larger base type ('unsigned int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size [performance-enum-size]