From 0abfe473322a8824a2889854763f5249781d715a Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Thu, 1 May 2025 13:20:17 -0700 Subject: [PATCH] hpb: skeleton impl for hpb(cpp) to plumb ::Serialize PiperOrigin-RevId: 753703483 --- hpb_generator/context.h | 28 ++++++++++---------- hpb_generator/protoc-gen-hpb.cc | 46 ++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/hpb_generator/context.h b/hpb_generator/context.h index 5c1ee3130526a..9dbc4a05ed43d 100644 --- a/hpb_generator/context.h +++ b/hpb_generator/context.h @@ -103,20 +103,22 @@ inline std::string ToPreproc(absl::string_view str) { return absl::AsciiStrToUpper(ToCIdent(str)); } +inline std::string StringifiedBackend(Context& ctx) { + return ctx.options().backend == Backend::UPB ? "upb" : "cpp"; +} + inline void EmitFileWarning(const google::protobuf::FileDescriptor* file, Context& ctx) { - ctx.EmitLegacy( - R"cc( - /* This file was generated by hpb_generator (Handle Protobuf) " - from the input - * file: - * - * $0 - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ - )cc", - file->name()); - ctx.Emit("\n"); + ctx.Emit({{"file_name", file->name()}, {"backend", StringifiedBackend(ctx)}}, + R"cc( + /* This file was generated by hpb ($backend$) " + from the input + * file: + * + * $file_name$ + * + * Do not edit -- your changes will be discarded when the file is + * regenerated. */ + )cc"); } // TODO: b/346865271 append ::hpb instead of ::protos after namespace swap diff --git a/hpb_generator/protoc-gen-hpb.cc b/hpb_generator/protoc-gen-hpb.cc index 8266d8750568c..4d658f437f7c5 100644 --- a/hpb_generator/protoc-gen-hpb.cc +++ b/hpb_generator/protoc-gen-hpb.cc @@ -30,8 +30,8 @@ namespace protobuf = ::proto2; using FileDescriptor = ::google::protobuf::FileDescriptor; using google::protobuf::Edition; -void WriteSource(const protobuf::FileDescriptor* file, Context& ctx); void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx); +void WriteSource(const protobuf::FileDescriptor* file, Context& ctx); void WriteMessageImplementations(const protobuf::FileDescriptor* file, Context& ctx); void WriteTypedefForwardingHeader( @@ -103,10 +103,42 @@ void WriteForwardDecls(const protobuf::FileDescriptor* file, Context& ctx) { } void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx) { - if (ctx.options().backend == Backend::CPP) { - abort(); - } EmitFileWarning(file, ctx); + + if (ctx.options().backend == Backend::CPP || true) { + const auto msgs = SortedMessages(file); + auto ns = absl::StrCat(absl::StrReplaceAll(file->package(), {{".", "::"}}), + "::hpb"); + for (auto message : msgs) { + ctx.Emit({{"class_name", ClassName(message)}, {"namespace", ns}}, + R"cc( + + namespace $namespace$ { + + class $class_name$ { + public: + using CProxy = bool; + using Proxy = bool; + using Access = bool; + + $class_name$() = default; + + $class_name$(void* msg) : msg_(msg) {} + + void* msg() const { return msg_; } + + // $class_name$::$class_name$(int x): x_(x) { + // } + + // friend class hpb::internal::backend::cpp::Shim; + // friend struct hpb::internal::PrivateAccess; + private: + void* msg_; + }; + } // namespace $namespace$ + )cc"); + } + } ctx.EmitLegacy( R"cc( #ifndef $0_HPB_PROTO_H_ @@ -171,10 +203,10 @@ void WriteHeader(const protobuf::FileDescriptor* file, Context& ctx) { // Writes a .hpb.cc source file. void WriteSource(const protobuf::FileDescriptor* file, Context& ctx) { - if (ctx.options().backend == Backend::CPP) { - abort(); - } EmitFileWarning(file, ctx); + if (ctx.options().backend == Backend::CPP || true) { + return; + } ctx.EmitLegacy( R"cc(