hpb: utilize layout_index as opposed to raw index

.index() is dependent on the order specified in the .proto file.
Minitables create their own ordering, which represent the true index that we're interested in set_alias.

This can be fetched via .layout_index when we have a upb::FieldDefPtr.

PiperOrigin-RevId: 691825782
pull/19044/head
Hong Shin 5 months ago committed by Copybara-Service
parent 163cff9070
commit b72722a8d6
  1. 22
      hpb_generator/context.h
  2. 4
      hpb_generator/gen_accessors.cc
  3. 8
      hpb_generator/protoc-gen-hpb.cc

@ -9,7 +9,6 @@
#define GOOGLE_PROTOBUF_COMPILER_HPB_CONTEXT_H__ #define GOOGLE_PROTOBUF_COMPILER_HPB_CONTEXT_H__
#include <string> #include <string>
#include <utility>
#include "absl/strings/ascii.h" #include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@ -21,7 +20,9 @@
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/io/printer.h" #include "google/protobuf/io/printer.h"
#include "google/protobuf/io/zero_copy_stream.h" #include "google/protobuf/io/zero_copy_stream.h"
#include "upb_generator/c/names.h" #include "upb/reflection/def.hpp"
#include "upb_generator/common/cpp_to_upb_def.h"
namespace google::protobuf::hpb_generator { namespace google::protobuf::hpb_generator {
enum class Backend { UPB, CPP }; enum class Backend { UPB, CPP };
@ -44,8 +45,11 @@ struct Options {
*/ */
class Context final { class Context final {
public: public:
Context(io::ZeroCopyOutputStream* stream, const Options& options) Context(const FileDescriptor* file, io::ZeroCopyOutputStream* stream,
: stream_(stream), printer_(stream_), options_(options) {} const Options& options)
: stream_(stream), printer_(stream_), options_(options) {
BuildDefPool(file);
}
void Emit(absl::Span<const io::Printer::Sub> vars, absl::string_view format, void Emit(absl::Span<const io::Printer::Sub> vars, absl::string_view format,
absl::SourceLocation loc = absl::SourceLocation::current()) { absl::SourceLocation loc = absl::SourceLocation::current()) {
@ -68,15 +72,25 @@ class Context final {
const Options& options() { return options_; } const Options& options() { return options_; }
io::Printer& printer() { return printer_; } io::Printer& printer() { return printer_; }
inline std::string GetLayoutIndex(const FieldDescriptor* field) {
return absl::StrCat(
upb::generator::FindBaseFieldDef(pool_, field).layout_index());
}
Context(const Context&) = delete; Context(const Context&) = delete;
Context& operator=(const Context&) = delete; Context& operator=(const Context&) = delete;
Context(Context&&) = delete; Context(Context&&) = delete;
Context& operator=(Context&&) = delete; Context& operator=(Context&&) = delete;
private: private:
inline void BuildDefPool(const FileDescriptor* file) {
upb::generator::AddFile(file, &pool_);
}
io::ZeroCopyOutputStream* stream_; io::ZeroCopyOutputStream* stream_;
io::Printer printer_; io::Printer printer_;
const Options& options_; const Options& options_;
upb::DefPool pool_;
}; };
// TODO: b/373438292 - re-house these 4 legacy funcs post io::Printer move // TODO: b/373438292 - re-house these 4 legacy funcs post io::Printer move

@ -286,7 +286,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
ABSL_CHECK(upb_Arena_IsFused(arena_, hpb::interop::upb::GetArena(target))); ABSL_CHECK(upb_Arena_IsFused(arena_, hpb::interop::upb::GetArena(target)));
upb_Message_SetBaseFieldMessage( upb_Message_SetBaseFieldMessage(
UPB_UPCAST(msg_), UPB_UPCAST(msg_),
upb_MiniTable_FindFieldByNumber($7::minitable(), $8), upb_MiniTable_GetFieldByIndex($7::minitable(), $8),
hpb::interop::upb::GetMessage(target)); hpb::interop::upb::GetMessage(target));
} }
)cc", )cc",
@ -294,7 +294,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
resolved_field_name, resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()), upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name, MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name,
arena_expression, ClassName(desc), field->number()); arena_expression, ClassName(desc), ctx.GetLayoutIndex(field));
} }
} }
} }

@ -81,20 +81,22 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
// Write model.upb.fwd.h // Write model.upb.fwd.h
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output_stream( std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output_stream(
context->Open(ForwardingHeaderFilename(file))); context->Open(ForwardingHeaderFilename(file)));
auto fwd_ctx = Context(output_stream.get(), Options{.backend = Backend::UPB}); Context fwd_ctx =
Context(file, output_stream.get(), Options{.backend = Backend::UPB});
WriteForwardingHeader(file, fwd_ctx); WriteForwardingHeader(file, fwd_ctx);
// Write model.upb.proto.h // Write model.upb.proto.h
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> header_output_stream( std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> header_output_stream(
context->Open(CppHeaderFilename(file))); context->Open(CppHeaderFilename(file)));
Context hdr_ctx(header_output_stream.get(), Options{.backend = Backend::UPB}); Context hdr_ctx(file, header_output_stream.get(),
Options{.backend = Backend::UPB});
WriteHeader(file, hdr_ctx, strip_nonfunctional_codegen); WriteHeader(file, hdr_ctx, strip_nonfunctional_codegen);
// Write model.upb.proto.cc // Write model.upb.proto.cc
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> cc_output_stream( std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> cc_output_stream(
context->Open(CppSourceFilename(file))); context->Open(CppSourceFilename(file)));
auto cc_ctx = auto cc_ctx =
Context(cc_output_stream.get(), Options{.backend = Backend::UPB}); Context(file, cc_output_stream.get(), Options{.backend = Backend::UPB});
WriteSource(file, cc_ctx, fasttable_enabled, strip_nonfunctional_codegen); WriteSource(file, cc_ctx, fasttable_enabled, strip_nonfunctional_codegen);
return true; return true;
} }

Loading…
Cancel
Save