hpb: expunge MessageName in favor of upb::generator::CApiMessageType

PiperOrigin-RevId: 686896344
pull/18855/head
Hong Shin 1 month ago committed by Copybara-Service
parent b956147ce1
commit d80ac3a7e6
  1. 5
      hpb_generator/context.h
  2. 98
      hpb_generator/gen_accessors.cc
  3. 28
      hpb_generator/gen_messages.cc
  4. 41
      hpb_generator/gen_repeated_fields.cc

@ -8,7 +8,6 @@
#ifndef GOOGLE_PROTOBUF_COMPILER_HPB_CONTEXT_H__
#define GOOGLE_PROTOBUF_COMPILER_HPB_CONTEXT_H__
#include <memory>
#include <string>
#include <utility>
@ -80,10 +79,6 @@ class Context final {
};
// TODO: b/373438292 - re-house these 4 legacy funcs post io::Printer move
inline std::string MessageName(const google::protobuf::Descriptor* descriptor) {
return upb::generator::CApiMessageType(descriptor->full_name());
}
inline std::string ToCIdent(absl::string_view str) {
return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}});
}

@ -128,7 +128,8 @@ void WriteFieldAccessorsInHeader(const protobuf::Descriptor* desc,
inline $0 $1() const { return $2_$3(msg_); }
inline void set_$1($0 value) { return $2_set_$3(msg_, value); }
)cc",
CppConstType(field), resolved_field_name, MessageName(desc),
CppConstType(field), resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()),
resolved_upbc_name);
}
}
@ -144,7 +145,9 @@ void WriteFieldAccessorHazzer(const protobuf::Descriptor* desc,
if (field->has_presence()) {
// Has presence.
ctx.EmitLegacy("inline bool has_$0() const { return $1_has_$2(msg_); }\n",
resolved_field_name, MessageName(desc), resolved_upbc_name);
resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()),
resolved_upbc_name);
}
}
@ -155,7 +158,8 @@ void WriteFieldAccessorClear(const protobuf::Descriptor* desc,
Context& ctx) {
if (field->has_presence()) {
ctx.EmitLegacy("void clear_$0() { $2_clear_$1(msg_); }\n",
resolved_field_name, resolved_upbc_name, MessageName(desc));
resolved_field_name, resolved_upbc_name,
upb::generator::CApiMessageType(desc->full_name()));
}
}
@ -173,8 +177,8 @@ void WriteMapFieldAccessors(const protobuf::Descriptor* desc,
inline void clear_$0() { $1_clear_$3(msg_); }
void delete_$0($2 key);
)cc",
resolved_field_name, MessageName(desc), CppConstType(key),
resolved_upbc_name);
resolved_field_name, upb::generator::CApiMessageType(desc->full_name()),
CppConstType(key), resolved_upbc_name);
if (val->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
ctx.Emit({{"field_name", resolved_field_name},
@ -240,7 +244,8 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(desc), resolved_upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
resolved_upbc_name);
// Set string.
ctx.EmitLegacy(
R"cc(
@ -249,7 +254,9 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
}
)cc",
class_name, CppConstType(field), resolved_field_name,
resolved_upbc_name, MessageName(desc), arena_expression);
resolved_upbc_name,
upb::generator::CApiMessageType(desc->full_name()),
arena_expression);
} else if (field->cpp_type() ==
protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
ctx.EmitLegacy(
@ -263,7 +270,8 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
}
)cc",
class_name, MessagePtrConstType(field, /* is_const */ true),
resolved_field_name, MessageName(desc),
resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false),
resolved_upbc_name);
@ -282,7 +290,8 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
}
)cc",
class_name, MessagePtrConstType(field, /* is_const */ false),
resolved_field_name, MessageName(desc),
resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), resolved_upbc_name,
arena_expression, ClassName(desc), field->index());
}
@ -292,7 +301,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Context& ctx) {
ctx.Emit("} // namespace internal\n\n");
}
void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
void WriteMapAccessorDefinitions(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view class_name,
@ -322,9 +331,10 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ true), MessageName(message),
MessageName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name,
MessagePtrConstType(val, /* is_const */ true),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
optional_conversion_code, converted_key_name, upbc_name,
::upb::generator::MiniTableMessageVarName(
val->message_type()->full_name()));
ctx.EmitLegacy(
@ -337,9 +347,10 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ false), MessageName(message),
MessageName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name,
MessagePtrConstType(val, /* is_const */ false),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
optional_conversion_code, converted_key_name, upbc_name,
::upb::generator::MiniTableMessageVarName(
val->message_type()->full_name()));
ctx.EmitLegacy(
@ -350,9 +361,10 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ true), MessageName(message),
MessageName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name);
MessagePtrConstType(val, /* is_const */ true),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
bool $0::set_alias_$1($2 key, $3 value) {
@ -361,9 +373,10 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ false), MessageName(message),
MessageName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name);
MessagePtrConstType(val, /* is_const */ false),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
absl::StatusOr<$3> $0::get_$1($2 key) {
@ -376,8 +389,9 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ true), MessageName(message),
MessageName(val->message_type()),
MessagePtrConstType(val, /* is_const */ true),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
QualifiedClassName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name);
ctx.Emit(
@ -385,8 +399,9 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
{"hpb_field_name", resolved_field_name},
{"const_key", CppConstType(key)},
{"PtrMut", MessagePtrConstType(val, false)},
{"upb_msg_name", MessageName(message)},
{"return_type", MessageName(val->message_type())},
{"upb_msg_name", upb::generator::CApiMessageType(desc->full_name())},
{"return_type",
upb::generator::CApiMessageType(val->message_type()->full_name())},
{"proto_class", QualifiedClassName(val->message_type())},
{"optional_conversion_code", optional_conversion_code},
{"converted_key_name", converted_key_name},
@ -410,9 +425,10 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
void $0::delete_$1($2 key) { $6$4_$8_delete(msg_, $7); }
)cc",
class_name, resolved_field_name, CppConstType(key),
MessagePtrConstType(val, /* is_const */ false), MessageName(message),
MessageName(val->message_type()), optional_conversion_code,
converted_key_name, upbc_name);
MessagePtrConstType(val, /* is_const */ false),
upb::generator::CApiMessageType(desc->full_name()),
upb::generator::CApiMessageType(val->message_type()->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
} else if (val->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING) {
ctx.EmitLegacy(
R"cc(
@ -423,8 +439,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
absl::StatusOr<$3> $0::get_$1($2 key) {
@ -437,15 +453,15 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
void $0::delete_$1($2 key) { $5$4_$7_delete(msg_, $6); }
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
} else {
ctx.EmitLegacy(
R"cc(
@ -454,8 +470,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
absl::StatusOr<$3> $0::get_$1($2 key) {
@ -468,15 +484,15 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
ctx.EmitLegacy(
R"cc(
void $0::delete_$1($2 key) { $5$4_$7_delete(msg_, $6); }
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),
MessageName(message), optional_conversion_code, converted_key_name,
upbc_name);
upb::generator::CApiMessageType(desc->full_name()),
optional_conversion_code, converted_key_name, upbc_name);
}
}

@ -22,6 +22,7 @@
#include "google/protobuf/compiler/hpb/gen_utils.h"
#include "google/protobuf/compiler/hpb/names.h"
#include "google/protobuf/descriptor.h"
#include "upb_generator/c/names.h"
#include "upb_generator/minitable/names.h"
namespace google::protobuf::hpb_generator {
@ -101,7 +102,8 @@ void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
assert(arena != nullptr);
} // NOLINT
)cc",
ClassName(descriptor), MessageName(descriptor));
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
WriteFieldAccessorsInHeader(descriptor, ctx);
WriteOneofAccessorsInHeader(descriptor, ctx);
ctx.EmitLegacy(
@ -114,7 +116,8 @@ void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
$1* msg_;
upb_Arena* arena_;
)cc",
ClassName(descriptor), MessageName(descriptor),
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n");
}
@ -203,7 +206,8 @@ void WriteModelPublicDeclaration(
)cc",
ClassName(descriptor),
::upb::generator::MiniTableMessageVarName(descriptor->full_name()),
MessageName(descriptor), QualifiedClassName(descriptor));
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessage, ctx);
WriteUsingEnumsInHeader(descriptor, file_enums, ctx);
@ -252,7 +256,8 @@ void WriteModelPublicDeclaration(
friend $0(hpb::interop::upb::MoveMessage<$0>(upb_Message* msg,
upb_Arena* arena));
)cc",
ClassName(descriptor), MessageName(descriptor),
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n\n");
}
@ -312,7 +317,8 @@ void WriteModelProxyDeclaration(const protobuf::Descriptor* descriptor,
lhs.arena_ = rhs.arena_;
}
)cc",
ClassName(descriptor), MessageName(descriptor),
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n\n");
}
@ -329,7 +335,8 @@ void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
: internal::$0Access(m->msg_, hpb::interop::upb::GetArena(m)) {}
$0CProxy($0Proxy m);
)cc",
ClassName(descriptor), MessageName(descriptor));
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessageCProxy,
ctx);
@ -358,7 +365,8 @@ void WriteModelCProxyDeclaration(const protobuf::Descriptor* descriptor,
lhs.arena_ = rhs.arena_;
}
)cc",
ClassName(descriptor), MessageName(descriptor));
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
ctx.Emit("};\n\n");
}
@ -407,7 +415,8 @@ void WriteMessageImplementation(
return *this;
}
)cc",
ClassName(descriptor), MessageName(descriptor),
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
::upb::generator::MiniTableMessageVarName(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("\n");
@ -436,7 +445,8 @@ void WriteMessageImplementation(
}
$0DefaultTypeInternal _$0_default_instance_ = _$0DefaultTypeBuilder();
)cc",
ClassName(descriptor), MessageName(descriptor));
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
ctx.EmitLegacy(
R"cc(

@ -81,7 +81,8 @@ void WriteRepeatedFieldsInMessageHeader(const protobuf::Descriptor* desc,
return len;
}
)cc",
MessageName(desc), resolved_field_name, resolved_upbc_name);
upb::generator::CApiMessageType(desc->full_name()), resolved_field_name,
resolved_upbc_name);
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
ctx.EmitLegacy(
@ -129,7 +130,7 @@ void WriteRepeatedFieldsInMessageHeader(const protobuf::Descriptor* desc,
}
}
void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message,
void WriteRepeatedMessageAccessor(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view class_name,
@ -147,7 +148,7 @@ void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, MessagePtrConstType(field, /* is_const */ true),
resolved_field_name, MessageName(message),
resolved_field_name, upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), upbc_name);
ctx.EmitLegacy(
R"cc(
@ -172,9 +173,9 @@ void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, MessagePtrConstType(field, /* const */ false),
resolved_field_name, MessageName(message),
resolved_field_name, upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), arena_expression,
upbc_name, ClassName(message), field->index(),
upbc_name, ClassName(desc), field->index(),
upb::generator::CApiMessageType(field->message_type()->full_name()));
ctx.EmitLegacy(
R"cc(
@ -186,7 +187,7 @@ void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, MessagePtrConstType(field, /* is_const */ false),
resolved_field_name, MessageName(message),
resolved_field_name, upb::generator::CApiMessageType(desc->full_name()),
MessageBaseType(field, /* maybe_const */ false), arena_expression,
upbc_name);
ctx.EmitLegacy(
@ -205,14 +206,14 @@ void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message,
class_name, // $0
MessageBaseType(field, /* maybe_const */ false), // $1
resolved_field_name, // $2
MessageName(message), // $3
upb::generator::CApiMessageType(desc->full_name()), // $3
upbc_name, // $4
upb::generator::kRepeatedFieldArrayGetterPostfix, // $5
upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6
);
}
void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
void WriteRepeatedStringAccessor(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view class_name,
@ -228,14 +229,15 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
bool $0::resize_$1(size_t len) {
return $2_resize_$3(msg_, len, arena_);
}
)cc",
class_name, resolved_field_name, MessageName(message), upbc_name);
class_name, resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
bool $0::add_$2($1 val) {
@ -245,7 +247,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
void $0::set_$2(size_t index, $1 val) {
@ -256,7 +258,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
const ::hpb::RepeatedField<$1>::CProxy $0::$2() const {
@ -273,14 +275,14 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
class_name, // $0
CppConstType(field), // $1
resolved_field_name, // $2
MessageName(message), // $3
upb::generator::CApiMessageType(desc->full_name()), // $3
upbc_name, // $4
upb::generator::kRepeatedFieldArrayGetterPostfix, // $5
upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6
);
}
void WriteRepeatedScalarAccessor(const protobuf::Descriptor* message,
void WriteRepeatedScalarAccessor(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view class_name,
@ -296,20 +298,21 @@ void WriteRepeatedScalarAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
bool $0::resize_$1(size_t len) {
return $2_resize_$3(msg_, len, arena_);
}
)cc",
class_name, resolved_field_name, MessageName(message), upbc_name);
class_name, resolved_field_name,
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
bool $0::add_$2($1 val) { return $3_add_$4(msg_, val, arena_); }
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
void $0::set_$2(size_t index, $1 val) {
@ -320,7 +323,7 @@ void WriteRepeatedScalarAccessor(const protobuf::Descriptor* message,
}
)cc",
class_name, CppConstType(field), resolved_field_name,
MessageName(message), upbc_name);
upb::generator::CApiMessageType(desc->full_name()), upbc_name);
ctx.EmitLegacy(
R"cc(
const ::hpb::RepeatedField<$1>::CProxy $0::$2() const {
@ -337,7 +340,7 @@ void WriteRepeatedScalarAccessor(const protobuf::Descriptor* message,
class_name, // $0
CppConstType(field), // $1
resolved_field_name, // $2
MessageName(message), // $3
upb::generator::CApiMessageType(desc->full_name()), // $3
upbc_name, // $4
upb::generator::kRepeatedFieldArrayGetterPostfix, // $5
upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6

Loading…
Cancel
Save