Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,16 @@ TypeRef GetEnumConstantType(ConstDeclRef DRef) {
return INTEROP_RETURN(nullptr);
}

size_t GetEnumConstantValue(ConstDeclRef DRef) {
int64_t GetEnumConstantValue(ConstDeclRef DRef) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a platform-independent typedef?

@aaronj0 aaronj0 Aug 31, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, int64_t is available on all platforms from <cstdint>. We already have API's using typedefs from cstdint: GetBaseClassOffset returns int64_t, and InsertOrReplaceJitSymbol accepts uint64_t param, which matches APSInt::getExtValue()

INTEROP_TRACE(DRef);
const auto* D = unwrap<clang::Decl>(DRef);
if (const auto* ECD = llvm::dyn_cast_or_null<clang::EnumConstantDecl>(D)) {
const llvm::APSInt& Val = ECD->getInitVal();
return INTEROP_RETURN(Val.getExtValue());
if (Val.isRepresentableByInt64())
return INTEROP_RETURN(Val.getExtValue());
// Do not round-trip through a string and std::stoul: unsigned long is
// 32 bit on LLP64/ILP32 and throws out_of_range for these values.
return INTEROP_RETURN((int64_t)Val.getZExtValue());
}
return INTEROP_RETURN(0);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/CppInterOp/CppInterOp.td
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,11 @@ def GetEnumConstantType : CppInterOpAPI {
}

def GetEnumConstantValue : CppInterOpAPI {
let Doc = [{Gets the index value (0,1,2, etcetera) of the enum constant
that was passed into this function.}];
let Doc = [{Gets the value of the enum constant that was passed into this
function, as its 64-bit bit pattern (values above INT64_MAX come back as
their two's complement representation).}];

let ReturnType = "size_t";
let ReturnType = "int64_t";
let Args = [
Arg<"ConstDeclRef", "DRef">
];
Expand Down
11 changes: 10 additions & 1 deletion unittests/CppInterOp/EnumReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnumConstantValue) {
MinusTen = -10,
MinusNine
};
enum Huge : unsigned long long {
Big = ((unsigned long long)1) << 63,
Max = 0xFFFFFFFFFFFFFFFFULL
};
int a = 10;
)";

Expand All @@ -250,7 +254,12 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnumConstantValue) {
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[4]), 54);
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[5]), -10);
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[6]), -9);
EXPECT_EQ(Cpp::GetEnumConstantValue(Decls[1]), 0); // Checking value of non enum constant
EXPECT_EQ(Cpp::GetEnumConstantValue(Decls[2]), 0); // Checking value of non enum constant

auto HugeConstants = Cpp::GetEnumConstants(Decls[1]);
EXPECT_EQ(Cpp::GetEnumConstantValue(HugeConstants[0]),
(int64_t)((uint64_t)1 << 63));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "int64_t" is directly included [misc-include-cleaner]

unittests/CppInterOp/EnumReflectionTest.cpp:7:

+ #include <cstdint>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "uint64_t" is directly included [misc-include-cleaner]

            (int64_t)((uint64_t)1 << 63));
                       ^

EXPECT_EQ(Cpp::GetEnumConstantValue(HugeConstants[1]), (int64_t)-1);
}

TYPED_TEST(CPPINTEROP_TEST_MODE, EnumReflection_GetEnums) {
Expand Down
Loading