hpb: update gen_enums.cc and start gen_messages from .EmitLegacy to .Emit

PiperOrigin-RevId: 690763137
pull/18993/head
Hong Shin 3 months ago committed by Copybara-Service
parent 0ad1d45e2e
commit 15487415e4
  1. 24
      hpb_generator/gen_enums.cc
  2. 111
      hpb_generator/gen_messages.cc

@ -21,6 +21,7 @@
namespace google::protobuf::hpb_generator {
namespace protobuf = ::proto2;
using Sub = protobuf::io::Printer::Sub;
// Convert enum value to C++ literal.
//
@ -94,21 +95,26 @@ void WriteEnumValues(const protobuf::EnumDescriptor* desc, Context& ctx) {
for (size_t i = 0; i < values.size(); i++) {
auto value = values[i];
ctx.EmitLegacy(" $0", EnumValueSymbolInNameSpace(desc, value));
ctx.EmitLegacy(" = $0", EnumInt32ToString(value->number()));
if (i != values.size() - 1) {
ctx.Emit(",");
}
ctx.Emit("\n");
ctx.Emit({{"name", EnumValueSymbolInNameSpace(desc, value)},
{"number", EnumInt32ToString(value->number())},
{"sep", i == values.size() - 1 ? "" : ","}},
R"cc(
$name$ = $number$$sep$
)cc");
}
}
void WriteEnumDeclarations(
const std::vector<const protobuf::EnumDescriptor*>& enums, Context& ctx) {
for (auto enumdesc : enums) {
ctx.EmitLegacy("enum $0 : int {\n", EnumTypeName(enumdesc));
WriteEnumValues(enumdesc, ctx);
ctx.Emit("};\n\n");
ctx.Emit({{"type", EnumTypeName(enumdesc)},
Sub("enum_vals", [&] { WriteEnumValues(enumdesc, ctx); })
.WithSuffix(",")},
R"cc(
enum $type$ : int {
$enum_vals$,
};
)cc");
}
}

@ -28,6 +28,7 @@
namespace google::protobuf::hpb_generator {
namespace protobuf = ::proto2;
using Sub = protobuf::io::Printer::Sub;
void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx);
@ -73,53 +74,77 @@ void WriteMessageClassDeclarations(
}
// Forward declaration of Proto Class for GCC handling of free friend method.
ctx.EmitLegacy("class $0;\n", ClassName(descriptor));
ctx.Emit("namespace internal {\n\n");
WriteModelAccessDeclaration(descriptor, ctx);
ctx.Emit("\n");
WriteInternalForwardDeclarationsInHeader(descriptor, ctx);
ctx.Emit("\n");
ctx.Emit("} // namespace internal\n\n");
WriteModelPublicDeclaration(descriptor, file_exts, file_enums, ctx);
ctx.Emit("namespace internal {\n");
WriteModelCProxyDeclaration(descriptor, ctx);
WriteModelProxyDeclaration(descriptor, ctx);
ctx.Emit("} // namespace internal\n\n");
ctx.Emit(
{Sub("class_name", ClassName(descriptor)),
Sub("model_access",
[&] { WriteModelAccessDeclaration(descriptor, ctx); })
.WithSuffix(";"),
Sub("fwd_decl",
[&] { WriteInternalForwardDeclarationsInHeader(descriptor, ctx); })
.WithSuffix(";"),
Sub("public_decl",
[&] {
WriteModelPublicDeclaration(descriptor, file_exts, file_enums,
ctx);
})
.WithSuffix(";"),
Sub("cproxy_decl", [&] { WriteModelCProxyDeclaration(descriptor, ctx); })
.WithSuffix(";"),
Sub("proxy_decl", [&] { WriteModelProxyDeclaration(descriptor, ctx); })
.WithSuffix(";")},
R"cc(
class $class_name$;
namespace internal {
$model_access$;
$fwd_decl$;
} // namespace internal
$public_decl$;
namespace internal {
$cproxy_decl$;
$proxy_decl$;
} // namespace internal
)cc");
}
void WriteModelAccessDeclaration(const protobuf::Descriptor* descriptor,
Context& ctx) {
ctx.EmitLegacy(
R"cc(
class $0Access {
public:
$0Access() {}
$0Access($1* msg, upb_Arena* arena) : msg_(msg), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
$0Access(const $1* msg, upb_Arena* arena)
: msg_(const_cast<$1*>(msg)), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()));
WriteFieldAccessorsInHeader(descriptor, ctx);
WriteOneofAccessorsInHeader(descriptor, ctx);
ctx.EmitLegacy(
R"cc(
private:
friend class $2;
friend class $0Proxy;
friend class $0CProxy;
friend struct ::hpb::internal::PrivateAccess;
$1* msg_;
upb_Arena* arena_;
)cc",
ClassName(descriptor),
upb::generator::CApiMessageType(descriptor->full_name()),
QualifiedClassName(descriptor));
ctx.Emit("};\n");
ctx.Emit({Sub("class_name", ClassName(descriptor)),
Sub("qualified_class_name", QualifiedClassName(descriptor)),
Sub("upb_msg_name",
upb::generator::CApiMessageType(descriptor->full_name())),
Sub("field_accessors",
[&] { WriteFieldAccessorsInHeader(descriptor, ctx); })
.WithSuffix(";"),
Sub("oneof_accessors",
[&] { WriteOneofAccessorsInHeader(descriptor, ctx); })
.WithSuffix(";")},
R"cc(
class $class_name$Access {
public:
$class_name$Access() {}
$class_name$Access($upb_msg_name$* msg, upb_Arena* arena)
: msg_(msg), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
$class_name$Access(const $upb_msg_name$* msg, upb_Arena* arena)
: msg_(const_cast<$upb_msg_name$*>(msg)), arena_(arena) {
assert(arena != nullptr);
} // NOLINT
$field_accessors$;
$oneof_accessors$;
private:
friend class $qualified_class_name$;
friend class $class_name$Proxy;
friend class $class_name$CProxy;
friend struct ::hpb::internal::PrivateAccess;
$upb_msg_name$* msg_;
upb_Arena* arena_;
};
)cc");
}
std::string UnderscoresToCamelCase(absl::string_view input,

Loading…
Cancel
Save