From 8bb789e4626688a2ccad3d5060f098ef59a2070e Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Tue, 20 Aug 2024 14:28:35 -0700 Subject: [PATCH] Introduce hpb::interop::upb and hpb::internal::backend::upb Migrate ::hpb::internal::GetMiniTable to ::hpb::interop::upb::GetMiniTable hpb.h now shows signs of backend-awareness, #including upb.h if the backend is known to be upb. If no backend is picked, we error out at this time since we solely support upb. ClearMessage has been migrated to backend-aware hpb. PiperOrigin-RevId: 665527832 --- hpb/BUILD | 4 ++++ hpb/backend/upb/BUILD | 38 +++++++++++++++++++++++++++++ hpb/backend/upb/interop.h | 30 +++++++++++++++++++++++ hpb/backend/upb/upb.h | 27 +++++++++++++++++++++ hpb/hpb.h | 45 +++++++++++++++++------------------ hpb/repeated_field.h | 3 ++- hpb_generator/gen_messages.cc | 8 +++---- protos/BUILD | 1 + protos/protos.h | 3 ++- 9 files changed, 130 insertions(+), 29 deletions(-) create mode 100644 hpb/backend/upb/BUILD create mode 100644 hpb/backend/upb/interop.h create mode 100644 hpb/backend/upb/upb.h diff --git a/hpb/BUILD b/hpb/BUILD index 3a05a8f35b..9c5dceb49d 100644 --- a/hpb/BUILD +++ b/hpb/BUILD @@ -32,6 +32,7 @@ cc_library( deps = [ ":hpb", ":traits", + "//hpb/backend/upb:interop", "//upb:base", "//upb:mem", "//upb:message", @@ -51,9 +52,12 @@ cc_library( ], compatible_with = ["//buildenv/target:non_prod"], copts = UPB_DEFAULT_CPPOPTS, + defines = ["HPB_BACKEND_UPB"], visibility = ["//visibility:public"], deps = [ "//hpb:ptr", + "//hpb/backend/upb", + "//hpb/backend/upb:interop", "//hpb/internal", "//hpb/internal:message_lock", "//hpb/internal:template_help", diff --git a/hpb/backend/upb/BUILD b/hpb/backend/upb/BUILD new file mode 100644 index 0000000000..0fb53571d5 --- /dev/null +++ b/hpb/backend/upb/BUILD @@ -0,0 +1,38 @@ +# Copyright (c) 2024, Google LLC +# All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd + +# begin:google_only +# package(default_applicable_licenses = ["//src/google/protobuf:license"]) +# end:google_only + +cc_library( + name = "upb", + hdrs = ["upb.h"], + compatible_with = ["//buildenv/target:non_prod"], + visibility = ["//upb:friends"], + deps = [ + ":interop", + "//hpb:ptr", + "//hpb/internal", + "//hpb/internal:template_help", + "//upb:mini_table", + ], +) + +cc_library( + name = "interop", + hdrs = ["interop.h"], + compatible_with = ["//buildenv/target:non_prod"], + visibility = [ + "//hpb:__subpackages__", + "//protos:__pkg__", + ], + deps = [ + "//hpb:ptr", + "//upb:mini_table", + ], +) diff --git a/hpb/backend/upb/interop.h b/hpb/backend/upb/interop.h new file mode 100644 index 0000000000..5cc338dae2 --- /dev/null +++ b/hpb/backend/upb/interop.h @@ -0,0 +1,30 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__ +#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__ + +// The sole public header in hpb/backend/upb + +#include "google/protobuf/hpb/ptr.h" +#include "upb/mini_table/message.h" + +namespace hpb::interop::upb { + +template +const upb_MiniTable* GetMiniTable(const T*) { + return T::minitable(); +} + +template +const upb_MiniTable* GetMiniTable(Ptr) { + return T::minitable(); +} + +} // namespace hpb::interop::upb + +#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__ diff --git a/hpb/backend/upb/upb.h b/hpb/backend/upb/upb.h new file mode 100644 index 0000000000..2e2c549b44 --- /dev/null +++ b/hpb/backend/upb/upb.h @@ -0,0 +1,27 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2024 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__ +#define GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__ + +#include "google/protobuf/hpb/backend/upb/interop.h" +#include "google/protobuf/hpb/internal/internal.h" +#include "google/protobuf/hpb/internal/template_help.h" +#include "google/protobuf/hpb/ptr.h" + +namespace hpb::internal::backend::upb { + +template +void ClearMessage(hpb::internal::PtrOrRaw message) { + auto ptr = Ptr(message); + auto minitable = hpb::interop::upb::GetMiniTable(ptr); + upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable); +} + +} // namespace hpb::internal::backend::upb + +#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_UPB_H__ diff --git a/hpb/hpb.h b/hpb/hpb.h index dcb27da1e9..afc977b5c1 100644 --- a/hpb/hpb.h +++ b/hpb/hpb.h @@ -15,6 +15,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" +#include "google/protobuf/hpb/backend/upb/interop.h" #include "google/protobuf/hpb/internal/internal.h" #include "google/protobuf/hpb/internal/template_help.h" #include "google/protobuf/hpb/ptr.h" @@ -25,6 +26,12 @@ #include "upb/wire/decode.h" #include "upb/wire/encode.h" +#ifdef HPB_BACKEND_UPB +#include "google/protobuf/hpb/backend/upb/upb.h" +#else +#error hpb backend must be specified +#endif + namespace hpb { class ExtensionRegistry; using Arena = ::upb::Arena; @@ -118,16 +125,6 @@ upb_Arena* GetArena(T* message) { return static_cast(message->GetInternalArena()); } -template -const upb_MiniTable* GetMiniTable(const T*) { - return T::minitable(); -} - -template -const upb_MiniTable* GetMiniTable(Ptr) { - return T::minitable(); -} - upb_ExtensionRegistry* GetUpbExtensions( const ExtensionRegistry& extension_registry); @@ -157,6 +154,10 @@ absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena, } // namespace internal +#ifdef HPB_BACKEND_UPB +namespace backend = ::hpb::internal::backend::upb; +#endif + class ExtensionRegistry { public: ExtensionRegistry( @@ -361,20 +362,18 @@ void DeepCopy(const T* source_message, T* target_message) { template void ClearMessage(hpb::internal::PtrOrRaw message) { - auto ptr = Ptr(message); - auto minitable = hpb::internal::GetMiniTable(ptr); - upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable); + backend::ClearMessage(message); } template ABSL_MUST_USE_RESULT bool Parse(Ptr message, absl::string_view bytes) { static_assert(!std::is_const_v); upb_Message_Clear(::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message)); + ::hpb::interop::upb::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), ::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message), + ::hpb::interop::upb::GetMiniTable(message), /* extreg= */ nullptr, /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; } @@ -385,11 +384,11 @@ ABSL_MUST_USE_RESULT bool Parse( const ::hpb::ExtensionRegistry& extension_registry) { static_assert(!std::is_const_v); upb_Message_Clear(::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message)); + ::hpb::interop::upb::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), ::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message), + ::hpb::interop::upb::GetMiniTable(message), /* extreg= */ ::hpb::internal::GetUpbExtensions(extension_registry), /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; @@ -407,11 +406,11 @@ template ABSL_MUST_USE_RESULT bool Parse(T* message, absl::string_view bytes) { static_assert(!std::is_const_v); upb_Message_Clear(::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message)); + ::hpb::interop::upb::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), ::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message), + ::hpb::interop::upb::GetMiniTable(message), /* extreg= */ nullptr, /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; } @@ -422,7 +421,7 @@ absl::StatusOr Parse(absl::string_view bytes, int options = 0) { auto* arena = static_cast(message.GetInternalArena()); upb_DecodeStatus status = upb_Decode(bytes.data(), bytes.size(), message.msg(), - ::hpb::internal::GetMiniTable(&message), + ::hpb::interop::upb::GetMiniTable(&message), /* extreg= */ nullptr, /* options= */ 0, arena); if (status == kUpb_DecodeStatus_Ok) { return message; @@ -438,7 +437,7 @@ absl::StatusOr Parse(absl::string_view bytes, auto* arena = static_cast(message.GetInternalArena()); upb_DecodeStatus status = upb_Decode(bytes.data(), bytes.size(), message.msg(), - ::hpb::internal::GetMiniTable(&message), + ::hpb::interop::upb::GetMiniTable(&message), ::hpb::internal::GetUpbExtensions(extension_registry), /* options= */ 0, arena); if (status == kUpb_DecodeStatus_Ok) { @@ -451,7 +450,7 @@ template absl::StatusOr Serialize(const T* message, upb::Arena& arena, int options = 0) { return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message), + ::hpb::interop::upb::GetMiniTable(message), arena.ptr(), options); } @@ -459,7 +458,7 @@ template absl::StatusOr Serialize(Ptr message, upb::Arena& arena, int options = 0) { return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message), - ::hpb::internal::GetMiniTable(message), + ::hpb::interop::upb::GetMiniTable(message), arena.ptr(), options); } diff --git a/hpb/repeated_field.h b/hpb/repeated_field.h index 49a60b7550..fd5a5dec0c 100644 --- a/hpb/repeated_field.h +++ b/hpb/repeated_field.h @@ -15,6 +15,7 @@ #include #include "absl/strings/string_view.h" +#include "google/protobuf/hpb/backend/upb/interop.h" #include "google/protobuf/hpb/hpb.h" #include "google/protobuf/hpb/repeated_field_iterator.h" #include "google/protobuf/hpb/traits.h" @@ -120,7 +121,7 @@ class RepeatedFieldProxy upb_MessageValue message_value; message_value.msg_val = upb_Message_DeepClone( ::hpb::internal::PrivateAccess::GetInternalMsg(&t), - ::hpb::internal::GetMiniTable(&t), this->arena_); + ::hpb::interop::upb::GetMiniTable(&t), this->arena_); upb_Array_Append(this->arr_, message_value, this->arena_); } diff --git a/hpb_generator/gen_messages.cc b/hpb_generator/gen_messages.cc index f683c970bd..450aed5e02 100644 --- a/hpb_generator/gen_messages.cc +++ b/hpb_generator/gen_messages.cc @@ -307,9 +307,9 @@ void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor, friend class ::hpb::Ptr<$0>; friend class ::hpb::Ptr; static const upb_MiniTable* minitable() { return $0::minitable(); } - friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>( + friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>( const $0Proxy* message); - friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0Proxy>( + friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0Proxy>( ::hpb::Ptr<$0Proxy> message); friend upb_Arena* ::hpb::internal::GetArena<$2>($2* message); friend upb_Arena* ::hpb::internal::GetArena<$2>(::hpb::Ptr<$2> message); @@ -356,9 +356,9 @@ void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor, friend class ::hpb::Ptr<$0>; friend class ::hpb::Ptr; static const upb_MiniTable* minitable() { return $0::minitable(); } - friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>( + friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>( const $0CProxy* message); - friend const upb_MiniTable* ::hpb::internal::GetMiniTable<$0CProxy>( + friend const upb_MiniTable* ::hpb::interop::upb::GetMiniTable<$0CProxy>( ::hpb::Ptr<$0CProxy> message); static void Rebind($0CProxy& lhs, const $0CProxy& rhs) { diff --git a/protos/BUILD b/protos/BUILD index ba1783eb5b..b25f8420a3 100644 --- a/protos/BUILD +++ b/protos/BUILD @@ -24,6 +24,7 @@ # visibility = ["//visibility:public"], # deps = [ # "//hpb", +# "//hpb/backend/upb:interop", # ], # ) # end:google_only diff --git a/protos/protos.h b/protos/protos.h index d1134340a9..a481f2195f 100644 --- a/protos/protos.h +++ b/protos/protos.h @@ -6,6 +6,7 @@ // https://developers.google.com/open-source/licenses/bsd #ifndef UPB_PROTOS_PROTOS_H_ #define UPB_PROTOS_PROTOS_H_ +#include "google/protobuf/hpb/backend/upb/interop.h" #include "google/protobuf/hpb/hpb.h" namespace protos { namespace internal { @@ -16,7 +17,6 @@ using hpb::internal::DeepCopy; using hpb::internal::ExtensionIdentifier; using hpb::internal::GetArena; using hpb::internal::GetInternalMsg; -using hpb::internal::GetMiniTable; using hpb::internal::GetOrPromoteExtension; using hpb::internal::GetUpbExtensions; using hpb::internal::HasExtensionOrUnknown; @@ -24,6 +24,7 @@ using hpb::internal::MoveExtension; using hpb::internal::PrivateAccess; using hpb::internal::Serialize; using hpb::internal::SetExtension; +using hpb::interop::upb::GetMiniTable; } // namespace internal using hpb::CreateMessage; using hpb::Parse;