From d13ca03a7550f8fdb4cc5ee89362e88ba4844990 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Wed, 26 Nov 2025 16:22:14 +0100 Subject: [PATCH] change all `enum` to `enum class` --- include/CppInterOp/CppInterOp.h | 39 +++++++++++++++---- lib/CppInterOp/CppInterOp.cpp | 6 +-- .../CppInterOp/FunctionReflectionTest.cpp | 8 ++-- unittests/CppInterOp/TypeReflectionTest.cpp | 10 ++--- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/include/CppInterOp/CppInterOp.h b/include/CppInterOp/CppInterOp.h index 79cffad12..adf696c2b 100644 --- a/include/CppInterOp/CppInterOp.h +++ b/include/CppInterOp/CppInterOp.h @@ -44,7 +44,19 @@ using TCppFuncAddr_t = void*; using TInterp_t = void*; using TCppObject_t = void*; -enum Operator : unsigned char { +template struct TruthValue { + T t; + constexpr TruthValue(T t) : t(t) {} + constexpr operator T() const { return t; } + constexpr explicit operator bool() const { return underlying(t); } +}; + +template using Underlying = std::underlying_type_t; +template constexpr Underlying underlying(T t) { + return Underlying(t); +} + +enum class Operator : unsigned char { OP_None, OP_New, OP_Delete, @@ -93,18 +105,31 @@ enum Operator : unsigned char { OP_Coawait, }; -enum OperatorArity : unsigned char { kUnary = 1, kBinary, kBoth }; +enum class OperatorArity : unsigned char { kUnary = 1, kBinary, kBoth }; + +constexpr TruthValue operator&(OperatorArity l, + OperatorArity r) { + return OperatorArity(underlying(l) & underlying(r)); +} + +constexpr TruthValue operator|(OperatorArity l, + OperatorArity r) { + return OperatorArity(underlying(l) | underlying(r)); +} /// Enum modelling CVR qualifiers. -enum QualKind : unsigned char { +enum class QualKind : unsigned char { Const = 1 << 0, Volatile = 1 << 1, Restrict = 1 << 2 }; -inline QualKind operator|(QualKind a, QualKind b) { - return static_cast(static_cast(a) | - static_cast(b)); +constexpr TruthValue operator&(QualKind l, QualKind r) { + return QualKind(underlying(l) & underlying(r)); +} + +constexpr TruthValue operator|(QualKind l, QualKind r) { + return QualKind(underlying(l) | underlying(r)); } /// A class modeling function calls for functions produced by the interpreter @@ -686,7 +711,7 @@ CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op); ///\returns list of operator overloads CPPINTEROP_API void GetOperator(TCppScope_t scope, Operator op, std::vector& operators, - OperatorArity kind = kBoth); + OperatorArity kind = OperatorArity::kBoth); /// Creates an owned instance of the interpreter we need for the various interop /// services and pushes it onto a stack. diff --git a/lib/CppInterOp/CppInterOp.cpp b/lib/CppInterOp/CppInterOp.cpp index 3a5a345f4..837b78bb2 100755 --- a/lib/CppInterOp/CppInterOp.cpp +++ b/lib/CppInterOp/CppInterOp.cpp @@ -3874,11 +3874,11 @@ OperatorArity GetOperatorArity(TCppFunction_t op) { #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ case OO_##Name: \ if ((Unary) && (Binary)) \ - return kBoth; \ + return OperatorArity::kBoth; \ if (Unary) \ - return kUnary; \ + return OperatorArity::kUnary; \ if (Binary) \ - return kBinary; \ + return OperatorArity::kBinary; \ break; #include "clang/Basic/OperatorKinds.def" default: diff --git a/unittests/CppInterOp/FunctionReflectionTest.cpp b/unittests/CppInterOp/FunctionReflectionTest.cpp index 96dd45477..afd3bb696 100644 --- a/unittests/CppInterOp/FunctionReflectionTest.cpp +++ b/unittests/CppInterOp/FunctionReflectionTest.cpp @@ -1858,7 +1858,7 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) { EXPECT_TRUE(toperator); std::vector operators; - Cpp::GetOperator(TOperator, Cpp::OP_Less, operators); + Cpp::GetOperator(TOperator, Cpp::Operator::OP_Less, operators); EXPECT_EQ(operators.size(), 1); Cpp::TCppScope_t op_templated = operators[0]; @@ -1901,8 +1901,8 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) { Cpp::TCppType_t K1 = Cpp::GetTypeFromScope(Cpp::GetNamed("K1")); Cpp::TCppType_t K2 = Cpp::GetTypeFromScope(Cpp::GetNamed("K2")); operators.clear(); - Cpp::GetOperator(Cpp::GetScope("N2", Cpp::GetScope("N1")), Cpp::OP_Plus, - operators); + Cpp::GetOperator(Cpp::GetScope("N2", Cpp::GetScope("N1")), + Cpp::Operator::OP_Plus, operators); EXPECT_EQ(operators.size(), 1); Cpp::TCppFunction_t kop = Cpp::BestOverloadFunctionMatch(operators, {}, {K1, K2}); @@ -2132,7 +2132,7 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestGetFunctionCallWrapper) { EXPECT_TRUE(KlassProduct_float); operators.clear(); - Cpp::GetOperator(KlassProduct_int, Cpp::OP_Star, operators); + Cpp::GetOperator(KlassProduct_int, Cpp::Operator::OP_Star, operators); EXPECT_EQ(operators.size(), 2); op = Cpp::BestOverloadFunctionMatch( diff --git a/unittests/CppInterOp/TypeReflectionTest.cpp b/unittests/CppInterOp/TypeReflectionTest.cpp index 2da308f2a..166a3341f 100644 --- a/unittests/CppInterOp/TypeReflectionTest.cpp +++ b/unittests/CppInterOp/TypeReflectionTest.cpp @@ -614,11 +614,11 @@ TYPED_TEST(CppInterOpTest, TypeReflectionTestIsFunctionPointerType) { } TYPED_TEST(CppInterOpTest, TypeReflectionTestOperatorSpelling) { - EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Less), "<"); - EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::OP_Plus), "+"); - EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::OP_Arrow); - EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::OP_Call); - EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::OP_None); + EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::Operator::OP_Less), "<"); + EXPECT_EQ(Cpp::GetSpellingFromOperator(Cpp::Operator::OP_Plus), "+"); + EXPECT_EQ(Cpp::GetOperatorFromSpelling("->"), Cpp::Operator::OP_Arrow); + EXPECT_EQ(Cpp::GetOperatorFromSpelling("()"), Cpp::Operator::OP_Call); + EXPECT_EQ(Cpp::GetOperatorFromSpelling("invalid"), Cpp::Operator::OP_None); } TYPED_TEST(CppInterOpTest, TypeReflectionTestTypeQualifiers) {