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
pull/17885/head
Hong Shin 3 months ago committed by Copybara-Service
parent b817373e6b
commit 8bb789e462
  1. 4
      hpb/BUILD
  2. 38
      hpb/backend/upb/BUILD
  3. 30
      hpb/backend/upb/interop.h
  4. 27
      hpb/backend/upb/upb.h
  5. 45
      hpb/hpb.h
  6. 3
      hpb/repeated_field.h
  7. 8
      hpb_generator/gen_messages.cc
  8. 1
      protos/BUILD
  9. 3
      protos/protos.h

@ -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",

@ -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",
],
)

@ -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 <typename T>
const upb_MiniTable* GetMiniTable(const T*) {
return T::minitable();
}
template <typename T>
const upb_MiniTable* GetMiniTable(Ptr<T>) {
return T::minitable();
}
} // namespace hpb::interop::upb
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_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 <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> 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__

@ -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<upb_Arena*>(message->GetInternalArena());
}
template <typename T>
const upb_MiniTable* GetMiniTable(const T*) {
return T::minitable();
}
template <typename T>
const upb_MiniTable* GetMiniTable(Ptr<T>) {
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 <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
auto ptr = Ptr(message);
auto minitable = hpb::internal::GetMiniTable(ptr);
upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable);
backend::ClearMessage(message);
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(Ptr<T> message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(message));
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(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<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(message));
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(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 <typename T>
ABSL_MUST_USE_RESULT bool Parse(T* message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(::hpb::internal::GetInternalMsg(message),
::hpb::internal::GetMiniTable(message));
::hpb::interop::upb::GetMiniTable(message));
auto* arena = static_cast<upb_Arena*>(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<T> Parse(absl::string_view bytes, int options = 0) {
auto* arena = static_cast<upb_Arena*>(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<T> Parse(absl::string_view bytes,
auto* arena = static_cast<upb_Arena*>(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 <typename T>
absl::StatusOr<absl::string_view> 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 <typename T>
absl::StatusOr<absl::string_view> Serialize(Ptr<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);
}

@ -15,6 +15,7 @@
#include <type_traits>
#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_);
}

@ -307,9 +307,9 @@ void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor,
friend class ::hpb::Ptr<$0>;
friend class ::hpb::Ptr<const $0>;
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<const $0>;
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) {

@ -24,6 +24,7 @@
# visibility = ["//visibility:public"],
# deps = [
# "//hpb",
# "//hpb/backend/upb:interop",
# ],
# )
# end:google_only

@ -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;

Loading…
Cancel
Save