diff --git a/hpb_generator/BUILD b/hpb_generator/BUILD index 84fe0531aa..e23647031e 100644 --- a/hpb_generator/BUILD +++ b/hpb_generator/BUILD @@ -60,7 +60,8 @@ cc_library( "//upb_generator:common", "//upb_generator:file_layout", "//upb_generator:keywords", - "//upb_generator:names", + "//upb_generator/c:names", + "//upb_generator/minitable:names", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/strings", ], @@ -74,6 +75,8 @@ cc_library( deps = [ "//:protobuf", "//src/google/protobuf/io", + "//upb_generator/c:names", + "//upb_generator/minitable:names", "@com_google_absl//absl/log:absl_log", "@com_google_absl//absl/strings", ], @@ -100,6 +103,7 @@ cc_library( deps = [ ":output", "//src/google/protobuf", + "//src/google/protobuf/compiler:code_generator", "//upb_generator:keywords", "@com_google_absl//absl/strings:string_view", ], diff --git a/hpb_generator/gen_accessors.cc b/hpb_generator/gen_accessors.cc index 388564cf94..13fee6b0c1 100644 --- a/hpb_generator/gen_accessors.cc +++ b/hpb_generator/gen_accessors.cc @@ -13,14 +13,14 @@ #include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "google/protobuf/descriptor.h" #include "google/protobuf/compiler/hpb/gen_repeated_fields.h" #include "google/protobuf/compiler/hpb/gen_utils.h" #include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/output.h" -#include "upb_generator/common.h" +#include "google/protobuf/descriptor.h" +#include "upb_generator/c/names.h" #include "upb_generator/keywords.h" -#include "upb_generator/names.h" +#include "upb_generator/minitable/names.h" namespace google::protobuf::hpb_generator { @@ -307,7 +307,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message, MessagePtrConstType(val, /* is_const */ true), MessageName(message), MessageName(val->message_type()), optional_conversion_code, converted_key_name, upbc_name, - ::upb::generator::MessageInit(val->message_type()->full_name())); + ::upb::generator::MiniTableMessageVarName( + val->message_type()->full_name())); output( R"cc( bool $0::set_$1($2 key, $3 value) { @@ -321,7 +322,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message, MessagePtrConstType(val, /* is_const */ false), MessageName(message), MessageName(val->message_type()), optional_conversion_code, converted_key_name, upbc_name, - ::upb::generator::MessageInit(val->message_type()->full_name())); + ::upb::generator::MiniTableMessageVarName( + val->message_type()->full_name())); output( R"cc( absl::StatusOr<$3> $0::get_$1($2 key) { diff --git a/hpb_generator/gen_messages.cc b/hpb_generator/gen_messages.cc index dabce56865..dba676647c 100644 --- a/hpb_generator/gen_messages.cc +++ b/hpb_generator/gen_messages.cc @@ -22,8 +22,7 @@ #include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/output.h" #include "google/protobuf/descriptor.h" -#include "upb_generator/common.h" -#include "upb_generator/file_layout.h" +#include "upb_generator/minitable/names.h" namespace google::protobuf::hpb_generator { @@ -206,7 +205,7 @@ void WriteModelPublicDeclaration( } )cc", ClassName(descriptor), - ::upb::generator::MessageInit(descriptor->full_name()), + ::upb::generator::MiniTableMessageVarName(descriptor->full_name()), MessageName(descriptor), QualifiedClassName(descriptor)); WriteUsingAccessorsInHeader(descriptor, MessageClassType::kMessage, output); @@ -416,7 +415,7 @@ void WriteMessageImplementation( } )cc", ClassName(descriptor), MessageName(descriptor), - ::upb::generator::MessageInit(descriptor->full_name()), + ::upb::generator::MiniTableMessageVarName(descriptor->full_name()), QualifiedClassName(descriptor)); output("\n"); // Minitable @@ -425,7 +424,7 @@ void WriteMessageImplementation( const upb_MiniTable* $0::minitable() { return &$1; } )cc", ClassName(descriptor), - ::upb::generator::MessageInit(descriptor->full_name())); + ::upb::generator::MiniTableMessageVarName(descriptor->full_name())); output("\n"); } diff --git a/hpb_generator/gen_repeated_fields.cc b/hpb_generator/gen_repeated_fields.cc index 2a52baa2b6..6e4e02c3c9 100644 --- a/hpb_generator/gen_repeated_fields.cc +++ b/hpb_generator/gen_repeated_fields.cc @@ -19,9 +19,9 @@ #include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/output.h" #include "google/protobuf/descriptor.h" +#include "upb_generator/c/names.h" #include "upb_generator/common.h" #include "upb_generator/file_layout.h" -#include "upb_generator/names.h" namespace google::protobuf::hpb_generator { diff --git a/hpb_generator/names.cc b/hpb_generator/names.cc index 864eca5aaf..a081c59757 100644 --- a/hpb_generator/names.cc +++ b/hpb_generator/names.cc @@ -10,6 +10,7 @@ #include #include "absl/strings/string_view.h" +#include "google/protobuf/compiler/code_generator.h" #include "upb_generator/keywords.h" namespace google::protobuf::hpb_generator { @@ -104,19 +105,19 @@ std::string QualifiedInternalClassName(const protobuf::Descriptor* descriptor) { } std::string CppSourceFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.proto.cc"; + return compiler::StripProto(file->name()) + ".upb.proto.cc"; } std::string ForwardingHeaderFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.fwd.h"; + return compiler::StripProto(file->name()) + ".upb.fwd.h"; } std::string UpbCFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.h"; + return compiler::StripProto(file->name()) + ".upb.h"; } std::string CppHeaderFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.proto.h"; + return compiler::StripProto(file->name()) + ".upb.proto.h"; } void WriteStartNamespace(const protobuf::FileDescriptor* file, Output& output) { diff --git a/hpb_generator/output.cc b/hpb_generator/output.cc index 32b34bcde2..ca7f59f74d 100644 --- a/hpb_generator/output.cc +++ b/hpb_generator/output.cc @@ -9,7 +9,8 @@ #include -#include "absl/strings/str_replace.h" +#include "upb_generator/c/names.h" +#include "upb_generator/minitable/names.h" namespace google::protobuf::hpb_generator { namespace { @@ -18,14 +19,6 @@ namespace protobuf = ::proto2; } // namespace -std::string StripExtension(absl::string_view fname) { - size_t lastdot = fname.find_last_of('.'); - if (lastdot == std::string::npos) { - return std::string(fname); - } - return std::string(fname.substr(0, lastdot)); -} - std::string ToCIdent(absl::string_view str) { return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); } @@ -51,19 +44,15 @@ void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) { } std::string MessageName(const protobuf::Descriptor* descriptor) { - return ToCIdent(descriptor->full_name()); + return upb::generator::CApiMessageType(descriptor->full_name()); } std::string FileLayoutName(const google::protobuf::FileDescriptor* file) { - return ToCIdent(file->name()) + "_upb_file_layout"; + return upb::generator::MiniTableFileVarName(file->name()); } std::string CHeaderFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.h"; -} - -std::string CSourceFilename(const google::protobuf::FileDescriptor* file) { - return StripExtension(file->name()) + ".upb.c"; + return upb::generator::CApiHeaderFilename(file->name()); } } // namespace protobuf diff --git a/hpb_generator/output.h b/hpb_generator/output.h index f2769e470e..a06ced92ba 100644 --- a/hpb_generator/output.h +++ b/hpb_generator/output.h @@ -137,14 +137,12 @@ class OutputIndenter { Output& output_; }; -std::string StripExtension(absl::string_view fname); std::string ToCIdent(absl::string_view str); std::string ToPreproc(absl::string_view str); void EmitFileWarning(const google::protobuf::FileDescriptor* file, Output& output); std::string MessageName(const google::protobuf::Descriptor* descriptor); std::string FileLayoutName(const google::protobuf::FileDescriptor* file); std::string CHeaderFilename(const google::protobuf::FileDescriptor* file); -std::string CSourceFilename(const google::protobuf::FileDescriptor* file); } // namespace protobuf } // namespace google::hpb_generator diff --git a/src/google/protobuf/compiler/rust/BUILD.bazel b/src/google/protobuf/compiler/rust/BUILD.bazel index 95226b71ef..91c81451af 100644 --- a/src/google/protobuf/compiler/rust/BUILD.bazel +++ b/src/google/protobuf/compiler/rust/BUILD.bazel @@ -250,7 +250,7 @@ cc_library( ], deps = [ "//src/google/protobuf", - "//upb_generator:mangle", + "//upb_generator/minitable:names", "@com_google_absl//absl/log:absl_check", ], ) diff --git a/src/google/protobuf/compiler/rust/upb_helpers.cc b/src/google/protobuf/compiler/rust/upb_helpers.cc index c57359928d..508d81f13d 100644 --- a/src/google/protobuf/compiler/rust/upb_helpers.cc +++ b/src/google/protobuf/compiler/rust/upb_helpers.cc @@ -5,7 +5,7 @@ #include "absl/log/absl_check.h" #include "google/protobuf/descriptor.h" -#include "upb_generator/mangle.h" +#include "upb_generator/minitable/names.h" namespace google { namespace protobuf { @@ -13,7 +13,7 @@ namespace compiler { namespace rust { std::string UpbMiniTableName(const Descriptor& msg) { - return upb::generator::MessageInit(msg.full_name()); + return upb::generator::MiniTableMessageVarName(msg.full_name()); } uint32_t UpbMiniTableFieldIndex(const FieldDescriptor& field) { diff --git a/upb/port/def.inc b/upb/port/def.inc index 75b02c5569..903bcaf049 100644 --- a/upb/port/def.inc +++ b/upb/port/def.inc @@ -90,6 +90,12 @@ Error, UINTPTR_MAX is undefined #define UPB_API_INLINE UPB_INLINE #endif +#ifdef EXPORT_UPBC +#define UPBC_API UPB_EXPORT +#else +#define UPBC_API +#endif + #define UPB_MALLOC_ALIGN 8 #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align)) #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align)) diff --git a/upb/port/undef.inc b/upb/port/undef.inc index d8453ffec1..bdec74f173 100644 --- a/upb/port/undef.inc +++ b/upb/port/undef.inc @@ -13,6 +13,7 @@ #undef UPB_EXPORT #undef UPB_INLINE #undef UPB_API +#undef UPBC_API #undef UPB_API_INLINE #undef UPB_ALIGN_UP #undef UPB_ALIGN_DOWN diff --git a/upb/reflection/stage0/google/protobuf/descriptor.upb.c b/upb/reflection/stage0/google/protobuf/descriptor.upb.c index 3f5c6734cd..c335e55edf 100644 --- a/upb/reflection/stage0/google/protobuf/descriptor.upb.c +++ b/upb/reflection/stage0/google/protobuf/descriptor.upb.c @@ -32,7 +32,7 @@ const upb_MiniTable* google__protobuf__FileDescriptorProto_msg_init() { upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 7), google__protobuf__FieldDescriptorProto_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 8), google__protobuf__FileOptions_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 9), google__protobuf__SourceCodeInfo_msg_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 14), google_protobuf_Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 14), google__protobuf__Edition_enum_init()); return mini_table; } @@ -85,7 +85,7 @@ const upb_MiniTable* google__protobuf__ExtensionRangeOptions_msg_init() { upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google__protobuf__UninterpretedOption_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google__protobuf__ExtensionRangeOptions__Declaration_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 50), google__protobuf__FeatureSet_msg_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_ExtensionRangeOptions_VerificationState_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google__protobuf__ExtensionRangeOptions__VerificationState_enum_init()); return mini_table; } @@ -106,8 +106,8 @@ const upb_MiniTable* google__protobuf__FieldDescriptorProto_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_FieldDescriptorProto_Label_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_FieldDescriptorProto_Type_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google__protobuf__FieldDescriptorProto__Label_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google__protobuf__FieldDescriptorProto__Type_enum_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 8), google__protobuf__FieldOptions_msg_init()); return mini_table; } @@ -187,7 +187,7 @@ const upb_MiniTable* google__protobuf__FileOptions_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 9), google_protobuf_FileOptions_OptimizeMode_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 9), google__protobuf__FileOptions__OptimizeMode_enum_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 50), google__protobuf__FeatureSet_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google__protobuf__UninterpretedOption_msg_init()); return mini_table; @@ -212,10 +212,10 @@ const upb_MiniTable* google__protobuf__FieldOptions_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google_protobuf_FieldOptions_CType_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google_protobuf_FieldOptions_JSType_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 17), google_protobuf_FieldOptions_OptionRetention_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 19), google_protobuf_FieldOptions_OptionTargetType_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google__protobuf__FieldOptions__CType_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google__protobuf__FieldOptions__JSType_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 17), google__protobuf__FieldOptions__OptionRetention_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 19), google__protobuf__FieldOptions__OptionTargetType_enum_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 20), google__protobuf__FieldOptions__EditionDefault_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 21), google__protobuf__FeatureSet_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 22), google__protobuf__FieldOptions__FeatureSupport_msg_init()); @@ -230,7 +230,7 @@ const upb_MiniTable* google__protobuf__FieldOptions__EditionDefault_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google__protobuf__Edition_enum_init()); return mini_table; } @@ -241,9 +241,9 @@ const upb_MiniTable* google__protobuf__FieldOptions__FeatureSupport_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google_protobuf_Edition_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_Edition_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google__protobuf__Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google__protobuf__Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google__protobuf__Edition_enum_init()); return mini_table; } @@ -303,7 +303,7 @@ const upb_MiniTable* google__protobuf__MethodOptions_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 34), google_protobuf_MethodOptions_IdempotencyLevel_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 34), google__protobuf__MethodOptions__IdempotencyLevel_enum_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 35), google__protobuf__FeatureSet_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google__protobuf__UninterpretedOption_msg_init()); return mini_table; @@ -337,12 +337,12 @@ const upb_MiniTable* google__protobuf__FeatureSet_msg_init() { mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google_protobuf_FeatureSet_FieldPresence_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_FeatureSet_EnumType_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_FeatureSet_RepeatedFieldEncoding_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_FeatureSet_Utf8Validation_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_FeatureSet_MessageEncoding_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google_protobuf_FeatureSet_JsonFormat_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google__protobuf__FeatureSet__FieldPresence_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google__protobuf__FeatureSet__EnumType_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google__protobuf__FeatureSet__Utf8Validation_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google__protobuf__FeatureSet__MessageEncoding_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google__protobuf__FeatureSet__JsonFormat_enum_init()); return mini_table; } @@ -354,8 +354,8 @@ const upb_MiniTable* google__protobuf__FeatureSetDefaults_msg_init() { upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 1), google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_Edition_enum_init()); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google__protobuf__Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google__protobuf__Edition_enum_init()); return mini_table; } @@ -366,7 +366,7 @@ const upb_MiniTable* google__protobuf__FeatureSetDefaults__FeatureSetEditionDefa mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_Edition_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google__protobuf__Edition_enum_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google__protobuf__FeatureSet_msg_init()); upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google__protobuf__FeatureSet_msg_init()); return mini_table; @@ -411,11 +411,11 @@ const upb_MiniTable* google__protobuf__GeneratedCodeInfo__Annotation_msg_init() mini_table = upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor), upb_BootstrapArena(), NULL); - upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_GeneratedCodeInfo_Annotation_Semantic_enum_init()); + upb_MiniTable_SetSubEnum(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init()); return mini_table; } -const upb_MiniTableEnum* google_protobuf_Edition_enum_init() { +const upb_MiniTableEnum* google__protobuf__Edition_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)~z!|a1qt_b)|i}{~~`!"; if (mini_table) return mini_table; @@ -425,7 +425,7 @@ const upb_MiniTableEnum* google_protobuf_Edition_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_ExtensionRangeOptions_VerificationState_enum_init() { +const upb_MiniTableEnum* google__protobuf__ExtensionRangeOptions__VerificationState_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!$"; if (mini_table) return mini_table; @@ -435,7 +435,7 @@ const upb_MiniTableEnum* google_protobuf_ExtensionRangeOptions_VerificationState return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_EnumType_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__EnumType_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -445,7 +445,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_EnumType_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_FieldPresence_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__FieldPresence_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!1"; if (mini_table) return mini_table; @@ -455,7 +455,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_FieldPresence_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_JsonFormat_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__JsonFormat_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -465,7 +465,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_JsonFormat_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_MessageEncoding_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__MessageEncoding_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -475,7 +475,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_MessageEncoding_enum_init() return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_RepeatedFieldEncoding_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -485,7 +485,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_RepeatedFieldEncoding_enum_i return mini_table; } -const upb_MiniTableEnum* google_protobuf_FeatureSet_Utf8Validation_enum_init() { +const upb_MiniTableEnum* google__protobuf__FeatureSet__Utf8Validation_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!/"; if (mini_table) return mini_table; @@ -495,7 +495,7 @@ const upb_MiniTableEnum* google_protobuf_FeatureSet_Utf8Validation_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Label_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldDescriptorProto__Label_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!0"; if (mini_table) return mini_table; @@ -505,7 +505,7 @@ const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Label_enum_init() return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Type_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldDescriptorProto__Type_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!@AA1"; if (mini_table) return mini_table; @@ -515,7 +515,7 @@ const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Type_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldOptions_CType_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldOptions__CType_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -525,7 +525,7 @@ const upb_MiniTableEnum* google_protobuf_FieldOptions_CType_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldOptions_JSType_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldOptions__JSType_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -535,7 +535,7 @@ const upb_MiniTableEnum* google_protobuf_FieldOptions_JSType_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionRetention_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldOptions__OptionRetention_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -545,7 +545,7 @@ const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionRetention_enum_init( return mini_table; } -const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionTargetType_enum_init() { +const upb_MiniTableEnum* google__protobuf__FieldOptions__OptionTargetType_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!AA"; if (mini_table) return mini_table; @@ -555,7 +555,7 @@ const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionTargetType_enum_init return mini_table; } -const upb_MiniTableEnum* google_protobuf_FileOptions_OptimizeMode_enum_init() { +const upb_MiniTableEnum* google__protobuf__FileOptions__OptimizeMode_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!0"; if (mini_table) return mini_table; @@ -565,7 +565,7 @@ const upb_MiniTableEnum* google_protobuf_FileOptions_OptimizeMode_enum_init() { return mini_table; } -const upb_MiniTableEnum* google_protobuf_GeneratedCodeInfo_Annotation_Semantic_enum_init() { +const upb_MiniTableEnum* google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; @@ -575,7 +575,7 @@ const upb_MiniTableEnum* google_protobuf_GeneratedCodeInfo_Annotation_Semantic_e return mini_table; } -const upb_MiniTableEnum* google_protobuf_MethodOptions_IdempotencyLevel_enum_init() { +const upb_MiniTableEnum* google__protobuf__MethodOptions__IdempotencyLevel_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; diff --git a/upb/reflection/stage0/google/protobuf/descriptor.upb.h b/upb/reflection/stage0/google/protobuf/descriptor.upb.h index fe36fa6117..5f5fb70150 100644 --- a/upb/reflection/stage0/google/protobuf/descriptor.upb.h +++ b/upb/reflection/stage0/google/protobuf/descriptor.upb.h @@ -6,8 +6,8 @@ * regenerated. * NO CHECKED-IN PROTOBUF GENCODE */ -#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ -#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ +#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ +#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ #include "upb/generated_code_support.h" @@ -51,23 +51,23 @@ extern const upb_MiniTable* google__protobuf__SourceCodeInfo_msg_init(void); extern const upb_MiniTable* google__protobuf__SourceCodeInfo__Location_msg_init(void); extern const upb_MiniTable* google__protobuf__GeneratedCodeInfo_msg_init(void); extern const upb_MiniTable* google__protobuf__GeneratedCodeInfo__Annotation_msg_init(void); -extern const upb_MiniTableEnum* google_protobuf_Edition_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_ExtensionRangeOptions_VerificationState_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_EnumType_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_FieldPresence_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_JsonFormat_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_MessageEncoding_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_RepeatedFieldEncoding_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FeatureSet_Utf8Validation_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Label_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldDescriptorProto_Type_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldOptions_CType_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldOptions_JSType_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionRetention_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FieldOptions_OptionTargetType_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_FileOptions_OptimizeMode_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_GeneratedCodeInfo_Annotation_Semantic_enum_init(void); -extern const upb_MiniTableEnum* google_protobuf_MethodOptions_IdempotencyLevel_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__Edition_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__ExtensionRangeOptions__VerificationState_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__EnumType_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__FieldPresence_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__JsonFormat_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__MessageEncoding_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FeatureSet__Utf8Validation_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldDescriptorProto__Label_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldDescriptorProto__Type_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldOptions__CType_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldOptions__JSType_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldOptions__OptionRetention_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FieldOptions__OptionTargetType_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__FileOptions__OptimizeMode_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__MethodOptions__IdempotencyLevel_enum_init(void); typedef struct google_protobuf_FileDescriptorSet { upb_Message UPB_PRIVATE(base); } google_protobuf_FileDescriptorSet; typedef struct google_protobuf_FileDescriptorProto { upb_Message UPB_PRIVATE(base); } google_protobuf_FileDescriptorProto; @@ -6746,4 +6746,4 @@ UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_semantic(google #include "upb/port/undef.inc" -#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */ +#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H__UPB_H_ */ diff --git a/upb_generator/BUILD b/upb_generator/BUILD index 0d83e4ad9e..70160e2aac 100644 --- a/upb_generator/BUILD +++ b/upb_generator/BUILD @@ -52,7 +52,6 @@ bootstrap_cc_library( copts = UPB_DEFAULT_CPPOPTS, visibility = ["//upb:friend_generators"], deps = [ - ":mangle", "//src/google/protobuf:port", "//upb:mini_table", "//upb:port", @@ -114,6 +113,7 @@ bootstrap_cc_library( "//upb:base", "//upb:mem", "//upb:port", + "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_log", @@ -121,30 +121,4 @@ bootstrap_cc_library( ], ) -cc_library( - name = "names", - srcs = ["names.cc"], - hdrs = ["names.h"], - copts = UPB_DEFAULT_CPPOPTS, - visibility = ["//upb:friend_generators"], - deps = [ - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/container:flat_hash_map", - "@com_google_absl//absl/strings", - ], -) - -cc_library( - name = "mangle", - srcs = ["mangle.cc"], - hdrs = ["mangle.h"], - copts = UPB_DEFAULT_CPPOPTS, - visibility = [ - "//rust:__subpackages__", - "//upb:friends", - ], - deps = [ - "//src/google/protobuf:port", - "@com_google_absl//absl/strings", - ], -) +cc_library(name = "mangle") diff --git a/upb_generator/c/BUILD b/upb_generator/c/BUILD index a79a679306..f198775264 100644 --- a/upb_generator/c/BUILD +++ b/upb_generator/c/BUILD @@ -1,3 +1,10 @@ +# Copyright (c) 2009-2024, Google LLC +# All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd + load( "//upb/bazel:build_defs.bzl", "UPB_DEFAULT_CPPOPTS", @@ -12,6 +19,28 @@ package(default_applicable_licenses = ["//:license"]) licenses(["notice"]) +cc_library( + name = "names", + srcs = ["names.cc"], + hdrs = ["names.h"], + visibility = ["//src/google/protobuf/compiler/hpb:__pkg__"], + deps = [ + ":names_internal", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "names_internal", + srcs = ["names_internal.cc"], + hdrs = ["names_internal.h"], + deps = [ + "//upb_generator/common:names", + "@com_google_absl//absl/strings", + ], +) + bootstrap_cc_binary( name = "protoc-gen-upb", bootstrap_deps = [":generator_with_main"], @@ -39,12 +68,16 @@ bootstrap_cc_library( copts = UPB_DEFAULT_CPPOPTS, visibility = ["//pkg:__pkg__"], deps = [ + ":names", + ":names_internal", "//upb:base", "//upb:mem", "//upb:mini_table", "//upb:port", "//upb:wire_reader", - "//upb_generator:names", + "//upb_generator/common:names", + "//upb_generator/minitable:names", + "//upb_generator/minitable:names_internal", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", diff --git a/upb_generator/c/generator.cc b/upb_generator/c/generator.cc index d7fe7de8b5..58f7195775 100644 --- a/upb_generator/c/generator.cc +++ b/upb_generator/c/generator.cc @@ -33,9 +33,13 @@ #include "upb/base/string_view.h" #include "upb/mini_table/field.h" #include "upb/reflection/def.hpp" +#include "upb_generator/c/names.h" +#include "upb_generator/c/names_internal.h" #include "upb_generator/common.h" +#include "upb_generator/common/names.h" #include "upb_generator/file_layout.h" -#include "upb_generator/names.h" +#include "upb_generator/minitable/names.h" +#include "upb_generator/minitable/names_internal.h" #include "upb_generator/plugin.h" // Must be last. @@ -50,6 +54,24 @@ struct Options { bool strip_nonfunctional_codegen = false; }; +// Local convenience aliases for the public names.h header files. + +std::string ExtensionIdentBase(upb::FieldDefPtr field) { + return CApiExtensionIdentBase(field.full_name()); +} + +std::string MessageType(upb::MessageDefPtr descriptor) { + return CApiMessageType(descriptor.full_name()); +} + +std::string EnumType(upb::EnumDefPtr descriptor) { + return CApiEnumType(descriptor.full_name()); +} + +std::string EnumValueSymbol(upb::EnumValDefPtr value) { + return CApiEnumValueSymbol(value.full_name()); +} + std::string SourceFilename(upb::FileDefPtr file) { return StripExtension(file.name()) + ".upb.c"; } @@ -57,49 +79,28 @@ std::string SourceFilename(upb::FileDefPtr file) { std::string MessageMiniTableRef(upb::MessageDefPtr descriptor, const Options& options) { if (options.bootstrap_stage == 0) { - return absl::StrCat(MessageInitName(descriptor), "()"); + return absl::StrCat(MiniTableMessageVarName(descriptor.full_name()), "()"); } else { - return absl::StrCat("&", MessageInitName(descriptor)); + return absl::StrCat("&", MiniTableMessageVarName(descriptor.full_name())); } } -std::string EnumInitName(upb::EnumDefPtr descriptor) { - return ToCIdent(descriptor.full_name()) + "_enum_init"; -} - std::string EnumMiniTableRef(upb::EnumDefPtr descriptor, const Options& options) { if (options.bootstrap_stage == 0) { - return absl::StrCat(EnumInitName(descriptor), "()"); + return absl::StrCat(MiniTableEnumVarName(descriptor.full_name()), "()"); } else { - return absl::StrCat("&", EnumInitName(descriptor)); + return absl::StrCat("&", MiniTableEnumVarName(descriptor.full_name())); } } -std::string ExtensionIdentBase(upb::FieldDefPtr ext) { - assert(ext.is_extension()); - if (ext.extension_scope()) { - return MessageName(ext.extension_scope()); - } else { - return ToCIdent(ext.file().package()); - } -} - -std::string ExtensionLayout(upb::FieldDefPtr ext) { - return absl::StrCat(ExtensionIdentBase(ext), "_", ext.name(), "_ext"); -} - -std::string EnumValueSymbol(upb::EnumValDefPtr value) { - return ToCIdent(value.full_name()); -} - std::string CTypeInternal(upb::FieldDefPtr field, bool is_const) { std::string maybe_const = is_const ? "const " : ""; switch (field.ctype()) { case kUpb_CType_Message: { std::string maybe_struct = field.file() != field.message_type().file() ? "struct " : ""; - return maybe_const + maybe_struct + MessageName(field.message_type()) + + return maybe_const + maybe_struct + MessageType(field.message_type()) + "*"; } case kUpb_CType_Bool: @@ -274,8 +275,8 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, return upb_Message_HasExtension((upb_Message*)msg, &$3); } )cc", - ExtensionIdentBase(ext), ext.name(), MessageName(ext.containing_type()), - ExtensionLayout(ext)); + ExtensionIdentBase(ext), ext.name(), MessageType(ext.containing_type()), + MiniTableExtensionVarName(ext.full_name())); output( R"cc( @@ -283,8 +284,8 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, upb_Message_ClearExtension((upb_Message*)msg, &$3); } )cc", - ExtensionIdentBase(ext), ext.name(), MessageName(ext.containing_type()), - ExtensionLayout(ext)); + ExtensionIdentBase(ext), ext.name(), MessageType(ext.containing_type()), + MiniTableExtensionVarName(ext.full_name())); if (ext.IsSequence()) { // TODO: We need generated accessors for repeated extensions. @@ -303,8 +304,9 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, } )cc", CTypeConst(ext), ExtensionIdentBase(ext), ext.name(), - MessageName(ext.containing_type()), ExtensionLayout(ext), - GetFieldRep(pools, ext), FieldDefault(ext)); + MessageType(ext.containing_type()), + MiniTableExtensionVarName(ext.full_name()), GetFieldRep(pools, ext), + FieldDefault(ext)); output( R"cc( UPB_INLINE void $1_set_$2(struct $3* msg, $0 val, upb_Arena* arena) { @@ -317,8 +319,8 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, } )cc", CTypeConst(ext), ExtensionIdentBase(ext), ext.name(), - MessageName(ext.containing_type()), ExtensionLayout(ext), - GetFieldRep(pools, ext)); + MessageType(ext.containing_type()), + MiniTableExtensionVarName(ext.full_name()), GetFieldRep(pools, ext)); // Message extensions also have a Msg_mutable_foo() accessor that will // create the sub-message if it doesn't already exist. @@ -335,8 +337,8 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, return sub; } )cc", - MessageName(ext.message_type()), ExtensionIdentBase(ext), ext.name(), - MessageName(ext.containing_type()), + MessageType(ext.message_type()), ExtensionIdentBase(ext), ext.name(), + MessageType(ext.containing_type()), MessageMiniTableRef(ext.message_type(), options)); } } @@ -383,13 +385,13 @@ void GenerateMessageFunctionsInHeader(upb::MessageDefPtr message, return ptr; } )cc", - MessageName(message), MessageMiniTableRef(message, options)); + MessageType(message), MessageMiniTableRef(message, options)); } void GenerateOneofInHeader(upb::OneofDefPtr oneof, const DefPoolPair& pools, absl::string_view msg_name, const Options& options, Output& output) { - std::string fullname = ToCIdent(oneof.full_name()); + std::string fullname = CApiOneofIdentBase(oneof.full_name()); output("typedef enum {\n"); for (int j = 0; j < oneof.field_count(); j++) { upb::FieldDefPtr field = oneof.field(j); @@ -715,7 +717,7 @@ void GenerateRepeatedSetters(upb::FieldDefPtr field, const DefPoolPair& pools, return sub; } )cc", - MessageName(field.message_type()), msg_name, resolved_name, + MessageType(field.message_type()), msg_name, resolved_name, MessageMiniTableRef(field.message_type(), options), FieldInitializerStrong(pools, field, options)); } else { @@ -785,7 +787,7 @@ void GenerateNonRepeatedSetters(upb::FieldDefPtr field, return sub; } )cc", - MessageName(field.message_type()), msg_name, field_name, + MessageType(field.message_type()), msg_name, field_name, MessageMiniTableRef(field.message_type(), options)); } } @@ -807,7 +809,7 @@ void GenerateMessageInHeader(upb::MessageDefPtr message, const DefPoolPair& pools, const Options& options, Output& output) { output("/* $0 */\n\n", message.full_name()); - std::string msg_name = ToCIdent(message.full_name()); + std::string msg_name = MessageType(message); if (!message.mapentry()) { GenerateMessageFunctionsInHeader(message, options, output); } @@ -869,19 +871,19 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, std::vector forward_messages = SortedForwardMessages(this_file_messages, this_file_exts); - EmitFileWarning(file.name(), output); + output(FileWarning(file.name())); output( "#ifndef $0_UPB_H_\n" "#define $0_UPB_H_\n\n" "#include \"upb/generated_code_support.h\"\n\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); for (int i = 0; i < file.public_dependency_count(); i++) { if (i == 0) { output("/* Public Imports. */\n"); } output("#include \"$0\"\n", - CApiHeaderFilename(file.public_dependency(i), + CApiHeaderFilename(file.public_dependency(i).name(), options.bootstrap_stage >= 0)); } if (file.public_dependency_count() > 0) { @@ -890,7 +892,7 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, if (options.bootstrap_stage != 0) { output("#include \"$0\"\n\n", - MiniTableHeaderFilename(file, options.bootstrap_stage >= 0)); + MiniTableHeaderFilename(file.name(), options.bootstrap_stage >= 0)); for (int i = 0; i < file.dependency_count(); i++) { if (options.strip_nonfunctional_codegen && google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) { @@ -898,7 +900,7 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, continue; } output("#include \"$0\"\n", - MiniTableHeaderFilename(file.dependency(i), + MiniTableHeaderFilename(file.dependency(i).name(), options.bootstrap_stage >= 0)); } output("\n"); @@ -916,14 +918,15 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, if (options.bootstrap_stage == 0) { for (auto message : this_file_messages) { output("extern const upb_MiniTable* $0(void);\n", - MessageInitName(message)); + MiniTableMessageVarName(message.full_name())); } for (auto message : forward_messages) { output("extern const upb_MiniTable* $0(void);\n", - MessageInitName(message)); + MiniTableMessageVarName(message.full_name())); } for (auto enumdesc : this_file_enums) { - output("extern const upb_MiniTableEnum* $0(void);\n", EnumInit(enumdesc)); + output("extern const upb_MiniTableEnum* $0(void);\n", + MiniTableEnumVarName(enumdesc.full_name())); } output("\n"); } @@ -931,13 +934,13 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, // Forward-declare types defined in this file. for (auto message : this_file_messages) { output("typedef struct $0 { upb_Message UPB_PRIVATE(base); } $0;\n", - ToCIdent(message.full_name())); + MessageType(message)); } // Forward-declare types not in this file, but used as submessages. // Order by full name for consistent ordering. for (auto msg : forward_messages) { - output("struct $0;\n", MessageName(msg)); + output("struct $0;\n", MessageType(msg)); } if (!this_file_messages.empty()) { @@ -947,7 +950,7 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, for (auto enumdesc : this_file_enums) { output("typedef enum {\n"); DumpEnumValues(enumdesc, output); - output("} $0;\n\n", ToCIdent(enumdesc.full_name())); + output("} $0;\n\n", EnumType(enumdesc)); } output("\n"); @@ -998,7 +1001,7 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file, "#include \"upb/port/undef.inc\"\n" "\n" "#endif /* $0_UPB_H_ */\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); } std::string FieldInitializer(upb::FieldDefPtr field, @@ -1017,8 +1020,9 @@ std::string FieldInitializer(upb::FieldDefPtr field, std::string StrongReferenceSingle(upb::FieldDefPtr field) { if (!field.message_type()) return ""; - return absl::Substitute(" UPB_PRIVATE(_upb_MiniTable_StrongReference)(&$0)", - MessageInitName(field.message_type())); + return absl::Substitute( + " UPB_PRIVATE(_upb_MiniTable_StrongReference)(&$0)", + MiniTableMessageVarName(field.message_type().full_name())); } std::string StrongReference(upb::FieldDefPtr field) { @@ -1080,7 +1084,8 @@ void WriteMessageMiniDescriptorInitializer(upb::MessageDefPtr msg, $2return mini_table; } )cc", - MessageInitName(msg), msg.MiniDescriptorEncode(), resolve_calls.output()); + MiniTableMessageVarName(msg.full_name()), msg.MiniDescriptorEncode(), + resolve_calls.output()); output("\n"); } @@ -1099,7 +1104,8 @@ void WriteEnumMiniDescriptorInitializer(upb::EnumDefPtr enum_def, return mini_table; } )cc", - EnumInitName(enum_def), enum_def.MiniDescriptorEncode()); + MiniTableEnumVarName(enum_def.full_name()), + enum_def.MiniDescriptorEncode()); output("\n"); } @@ -1109,16 +1115,16 @@ void WriteMiniDescriptorSource(const DefPoolPair& pools, upb::FileDefPtr file, "#include \n" "#include \"upb/generated_code_support.h\"\n" "#include \"$0\"\n\n", - CApiHeaderFilename(file, options.bootstrap_stage >= 0)); + CApiHeaderFilename(file.name(), options.bootstrap_stage >= 0)); for (int i = 0; i < file.dependency_count(); i++) { if (options.strip_nonfunctional_codegen && google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) { continue; } - output( - "#include \"$0\"\n", - CApiHeaderFilename(file.dependency(i), options.bootstrap_stage >= 0)); + output("#include \"$0\"\n", + CApiHeaderFilename(file.dependency(i).name(), + options.bootstrap_stage >= 0)); } output( @@ -1145,7 +1151,8 @@ void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file, const Options& options, Plugin* plugin) { Output h_output; WriteHeader(pools, file, options, h_output); - plugin->AddOutputFile(CApiHeaderFilename(file, false), h_output.output()); + plugin->AddOutputFile(CApiHeaderFilename(file.name(), false), + h_output.output()); if (options.bootstrap_stage == 0) { Output c_output; diff --git a/upb_generator/names.cc b/upb_generator/c/names.cc similarity index 62% rename from upb_generator/names.cc rename to upb_generator/c/names.cc index 7167083b87..ddca7d7fa6 100644 --- a/upb_generator/names.cc +++ b/upb_generator/c/names.cc @@ -5,22 +5,61 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include "upb_generator/names.h" +#include "upb_generator/c/names.h" #include #include #include +#include #include "absl/container/flat_hash_map.h" #include "absl/strings/match.h" #include "absl/strings/str_cat.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/str_split.h" #include "absl/strings/string_view.h" +#include "upb_generator/c/names_internal.h" namespace upb { namespace generator { namespace { +std::string ToCIdent(absl::string_view str) { + return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); +} + +} // namespace + +std::string CApiHeaderFilename(absl::string_view proto_filename) { + return CApiHeaderFilename(proto_filename, false); +} + +std::string CApiMessageType(absl::string_view full_name) { + return ToCIdent(full_name); +} + +std::string CApiEnumType(absl::string_view full_name) { + return ToCIdent(full_name); +} + +std::string CApiEnumValueSymbol(absl::string_view full_name) { + return ToCIdent(full_name); +} + +std::string CApiExtensionIdentBase(absl::string_view full_name) { + std::vector parts = absl::StrSplit(full_name, '.'); + parts.pop_back(); + return ToCIdent(absl::StrJoin(parts, ".")); +} + +std::string CApiOneofIdentBase(absl::string_view full_name) { + return ToCIdent(full_name); +} + +namespace { + struct Prefix { absl::string_view name; uint32_t conflict_set; diff --git a/upb_generator/names.h b/upb_generator/c/names.h similarity index 58% rename from upb_generator/names.h rename to upb_generator/c/names.h index d33b7890de..3416dbd18e 100644 --- a/upb_generator/names.h +++ b/upb_generator/c/names.h @@ -5,18 +5,54 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#ifndef UPB_PROTOS_GENERATOR_NAMES_H -#define UPB_PROTOS_GENERATOR_NAMES_H +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_H_ #include -#include "absl/base/attributes.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" namespace upb { namespace generator { +// Note: these names are not currently exported, in hopes that no code +// generators outside of the protobuf repo will ever use the generated C API. + +// Maps: foo/bar/baz.proto -> foo/bar/baz.upb.h +std::string CApiHeaderFilename(absl::string_view proto_filename); + +// The foo.upb.h file defines far more symbols than we currently enumerate here. +// We do the bare minimum by by defining the type name for messages and enums, +// which also forms the symbol prefix for associated functions. +// +// typedef struct { /* ... */ } ; +// typedef enum { = X, ... } ; +// +// Oneofs and extensions have a base name that forms the prefix for associated +// functions. + +std::string CApiMessageType(absl::string_view full_name); +std::string CApiEnumType(absl::string_view full_name); +std::string CApiEnumValueSymbol(absl::string_view full_name); +std::string CApiExtensionIdentBase(absl::string_view full_name); +std::string CApiOneofIdentBase(absl::string_view full_name); + +// Name mangling for individual fields. NameMangler maps each field name to a +// mangled name, which tries to avoid collisions with other field accessors. +// +// For example, a field named "clear_foo" might be renamed to "clear_foo_" if +// there is a field named "foo" in the same message. +// +// This API would be more principled if it generated a full symbol name for each +// generated API function, eg. +// mangler.GetSetter("clear_foo") -> "mypkg_MyMessage_set_clear_foo_" +// mangler.GetHazzer("clear_foo") -> "mypkg_MyMessage_has_clear_foo_" +// +// But that would be a larger and more complicated API. In the long run, we +// probably don't want to have other code generators wrapping these APIs, so +// it's probably not worth designing a fully principled API. + enum FieldClass { kStringField = 1 << 0, kContainerField = 1 << 1, @@ -86,4 +122,4 @@ ABSL_CONST_INIT const absl::string_view kMutableMapGetterPostfix = } // namespace generator } // namespace upb -#endif // UPB_PROTOS_GENERATOR_NAMES_H +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_H_ diff --git a/upb_generator/c/names_internal.cc b/upb_generator/c/names_internal.cc new file mode 100644 index 0000000000..b2f29d4401 --- /dev/null +++ b/upb_generator/c/names_internal.cc @@ -0,0 +1,31 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb_generator/c/names_internal.h" + +#include + +#include "absl/strings/string_view.h" +#include "upb_generator/common/names.h" + +namespace upb { +namespace generator { + +std::string CApiHeaderFilename(absl::string_view proto_filename, + bool bootstrap) { + if (bootstrap) { + if (IsDescriptorProto(proto_filename)) { + return "upb/reflection/descriptor_bootstrap.h"; + } else { + return "upb_generator/plugin_bootstrap.h"; + } + } + return StripExtension(proto_filename) + ".upb.h"; +} + +} // namespace generator +} // namespace upb diff --git a/upb_generator/c/names_internal.h b/upb_generator/c/names_internal.h new file mode 100644 index 0000000000..336881e4c0 --- /dev/null +++ b/upb_generator/c/names_internal.h @@ -0,0 +1,26 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_INTERNAL_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_INTERNAL_H_ + +#include + +#include "absl/strings/string_view.h" + +namespace upb { +namespace generator { + +// Like the public CApiHeaderFilename, but also handles the case where we are +// generating a bootstrap header. +std::string CApiHeaderFilename(absl::string_view proto_filename, + bool bootstrap); + +} // namespace generator +} // namespace upb + +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_C_NAMES_INTERNAL_H_ diff --git a/upb_generator/common.cc b/upb_generator/common.cc index ef2038323c..f5ce42b110 100644 --- a/upb_generator/common.cc +++ b/upb_generator/common.cc @@ -13,14 +13,11 @@ #include -#include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" -#include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" #include "upb/mini_table/field.h" #include "upb/reflection/def.hpp" -#include "upb_generator/mangle.h" // Must be last #include "upb/port/def.inc" @@ -28,89 +25,6 @@ namespace upb { namespace generator { -std::string StripExtension(absl::string_view fname) { - size_t lastdot = fname.find_last_of('.'); - if (lastdot == std::string::npos) { - return std::string(fname); - } - return std::string(fname.substr(0, lastdot)); -} - -std::string ToCIdent(absl::string_view str) { - return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); -} - -std::string ToPreproc(absl::string_view str) { - return absl::AsciiStrToUpper(ToCIdent(str)); -} - -void EmitFileWarning(absl::string_view name, Output& output) { - output( - "/* This file was generated by upb_generator from the input file:\n" - " *\n" - " * $0\n" - " *\n" - " * Do not edit -- your changes will be discarded when the file is\n" - " * regenerated.\n" - " * NO CHECKED-IN " - // Intentional line break. - "PROTOBUF GENCODE */\n" - "\n", - name); -} - -std::string MessageInitName(upb::MessageDefPtr descriptor) { - return MessageInit(descriptor.full_name()); -} - -std::string PadPrefix(absl::string_view tag) { - return tag.empty() ? "" : absl::StrCat(" ", tag); -} - -std::string MessageName(upb::MessageDefPtr descriptor) { - return ToCIdent(descriptor.full_name()); -} - -std::string FileLayoutName(upb::FileDefPtr file) { - return ToCIdent(file.name()) + "_upb_file_layout"; -} - -bool HasFilename(upb::FileDefPtr file, absl::string_view filename) { - return file.name() == filename; -} - -bool IsDescriptorProto(upb::FileDefPtr file) { - return HasFilename(file, "net/proto2/proto/descriptor.proto") || - HasFilename(file, "google/protobuf/descriptor.proto"); -} - -std::string CApiHeaderFilename(upb::FileDefPtr file, bool bootstrap) { - if (bootstrap) { - if (IsDescriptorProto(file)) { - return "upb/reflection/descriptor_bootstrap.h"; - } else { - return "upb_generator/plugin_bootstrap.h"; - } - } - return StripExtension(file.name()) + ".upb.h"; -} - -std::string MiniTableHeaderFilename(upb::FileDefPtr file, bool bootstrap) { - std::string base; - if (bootstrap) { - if (IsDescriptorProto(file)) { - base = "upb/reflection/stage1/"; - } else { - base = "upb_generator/stage1/"; - } - } - return base + StripExtension(file.name()) + ".upb_minitable.h"; -} - -std::string EnumInit(upb::EnumDefPtr descriptor) { - return ToCIdent(descriptor.full_name()) + "_enum_init"; -} - std::string FieldInitializer(upb::FieldDefPtr field, const upb_MiniTableField* field64, const upb_MiniTableField* field32) { diff --git a/upb_generator/common.h b/upb_generator/common.h index 415fa2296f..6dc2f8c84d 100644 --- a/upb_generator/common.h +++ b/upb_generator/common.h @@ -58,20 +58,6 @@ class Output { std::string output_; }; -std::string StripExtension(absl::string_view fname); -std::string ToCIdent(absl::string_view str); -std::string ToPreproc(absl::string_view str); -void EmitFileWarning(absl::string_view name, Output& output); -PROTOC_EXPORT std::string MessageInit(absl::string_view full_name); -std::string MessageInitName(upb::MessageDefPtr descriptor); -std::string MessageName(upb::MessageDefPtr descriptor); -std::string FileLayoutName(upb::FileDefPtr file); -std::string MiniTableHeaderFilename(upb::FileDefPtr file, bool bootstrap); -std::string CApiHeaderFilename(upb::FileDefPtr file, bool bootstrap); -std::string PadPrefix(absl::string_view tag); - -std::string EnumInit(upb::EnumDefPtr descriptor); - std::string FieldInitializer(upb::FieldDefPtr field, const upb_MiniTableField* field64, const upb_MiniTableField* field32); diff --git a/upb_generator/common/BUILD b/upb_generator/common/BUILD new file mode 100644 index 0000000000..8687b7f87e --- /dev/null +++ b/upb_generator/common/BUILD @@ -0,0 +1,22 @@ +# Copyright (c) 2009-2024, Google LLC +# All rights reserved. +# +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file or at +# https://developers.google.com/open-source/licenses/bsd + +package(default_applicable_licenses = ["//:license"]) + +# Note: this library should not depend on upb reflection or C++ proto +# reflection. It is intended to be a lightweight target that can be used by +# other code generators without pulling in reflection of either library. +cc_library( + name = "names", + srcs = ["names.cc"], + hdrs = ["names.h"], + visibility = ["//upb_generator:__subpackages__"], + deps = [ + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:string_view", + ], +) diff --git a/upb_generator/common/README.md b/upb_generator/common/README.md new file mode 100644 index 0000000000..24f2f00182 --- /dev/null +++ b/upb_generator/common/README.md @@ -0,0 +1,2 @@ +This directory contains APIs that are used by multiple code generators, but not +public to users. diff --git a/upb_generator/common/names.cc b/upb_generator/common/names.cc new file mode 100644 index 0000000000..8cec63fdbe --- /dev/null +++ b/upb_generator/common/names.cc @@ -0,0 +1,71 @@ +// Copyright (c) 2009-2024, Google LLC +// All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb_generator/common/names.h" + +#include +#include + +#include "absl/strings/ascii.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_replace.h" +#include "absl/strings/string_view.h" +#include "absl/strings/substitute.h" + +namespace upb { +namespace generator { + +namespace { + +std::string ToCIdent(absl::string_view str) { + return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); +} + +std::string ToPreproc(absl::string_view str) { + return absl::AsciiStrToUpper(ToCIdent(str)); +} + +} // namespace + +bool IsDescriptorProto(absl::string_view filename) { + return filename == "net/proto2/proto/descriptor.proto" || + filename == "google/protobuf/descriptor.proto"; +} + +std::string StripExtension(absl::string_view fname) { + size_t lastdot = fname.find_last_of('.'); + if (lastdot == std::string::npos) { + return std::string(fname); + } + return std::string(fname.substr(0, lastdot)); +} + +std::string IncludeGuard(absl::string_view filename) { + return ToPreproc(filename) + "_UPB_H_"; +} + +std::string FileWarning(absl::string_view filename) { + return absl::Substitute( + "/* This file was generated by upb_generator from the input file:\n" + " *\n" + " * $0\n" + " *\n" + " * Do not edit -- your changes will be discarded when the file is\n" + " * regenerated.\n" + " * NO CHECKED-IN " + // Intentional line break. + "PROTOBUF GENCODE */\n" + "\n", + filename); +} + +std::string PadPrefix(absl::string_view tag) { + return tag.empty() ? "" : absl::StrCat(" ", tag); +} + +} // namespace generator +} // namespace upb diff --git a/upb_generator/common/names.h b/upb_generator/common/names.h new file mode 100644 index 0000000000..d8db3a1fb0 --- /dev/null +++ b/upb_generator/common/names.h @@ -0,0 +1,30 @@ +// Copyright (c) 2009-2024, Google LLC +// All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +// This file contains common functionality used by names.h in other code +// generators. + +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_COMMON_NAMES_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_COMMON_NAMES_H_ + +#include + +#include "absl/strings/string_view.h" + +namespace upb { +namespace generator { + +bool IsDescriptorProto(absl::string_view filename); +std::string StripExtension(absl::string_view fname); +std::string IncludeGuard(absl::string_view filename); +std::string FileWarning(absl::string_view filename); +std::string PadPrefix(absl::string_view tag); + +} // namespace generator +} // namespace upb + +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_COMMON_NAMES_H_ diff --git a/upb_generator/mangle.cc b/upb_generator/mangle.cc deleted file mode 100644 index c8a69f02ee..0000000000 --- a/upb_generator/mangle.cc +++ /dev/null @@ -1,21 +0,0 @@ -#include "upb_generator/mangle.h" - -#include - -#include "absl/strings/str_replace.h" -#include "absl/strings/string_view.h" - -// Generate a mangled C name for a proto object. -static std::string MangleName(absl::string_view name) { - return absl::StrReplaceAll(name, {{"_", "_0"}, {".", "__"}}); -} - -namespace upb { -namespace generator { - -std::string MessageInit(absl::string_view full_name) { - return MangleName(full_name) + "_msg_init"; -} - -} // namespace generator -} // namespace upb diff --git a/upb_generator/mangle.h b/upb_generator/mangle.h deleted file mode 100644 index 04b7c9864c..0000000000 --- a/upb_generator/mangle.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_ -#define THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_ - -#include - -#include "absl/strings/string_view.h" - -// Must be last. -#include "google/protobuf/port_def.inc" - -namespace upb { -namespace generator { - -PROTOC_EXPORT std::string MessageInit(absl::string_view full_name); - -} // namespace generator -} // namespace upb - -#include "google/protobuf/port_undef.inc" - -#endif // THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_ diff --git a/upb_generator/minitable/BUILD b/upb_generator/minitable/BUILD index eba318d08f..bd4695f5f6 100644 --- a/upb_generator/minitable/BUILD +++ b/upb_generator/minitable/BUILD @@ -34,6 +34,8 @@ bootstrap_cc_library( ], copts = UPB_DEFAULT_CPPOPTS, deps = [ + ":names", + ":names_internal", "//src/google/protobuf/compiler:code_generator_lite", "//upb:base", "//upb:mem", @@ -41,7 +43,7 @@ bootstrap_cc_library( "//upb:port", "//upb:wire_reader", "//upb/mini_table:internal", - "//upb_generator:names", + "//upb_generator/common:names", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/log:absl_check", @@ -50,6 +52,37 @@ bootstrap_cc_library( ], ) +cc_library( + name = "names", + srcs = ["names.cc"], + hdrs = ["names.h"], + visibility = [ + "//src/google/protobuf/compiler/hpb:__pkg__", + "//src/google/protobuf/compiler/rust:__pkg__", + "//third_party/kotlin/protobuf/generator/native:__pkg__", + "//upb_generator:__subpackages__", + ], + deps = [ + ":names_internal", + "//upb:port", + "@com_google_absl//absl/strings", + ], +) + +cc_library( + name = "names_internal", + srcs = ["names_internal.cc"], + hdrs = ["names_internal.h"], + visibility = [ + # For bootstrapping. + "//upb_generator/c:__pkg__", + ], + deps = [ + "//upb_generator/common:names", + "@com_google_absl//absl/strings", + ], +) + bootstrap_cc_binary( name = "protoc-gen-upb_minitable", bootstrap_deps = [ @@ -69,18 +102,21 @@ bootstrap_cc_library( name = "generator_with_main", srcs = ["main.cc"], bootstrap_deps = [ + ":generator", "//upb_generator:file_layout", "//upb_generator:common", "//upb_generator:plugin", - ":generator", "//upb/reflection:reflection", ], copts = UPB_DEFAULT_CPPOPTS, visibility = ["//pkg:__pkg__"], deps = [ + ":names", + ":names_internal", "//src/google/protobuf/compiler:code_generator_lite", "//upb:base", "//upb:port", + "//upb_generator/common:names", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_log", "@com_google_absl//absl/strings", diff --git a/upb_generator/minitable/fasttable.cc b/upb_generator/minitable/fasttable.cc index 3b530b54ec..9986d01f5a 100644 --- a/upb_generator/minitable/fasttable.cc +++ b/upb_generator/minitable/fasttable.cc @@ -1,3 +1,10 @@ +// Copyright (c) 2009-2024, Google LLC +// All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + #include "upb_generator/minitable/fasttable.h" #include diff --git a/upb_generator/minitable/fasttable.h b/upb_generator/minitable/fasttable.h index 971aa2e08b..9aadc46808 100644 --- a/upb_generator/minitable/fasttable.h +++ b/upb_generator/minitable/fasttable.h @@ -1,3 +1,10 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + #ifndef THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_FASTTABLE_H_ #define THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_FASTTABLE_H_ diff --git a/upb_generator/minitable/generator.cc b/upb_generator/minitable/generator.cc index c2f955bcb6..99c63da067 100644 --- a/upb_generator/minitable/generator.cc +++ b/upb_generator/minitable/generator.cc @@ -1,4 +1,4 @@ -// Protocol Buffers - Google's data interchange format +// Protocol Buffers - Google's data interrdchange format // Copyright 2023 Google LLC. All rights reserved. // // Use of this source code is governed by a BSD-style @@ -18,15 +18,17 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/strings/substitute.h" -#include "google/protobuf/compiler/code_generator_lite.h" #include "upb/mini_table/enum.h" #include "upb/mini_table/field.h" #include "upb/mini_table/internal/field.h" #include "upb/mini_table/message.h" #include "upb/reflection/def.hpp" #include "upb_generator/common.h" +#include "upb_generator/common/names.h" #include "upb_generator/file_layout.h" #include "upb_generator/minitable/fasttable.h" +#include "upb_generator/minitable/names.h" +#include "upb_generator/minitable/names_internal.h" #include "upb_generator/plugin.h" // Must be last. @@ -36,27 +38,31 @@ namespace upb { namespace generator { namespace { -std::string ExtensionIdentBase(upb::FieldDefPtr ext) { - UPB_ASSERT(ext.is_extension()); - std::string ext_scope; - if (ext.extension_scope()) { - return MessageName(ext.extension_scope()); - } else { - return ToCIdent(ext.file().package()); - } +// Some local convenience aliases for MiniTable variable names. + +std::string MessageVarName(upb::MessageDefPtr message) { + return MiniTableMessageVarName(message.full_name()); +} + +std::string MessagePtrVarName(upb::MessageDefPtr message) { + return MiniTableMessagePtrVarName(message.full_name()); } -std::string ExtensionLayout(upb::FieldDefPtr ext) { - return absl::StrCat(ExtensionIdentBase(ext), "_", ext.name(), "_ext"); +std::string EnumVarName(upb::EnumDefPtr e) { + return MiniTableEnumVarName(e.full_name()); } -std::string MessagePtrName(upb::MessageDefPtr message) { - return MessageInitName(message) + "_ptr"; +std::string ExtensionVarName(upb::FieldDefPtr ext) { + return MiniTableExtensionVarName(ext.full_name()); } -const char* kEnumsInit = "enums_layout"; -const char* kExtensionsInit = "extensions_layout"; -const char* kMessagesInit = "messages_layout"; +std::string FileVarName(upb::FileDefPtr file) { + return MiniTableFileVarName(file.name()); +} + +std::string HeaderFilename(upb::FileDefPtr file, bool bootstrap) { + return MiniTableHeaderFilename(file.name(), bootstrap); +} std::string ArchDependentSize(int64_t size32, int64_t size64) { if (size32 == size64) return absl::StrCat(size32); @@ -78,14 +84,14 @@ void WriteMessageField(upb::FieldDefPtr field, std::string GetSub(upb::FieldDefPtr field, bool is_extension) { if (auto message_def = field.message_type()) { return absl::Substitute("{.UPB_PRIVATE(submsg) = &$0}", - is_extension ? MessageInitName(message_def) - : MessagePtrName(message_def)); + is_extension ? MessageVarName(message_def) + : MessagePtrVarName(message_def)); } if (auto enum_def = field.enum_subdef()) { if (enum_def.is_closed()) { return absl::Substitute("{.UPB_PRIVATE(subenum) = &$0}", - EnumInit(enum_def)); + MiniTableEnumVarName(enum_def.full_name())); } } @@ -99,7 +105,6 @@ bool IsCrossFile(upb::FieldDefPtr field) { // Writes a single message into a .upb.c source file. void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, const MiniTableOptions& options, Output& output) { - std::string msg_name = ToCIdent(message.full_name()); std::string fields_array_ref = "NULL"; std::string submsgs_array_ref = "NULL"; std::string subenums_array_ref = "NULL"; @@ -123,7 +128,7 @@ void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, output( "__attribute__((weak)) const upb_MiniTable* $0 =" " &UPB_PRIVATE(_kUpb_MiniTable_StaticallyTreeShaken);\n", - MessagePtrName(field.message_type())); + MessagePtrVarName(field.message_type())); } } } @@ -131,7 +136,8 @@ void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, // Write upb_MiniTableSubInternal table for sub messages referenced from // fields. if (!subs.empty()) { - std::string submsgs_array_name = msg_name + "_submsgs"; + std::string submsgs_array_name = + MiniTableSubMessagesVarName(message.full_name()); submsgs_array_ref = "&" + submsgs_array_name + "[0]"; output("static const upb_MiniTableSubInternal $0[$1] = {\n", submsgs_array_name, subs.size()); @@ -147,7 +153,7 @@ void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, // Write upb_MiniTableField table. if (mt_64->UPB_PRIVATE(field_count) > 0) { - std::string fields_array_name = msg_name + "__fields"; + std::string fields_array_name = MiniTableFieldsVarName(message.full_name()); fields_array_ref = "&" + fields_array_name + "[0]"; output("static const upb_MiniTableField $0[$1] = {\n", fields_array_name, mt_64->UPB_PRIVATE(field_count)); @@ -180,7 +186,7 @@ void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, } } - output("const upb_MiniTable $0 = {\n", MessageInitName(message)); + output("const upb_MiniTable $0 = {\n", MessageVarName(message)); output(" $0,\n", submsgs_array_ref); output(" $0,\n", fields_array_ref); output(" $0, $1, $2, $3, UPB_FASTTABLE_MASK($4), $5,\n", @@ -200,8 +206,8 @@ void WriteMessage(upb::MessageDefPtr message, const DefPoolPair& pools, output(" })\n"); } output("};\n\n"); - output("const upb_MiniTable* $0 = &$1;\n", MessagePtrName(message), - MessageInitName(message)); + output("const upb_MiniTable* $0 = &$1;\n", MessagePtrVarName(message), + MessageVarName(message)); } void WriteEnum(upb::EnumDefPtr e, Output& output) { @@ -223,7 +229,7 @@ void WriteEnum(upb::EnumDefPtr e, Output& output) { $3, }; )cc", - EnumInit(e), mt->UPB_PRIVATE(mask_limit), mt->UPB_PRIVATE(value_count), + EnumVarName(e), mt->UPB_PRIVATE(mask_limit), mt->UPB_PRIVATE(value_count), values_init); output("\n"); } @@ -231,9 +237,9 @@ void WriteEnum(upb::EnumDefPtr e, Output& output) { void WriteExtension(const DefPoolPair& pools, upb::FieldDefPtr ext, Output& output) { output("UPB_LINKARR_APPEND(upb_AllExts)\n"); - output("const upb_MiniTableExtension $0 = {\n ", ExtensionLayout(ext)); + output("const upb_MiniTableExtension $0 = {\n ", ExtensionVarName(ext)); output("$0,\n", FieldInitializer(pools, ext)); - output(" &$0,\n", MessageInitName(ext.containing_type())); + output(" &$0,\n", MessageVarName(ext.containing_type())); output(" $0,\n", GetSub(ext, true)); output("\n};\n"); } @@ -242,20 +248,19 @@ void WriteExtension(const DefPoolPair& pools, upb::FieldDefPtr ext, void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file, const MiniTableOptions& options, Output& output) { - EmitFileWarning(file.name(), output); + output(FileWarning(file.name())); output( "#ifndef $0_UPB_MINITABLE_H_\n" "#define $0_UPB_MINITABLE_H_\n\n" "#include \"upb/generated_code_support.h\"\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); for (int i = 0; i < file.public_dependency_count(); i++) { if (i == 0) { output("/* Public Imports. */\n"); } - output( - "#include \"$0\"\n", - MiniTableHeaderFilename(file.public_dependency(i), options.bootstrap)); + output("#include \"$0\"\n", + HeaderFilename(file.public_dependency(i), options.bootstrap)); if (i == file.public_dependency_count() - 1) { output("\n"); } @@ -276,11 +281,11 @@ void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file, const std::vector this_file_exts = SortedExtensions(file); for (auto message : this_file_messages) { - output("extern const upb_MiniTable $0;\n", MessageInitName(message)); - output("extern const upb_MiniTable* $0;\n", MessagePtrName(message)); + output("extern const upb_MiniTable $0;\n", MessageVarName(message)); + output("extern const upb_MiniTable* $0;\n", MessagePtrVarName(message)); } for (auto ext : this_file_exts) { - output("extern const upb_MiniTableExtension $0;\n", ExtensionLayout(ext)); + output("extern const upb_MiniTableExtension $0;\n", ExtensionVarName(ext)); } output("\n"); @@ -289,10 +294,10 @@ void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file, SortedEnums(file, kClosedEnums); for (const auto enumdesc : this_file_enums) { - output("extern const upb_MiniTableEnum $0;\n", EnumInit(enumdesc)); + output("extern const upb_MiniTableEnum $0;\n", EnumVarName(enumdesc)); } - output("extern const upb_MiniTableFile $0;\n\n", FileLayoutName(file)); + output("extern const upb_MiniTableFile $0;\n\n", FileVarName(file)); output( "#ifdef __cplusplus\n" @@ -302,19 +307,19 @@ void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file, "#include \"upb/port/undef.inc\"\n" "\n" "#endif /* $0_UPB_MINITABLE_H_ */\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); } void WriteMiniTableSourceIncludes(upb::FileDefPtr file, const MiniTableOptions& options, Output& output) { - EmitFileWarning(file.name(), output); + output(FileWarning(file.name())); output( "#include \n" "#include \"upb/generated_code_support.h\"\n" "#include \"$0\"\n", - MiniTableHeaderFilename(file, options.bootstrap)); + HeaderFilename(file, options.bootstrap)); for (int i = 0; i < file.dependency_count(); i++) { if (options.strip_nonfunctional_codegen && @@ -323,7 +328,7 @@ void WriteMiniTableSourceIncludes(upb::FileDefPtr file, continue; } output("#include \"$0\"\n", - MiniTableHeaderFilename(file.dependency(i), options.bootstrap)); + HeaderFilename(file.dependency(i), options.bootstrap)); } output( @@ -347,13 +352,14 @@ void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, if (options.one_output_per_message) { for (auto message : messages) { - output("extern const upb_MiniTable* $0;\n", MessagePtrName(message)); + output("extern const upb_MiniTable* $0;\n", MessagePtrVarName(message)); } for (const auto e : enums) { - output("extern const upb_MiniTableEnum $0;\n", EnumInit(e)); + output("extern const upb_MiniTableEnum $0;\n", EnumVarName(e)); } for (const auto ext : extensions) { - output("extern const upb_MiniTableExtension $0;\n", ExtensionLayout(ext)); + output("extern const upb_MiniTableExtension $0;\n", + ExtensionVarName(ext)); } } else { for (auto message : messages) { @@ -372,7 +378,7 @@ void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, output("static const upb_MiniTable *$0[$1] = {\n", kMessagesInit, messages.size()); for (auto message : messages) { - output(" &$0,\n", MessageInitName(message)); + output(" &$0,\n", MessageVarName(message)); } output("};\n"); output("\n"); @@ -383,7 +389,7 @@ void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, output("static const upb_MiniTableEnum *$0[$1] = {\n", kEnumsInit, enums.size()); for (const auto e : enums) { - output(" &$0,\n", EnumInit(e)); + output(" &$0,\n", EnumVarName(e)); } output("};\n"); output("\n"); @@ -397,7 +403,7 @@ void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, kExtensionsInit, extensions.size()); for (auto ext : extensions) { - output(" &$0,\n", ExtensionLayout(ext)); + output(" &$0,\n", ExtensionVarName(ext)); } output( @@ -405,7 +411,7 @@ void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file, "\n"); } - output("const upb_MiniTableFile $0 = {\n", FileLayoutName(file)); + output("const upb_MiniTableFile $0 = {\n", FileVarName(file)); output(" $0,\n", messages.empty() ? "NULL" : kMessagesInit); output(" $0,\n", enums.empty() ? "NULL" : kEnumsInit); output(" $0,\n", extensions.empty() ? "NULL" : kExtensionsInit); diff --git a/upb_generator/minitable/generator.h b/upb_generator/minitable/generator.h index 358a157d7f..1422974457 100644 --- a/upb_generator/minitable/generator.h +++ b/upb_generator/minitable/generator.h @@ -5,18 +5,7 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - +#include "upb/reflection/def.hpp" #include "upb_generator/common.h" #include "upb_generator/file_layout.h" #include "upb_generator/plugin.h" diff --git a/upb_generator/minitable/main.cc b/upb_generator/minitable/main.cc index 9f3695bcef..eff0186dd3 100644 --- a/upb_generator/minitable/main.cc +++ b/upb_generator/minitable/main.cc @@ -17,8 +17,10 @@ #include "upb/base/string_view.h" #include "upb/reflection/def.hpp" #include "upb_generator/common.h" +#include "upb_generator/common/names.h" #include "upb_generator/file_layout.h" #include "upb_generator/minitable/generator.h" +#include "upb_generator/minitable/names_internal.h" #include "upb_generator/plugin.h" // Must be last. @@ -39,7 +41,7 @@ void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file, const MiniTableOptions& options, Plugin* plugin) { Output h_output; WriteMiniTableHeader(pools, file, options, h_output); - plugin->AddOutputFile(MiniTableHeaderFilename(file, false), + plugin->AddOutputFile(MiniTableHeaderFilename(file.name(), false), h_output.output()); Output c_output; diff --git a/upb_generator/minitable/names.cc b/upb_generator/minitable/names.cc new file mode 100644 index 0000000000..976f772370 --- /dev/null +++ b/upb_generator/minitable/names.cc @@ -0,0 +1,56 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb_generator/minitable/names.h" + +#include + +#include "absl/strings/str_replace.h" +#include "absl/strings/string_view.h" +#include "upb_generator/minitable/names_internal.h" + +namespace upb { +namespace generator { + +namespace { + +std::string ToCIdent(absl::string_view str) { + return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); +} + +std::string MangleName(absl::string_view name) { + return absl::StrReplaceAll(name, {{"_", "_0"}, {".", "__"}}); +} + +} // namespace + +std::string MiniTableHeaderFilename(absl::string_view proto_filename) { + return MiniTableHeaderFilename(proto_filename, false); +} + +std::string MiniTableMessageVarName(absl::string_view full_name) { + return MangleName(full_name) + "_msg_init"; +} + +std::string MiniTableMessagePtrVarName(absl::string_view full_name) { + return MiniTableMessageVarName(full_name) + "_ptr"; +} + +std::string MiniTableEnumVarName(absl::string_view full_name) { + return MangleName(full_name) + "_enum_init"; +} + +std::string MiniTableExtensionVarName(absl::string_view full_name) { + return ToCIdent(full_name) + "_ext"; +} + +std::string MiniTableFileVarName(absl::string_view proto_filename) { + return ToCIdent(proto_filename) + "_upb_file_layout"; +} + +} // namespace generator +} // namespace upb diff --git a/upb_generator/minitable/names.h b/upb_generator/minitable/names.h new file mode 100644 index 0000000000..5e6156e4f8 --- /dev/null +++ b/upb_generator/minitable/names.h @@ -0,0 +1,46 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_H_ + +#include + +#include "absl/strings/string_view.h" + +// Must be last. +#include "upb/port/def.inc" + +namespace upb { +namespace generator { + +// Maps: foo/bar/baz.proto -> foo/bar/baz.upb_minitable.h +UPBC_API std::string MiniTableHeaderFilename(absl::string_view proto_filename); + +// These are the publicly visible symbols defined in foo.upb_minitable.h. +// extern const upb_MiniTable ; // One for each message. +// extern const upb_MiniTableEnum ; // One for each enum. +// extern const upb_MiniTableExtension ; // One for each ext. +// extern const upb_MiniTableFile ; // One for each file. +// +// extern const upb_MiniTable* ; + +UPBC_API std::string MiniTableMessageVarName(absl::string_view full_name); +UPBC_API std::string MiniTableEnumVarName(absl::string_view full_name); +UPBC_API std::string MiniTableExtensionVarName(absl::string_view full_name); +UPBC_API std::string MiniTableFileVarName(absl::string_view proto_filename); + +// This is used for weak linking and tree shaking. Other translation units may +// define weak versions of this symbol that point to a dummy message, to +// gracefully degrade the behavior of the generated code when the message is not +// linked into the current binary. +UPBC_API std::string MiniTableMessagePtrVarName(absl::string_view full_name); + +} // namespace generator +} // namespace upb + +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_H_ diff --git a/upb_generator/minitable/names_internal.cc b/upb_generator/minitable/names_internal.cc new file mode 100644 index 0000000000..86058f6fbf --- /dev/null +++ b/upb_generator/minitable/names_internal.cc @@ -0,0 +1,49 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb_generator/minitable/names_internal.h" + +#include + +#include "absl/strings/str_replace.h" +#include "absl/strings/string_view.h" +#include "upb_generator/common/names.h" + +namespace upb { +namespace generator { + +namespace { + +std::string ToCIdent(absl::string_view str) { + return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); +} + +} // namespace + +std::string MiniTableHeaderFilename(absl::string_view proto_filename, + bool bootstrap) { + std::string base; + if (bootstrap) { + if (IsDescriptorProto(proto_filename)) { + base = "upb/reflection/stage1/"; + } else { + base = "upb_generator/stage1/"; + } + } + return base + StripExtension(proto_filename) + ".upb_minitable.h"; +} + +std::string MiniTableFieldsVarName(absl::string_view msg_full_name) { + return ToCIdent(msg_full_name) + "__fields"; +} + +std::string MiniTableSubMessagesVarName(absl::string_view msg_full_name) { + return ToCIdent(msg_full_name) + "__submsgs"; +} + +} // namespace generator +} // namespace upb diff --git a/upb_generator/minitable/names_internal.h b/upb_generator/minitable/names_internal.h new file mode 100644 index 0000000000..be4fae82d7 --- /dev/null +++ b/upb_generator/minitable/names_internal.h @@ -0,0 +1,35 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_INTERNAL_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_INTERNAL_H_ + +#include + +#include "absl/strings/string_view.h" + +namespace upb { +namespace generator { + +// Like the public MiniTableHeaderFilename, but also handles the case where we +// are generating a bootstrap header. +std::string MiniTableHeaderFilename(absl::string_view proto_filename, + bool bootstrap); + +// Global static variables used in the generated .c file. +constexpr char kEnumsInit[] = "enums_layout"; +constexpr char kExtensionsInit[] = "extensions_layout"; +constexpr char kMessagesInit[] = "messages_layout"; + +// Per-message static variables used in the generated .c file. +std::string MiniTableFieldsVarName(absl::string_view msg_full_name); +std::string MiniTableSubMessagesVarName(absl::string_view msg_full_name); + +} // namespace generator +} // namespace upb + +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_MINITABLE_NAMES_INTERNAL_H_ diff --git a/upb_generator/reflection/BUILD b/upb_generator/reflection/BUILD index be01fb7f19..645463baea 100644 --- a/upb_generator/reflection/BUILD +++ b/upb_generator/reflection/BUILD @@ -11,6 +11,17 @@ package(default_applicable_licenses = ["//:license"]) licenses(["notice"]) +cc_library( + name = "names", + srcs = ["names.cc"], + hdrs = ["names.h"], + deps = [ + "//upb:port", + "//upb_generator/common:names", + "@com_google_absl//absl/strings", + ], +) + cc_binary( name = "protoc-gen-upbdefs", linkopts = ["-lm"], @@ -30,12 +41,18 @@ cc_library( # /usr/bin/x86_64-linux-gnu-ld: /opt/manylinux/2014/x86_64/lib64/libm.so.6: error adding symbols: DSO missing from command line # clang-14: error: linker command failed with exit code 1 (use -v to see invocation) deps = [ + ":names", "//src/google/protobuf:descriptor_upb_c_proto", + "//upb:mem", "//upb:reflection", "//upb/util:def_to_proto", "//upb_generator:common", "//upb_generator:file_layout", "//upb_generator:plugin", + "//upb_generator/common:names", + "//upb_generator/minitable:names", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:string_view", ], ) diff --git a/upb_generator/reflection/generator.cc b/upb_generator/reflection/generator.cc index e2742a5dc9..3c748f9650 100644 --- a/upb_generator/reflection/generator.cc +++ b/upb_generator/reflection/generator.cc @@ -5,14 +5,22 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd -#include +#include +#include #include "google/protobuf/descriptor.upb.h" +#include "absl/strings/escaping.h" +#include "absl/strings/string_view.h" +#include "absl/strings/substitute.h" +#include "upb/mem/arena.hpp" #include "upb/reflection/def.hpp" #include "upb/util/def_to_proto.h" #include "upb_generator/common.h" +#include "upb_generator/common/names.h" #include "upb_generator/file_layout.h" +#include "upb_generator/minitable/names.h" #include "upb_generator/plugin.h" +#include "upb_generator/reflection/names.h" namespace upb { namespace generator { @@ -23,7 +31,7 @@ struct Options { }; std::string DefInitSymbol(upb::FileDefPtr file) { - return ToCIdent(file.name()) + "_upbdefinit"; + return ReflectionFileSymbol(file.name()); } static std::string DefHeaderFilename(upb::FileDefPtr file) { @@ -35,8 +43,8 @@ static std::string DefSourceFilename(upb::FileDefPtr file) { } void GenerateMessageDefAccessor(upb::MessageDefPtr d, Output& output) { - output("UPB_INLINE const upb_MessageDef *$0_getmsgdef(upb_DefPool *s) {\n", - ToCIdent(d.full_name())); + output("UPB_INLINE const upb_MessageDef *$0(upb_DefPool *s) {\n", + ReflectionGetMessageSymbol(d.full_name())); output(" _upb_DefPool_LoadDefInit(s, &$0);\n", DefInitSymbol(d.file())); output(" return upb_DefPool_FindMessageByName(s, \"$0\");\n", d.full_name()); output("}\n"); @@ -45,7 +53,7 @@ void GenerateMessageDefAccessor(upb::MessageDefPtr d, Output& output) { void WriteDefHeader(upb::FileDefPtr file, const Options& options, Output& output) { - EmitFileWarning(file.name(), output); + output(FileWarning(file.name())); output( "#ifndef $0_UPBDEFS_H_\n" @@ -57,7 +65,7 @@ void WriteDefHeader(upb::FileDefPtr file, const Options& options, "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif\n\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); output("extern$1 _upb_DefPool_Init $0;\n", DefInitSymbol(file), PadPrefix(options.dllexport_decl)); @@ -75,16 +83,16 @@ void WriteDefHeader(upb::FileDefPtr file, const Options& options, "#include \"upb/port/undef.inc\"\n" "\n" "#endif /* $0_UPBDEFS_H_ */\n", - ToPreproc(file.name())); + IncludeGuard(file.name())); } void WriteDefSource(upb::FileDefPtr file, const Options& options, Output& output) { - EmitFileWarning(file.name(), output); + output(FileWarning(file.name())); output("#include \"upb/reflection/def.h\"\n"); output("#include \"$0\"\n", DefHeaderFilename(file)); - output("#include \"$0\"\n", MiniTableHeaderFilename(file, false)); + output("#include \"$0\"\n", MiniTableHeaderFilename(file.name())); output("\n"); for (int i = 0; i < file.dependency_count(); i++) { @@ -126,7 +134,7 @@ void WriteDefSource(upb::FileDefPtr file, const Options& options, output("_upb_DefPool_Init $0 = {\n", DefInitSymbol(file)); output(" deps,\n"); - output(" &$0,\n", FileLayoutName(file)); + output(" &$0,\n", MiniTableFileVarName(file.name())); output(" \"$0\",\n", file.name()); output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size()); output("};\n"); diff --git a/upb_generator/reflection/names.cc b/upb_generator/reflection/names.cc new file mode 100644 index 0000000000..15050f5c4f --- /dev/null +++ b/upb_generator/reflection/names.cc @@ -0,0 +1,34 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "upb_generator/reflection/names.h" + +#include + +#include "absl/strings/str_replace.h" +#include "absl/strings/string_view.h" + +namespace upb { +namespace generator { + +namespace { + +std::string ToCIdent(absl::string_view str) { + return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}}); +} + +} // namespace + +std::string ReflectionGetMessageSymbol(absl::string_view full_name) { + return ToCIdent(full_name) + "_getmsgdef"; +} +std::string ReflectionFileSymbol(absl::string_view filename) { + return ToCIdent(filename) + "_upbdefinit"; +} + +} // namespace generator +} // namespace upb diff --git a/upb_generator/reflection/names.h b/upb_generator/reflection/names.h new file mode 100644 index 0000000000..70c06d1cf3 --- /dev/null +++ b/upb_generator/reflection/names.h @@ -0,0 +1,33 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef THIRD_PARTY_UPB_UPB_GENERATOR_REFLECTION_NAMES_H_ +#define THIRD_PARTY_UPB_UPB_GENERATOR_REFLECTION_NAMES_H_ + +#include + +#include "absl/strings/string_view.h" + +// Must be last. +#include "upb/port/def.inc" + +namespace upb { +namespace generator { + +// These are the publicly visible symbols defined in foo.upbdefs.h. +// const upb_MessageDef* (upb_DefPool *s); +// extern const _upb_DefPool_Init ; + +UPBC_API std::string ReflectionGetMessageSymbol(absl::string_view full_name); +UPBC_API std::string ReflectionFileSymbol(absl::string_view filename); + +} // namespace generator +} // namespace upb + +#include "upb/port/undef.inc" + +#endif // THIRD_PARTY_UPB_UPB_GENERATOR_REFLECTION_NAMES_H_ diff --git a/upb_generator/stage0/google/protobuf/compiler/plugin.upb.c b/upb_generator/stage0/google/protobuf/compiler/plugin.upb.c index ae9abc87fa..f460319d27 100644 --- a/upb_generator/stage0/google/protobuf/compiler/plugin.upb.c +++ b/upb_generator/stage0/google/protobuf/compiler/plugin.upb.c @@ -54,7 +54,7 @@ const upb_MiniTable* google__protobuf__compiler__CodeGeneratorResponse__File_msg return mini_table; } -const upb_MiniTableEnum* google_protobuf_compiler_CodeGeneratorResponse_Feature_enum_init() { +const upb_MiniTableEnum* google__protobuf__compiler__CodeGeneratorResponse__Feature_enum_init() { static const upb_MiniTableEnum* mini_table = NULL; static const char* mini_descriptor = "!)"; if (mini_table) return mini_table; diff --git a/upb_generator/stage0/google/protobuf/compiler/plugin.upb.h b/upb_generator/stage0/google/protobuf/compiler/plugin.upb.h index aeb241478d..a6a5baed8f 100644 --- a/upb_generator/stage0/google/protobuf/compiler/plugin.upb.h +++ b/upb_generator/stage0/google/protobuf/compiler/plugin.upb.h @@ -6,8 +6,8 @@ * regenerated. * NO CHECKED-IN PROTOBUF GENCODE */ -#ifndef GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_ -#define GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_ +#ifndef GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H__UPB_H_ +#define GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H__UPB_H_ #include "upb/generated_code_support.h" @@ -24,7 +24,7 @@ extern const upb_MiniTable* google__protobuf__compiler__CodeGeneratorResponse_ms extern const upb_MiniTable* google__protobuf__compiler__CodeGeneratorResponse__File_msg_init(void); extern const upb_MiniTable* google__protobuf__FileDescriptorProto_msg_init(void); extern const upb_MiniTable* google__protobuf__GeneratedCodeInfo_msg_init(void); -extern const upb_MiniTableEnum* google_protobuf_compiler_CodeGeneratorResponse_Feature_enum_init(void); +extern const upb_MiniTableEnum* google__protobuf__compiler__CodeGeneratorResponse__Feature_enum_init(void); typedef struct google_protobuf_compiler_Version { upb_Message UPB_PRIVATE(base); } google_protobuf_compiler_Version; typedef struct google_protobuf_compiler_CodeGeneratorRequest { upb_Message UPB_PRIVATE(base); } google_protobuf_compiler_CodeGeneratorRequest; @@ -741,4 +741,4 @@ UPB_INLINE struct google_protobuf_GeneratedCodeInfo* google_protobuf_compiler_Co #include "upb/port/undef.inc" -#endif /* GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_ */ +#endif /* GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H__UPB_H_ */