Move message.cc off of upb c codegen

The extern "C" block in messages is now:
- Cpp kernel: All of the message/accessor/oneof externs
- Upb kernel: Only the upb_MiniTable extern

PiperOrigin-RevId: 668518308
pull/18009/head
Protobuf Team Bot 6 months ago committed by Copybara-Service
parent f13b593e1c
commit 8bb7e735a0
  1. 1
      src/google/protobuf/compiler/rust/accessors/generator.h
  2. 2
      src/google/protobuf/compiler/rust/accessors/map.cc
  3. 2
      src/google/protobuf/compiler/rust/accessors/repeated_field.cc
  4. 2
      src/google/protobuf/compiler/rust/accessors/singular_cord.cc
  5. 3
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  6. 5
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc
  7. 2
      src/google/protobuf/compiler/rust/accessors/singular_string.cc
  8. 3
      src/google/protobuf/compiler/rust/accessors/with_presence.cc
  9. 144
      src/google/protobuf/compiler/rust/message.cc
  10. 2
      src/google/protobuf/compiler/rust/oneof.cc

@ -48,6 +48,7 @@ class AccessorGenerator {
ctx.printer().PrintRaw("\n");
}
void GenerateExternC(Context& ctx, const FieldDescriptor& field) const {
ABSL_CHECK(ctx.is_cpp());
InExternC(ctx, field);
ctx.printer().PrintRaw("\n");
}

@ -144,7 +144,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
void Map::InExternC(Context& ctx, const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
ctx.Emit(
{

@ -160,7 +160,7 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
void RepeatedField::InExternC(Context& ctx,
const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},

@ -184,7 +184,7 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
void SingularCord::InExternC(Context& ctx, const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) {
WithPresenceAccessorsInExternC(ctx, field);

@ -169,7 +169,8 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
void SingularMessage::InExternC(Context& ctx,
const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) {
WithPresenceAccessorsInExternC(ctx, field);
}

@ -143,8 +143,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
void SingularScalar::InExternC(Context& ctx,
const FieldDescriptor& field) const {
// Only cpp kernel uses thunks for singular scalars.
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) {
WithPresenceAccessorsInExternC(ctx, field);
@ -168,6 +167,8 @@ void SingularScalar::InExternC(Context& ctx,
void SingularScalar::InThunkCc(Context& ctx,
const FieldDescriptor& field) const {
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) {
WithPresenceAccessorsInThunkCc(ctx, field);
}

@ -131,7 +131,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
void SingularString::InExternC(Context& ctx,
const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) {
WithPresenceAccessorsInExternC(ctx, field);

@ -100,10 +100,9 @@ void WithPresenceAccessorsInMsgImpl(Context& ctx, const FieldDescriptor& field,
void WithPresenceAccessorsInExternC(Context& ctx,
const FieldDescriptor& field) {
ABSL_CHECK(ctx.is_cpp());
ABSL_CHECK(field.has_presence());
if (ctx.is_upb()) return;
ctx.Emit(
{
{"hazzer_thunk", ThunkName(ctx, field, "has")},

@ -39,11 +39,16 @@ void MessageNew(Context& ctx, const Descriptor& msg) {
return;
case Kernel::kUpb:
ctx.Emit({{"new_thunk", ThunkName(ctx, msg, "new")}}, R"rs(
ctx.Emit(R"rs(
let arena = $pbr$::Arena::new();
let raw_msg = unsafe {
$pbr$::upb_Message_New(
<Self as $pbr$::AssociatedMiniTable>::mini_table(),
arena.raw()).unwrap()
};
Self {
inner: $pbr$::MessageInner {
msg: unsafe { $new_thunk$(arena.raw()) },
msg: raw_msg,
arena,
}
}
@ -186,55 +191,35 @@ void MessageDebug(Context& ctx, const Descriptor& msg) {
ABSL_LOG(FATAL) << "unreachable";
}
void MessageExterns(Context& ctx, const Descriptor& msg) {
switch (ctx.opts().kernel) {
case Kernel::kCpp:
ctx.Emit(
{{"new_thunk", ThunkName(ctx, msg, "new")},
{"placement_new_thunk", ThunkName(ctx, msg, "placement_new")},
{"repeated_new_thunk", ThunkName(ctx, msg, "repeated_new")},
{"repeated_free_thunk", ThunkName(ctx, msg, "repeated_free")},
{"repeated_len_thunk", ThunkName(ctx, msg, "repeated_len")},
{"repeated_get_thunk", ThunkName(ctx, msg, "repeated_get")},
{"repeated_get_mut_thunk", ThunkName(ctx, msg, "repeated_get_mut")},
{"repeated_add_thunk", ThunkName(ctx, msg, "repeated_add")},
{"repeated_clear_thunk", ThunkName(ctx, msg, "repeated_clear")},
{"repeated_copy_from_thunk",
ThunkName(ctx, msg, "repeated_copy_from")},
{"repeated_reserve_thunk", ThunkName(ctx, msg, "repeated_reserve")},
{"map_size_info_thunk", ThunkName(ctx, msg, "size_info")}},
R"rs(
fn $new_thunk$() -> $pbr$::RawMessage;
fn $placement_new_thunk$(ptr: *mut std::ffi::c_void, m: $pbr$::RawMessage);
fn $repeated_new_thunk$() -> $pbr$::RawRepeatedField;
fn $repeated_free_thunk$(raw: $pbr$::RawRepeatedField);
fn $repeated_len_thunk$(raw: $pbr$::RawRepeatedField) -> usize;
fn $repeated_add_thunk$(raw: $pbr$::RawRepeatedField) -> $pbr$::RawMessage;
fn $repeated_get_thunk$(raw: $pbr$::RawRepeatedField, index: usize) -> $pbr$::RawMessage;
fn $repeated_get_mut_thunk$(raw: $pbr$::RawRepeatedField, index: usize) -> $pbr$::RawMessage;
fn $repeated_clear_thunk$(raw: $pbr$::RawRepeatedField);
fn $repeated_copy_from_thunk$(dst: $pbr$::RawRepeatedField, src: $pbr$::RawRepeatedField);
fn $repeated_reserve_thunk$(raw: $pbr$::RawRepeatedField, additional: usize);
fn $map_size_info_thunk$(i: $pbr$::MapNodeSizeInfoIndex) -> $pbr$::MapNodeSizeInfo;
)rs");
return;
case Kernel::kUpb:
ctx.Emit(
{
{"new_thunk", ThunkName(ctx, msg, "new")},
{"minitable", UpbMiniTableName(msg)},
},
R"rs(
fn $new_thunk$(arena: $pbr$::RawArena) -> $pbr$::RawMessage;
/// Opaque wrapper for this message's MiniTable. The only valid way to
/// reference this static is with `std::ptr::addr_of!(..)`.
static $minitable$: $pbr$::upb_MiniTable;
)rs");
return;
}
ABSL_LOG(FATAL) << "unreachable";
void CppMessageExterns(Context& ctx, const Descriptor& msg) {
ABSL_CHECK(ctx.is_cpp());
ctx.Emit(
{{"new_thunk", ThunkName(ctx, msg, "new")},
{"placement_new_thunk", ThunkName(ctx, msg, "placement_new")},
{"repeated_new_thunk", ThunkName(ctx, msg, "repeated_new")},
{"repeated_free_thunk", ThunkName(ctx, msg, "repeated_free")},
{"repeated_len_thunk", ThunkName(ctx, msg, "repeated_len")},
{"repeated_get_thunk", ThunkName(ctx, msg, "repeated_get")},
{"repeated_get_mut_thunk", ThunkName(ctx, msg, "repeated_get_mut")},
{"repeated_add_thunk", ThunkName(ctx, msg, "repeated_add")},
{"repeated_clear_thunk", ThunkName(ctx, msg, "repeated_clear")},
{"repeated_copy_from_thunk", ThunkName(ctx, msg, "repeated_copy_from")},
{"repeated_reserve_thunk", ThunkName(ctx, msg, "repeated_reserve")},
{"map_size_info_thunk", ThunkName(ctx, msg, "size_info")}},
R"rs(
fn $new_thunk$() -> $pbr$::RawMessage;
fn $placement_new_thunk$(ptr: *mut std::ffi::c_void, m: $pbr$::RawMessage);
fn $repeated_new_thunk$() -> $pbr$::RawRepeatedField;
fn $repeated_free_thunk$(raw: $pbr$::RawRepeatedField);
fn $repeated_len_thunk$(raw: $pbr$::RawRepeatedField) -> usize;
fn $repeated_add_thunk$(raw: $pbr$::RawRepeatedField) -> $pbr$::RawMessage;
fn $repeated_get_thunk$(raw: $pbr$::RawRepeatedField, index: usize) -> $pbr$::RawMessage;
fn $repeated_get_mut_thunk$(raw: $pbr$::RawRepeatedField, index: usize) -> $pbr$::RawMessage;
fn $repeated_clear_thunk$(raw: $pbr$::RawRepeatedField);
fn $repeated_copy_from_thunk$(dst: $pbr$::RawRepeatedField, src: $pbr$::RawRepeatedField);
fn $repeated_reserve_thunk$(raw: $pbr$::RawRepeatedField, additional: usize);
fn $map_size_info_thunk$(i: $pbr$::MapNodeSizeInfoIndex) -> $pbr$::MapNodeSizeInfo;
)rs");
}
void MessageDrop(Context& ctx, const Descriptor& msg) {
@ -244,7 +229,7 @@ void MessageDrop(Context& ctx, const Descriptor& msg) {
return;
}
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
unsafe { $pbr$::proto2_rust_Message_delete(self.raw_msg()); }
)rs");
}
@ -252,7 +237,7 @@ void MessageDrop(Context& ctx, const Descriptor& msg) {
void IntoProxiedForMessage(Context& ctx, const Descriptor& msg) {
switch (ctx.opts().kernel) {
case Kernel::kCpp:
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
impl<'msg> $pb$::IntoProxied<$Msg$> for $Msg$View<'msg> {
fn into_proxied(self, _private: $pbi$::Private) -> $Msg$ {
let dst = $Msg$::new();
@ -843,7 +828,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
{"Msg::drop", [&] { MessageDrop(ctx, msg); }},
{"Msg::debug", [&] { MessageDebug(ctx, msg); }},
{"MsgMut::merge_from", [&] { MessageMutMergeFrom(ctx, msg); }},
{"Msg_externs", [&] { MessageExterns(ctx, msg); }},
{"accessor_fns",
[&] {
for (int i = 0; i < msg.field_count(); ++i) {
@ -854,18 +838,6 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
AccessorCase::OWNED);
}
}},
{"accessor_externs",
[&] {
for (int i = 0; i < msg.field_count(); ++i) {
GenerateAccessorExternC(ctx, *msg.field(i));
}
}},
{"oneof_externs",
[&] {
for (int i = 0; i < msg.real_oneof_decl_count(); ++i) {
GenerateOneofExternC(ctx, *msg.real_oneof_decl(i));
}
}},
{"nested_in_msg",
[&] {
// If we have no nested types, enums, or oneofs, bail out without
@ -1276,16 +1248,44 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
$upb_generated_message_trait_impls$
extern "C" {
$Msg_externs$
$nested_in_msg$
)rs");
if (ctx.is_cpp()) {
ctx.Emit(
{
{"message_externs", [&] { CppMessageExterns(ctx, msg); }},
{"accessor_externs",
[&] {
for (int i = 0; i < msg.field_count(); ++i) {
GenerateAccessorExternC(ctx, *msg.field(i));
}
}},
{"oneof_externs",
[&] {
for (int i = 0; i < msg.real_oneof_decl_count(); ++i) {
GenerateOneofExternC(ctx, *msg.real_oneof_decl(i));
}
}},
},
R"rs(
extern "C" {
$message_externs$
$accessor_externs$
$oneof_externs$
} // extern "C" for $Msg$
$nested_in_msg$
}
)rs");
} else {
ctx.Emit({{"minitable_symbol_name", UpbMiniTableName(msg)}},
R"rs(
extern "C" {
/// Opaque static extern for this message's MiniTable, generated
/// by the upb C MiniTable codegen. The only valid way to
/// reference this static is with `std::ptr::addr_of!(..)`.
static $minitable_symbol_name$: $pbr$::upb_MiniTable;
}
)rs");
}
ctx.printer().PrintRaw("\n");
if (ctx.is_cpp()) {

@ -270,7 +270,7 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
}
void GenerateOneofExternC(Context& ctx, const OneofDescriptor& oneof) {
if (ctx.is_upb()) return;
ABSL_CHECK(ctx.is_cpp());
ctx.Emit(
{

Loading…
Cancel
Save