From 3ab43b874420f24bd09fb047f1a1720d449950c8 Mon Sep 17 00:00:00 2001 From: Mikhail Kornaukhov <66915223+mkornaukhov03@users.noreply.github.com> Date: Mon, 20 Jan 2025 11:42:46 +0300 Subject: [PATCH] Add to_underlying() and improve fmt usage (#1218) --- common/wrappers/to_underlying.h | 14 ++++++++++++++ compiler/code-gen/vertex-compiler.cpp | 3 ++- compiler/pipes/calc-const-types.cpp | 3 ++- compiler/vertex-meta_op_base.h | 5 +++-- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 common/wrappers/to_underlying.h diff --git a/common/wrappers/to_underlying.h b/common/wrappers/to_underlying.h new file mode 100644 index 0000000000..408f06e283 --- /dev/null +++ b/common/wrappers/to_underlying.h @@ -0,0 +1,14 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2025 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include + +namespace vk { +template +constexpr std::underlying_type_t to_underlying(Enum e) noexcept { + return static_cast>(e); +} +} // namespace vk diff --git a/compiler/code-gen/vertex-compiler.cpp b/compiler/code-gen/vertex-compiler.cpp index 0824d8a5f0..cd4c192d1d 100644 --- a/compiler/code-gen/vertex-compiler.cpp +++ b/compiler/code-gen/vertex-compiler.cpp @@ -10,6 +10,7 @@ #include "common/wrappers/field_getter.h" #include "common/wrappers/likely.h" #include "common/wrappers/string_view.h" +#include "common/wrappers/to_underlying.h" #include "compiler/code-gen/code-generator.h" #include "compiler/code-gen/common.h" #include "compiler/code-gen/const-globals-batched-mem.h" @@ -2522,7 +2523,7 @@ void compile_vertex(VertexPtr root, CodeGenerator &W) { compile_conv_op(root.as(), W); break; default: - fmt_print("{}: {}\n", tp, root->type()); + fmt_print("{}: {}\n", vk::to_underlying(tp), vk::to_underlying(root->type())); assert (0); break; } diff --git a/compiler/pipes/calc-const-types.cpp b/compiler/pipes/calc-const-types.cpp index 345d9d11b3..17af0b3033 100644 --- a/compiler/pipes/calc-const-types.cpp +++ b/compiler/pipes/calc-const-types.cpp @@ -4,6 +4,7 @@ #include "compiler/pipes/calc-const-types.h" +#include "common/wrappers/to_underlying.h" #include "compiler/data/class-data.h" #include "compiler/data/src-file.h" #include "compiler/data/var-data.h" @@ -63,7 +64,7 @@ VertexPtr CalcConstTypePass::on_exit_vertex(VertexPtr v) { break; } default: - kphp_error (0, fmt_format("Unknown cnst-type for [op = {}]", v->type())); + kphp_error (0, fmt_format("Unknown cnst-type for [op = {}]", vk::to_underlying(v->type()))); kphp_fail(); break; } diff --git a/compiler/vertex-meta_op_base.h b/compiler/vertex-meta_op_base.h index 8a547636ce..4bd5ba07e9 100644 --- a/compiler/vertex-meta_op_base.h +++ b/compiler/vertex-meta_op_base.h @@ -7,6 +7,7 @@ #include #include "common/wrappers/iterator_range.h" +#include "common/wrappers/to_underlying.h" #include "compiler/data/data_ptr.h" #include "compiler/data/vertex-adaptor.h" @@ -163,9 +164,9 @@ class vertex_inner { const Operation &type() const { return type_; } - virtual const std::string &get_string() const { kphp_fail_msg (fmt_format("not supported [{}:{}]", type_, OpInfo::str(type_))); } + virtual const std::string &get_string() const { kphp_fail_msg (fmt_format("not supported [{}:{}]", vk::to_underlying(type_), OpInfo::str(type_))); } - virtual void set_string(std::string) { kphp_fail_msg (fmt_format("not supported [{}:{}]", type_, OpInfo::str(type_))); } + virtual void set_string(std::string) { kphp_fail_msg (fmt_format("not supported [{}:{}]", vk::to_underlying(type_), OpInfo::str(type_))); } virtual bool has_get_string() const { return false; }