upb is self-hosting!

This CL changes the upb compiler to no longer depend on C++ protobuf libraries.  upb now uses its own reflection libraries to implement its code generator.

# Key Benefits

1. upb can now use its own reflection libraries throughout the compiler.  This makes upb more consistent and principled, and gives us more chances to dogfood our own C++ reflection API.  This highlighted several parts of the C++ reflection API that were incomplete.

2. This CL removes code duplication that previously existed in the compiler.  The upb reflection library has code to build MiniDescriptors and MiniTables out of descriptors, but prior to this CL the upb compiler could not use it.  The upb compiler had a separate copy of this logic, and the compiler's copy of this logic was especially tricky and hard to maintain.  This CL removes the separate copy of that logic.

3. This CL (mostly) removes upb's dependency on the C++ protobuf library.  We still depend on `protoc` (the binary), but the runtime and compiler no longer link against C++'s libraries.  This opens up the possibility of speeding up some builds significantly if we can use a prebuilt `protoc` binary.

# Bootstrap Stages

To bootstrap, we check in a copy of our generated code for `descriptor.proto` and `plugin.proto`.  This allows the compiler to depend on the generated code for these two protos without creating a circular dependency. This code is checked in to the `stage0` directory.

The bootstrapping process is divided into a few stages.  All `cc_library()`, `upb_proto_library()`, and `cc_binary()` targets that would otherwise be circular participate in this staging process.  That currently includes:

* `//third_party/upb:descriptor_upb_proto`
* `//third_party/upb:plugin_upb_proto`
* `//third_party/upb:reflection`
* `//third_party/upb:reflection_internal`
* `//third_party/upbc:common`
* `//third_party/upbc:file_layout`
* `//third_party/upbc:plugin`
* `//third_party/upbc:protoc-gen-upb`

For each of these targets, we produce a rule for each stage (the logic for this is nicely encapsulated in Blaze/Bazel macros like `bootstrap_cc_library()` and `bootstrap_upb_proto_library()`, so the `BUILD` file remains readable).  For example:

* `//third_party/upb:descriptor_upb_proto_stage0`
* `//third_party/upb:descriptor_upb_proto_stage1`
* `//third_party/upb:descriptor_upb_proto`

The stages are:

1. `stage0`: This uses the checked-in version of the generated code.  The stage0 compiler is correct and outputs the same code as all other compilers, but it is unnecessarily slow because its protos were compiled in bootstrap mode.  The stage0 compiler is used to generate protos for stage1.

2. `stage1`: The stage1 compiler is correct and fast, and therefore we use it in almost all cases (eg. `upb_proto_library()`).  However its own protos were not generated using `upb_proto_library()`, so its `cc_library()` targets cannot be safely mixed with `upb_proto_library()`, as this would lead to duplicate symbols.

3. final (no stage): The final compiler is identical to the `stage1` compiler.  The only difference is that its protos were built with `upb_proto_library()`.  This doesn't matter very much for the compiler binary, but for the `cc_library()` targets like `//third_party/upb:reflection`, only the final targets can be safely linked in by other applications.

# "Bootstrap Mode" Protos

The checked-in generated code is generated in a special "bootstrap" mode that is a bit different than normal generated code.  Bootstrap mode avoids depending on the internal representation of MiniTables or the messages, at the cost of slower runtime performance.

Bootstrap mode only interacts with MiniTables and messages using public APIs such as `upb_MiniTable_Build()`, `upb_Message_GetInt32()`, etc.  This is very important as it allows us to change the internal representation without needing to regenerate our bootstrap protos.  This will make it far easier to write CLs that change the internal representation, because it avoids the awkward dance of trying to regenerate the bootstrap protos when the compiler itself is broken due to bootstrap protos being out of date.

The bootstrap generated code does have two downsides:

1. The accessors are less efficient, because they look up MiniTable fields by number instead of hard-coding the MiniTableField into the generated code.

2. It requires runtime initialization of the MiniTables, which costs CPU cycles at startup, and also allocates memory which is never freed.  Per google3 rules this is not really a leak, since this memory is still reachable via static variables, but it is undesirable in many contexts. We could fix this part by introducing the equivalent of `google::protobuf::ShutdownProtobufLibrary()`).

These downsides are fine for the bootstrapping process, but they are reason enough not to enable bootstrap mode in general for all protos.

# Bootstrapping Always Uses OSS Protos

To enable smooth syncing between Google3 and OSS, we always use an OSS version of the checked in generated code for `stage0`, even in google3.

This requires that the google3 code can be switched to reference the OSS proto names using a preprocessor define.  We introduce the `UPB_DESC(xyz)` macro for this, which will expand into either `proto2_xyz` or `google_protobuf_xyz`.  Any libraries used in `stage0` must use `UPB_DESC(xyz)` rather than refer to the symbol names directly.

PiperOrigin-RevId: 501458451
pull/13171/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 9b5a084118
commit e41a2d7ba0
  1. 24
      BUILD
  2. 16
      bazel/protobuf.patch
  3. 2
      bazel/upb_proto_library.bzl
  4. 5
      cmake/build_defs.bzl
  5. 6
      cmake/make_cmakelists.py
  6. 36
      protos_generator/gen_accessors.cc
  7. 4
      protos_generator/gen_accessors.h
  8. 5
      protos_generator/gen_messages.cc
  9. 78
      protos_generator/gen_utils.cc
  10. 9
      protos_generator/gen_utils.h
  11. 41
      protos_generator/protoc-gen-upb-protos.cc
  12. 1
      upb/message/test.cc
  13. 4
      upb/port/def.inc
  14. 2
      upb/port/undef.inc
  15. 10
      upb/reflection/common.h
  16. 219
      upb/reflection/def.hpp
  17. 1
      upb/reflection/def_builder_internal.h
  18. 9
      upb/reflection/def_pool.c
  19. 2
      upb/reflection/def_pool_internal.h
  20. 4
      upb/reflection/message_def.c
  21. 397
      upb/reflection/stage0/google/protobuf/descriptor.upb.c
  22. 4683
      upb/reflection/stage0/google/protobuf/descriptor.upb.h
  23. 102
      upbc/BUILD
  24. 166
      upbc/bootstrap_compiler.bzl
  25. 30
      upbc/common.cc
  26. 46
      upbc/common.h
  27. 373
      upbc/file_layout.cc
  28. 196
      upbc/file_layout.h
  29. 33
      upbc/names.cc
  30. 12
      upbc/names.h
  31. 198
      upbc/plugin.h
  32. 1041
      upbc/protoc-gen-upb.cc
  33. 132
      upbc/protoc-gen-upbdefs.cc
  34. 68
      upbc/stage0/google/protobuf/compiler/plugin.upb.c
  35. 585
      upbc/stage0/google/protobuf/compiler/plugin.upb.h

24
BUILD

@ -36,6 +36,11 @@ load(
"upb_proto_reflection_library",
)
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load(
"//upbc:bootstrap_compiler.bzl",
"bootstrap_cc_library",
"bootstrap_upb_proto_library",
)
# begin:google_only
# load(
@ -415,14 +420,21 @@ cc_library(
":base",
":descriptor_upb_proto",
":hash",
":mini_table_internal",
":reflection_internal",
],
)
upb_proto_library(
bootstrap_upb_proto_library(
name = "descriptor_upb_proto",
base_dir = "upb/reflection/",
google3_src_files = ["net/proto2/proto/descriptor.proto"],
google3_src_rules = ["//net/proto2/proto:descriptor_proto_source"],
oss_src_files = ["google/protobuf/descriptor.proto"],
oss_src_rules = ["@com_google_protobuf//:descriptor_proto_srcs"],
oss_strip_prefix = "third_party/protobuf/github/bootstrap/src",
proto_lib_deps = ["@com_google_protobuf//:descriptor_proto"],
visibility = ["//visibility:public"],
deps = ["@com_google_protobuf//:descriptor_proto"],
)
upb_proto_reflection_library(
@ -476,7 +488,7 @@ cc_library(
# TODO(b/232091617): Once we can delete the deprecated forwarding headers
# (= everything in upb/) we can move this build target down into reflection/
cc_library(
bootstrap_cc_library(
name = "reflection",
hdrs = [
"upb/def.h",
@ -488,17 +500,17 @@ cc_library(
"upb/reflection/message.h",
"upb/reflection/message.hpp",
],
bootstrap_deps = [":reflection_internal"],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":collections",
":port",
":reflection_internal",
":upb",
],
)
cc_library(
bootstrap_cc_library(
name = "reflection_internal",
srcs = [
"upb/reflection/def_builder.c",
@ -552,11 +564,11 @@ cc_library(
"upb/reflection/service_def.h",
"upb/reflection/service_def_internal.h",
],
bootstrap_deps = [":descriptor_upb_proto"],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":collections",
":descriptor_upb_proto",
":hash",
":message_accessors",
":mini_table_internal",

@ -1,3 +1,19 @@
--- src/google/protobuf/compiler/BUILD.bazel
+++ src/google/protobuf/compiler/BUILD.bazel
@@ -169,6 +169,12 @@ filegroup(
],
)
+filegroup(
+ name = "plugin_proto_src",
+ srcs = ["plugin.proto"],
+ visibility = ["@upb//:__subpackages__"],
+)
+
exports_files(
srcs = ["plugin.proto"],
visibility = ["//:__pkg__"],
--- python/google/protobuf/internal/test_util.py
+++ python/google/protobuf/internal/test_util.py
@@ -39,6 +39,7 @@ __author__ = 'robinson@google.com (Will Robinson)'

@ -320,7 +320,7 @@ upb_proto_library_aspect = aspect(
"_gen_upb": attr.label(
executable = True,
cfg = "exec",
default = "//upbc:protoc-gen-upb",
default = "//upbc:protoc-gen-upb_stage1",
),
"_protoc": attr.label(
executable = True,

@ -25,7 +25,7 @@
"""Bazel support functions related to CMake support."""
def staleness_test(name, outs, generated_pattern, **kwargs):
def staleness_test(name, outs, generated_pattern, target_files = None, **kwargs):
"""Tests that checked-in file(s) match the contents of generated file(s).
The resulting test will verify that all output files exist and have the
@ -38,6 +38,9 @@ def staleness_test(name, outs, generated_pattern, **kwargs):
generated_pattern: the pattern for transforming each "out" file into a
generated file. For example, if generated_pattern="generated/%s" then
a file foo.txt will look for generated file generated/foo.txt.
target_files: A glob representing all of the files that should be
covered by this rule. Files in this glob but not generated will
be deleted. (Not currently implemented in OSS).
**kwargs: Additional keyword arguments to pass through to py_test().
"""

@ -200,6 +200,12 @@ class BuildFileFunctions(object):
def bool_flag(self, **kwargs):
pass
def bootstrap_upb_proto_library(self, **kwargs):
pass
def bootstrap_cc_library(self, **kwargs):
pass
class WorkspaceFileFunctions(object):
def __init__(self, converter):

@ -31,7 +31,6 @@
#include "google/protobuf/descriptor.h"
#include "protos_generator/gen_utils.h"
#include "protos_generator/output.h"
#include "upbc/file_layout.h"
#include "upbc/keywords.h"
#include "upbc/names.h"
@ -39,7 +38,6 @@ namespace protos_generator {
namespace protobuf = ::google::protobuf;
using FileLayout = ::upbc::FileLayout;
using NameToFieldDescriptorMap =
absl::flat_hash_map<absl::string_view, const protobuf::FieldDescriptor*>;
@ -47,12 +45,12 @@ void WriteFieldAccessorHazzer(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view resolved_upbc_name,
const FileLayout& layout, Output& output);
Output& output);
void WriteFieldAccessorClear(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view resolved_upbc_name,
const FileLayout& layout, Output& output);
Output& output);
void WriteMapFieldAccessors(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
@ -99,22 +97,20 @@ NameToFieldDescriptorMap CreateFieldNameMap(
void WriteFieldAccessorsInHeader(const protobuf::Descriptor* desc,
Output& output) {
FileLayout layout(desc->file());
// Generate const methods.
OutputIndenter i(output);
auto field_names = CreateFieldNameMap(desc);
auto upbc_field_names = upbc::CreateFieldNameMap(desc);
for (auto field : ::upbc::FieldNumberOrder(desc)) {
for (const auto* field : FieldNumberOrder(desc)) {
std::string resolved_field_name = ResolveFieldName(field, field_names);
std::string resolved_upbc_name =
upbc::ResolveFieldName(field, upbc_field_names);
WriteFieldAccessorHazzer(desc, field, resolved_field_name,
resolved_upbc_name, layout, output);
resolved_upbc_name, output);
WriteFieldAccessorClear(desc, field, resolved_field_name,
resolved_upbc_name, layout, output);
resolved_upbc_name, output);
if (field->is_map()) {
WriteMapFieldAccessors(desc, field, resolved_field_name,
@ -188,9 +184,9 @@ void WriteFieldAccessorHazzer(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view resolved_upbc_name,
const FileLayout& layout, Output& output) {
Output& output) {
// Generate hazzer (if any).
if (layout.HasHasbit(field) || field->real_containing_oneof()) {
if (field->has_presence()) {
// Has presence.
output("inline bool has_$0() const { return $1_has_$2(msg_); }\n",
resolved_field_name, MessageName(desc), resolved_upbc_name);
@ -201,8 +197,8 @@ void WriteFieldAccessorClear(const protobuf::Descriptor* desc,
const protobuf::FieldDescriptor* field,
const absl::string_view resolved_field_name,
const absl::string_view resolved_upbc_name,
const FileLayout& layout, Output& output) {
if (layout.HasHasbit(field) || field->real_containing_oneof()) {
Output& output) {
if (field->has_presence()) {
output("void clear_$0() { $2_clear_$1(msg_); }\n", resolved_field_name,
resolved_upbc_name, MessageName(desc));
}
@ -245,8 +241,7 @@ void WriteMapFieldAccessors(const protobuf::Descriptor* desc,
}
}
void WriteAccessorsInSource(const protobuf::Descriptor* desc,
const FileLayout& layout, Output& output) {
void WriteAccessorsInSource(const protobuf::Descriptor* desc, Output& output) {
std::string class_name = ClassName(desc);
absl::StrAppend(&class_name, "Access");
output("namespace internal {\n");
@ -256,7 +251,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc,
// Generate const methods.
OutputIndenter i(output);
for (auto field : ::upbc::FieldNumberOrder(desc)) {
for (const auto* field : FieldNumberOrder(desc)) {
std::string resolved_field_name = ResolveFieldName(field, field_names);
std::string resolved_upbc_name =
upbc::ResolveFieldName(field, upbc_field_names);
@ -601,7 +596,6 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
void WriteUsingAccessorsInHeader(const protobuf::Descriptor* desc,
MessageClassType handle_type, Output& output) {
FileLayout layout(desc->file());
bool read_only = handle_type == MessageClassType::kMessageCProxy;
// Generate const methods.
@ -609,15 +603,11 @@ void WriteUsingAccessorsInHeader(const protobuf::Descriptor* desc,
std::string class_name = ClassName(desc);
auto field_names = CreateFieldNameMap(desc);
for (auto field : ::upbc::FieldNumberOrder(desc)) {
for (const auto* field : FieldNumberOrder(desc)) {
std::string resolved_field_name = ResolveFieldName(field, field_names);
// Generate hazzer (if any).
if (layout.HasHasbit(field) || field->real_containing_oneof()) {
// Has presence.
if (field->has_presence()) {
output("using $0Access::has_$1;\n", class_name, resolved_field_name);
}
// Generate clear.
if (layout.HasHasbit(field) || field->real_containing_oneof()) {
output("using $0Access::clear_$1;\n", class_name, resolved_field_name);
}
if (field->is_map()) {

@ -29,7 +29,6 @@
#include "google/protobuf/descriptor.h"
#include "protos_generator/gen_utils.h"
#include "protos_generator/output.h"
#include "upbc/file_layout.h"
namespace protos_generator {
@ -37,8 +36,7 @@ namespace protobuf = ::google::protobuf;
void WriteFieldAccessorsInHeader(const protobuf::Descriptor* desc,
Output& output);
void WriteAccessorsInSource(const protobuf::Descriptor* desc,
const ::upbc::FileLayout& layout, Output& output);
void WriteAccessorsInSource(const protobuf::Descriptor* desc, Output& output);
void WriteUsingAccessorsInHeader(const protobuf::Descriptor* desc,
MessageClassType handle_type, Output& output);
} // namespace protos_generator

@ -313,11 +313,10 @@ void WriteMessageImplementation(
R"cc(
const upb_MiniTable* $0::minitable() { return &$1; }
)cc",
ClassName(descriptor), ::upbc::MessageInit(descriptor));
ClassName(descriptor), ::upbc::MessageInit(descriptor->full_name()));
}
::upbc::FileLayout layout(descriptor->file());
WriteAccessorsInSource(descriptor, layout, output);
WriteAccessorsInSource(descriptor, output);
if (!message_is_map_entry) {
output(

@ -207,4 +207,82 @@ std::string MessageProxyType(const protobuf::FieldDescriptor* field,
"Proxy";
}
void AddEnums(const protobuf::Descriptor* message,
std::vector<const protobuf::EnumDescriptor*>* enums) {
enums->reserve(enums->size() + message->enum_type_count());
for (int i = 0; i < message->enum_type_count(); i++) {
enums->push_back(message->enum_type(i));
}
for (int i = 0; i < message->nested_type_count(); i++) {
AddEnums(message->nested_type(i), enums);
}
}
std::vector<const protobuf::EnumDescriptor*> SortedEnums(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::EnumDescriptor*> enums;
enums.reserve(file->enum_type_count());
for (int i = 0; i < file->enum_type_count(); i++) {
enums.push_back(file->enum_type(i));
}
for (int i = 0; i < file->message_type_count(); i++) {
AddEnums(file->message_type(i), &enums);
}
return enums;
}
void AddMessages(const protobuf::Descriptor* message,
std::vector<const protobuf::Descriptor*>* messages) {
messages->push_back(message);
for (int i = 0; i < message->nested_type_count(); i++) {
AddMessages(message->nested_type(i), messages);
}
}
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::Descriptor*> messages;
for (int i = 0; i < file->message_type_count(); i++) {
AddMessages(file->message_type(i), &messages);
}
return messages;
}
void AddExtensionsFromMessage(
const protobuf::Descriptor* message,
std::vector<const protobuf::FieldDescriptor*>* exts) {
for (int i = 0; i < message->extension_count(); i++) {
exts->push_back(message->extension(i));
}
for (int i = 0; i < message->nested_type_count(); i++) {
AddExtensionsFromMessage(message->nested_type(i), exts);
}
}
std::vector<const protobuf::FieldDescriptor*> SortedExtensions(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::FieldDescriptor*> ret;
for (int i = 0; i < file->extension_count(); i++) {
ret.push_back(file->extension(i));
}
for (int i = 0; i < file->message_type_count(); i++) {
AddExtensionsFromMessage(file->message_type(i), &ret);
}
return ret;
}
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message) {
std::vector<const protobuf::FieldDescriptor*> fields;
for (int i = 0; i < message->field_count(); i++) {
fields.push_back(message->field(i));
}
std::sort(fields.begin(), fields.end(),
[](const protobuf::FieldDescriptor* a,
const protobuf::FieldDescriptor* b) {
return a->number() < b->number();
});
return fields;
}
} // namespace protos_generator

@ -77,6 +77,15 @@ std::string MessageCProxyType(const protobuf::FieldDescriptor* field,
std::string MessageProxyType(const protobuf::FieldDescriptor* field,
bool is_const);
std::vector<const protobuf::EnumDescriptor*> SortedEnums(
const protobuf::FileDescriptor* file);
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file);
std::vector<const protobuf::FieldDescriptor*> SortedExtensions(
const protobuf::FileDescriptor* file);
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message);
inline constexpr absl::string_view kNoPackageNamePrefix = "protos_";
} // namespace protos_generator

@ -28,6 +28,7 @@
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/plugin.h"
#include "google/protobuf/descriptor.h"
#include "protos_generator/gen_enums.h"
#include "protos_generator/gen_extensions.h"
#include "protos_generator/gen_messages.h"
@ -41,12 +42,12 @@ namespace {
namespace protoc = ::google::protobuf::compiler;
namespace protobuf = ::google::protobuf;
using FileDescriptor = ::google::protobuf::FileDescriptor;
using FileLayout = ::upbc::FileLayout;
void WriteSource(const FileLayout& layout, Output& output,
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled);
void WriteHeader(const FileLayout& layout, Output& output);
void WriteForwardingHeader(const FileLayout& layout, Output& output);
void WriteHeader(const protobuf::FileDescriptor* file, Output& output);
void WriteForwardingHeader(const protobuf::FileDescriptor* file,
Output& output);
void WriteMessageImplementations(const protobuf::FileDescriptor* file,
Output& output);
void WriteTypedefForwardingHeader(
@ -87,26 +88,24 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
}
}
FileLayout layout(file);
// Write model.upb.fwd.h
Output forwarding_header_output(
context->Open(ForwardingHeaderFilename(file)));
WriteForwardingHeader(layout, forwarding_header_output);
WriteForwardingHeader(file, forwarding_header_output);
// Write model.upb.proto.h
Output header_output(context->Open(CppHeaderFilename(file)));
WriteHeader(layout, header_output);
WriteHeader(file, header_output);
// Write model.upb.proto.cc
Output cc_output(context->Open(CppSourceFilename(file)));
WriteSource(layout, cc_output, fasttable_enabled);
WriteSource(file, cc_output, fasttable_enabled);
return true;
}
// The forwarding header defines Access/Proxy/CProxy for message classes
// used to include when referencing dependencies to prevent transitive
// dependency headers from being included.
void WriteForwardingHeader(const FileLayout& layout, Output& output) {
const protobuf::FileDescriptor* file = layout.descriptor();
void WriteForwardingHeader(const protobuf::FileDescriptor* file,
Output& output) {
EmitFileWarning(file, output);
output(
R"cc(
@ -116,13 +115,12 @@ void WriteForwardingHeader(const FileLayout& layout, Output& output) {
ToPreproc(file->name()));
output("\n");
const std::vector<const protobuf::Descriptor*> this_file_messages =
::upbc::SortedMessages(file);
SortedMessages(file);
WriteTypedefForwardingHeader(file, this_file_messages, output);
output("#endif /* $0_UPB_FWD_H_ */\n", ToPreproc(file->name()));
}
void WriteHeader(const FileLayout& layout, Output& output) {
const protobuf::FileDescriptor* file = layout.descriptor();
void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
output(
R"cc(
@ -153,9 +151,9 @@ void WriteHeader(const FileLayout& layout, Output& output) {
output("#include \"upb/port/def.inc\"\n");
const std::vector<const protobuf::Descriptor*> this_file_messages =
::upbc::SortedMessages(file);
SortedMessages(file);
const std::vector<const protobuf::FieldDescriptor*> this_file_exts =
::upbc::SortedExtensions(file);
SortedExtensions(file);
if (!this_file_messages.empty()) {
output("\n");
@ -166,7 +164,7 @@ void WriteHeader(const FileLayout& layout, Output& output) {
WriteStartNamespace(file, output);
std::vector<const protobuf::EnumDescriptor*> this_file_enums =
::upbc::SortedEnums(file);
SortedEnums(file);
// Write Class and Enums.
WriteEnumDeclarations(this_file_enums, output);
@ -189,9 +187,8 @@ void WriteHeader(const FileLayout& layout, Output& output) {
}
// Writes a .upb.cc source file.
void WriteSource(const FileLayout& layout, Output& output,
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled) {
const protobuf::FileDescriptor* file = layout.descriptor();
EmitFileWarning(file, output);
output(
@ -212,7 +209,7 @@ void WriteSource(const FileLayout& layout, Output& output,
WriteStartNamespace(file, output);
WriteMessageImplementations(file, output);
const std::vector<const protobuf::FieldDescriptor*> this_file_exts =
::upbc::SortedExtensions(file);
SortedExtensions(file);
WriteExtensionIdentifiers(this_file_exts, output);
WriteEndNamespace(file, output);
@ -222,9 +219,9 @@ void WriteSource(const FileLayout& layout, Output& output,
void WriteMessageImplementations(const protobuf::FileDescriptor* file,
Output& output) {
const std::vector<const protobuf::FieldDescriptor*> file_exts =
::upbc::SortedExtensions(file);
SortedExtensions(file);
const std::vector<const protobuf::Descriptor*> this_file_messages =
::upbc::SortedMessages(file);
SortedMessages(file);
for (auto message : this_file_messages) {
WriteMessageImplementation(message, file_exts, output);
}

@ -271,6 +271,7 @@ TEST(MessageTest, Proto2Enum) {
pb = upb_test_Proto2EnumMessage_serialize(enum_msg, arena.ptr(), &size);
upb_test_Proto2FakeEnumMessage* fake_msg2 =
upb_test_Proto2FakeEnumMessage_parse(pb, size, arena.ptr());
ASSERT_TRUE(fake_msg2 != nullptr);
EXPECT_EQ(true, upb_test_Proto2FakeEnumMessage_has_optional_enum(fake_msg2));
EXPECT_EQ(999, upb_test_Proto2FakeEnumMessage_optional_enum(fake_msg2));

@ -246,8 +246,10 @@
* degrade to non-fasttable if the runtime or platform do not support it. */
#if !UPB_FASTTABLE
#define UPB_FASTTABLE_INIT(...)
#define UPB_FASTTABLE_MASK(mask) -1
#else
#define UPB_FASTTABLE_INIT(...) __VA_ARGS__
#define UPB_FASTTABLE_MASK(mask) mask
#endif
#undef UPB_FASTTABLE_SUPPORTED
@ -307,7 +309,7 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
// #define UPB_IS_GOOGLE3
// end:google_only
#if defined(UPB_IS_GOOGLE3)
#if defined(UPB_IS_GOOGLE3) && !defined(UPB_BOOTSTRAP_STAGE0)
#define UPB_DESC(sym) proto2_##sym
#else
#define UPB_DESC(sym) google_protobuf_##sym

@ -56,6 +56,7 @@
#undef UPB_PTRADD
#undef UPB_MUSTTAIL
#undef UPB_FASTTABLE_SUPPORTED
#undef UPB_FASTTABLE_MASK
#undef UPB_FASTTABLE
#undef UPB_FASTTABLE_INIT
#undef UPB_POISON_MEMORY_REGION
@ -64,5 +65,6 @@
#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
#undef UPB_DESC
#undef UPB_IS_GOOGLE3

@ -32,7 +32,17 @@
#ifndef UPB_REFLECTION_COMMON_H_
#define UPB_REFLECTION_COMMON_H_
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "net/proto2/proto/descriptor.upb.h"
// #else
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/descriptor.upb.h"
// end:github_only
typedef enum { kUpb_Syntax_Proto2 = 2, kUpb_Syntax_Proto3 = 3 } upb_Syntax;

@ -32,9 +32,14 @@
#include <vector>
#include "upb/reflection/def.h"
#include "upb/reflection/def_pool_internal.h"
#include "upb/reflection/enum_def_internal.h"
#include "upb/reflection/message.h"
#include "upb/upb.hpp"
// Must be last
#include "upb/port/def.inc"
namespace upb {
typedef upb_MessageValue MessageValue;
@ -53,20 +58,31 @@ class FieldDefPtr {
explicit FieldDefPtr(const upb_FieldDef* ptr) : ptr_(ptr) {}
const upb_FieldDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
typedef upb_CType Type;
typedef upb_FieldType Type;
typedef upb_CType CType;
typedef upb_Label Label;
typedef upb_FieldType DescriptorType;
FileDefPtr file() const;
const char* full_name() const { return upb_FieldDef_FullName(ptr_); }
Type type() const { return upb_FieldDef_CType(ptr_); }
const upb_MiniTableField* mini_table() const {
return upb_FieldDef_MiniTable(ptr_);
}
const UPB_DESC(FieldOptions) * options() const {
return upb_FieldDef_Options(ptr_);
}
Type type() const { return upb_FieldDef_Type(ptr_); }
CType ctype() const { return upb_FieldDef_CType(ptr_); }
Label label() const { return upb_FieldDef_Label(ptr_); }
const char* name() const { return upb_FieldDef_Name(ptr_); }
const char* json_name() const { return upb_FieldDef_JsonName(ptr_); }
uint32_t number() const { return upb_FieldDef_Number(ptr_); }
bool is_extension() const { return upb_FieldDef_IsExtension(ptr_); }
bool is_required() const { return upb_FieldDef_IsRequired(ptr_); }
bool has_presence() const { return upb_FieldDef_HasPresence(ptr_); }
// For non-string, non-submessage fields, this indicates whether binary
// protobufs are encoded in packed or non-packed format.
@ -81,26 +97,18 @@ class FieldDefPtr {
// been finalized.
uint32_t index() const { return upb_FieldDef_Index(ptr_); }
// The MessageDef to which this field belongs.
//
// If this field has been added to a MessageDef, that message can be retrieved
// directly (this is always the case for frozen FieldDefs).
//
// If the field has not yet been added to a MessageDef, you can set the name
// of the containing type symbolically instead. This is mostly useful for
// extensions, where the extension is declared separately from the message.
// The MessageDef to which this field belongs (for extensions, the extended
// message).
MessageDefPtr containing_type() const;
// For extensions, the message the extension is declared inside, or NULL if
// none.
MessageDefPtr extension_scope() const;
// The OneofDef to which this field belongs, or NULL if this field is not part
// of a oneof.
OneofDefPtr containing_oneof() const;
// The field's type according to the enum in descriptor.proto. This is not
// the same as UPB_TYPE_*, because it distinguishes between (for example)
// INT32 and SINT32, whereas our "type" enum does not. This return of
// descriptor_type() is a function of type(), integer_format(), and
// is_tag_delimited().
DescriptorType descriptor_type() const { return upb_FieldDef_Type(ptr_); }
OneofDefPtr real_containing_oneof() const;
// Convenient field type tests.
bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
@ -115,7 +123,17 @@ class FieldDefPtr {
// type must match (ie. you may only call enum_subdef() for fields where
// type() == kUpb_CType_Enum).
EnumDefPtr enum_subdef() const;
MessageDefPtr message_subdef() const;
MessageDefPtr message_type() const;
explicit operator bool() const { return ptr_ != nullptr; }
friend bool operator==(FieldDefPtr lhs, FieldDefPtr rhs) {
return lhs.ptr_ == rhs.ptr_;
}
friend bool operator!=(FieldDefPtr lhs, FieldDefPtr rhs) {
return !(lhs == rhs);
}
private:
const upb_FieldDef* ptr_;
@ -130,11 +148,16 @@ class OneofDefPtr {
const upb_OneofDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
const UPB_DESC(OneofOptions) * options() const {
return upb_OneofDef_Options(ptr_);
}
// Returns the MessageDef that contains this OneofDef.
MessageDefPtr containing_type() const;
// Returns the name of this oneof.
const char* name() const { return upb_OneofDef_Name(ptr_); }
const char* full_name() const { return upb_OneofDef_FullName(ptr_); }
// Returns the number of fields in the oneof.
int field_count() const { return upb_OneofDef_FieldCount(ptr_); }
@ -170,14 +193,28 @@ class MessageDefPtr {
MessageDefPtr() : ptr_(nullptr) {}
explicit MessageDefPtr(const upb_MessageDef* ptr) : ptr_(ptr) {}
const UPB_DESC(MessageOptions) * options() const {
return upb_MessageDef_Options(ptr_);
}
std::string MiniDescriptorEncode() const {
upb::Arena arena;
upb_StringView md;
upb_MessageDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
return std::string(md.data, md.size);
}
const upb_MessageDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
FileDefPtr file() const;
const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
const char* name() const { return upb_MessageDef_Name(ptr_); }
const upb_MiniTable* mini_table() const {
return upb_MessageDef_MiniTable(ptr_);
}
// The number of fields that belong to the MessageDef.
int field_count() const { return upb_MessageDef_FieldCount(ptr_); }
FieldDefPtr field(int i) const {
@ -186,10 +223,32 @@ class MessageDefPtr {
// The number of oneofs that belong to the MessageDef.
int oneof_count() const { return upb_MessageDef_OneofCount(ptr_); }
int real_oneof_count() const { return upb_MessageDef_RealOneofCount(ptr_); }
OneofDefPtr oneof(int i) const {
return OneofDefPtr(upb_MessageDef_Oneof(ptr_, i));
}
int enum_type_count() const { return upb_MessageDef_NestedEnumCount(ptr_); }
EnumDefPtr enum_type(int i) const;
int nested_message_count() const {
return upb_MessageDef_NestedMessageCount(ptr_);
}
MessageDefPtr nested_message(int i) const {
return MessageDefPtr(upb_MessageDef_NestedMessage(ptr_, i));
}
int nested_extension_count() const {
return upb_MessageDef_NestedExtensionCount(ptr_);
}
FieldDefPtr nested_extension(int i) const {
return FieldDefPtr(upb_MessageDef_NestedExtension(ptr_, i));
}
int extension_range_count() const {
return upb_MessageDef_ExtensionRangeCount(ptr_);
}
upb_Syntax syntax() const { return upb_MessageDef_Syntax(ptr_); }
// These return null pointers if the field is not found.
@ -224,12 +283,32 @@ class MessageDefPtr {
// Is this message a map entry?
bool mapentry() const { return upb_MessageDef_IsMapEntry(ptr_); }
FieldDefPtr map_key() const {
if (!mapentry()) return FieldDefPtr();
return FieldDefPtr(upb_MessageDef_Field(ptr_, 0));
}
FieldDefPtr map_value() const {
if (!mapentry()) return FieldDefPtr();
return FieldDefPtr(upb_MessageDef_Field(ptr_, 1));
}
// Return the type of well known type message. kUpb_WellKnown_Unspecified for
// non-well-known message.
upb_WellKnown wellknowntype() const {
return upb_MessageDef_WellKnownType(ptr_);
}
explicit operator bool() const { return ptr_ != nullptr; }
friend bool operator==(MessageDefPtr lhs, MessageDefPtr rhs) {
return lhs.ptr_ == rhs.ptr_;
}
friend bool operator!=(MessageDefPtr lhs, MessageDefPtr rhs) {
return !(lhs == rhs);
}
private:
class FieldIter {
public:
@ -296,6 +375,10 @@ class EnumValDefPtr {
EnumValDefPtr() : ptr_(nullptr) {}
explicit EnumValDefPtr(const upb_EnumValueDef* ptr) : ptr_(ptr) {}
const UPB_DESC(EnumValueOptions) * options() const {
return upb_EnumValueDef_Options(ptr_);
}
int32_t number() const { return upb_EnumValueDef_Number(ptr_); }
const char* full_name() const { return upb_EnumValueDef_FullName(ptr_); }
const char* name() const { return upb_EnumValueDef_Name(ptr_); }
@ -309,11 +392,27 @@ class EnumDefPtr {
EnumDefPtr() : ptr_(nullptr) {}
explicit EnumDefPtr(const upb_EnumDef* ptr) : ptr_(ptr) {}
const UPB_DESC(EnumOptions) * options() const {
return upb_EnumDef_Options(ptr_);
}
const upb_MiniTableEnum* mini_table() const {
return _upb_EnumDef_MiniTable(ptr_);
}
std::string MiniDescriptorEncode() const {
upb::Arena arena;
upb_StringView md;
upb_EnumDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
return std::string(md.data, md.size);
}
const upb_EnumDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
const char* name() const { return upb_EnumDef_Name(ptr_); }
bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); }
// The value that is used as the default when no field default is specified.
// If not set explicitly, the first value that was added will be used.
@ -325,6 +424,9 @@ class EnumDefPtr {
// multiple names can refer to the same number, so this may be greater than
// the total number of unique numbers.
int value_count() const { return upb_EnumDef_ValueCount(ptr_); }
EnumValDefPtr value(int i) const {
return EnumValDefPtr(upb_EnumDef_Value(ptr_, i));
}
// Lookups from name to integer, returning true if found.
EnumValDefPtr FindValueByName(const char* name) const {
@ -350,8 +452,11 @@ class FileDefPtr {
public:
explicit FileDefPtr(const upb_FileDef* ptr) : ptr_(ptr) {}
const UPB_DESC(FileOptions) * options() const {
return upb_FileDef_Options(ptr_);
}
const upb_FileDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
// Get/set name of the file (eg. "foo/bar.proto").
const char* name() const { return upb_FileDef_Name(ptr_); }
@ -365,10 +470,48 @@ class FileDefPtr {
// Get the list of dependencies from the file. These are returned in the
// order that they were added to the FileDefPtr.
int dependency_count() const { return upb_FileDef_DependencyCount(ptr_); }
const FileDefPtr dependency(int index) const {
FileDefPtr dependency(int index) const {
return FileDefPtr(upb_FileDef_Dependency(ptr_, index));
}
int public_dependency_count() const {
return upb_FileDef_PublicDependencyCount(ptr_);
}
FileDefPtr public_dependency(int index) const {
return FileDefPtr(upb_FileDef_PublicDependency(ptr_, index));
}
int toplevel_enum_count() const {
return upb_FileDef_TopLevelEnumCount(ptr_);
}
EnumDefPtr toplevel_enum(int index) const {
return EnumDefPtr(upb_FileDef_TopLevelEnum(ptr_, index));
}
int toplevel_message_count() const {
return upb_FileDef_TopLevelMessageCount(ptr_);
}
MessageDefPtr toplevel_message(int index) const {
return MessageDefPtr(upb_FileDef_TopLevelMessage(ptr_, index));
}
int toplevel_extension_count() const {
return upb_FileDef_TopLevelExtensionCount(ptr_);
}
FieldDefPtr toplevel_extension(int index) const {
return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index));
}
explicit operator bool() const { return ptr_ != nullptr; }
friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) {
return lhs.ptr_ == rhs.ptr_;
}
friend bool operator!=(FileDefPtr lhs, FileDefPtr rhs) {
return !(lhs == rhs);
}
private:
const upb_FileDef* ptr_;
};
@ -396,10 +539,18 @@ class DefPool {
return FileDefPtr(upb_DefPool_FindFileByName(ptr_.get(), name));
}
FieldDefPtr FindExtensionByName(const char* name) const {
return FieldDefPtr(upb_DefPool_FindExtensionByName(ptr_.get(), name));
}
void _SetPlatform(upb_MiniTablePlatform platform) {
_upb_DefPool_SetPlatform(ptr_.get(), platform);
}
// TODO: iteration?
// Adds the given serialized FileDescriptorProto to the pool.
FileDefPtr AddFile(const google_protobuf_FileDescriptorProto* file_proto,
FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto,
Status* status) {
return FileDefPtr(
upb_DefPool_AddFile(ptr_.get(), file_proto, status->ptr()));
@ -412,11 +563,19 @@ class DefPool {
// TODO(b/236632406): This typedef is deprecated. Delete it.
using SymbolTable = DefPool;
inline FileDefPtr FieldDefPtr::file() const {
return FileDefPtr(upb_FieldDef_File(ptr_));
}
inline FileDefPtr MessageDefPtr::file() const {
return FileDefPtr(upb_MessageDef_File(ptr_));
}
inline MessageDefPtr FieldDefPtr::message_subdef() const {
inline EnumDefPtr MessageDefPtr::enum_type(int i) const {
return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i));
}
inline MessageDefPtr FieldDefPtr::message_type() const {
return MessageDefPtr(upb_FieldDef_MessageSubDef(ptr_));
}
@ -424,6 +583,10 @@ inline MessageDefPtr FieldDefPtr::containing_type() const {
return MessageDefPtr(upb_FieldDef_ContainingType(ptr_));
}
inline MessageDefPtr FieldDefPtr::extension_scope() const {
return MessageDefPtr(upb_FieldDef_ExtensionScope(ptr_));
}
inline MessageDefPtr OneofDefPtr::containing_type() const {
return MessageDefPtr(upb_OneofDef_ContainingType(ptr_));
}
@ -432,10 +595,16 @@ inline OneofDefPtr FieldDefPtr::containing_oneof() const {
return OneofDefPtr(upb_FieldDef_ContainingOneof(ptr_));
}
inline OneofDefPtr FieldDefPtr::real_containing_oneof() const {
return OneofDefPtr(upb_FieldDef_RealContainingOneof(ptr_));
}
inline EnumDefPtr FieldDefPtr::enum_subdef() const {
return EnumDefPtr(upb_FieldDef_EnumSubDef(ptr_));
}
} // namespace upb
#include "upb/port/undef.inc"
#endif // UPB_REFLECTION_DEF_HPP_

@ -61,6 +61,7 @@ struct upb_DefBuilder {
upb_Arena* tmp_arena; // For temporary allocations.
upb_Status* status; // Record errors here.
const upb_MiniTableFile* layout; // NULL if we should build layouts.
upb_MiniTablePlatform platform; // Platform we are targeting.
int enum_count; // Count of enums built so far.
int msg_count; // Count of messages built so far.
int ext_count; // Count of extensions built so far.

@ -46,6 +46,7 @@ struct upb_DefPool {
upb_strtable files; // file_name -> (upb_FileDef*)
upb_inttable exts; // (upb_MiniTableExtension*) -> (upb_FieldDef*)
upb_ExtensionRegistry* extreg;
upb_MiniTablePlatform platform;
void* scratch_data;
size_t scratch_size;
size_t bytes_loaded;
@ -75,6 +76,8 @@ upb_DefPool* upb_DefPool_New(void) {
s->extreg = upb_ExtensionRegistry_New(s->arena);
if (!s->extreg) goto err;
s->platform = kUpb_MiniTablePlatform_Native;
return s;
err:
@ -128,6 +131,11 @@ size_t* _upb_DefPool_ScratchSize(const upb_DefPool* s) {
return (size_t*)&s->scratch_size;
}
void _upb_DefPool_SetPlatform(upb_DefPool* s, upb_MiniTablePlatform platform) {
assert(upb_strtable_count(&s->files) == 0);
s->platform = platform;
}
const upb_MessageDef* upb_DefPool_FindMessageByName(const upb_DefPool* s,
const char* sym) {
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_MSG);
@ -305,6 +313,7 @@ static const upb_FileDef* _upb_DefPool_AddFile(
upb_DefBuilder ctx = {
.symtab = s,
.layout = layout,
.platform = s->platform,
.msg_count = 0,
.enum_count = 0,
.ext_count = 0,

@ -28,6 +28,7 @@
#ifndef UPB_REFLECTION_DEF_POOL_INTERNAL_H_
#define UPB_REFLECTION_DEF_POOL_INTERNAL_H_
#include "upb/mini_table/decode.h"
#include "upb/reflection/def_pool.h"
// Must be last.
@ -50,6 +51,7 @@ bool _upb_DefPool_LookupSym(const upb_DefPool* s, const char* sym, size_t size,
void** _upb_DefPool_ScratchData(const upb_DefPool* s);
size_t* _upb_DefPool_ScratchSize(const upb_DefPool* s);
void _upb_DefPool_SetPlatform(upb_DefPool* s, upb_MiniTablePlatform platform);
// For generated code only: loads a generated descriptor.
typedef struct _upb_DefPool_Init {

@ -355,8 +355,8 @@ static upb_MiniTable* _upb_MessageDef_MakeMiniTable(upb_DefBuilder* ctx,
void** scratch_data = _upb_DefPool_ScratchData(ctx->symtab);
size_t* scratch_size = _upb_DefPool_ScratchSize(ctx->symtab);
upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(
desc.data, desc.size, kUpb_MiniTablePlatform_Native, ctx->arena,
scratch_data, scratch_size, ctx->status);
desc.data, desc.size, ctx->platform, ctx->arena, scratch_data,
scratch_size, ctx->status);
if (!ret) _upb_DefBuilder_FailJmp(ctx);
return ret;

@ -0,0 +1,397 @@
#include <stddef.h>
#include "upb/collections/array_internal.h"
#include "upb/message/internal.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/enum_internal.h"
#include "google/protobuf/descriptor.upb.h"
static upb_Arena* upb_BootstrapArena() {
static upb_Arena* arena = NULL;
if (!arena) arena = upb_Arena_New();
return arena;
}
const upb_MiniTable* google_protobuf_FileDescriptorSet_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$G";
if (mini_table) return mini_table;
mini_table =
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_FileDescriptorProto_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_FileDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$11EGGGG33<<11";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_DescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_EnumDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google_protobuf_ServiceDescriptorProto_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());
return mini_table;
}
const upb_MiniTable* google_protobuf_DescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1GGGGG3GGE";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_FieldDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 6), google_protobuf_FieldDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_DescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_EnumDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 5), google_protobuf_DescriptorProto_ExtensionRange_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 8), google_protobuf_OneofDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 7), google_protobuf_MessageOptions_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 9), google_protobuf_DescriptorProto_ReservedRange_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_DescriptorProto_ExtensionRange_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$((3";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_ExtensionRangeOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_DescriptorProto_ReservedRange_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$((";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
const upb_MiniTable* google_protobuf_ExtensionRangeOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$Pf~G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_FieldDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$11(44113(1f/";
if (mini_table) return mini_table;
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_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 8), google_protobuf_FieldOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_OneofDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$13";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_OneofOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_EnumDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1G3GE";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_EnumValueDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_EnumOptions_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_EnumDescriptorProto_EnumReservedRange_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_EnumDescriptorProto_EnumReservedRange_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$((";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
const upb_MiniTable* google_protobuf_EnumValueDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1(3";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_EnumValueOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_ServiceDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1G3";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_MethodDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_ServiceOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_MethodDescriptorProto_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1113//";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 4), google_protobuf_MethodOptions_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_FileOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P1f14/1d///a/b/c/c/d11a111/a11y|G";
if (mini_table) return mini_table;
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_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_MessageOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P///c/c/{}G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_FieldOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P4//a/4c/d//v}G";
if (mini_table) return mini_table;
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_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_OneofOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$Pf~G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_EnumOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$Pa//b/`~G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_EnumValueOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P/e~G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_ServiceOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P``/e}G";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_MethodOptions_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$P``/4d}G";
if (mini_table) return mini_table;
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_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 999), google_protobuf_UninterpretedOption_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_UninterpretedOption_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$aG1,+ 01";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 2), google_protobuf_UninterpretedOption_NamePart_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_UninterpretedOption_NamePart_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1N/N";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
const upb_MiniTable* google_protobuf_SourceCodeInfo_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$G";
if (mini_table) return mini_table;
mini_table =
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_SourceCodeInfo_Location_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_SourceCodeInfo_Location_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$<M<M11aE";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
const upb_MiniTable* google_protobuf_GeneratedCodeInfo_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$G";
if (mini_table) return mini_table;
mini_table =
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_GeneratedCodeInfo_Annotation_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_GeneratedCodeInfo_Annotation_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$<M1((4";
if (mini_table) return mini_table;
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());
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}

File diff suppressed because it is too large Load Diff

@ -33,6 +33,12 @@ load(
"upb_proto_library",
"upb_proto_reflection_library",
)
load(
"//upbc:bootstrap_compiler.bzl",
"bootstrap_cc_binary",
"bootstrap_cc_library",
"bootstrap_upb_proto_library",
)
licenses(["notice"])
@ -55,10 +61,27 @@ upb_proto_reflection_library(
deps = [":code_generator_request"],
)
upb_proto_library(
bootstrap_upb_proto_library(
name = "plugin_upb_proto",
base_dir = "",
google3_src_files = [
"src/google/protobuf/compiler/plugin.proto",
"net/proto2/compiler/proto/profile.proto",
],
google3_src_rules = [
"//net/proto2/proto:descriptor_proto_source",
"//net/proto2/compiler/proto:plugin.proto",
"//net/proto2/compiler/proto:profile.proto",
],
oss_src_files = ["google/protobuf/compiler/plugin.proto"],
oss_src_rules = [
"@com_google_protobuf//:descriptor_proto_srcs",
"@com_google_protobuf//src/google/protobuf/compiler:plugin_proto_src",
],
oss_strip_prefix = "third_party/protobuf/github/bootstrap/src",
proto_lib_deps = ["@com_google_protobuf//:compiler_plugin_proto"],
visibility = ["//:friends"],
deps = ["@com_google_protobuf//:compiler_plugin_proto"],
deps = ["//:descriptor_upb_proto"],
)
upb_proto_reflection_library(
@ -85,7 +108,7 @@ cc_binary(
],
)
cc_library(
bootstrap_cc_library(
name = "common",
srcs = [
"common.cc",
@ -93,15 +116,17 @@ cc_library(
hdrs = [
"common.h",
],
bootstrap_deps = [
"//:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//protos_generator:__pkg__"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
],
)
cc_library(
bootstrap_cc_library(
name = "file_layout",
srcs = [
"file_layout.cc",
@ -109,16 +134,20 @@ cc_library(
hdrs = [
"file_layout.h",
],
bootstrap_deps = [
":common",
"//:reflection",
"//:descriptor_upb_proto",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//protos_generator:__pkg__"],
deps = [
":common",
"//:mini_table",
"//:mini_table_internal",
"//:port",
"//:upb",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_protobuf//src/google/protobuf/compiler:code_generator",
],
)
@ -134,7 +163,28 @@ cc_library(
visibility = ["//protos_generator:__pkg__"],
)
cc_library(
bootstrap_cc_library(
name = "plugin",
hdrs = [
"plugin.h",
],
bootstrap_deps = [
":plugin_upb_proto",
"//:descriptor_upb_proto",
"//:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//protos_generator:__pkg__"],
deps = [
"//:port",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
],
)
bootstrap_cc_library(
name = "names",
srcs = [
"names.cc",
@ -142,6 +192,9 @@ cc_library(
hdrs = [
"names.h",
],
bootstrap_deps = [
"//:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//protos_generator:__pkg__"],
deps = [
@ -177,22 +230,31 @@ cc_library(
],
)
cc_binary(
bootstrap_cc_binary(
name = "protoc-gen-upb",
srcs = ["protoc-gen-upb.cc"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
bootstrap_deps = [
":common",
":file_layout",
":names",
":plugin",
":plugin_upb_proto",
"//:descriptor_upb_proto",
"//:reflection",
],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
"//:base",
"//:mem",
"//:mini_table_internal",
"//:port",
"//:wire_types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/compiler:code_generator",
],
)
@ -202,11 +264,19 @@ cc_binary(
"protoc-gen-upbdefs.cc",
],
copts = UPB_DEFAULT_CPPOPTS,
# To work around the following link error from ABSL:
# /usr/bin/x86_64-linux-gnu-ld: bazel-out/k8-opt-exec-2B5CBBC6-ST-c1776f9924ec/bin/external/com_google_absl/absl/time/libtime.a(duration.o): undefined reference to symbol 'floor@@GLIBC_2.2.5'
# /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)
linkopts = ["-lm"],
visibility = ["//visibility:public"],
deps = [
":common",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//src/google/protobuf/compiler:code_generator",
":file_layout",
":plugin",
"//:descriptor_upb_proto",
"//:reflection",
"//upb/util:def_to_proto",
],
)

@ -0,0 +1,166 @@
"""Macros that implement bootstrapping for the upb code generator."""
load(
"//bazel:upb_proto_library.bzl",
"upb_proto_library",
)
load(
"//cmake:build_defs.bzl",
"staleness_test",
)
_stages = ["_stage0", "_stage1", ""]
_protoc = "@com_google_protobuf//:protoc"
_upbc_base = "//upbc:protoc-gen-upb"
# begin:google_only
# _is_google3 = True
# _extra_proto_path = ""
# end:google_only
# begin:github_only
_is_google3 = False
_extra_proto_path = "-Iexternal/com_google_protobuf/src "
# end:github_only
def _upbc(stage):
return _upbc_base + _stages[stage]
def bootstrap_cc_library(name, visibility, deps, bootstrap_deps, **kwargs):
for stage in _stages:
stage_visibility = visibility if stage == "" else ["//upbc:__pkg__"]
native.cc_library(
name = name + stage,
deps = deps + [dep + stage for dep in bootstrap_deps],
visibility = stage_visibility,
**kwargs
)
def bootstrap_cc_binary(name, deps, bootstrap_deps, **kwargs):
for stage in _stages:
native.cc_binary(
name = name + stage,
deps = deps + [dep + stage for dep in bootstrap_deps],
**kwargs
)
def _generated_srcs_for_suffix(prefix, srcs, suffix):
return [prefix + "/" + src[:-len(".proto")] + suffix for src in srcs]
def _generated_srcs(prefix, srcs):
return _generated_srcs_for_suffix(prefix, srcs, ".upb.h") + _generated_srcs_for_suffix(prefix, srcs, ".upb.c")
def _stage0_proto_staleness_test(name, base_dir, src_files, src_rules, strip_prefix):
native.genrule(
name = name + "_generate_bootstrap",
srcs = src_rules,
outs = _generated_srcs("bootstrap_generated_sources/" + base_dir + "stage0", src_files),
tools = [_protoc, _upbc(0)],
cmd =
"$(location " + _protoc + ") " +
"-I$(GENDIR)/" + strip_prefix + " " + _extra_proto_path +
"--plugin=protoc-gen-upb=$(location " + _upbc(0) + ") " +
"--upb_out=bootstrap_upb:$(@D)/bootstrap_generated_sources/" + base_dir + "stage0 " +
" ".join(src_files),
)
staleness_test(
name = name + "_staleness_test",
outs = _generated_srcs(base_dir + "stage0", src_files),
generated_pattern = "bootstrap_generated_sources/%s",
target_files = native.glob([base_dir + "stage0/**"]),
# To avoid skew problems for descriptor.proto/pluging.proto between
# GitHub repos. It's not critical that the checked-in protos are up to
# date for every change, they just needs to be complete enough to have
# everything needed by the code generator itself.
tags = ["manual"],
)
def bootstrap_upb_proto_library(
name,
base_dir,
google3_src_files,
google3_src_rules,
oss_src_files,
oss_src_rules,
oss_strip_prefix,
proto_lib_deps,
visibility,
deps = [],
**kwargs):
"""A version of upb_proto_library() that is augmented to allow for bootstrapping the compiler.
Args:
name: Name of this rule. This name will resolve to a upb_proto_library().
base_dir: The directory that all generated files should be placed under.
google3_src_files: Google3 filenames of .proto files that should be built by this rule.
The names should be relative to the depot base.
google3_src_rules: Target names of the Blaze rules that will provide these filenames.
oss_src_files: OSS filenames of .proto files that should be built by this rule.
oss_src_rules: Target names of the Bazel rules that will provide these filenames.
oss_strip_prefix: Prefix that should be stripped from OSS file names.
proto_lib_deps: proto_library() rules that we will use to build the protos when we are
not bootstrapping.
visibility: Visibility list for the final upb_proto_library() rule. Bootstrapping rules
will always be hidden, and will not honor the visibility parameter passed here.
deps: other bootstrap_upb_proto_library() rules that this one depends on.
**kwargs: Other arguments that will be passed through to cc_library(), genrule(), and
upb_proto_library().
"""
_stage0_proto_staleness_test(name, base_dir, oss_src_files, oss_src_rules, oss_strip_prefix)
# stage0 uses checked-in protos.
native.cc_library(
name = name + "_stage0",
srcs = _generated_srcs_for_suffix(base_dir + "stage0", oss_src_files, ".upb.c"),
hdrs = _generated_srcs_for_suffix(base_dir + "stage0", oss_src_files, ".upb.h"),
includes = [base_dir + "stage0"],
visibility = ["//upbc:__pkg__"],
# This macro signals to the runtime that it must use OSS APIs for descriptor.proto/plugin.proto.
defines = ["UPB_BOOTSTRAP_STAGE0"],
deps = [
"//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
"//:mini_table",
] + [dep + "_stage0" for dep in deps],
**kwargs
)
src_files = google3_src_files if _is_google3 else oss_src_files
src_rules = google3_src_rules if _is_google3 else oss_src_rules
# Generate stage1 protos using stage0 compiler.
native.genrule(
name = "gen_" + name + "_stage1",
srcs = src_rules,
outs = _generated_srcs(base_dir + "stage1", src_files),
cmd = "$(location " + _protoc + ") " +
"--plugin=protoc-gen-upb=$(location " + _upbc(0) + ") " + _extra_proto_path +
"--upb_out=$(@D)/" + base_dir + "stage1 " +
" ".join(src_files),
visibility = ["//upbc:__pkg__"],
tools = [
_protoc,
_upbc(0),
],
**kwargs
)
native.cc_library(
name = name + "_stage1",
srcs = _generated_srcs_for_suffix(base_dir + "stage1", src_files, ".upb.c"),
hdrs = _generated_srcs_for_suffix(base_dir + "stage1", src_files, ".upb.h"),
includes = [base_dir + "stage1"],
visibility = ["//upbc:__pkg__"],
deps = [
"//:generated_code_support__only_for_generated_code_do_not_use__i_give_permission_to_break_me",
] + [dep + "_stage1" for dep in deps],
**kwargs
)
# The final protos are generated via normal upb_proto_library().
upb_proto_library(
name = name,
deps = proto_lib_deps,
visibility = visibility,
**kwargs
)

@ -26,13 +26,9 @@
#include "upbc/common.h"
#include "absl/strings/str_replace.h"
#include "upb/reflection/def.hpp"
namespace upbc {
namespace {
namespace protobuf = ::google::protobuf;
} // namespace
std::string StripExtension(absl::string_view fname) {
size_t lastdot = fname.find_last_of('.');
@ -50,7 +46,7 @@ std::string ToPreproc(absl::string_view str) {
return absl::AsciiStrToUpper(ToCIdent(str));
}
void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) {
void EmitFileWarning(absl::string_view name, Output& output) {
output(
"/* This file was generated by upbc (the upb compiler) from the input\n"
" * file:\n"
@ -59,27 +55,27 @@ void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) {
" *\n"
" * Do not edit -- your changes will be discarded when the file is\n"
" * regenerated. */\n\n",
file->name());
name);
}
std::string MessageName(const protobuf::Descriptor* descriptor) {
return ToCIdent(descriptor->full_name());
std::string MessageName(upb::MessageDefPtr descriptor) {
return ToCIdent(descriptor.full_name());
}
std::string FileLayoutName(const google::protobuf::FileDescriptor* file) {
return ToCIdent(file->name()) + "_upb_file_layout";
std::string FileLayoutName(upb::FileDefPtr file) {
return ToCIdent(file.name()) + "_upb_file_layout";
}
std::string HeaderFilename(const google::protobuf::FileDescriptor* file) {
return StripExtension(file->name()) + ".upb.h";
std::string HeaderFilename(upb::FileDefPtr file) {
return StripExtension(file.name()) + ".upb.h";
}
std::string MessageInit(const protobuf::Descriptor* descriptor) {
return MessageName(descriptor) + "_msg_init";
std::string MessageInit(absl::string_view full_name) {
return ToCIdent(full_name) + "_msg_init";
}
std::string EnumInit(const protobuf::EnumDescriptor* descriptor) {
return ToCIdent(descriptor->full_name()) + "_enum_init";
std::string EnumInit(upb::EnumDefPtr descriptor) {
return ToCIdent(descriptor.full_name()) + "_enum_init";
}
} // namespace upbc

@ -32,21 +32,19 @@
#include "absl/strings/str_replace.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "upb/reflection/def.hpp"
namespace upbc {
class Output {
public:
Output(google::protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {}
~Output() { stream_->BackUp((int)size_); }
template <class... Arg>
void operator()(absl::string_view format, const Arg&... arg) {
Write(absl::Substitute(format, arg...));
}
absl::string_view output() const { return output_; }
private:
void Write(absl::string_view data) {
std::string stripped;
@ -69,44 +67,22 @@ class Output {
data = stripped;
}
}
while (!data.empty()) {
RefreshOutput();
size_t to_write = std::min(data.size(), size_);
memcpy(ptr_, data.data(), to_write);
data.remove_prefix(to_write);
ptr_ += to_write;
size_ -= to_write;
}
}
void RefreshOutput() {
while (size_ == 0) {
void* ptr;
int size;
if (!stream_->Next(&ptr, &size)) {
fprintf(stderr, "upbc: Failed to write to to output\n");
abort();
}
ptr_ = static_cast<char*>(ptr);
size_ = size;
}
absl::StrAppend(&output_, data);
}
google::protobuf::io::ZeroCopyOutputStream* stream_;
char* ptr_ = nullptr;
size_t size_ = 0;
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(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 HeaderFilename(const google::protobuf::FileDescriptor* file);
void EmitFileWarning(absl::string_view name, Output& output);
std::string MessageName(upb::MessageDefPtr descriptor);
std::string FileLayoutName(upb::FileDefPtr file);
std::string HeaderFilename(upb::FileDefPtr file);
std::string MessageInit(const google::protobuf::Descriptor* descriptor);
std::string EnumInit(const google::protobuf::EnumDescriptor* descriptor);
std::string MessageInit(absl::string_view full_name);
std::string EnumInit(upb::EnumDefPtr descriptor);
} // namespace upbc

@ -28,48 +28,42 @@
#include <string>
#include <unordered_set>
#include "upb/mini_table/encode_internal.hpp"
#include "upb/mini_table/extension_internal.h"
#include "upbc/common.h"
namespace upbc {
namespace protobuf = ::google::protobuf;
const char* kEnumsInit = "enums_layout";
const char* kExtensionsInit = "extensions_layout";
const char* kMessagesInit = "messages_layout";
void AddEnums(const protobuf::Descriptor* message,
std::vector<const protobuf::EnumDescriptor*>* enums) {
enums->reserve(enums->size() + message->enum_type_count());
for (int i = 0; i < message->enum_type_count(); i++) {
enums->push_back(message->enum_type(i));
void AddEnums(upb::MessageDefPtr message, std::vector<upb::EnumDefPtr>* enums) {
enums->reserve(enums->size() + message.enum_type_count());
for (int i = 0; i < message.enum_type_count(); i++) {
enums->push_back(message.enum_type(i));
}
for (int i = 0; i < message->nested_type_count(); i++) {
AddEnums(message->nested_type(i), enums);
for (int i = 0; i < message.nested_message_count(); i++) {
AddEnums(message.nested_message(i), enums);
}
}
std::vector<const protobuf::EnumDescriptor*> SortedEnums(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::EnumDescriptor*> enums;
enums.reserve(file->enum_type_count());
for (int i = 0; i < file->enum_type_count(); i++) {
enums.push_back(file->enum_type(i));
std::vector<upb::EnumDefPtr> SortedEnums(upb::FileDefPtr file) {
std::vector<upb::EnumDefPtr> enums;
enums.reserve(file.toplevel_enum_count());
for (int i = 0; i < file.toplevel_enum_count(); i++) {
enums.push_back(file.toplevel_enum(i));
}
for (int i = 0; i < file->message_type_count(); i++) {
AddEnums(file->message_type(i), &enums);
for (int i = 0; i < file.toplevel_message_count(); i++) {
AddEnums(file.toplevel_message(i), &enums);
}
return enums;
}
std::vector<uint32_t> SortedUniqueEnumNumbers(
const protobuf::EnumDescriptor* e) {
std::vector<uint32_t> SortedUniqueEnumNumbers(upb::EnumDefPtr e) {
std::vector<uint32_t> values;
values.reserve(e->value_count());
for (int i = 0; i < e->value_count(); i++) {
values.push_back(static_cast<uint32_t>(e->value(i)->number()));
values.reserve(e.value_count());
for (int i = 0; i < e.value_count(); i++) {
values.push_back(static_cast<uint32_t>(e.value(i).number()));
}
std::sort(values.begin(), values.end());
auto last = std::unique(values.begin(), values.end());
@ -77,11 +71,11 @@ std::vector<uint32_t> SortedUniqueEnumNumbers(
return values;
}
void AddMessages(const protobuf::Descriptor* message,
std::vector<const protobuf::Descriptor*>* messages) {
void AddMessages(upb::MessageDefPtr message,
std::vector<upb::MessageDefPtr>* messages) {
messages->push_back(message);
for (int i = 0; i < message->nested_type_count(); i++) {
AddMessages(message->nested_type(i), messages);
for (int i = 0; i < message.nested_message_count(); i++) {
AddMessages(message.nested_message(i), messages);
}
}
@ -90,23 +84,21 @@ void AddMessages(const protobuf::Descriptor* message,
// The ordering is significant because each upb_MessageDef* will point at the
// corresponding upb_MiniTable and we just iterate through the list without
// any search or lookup.
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::Descriptor*> messages;
for (int i = 0; i < file->message_type_count(); i++) {
AddMessages(file->message_type(i), &messages);
std::vector<upb::MessageDefPtr> SortedMessages(upb::FileDefPtr file) {
std::vector<upb::MessageDefPtr> messages;
for (int i = 0; i < file.toplevel_message_count(); i++) {
AddMessages(file.toplevel_message(i), &messages);
}
return messages;
}
void AddExtensionsFromMessage(
const protobuf::Descriptor* message,
std::vector<const protobuf::FieldDescriptor*>* exts) {
for (int i = 0; i < message->extension_count(); i++) {
exts->push_back(message->extension(i));
void AddExtensionsFromMessage(upb::MessageDefPtr message,
std::vector<upb::FieldDefPtr>* exts) {
for (int i = 0; i < message.nested_extension_count(); i++) {
exts->push_back(message.nested_extension(i));
}
for (int i = 0; i < message->nested_type_count(); i++) {
AddExtensionsFromMessage(message->nested_type(i), exts);
for (int i = 0; i < message.nested_message_count(); i++) {
AddExtensionsFromMessage(message.nested_message(i), exts);
}
}
@ -115,304 +107,29 @@ void AddExtensionsFromMessage(
// The ordering is significant because each upb_FieldDef* will point at the
// corresponding upb_MiniTableExtension and we just iterate through the list
// without any search or lookup.
std::vector<const protobuf::FieldDescriptor*> SortedExtensions(
const protobuf::FileDescriptor* file) {
std::vector<const protobuf::FieldDescriptor*> ret;
for (int i = 0; i < file->extension_count(); i++) {
ret.push_back(file->extension(i));
std::vector<upb::FieldDefPtr> SortedExtensions(upb::FileDefPtr file) {
std::vector<upb::FieldDefPtr> ret;
ret.reserve(file.toplevel_extension_count());
for (int i = 0; i < file.toplevel_extension_count(); i++) {
ret.push_back(file.toplevel_extension(i));
}
for (int i = 0; i < file->message_type_count(); i++) {
AddExtensionsFromMessage(file->message_type(i), &ret);
for (int i = 0; i < file.toplevel_message_count(); i++) {
AddExtensionsFromMessage(file.toplevel_message(i), &ret);
}
return ret;
}
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message) {
std::vector<const protobuf::FieldDescriptor*> fields;
for (int i = 0; i < message->field_count(); i++) {
fields.push_back(message->field(i));
std::vector<upb::FieldDefPtr> FieldNumberOrder(upb::MessageDefPtr message) {
std::vector<upb::FieldDefPtr> fields;
fields.reserve(message.field_count());
for (int i = 0; i < message.field_count(); i++) {
fields.push_back(message.field(i));
}
std::sort(fields.begin(), fields.end(),
[](const protobuf::FieldDescriptor* a,
const protobuf::FieldDescriptor* b) {
return a->number() < b->number();
[](upb::FieldDefPtr a, upb::FieldDefPtr b) {
return a.number() < b.number();
});
return fields;
}
upb_MiniTable* FilePlatformLayout::GetMiniTable(
const protobuf::Descriptor* m) const {
auto it = table_map_.find(m);
assert(it != table_map_.end());
return it->second;
}
upb_MiniTableEnum* FilePlatformLayout::GetEnumTable(
const protobuf::EnumDescriptor* d) const {
auto it = enum_map_.find(d);
assert(it != enum_map_.end());
return it->second;
}
const upb_MiniTableExtension* FilePlatformLayout::GetExtension(
const protobuf::FieldDescriptor* fd) const {
auto it = extension_map_.find(fd);
assert(it != extension_map_.end());
return &it->second;
}
void FilePlatformLayout::ResolveIntraFileReferences() {
// This properly resolves references within a file, in order to set any
// necessary flags (eg. is a map).
for (const auto& pair : table_map_) {
upb_MiniTable* mt = pair.second;
// First we properly resolve for defs within the file.
for (const auto* f : FieldNumberOrder(pair.first)) {
if (f->message_type() && f->message_type()->file() == f->file()) {
// const_cast is safe because the mini-table is owned exclusively
// by us, and was allocated from an arena (known-writable memory).
upb_MiniTableField* mt_f = const_cast<upb_MiniTableField*>(
upb_MiniTable_FindFieldByNumber(mt, f->number()));
upb_MiniTable* sub_mt = GetMiniTable(f->message_type());
upb_MiniTable_SetSubMessage(mt, mt_f, sub_mt);
}
// We don't worry about enums here, because resolving an enum will
// never alter the mini-table.
}
}
}
upb_MiniTableSub FilePlatformLayout::PackSub(const char* data, SubTag tag) {
uintptr_t val = reinterpret_cast<uintptr_t>(data);
assert((val & kMask) == 0);
upb_MiniTableSub sub;
sub.submsg = reinterpret_cast<upb_MiniTable*>(val | tag);
return sub;
}
bool FilePlatformLayout::IsNull(upb_MiniTableSub sub) {
return reinterpret_cast<uintptr_t>(sub.subenum) == 0;
}
std::string FilePlatformLayout::GetSub(upb_MiniTableSub sub) {
uintptr_t as_int = reinterpret_cast<uintptr_t>(sub.submsg);
const char* str = reinterpret_cast<const char*>(as_int & ~SubTag::kMask);
switch (as_int & SubTag::kMask) {
case SubTag::kMessage:
return absl::Substitute("{.submsg = &$0}", str);
case SubTag::kEnum:
return absl::Substitute("{.subenum = &$0}", str);
default:
return std::string("{.submsg = NULL}");
}
return std::string("ERROR in GetSub");
}
void FilePlatformLayout::SetSubTableStrings() {
for (const auto& pair : table_map_) {
upb_MiniTable* mt = pair.second;
for (const auto* f : FieldNumberOrder(pair.first)) {
upb_MiniTableField* mt_f = const_cast<upb_MiniTableField*>(
upb_MiniTable_FindFieldByNumber(mt, f->number()));
assert(mt_f);
upb_MiniTableSub sub = PackSubForField(f, mt_f);
if (IsNull(sub)) continue;
// const_cast is safe because the mini-table is owned exclusively
// by us, and was allocated from an arena (known-writable memory).
*const_cast<upb_MiniTableSub*>(&mt->subs[mt_f->submsg_index]) = sub;
}
}
}
upb_MiniTableSub FilePlatformLayout::PackSubForField(
const protobuf::FieldDescriptor* f, const upb_MiniTableField* mt_f) {
if (mt_f->submsg_index == kUpb_NoSub) {
return PackSub(nullptr, SubTag::kNull);
} else if (f->message_type()) {
return PackSub(AllocStr(MessageInit(f->message_type())), SubTag::kMessage);
} else {
ABSL_ASSERT(f->enum_type());
return PackSub(AllocStr(EnumInit(f->enum_type())), SubTag::kEnum);
}
}
const char* FilePlatformLayout::AllocStr(absl::string_view str) {
char* ret =
static_cast<char*>(upb_Arena_Malloc(arena_.ptr(), str.size() + 1));
memcpy(ret, str.data(), str.size());
ret[str.size()] = '\0';
return ret;
}
void FilePlatformLayout::BuildMiniTables(const protobuf::FileDescriptor* fd) {
for (const auto& m : SortedMessages(fd)) {
table_map_[m] = MakeMiniTable(m);
}
for (const auto& e : SortedEnums(fd)) {
enum_map_[e] = MakeMiniTableEnum(e);
}
ResolveIntraFileReferences();
SetSubTableStrings();
}
void FilePlatformLayout::BuildExtensions(const protobuf::FileDescriptor* fd) {
std::vector<const protobuf::FieldDescriptor*> sorted = SortedExtensions(fd);
upb::Status status;
for (const auto* f : sorted) {
upb::MtDataEncoder e;
e.EncodeExtension(static_cast<upb_FieldType>(f->type()), f->number(),
GetFieldModifiers(f));
upb_MiniTableExtension& ext = extension_map_[f];
upb_MiniTableSub sub;
// The extendee may be from another file, so we build a temporary MiniTable
// for it, just for the purpose of building the extension.
// Note, we are not caching so this could use more memory than is necessary.
upb_MiniTable* extendee = MakeMiniTable(f->containing_type());
bool ok =
_upb_MiniTableExtension_Build(e.data().data(), e.data().size(), &ext,
extendee, sub, platform_, status.ptr());
if (!ok) {
// TODO(haberman): Use ABSL CHECK() when it is available.
fprintf(stderr, "Error building mini-table: %s\n",
status.error_message());
}
ABSL_ASSERT(ok);
ext.extendee = reinterpret_cast<const upb_MiniTable*>(
AllocStr(MessageInit(f->containing_type())));
ext.sub = PackSubForField(f, &ext.field);
}
}
upb_MiniTable* FilePlatformLayout::MakeMiniTable(
const protobuf::Descriptor* m) {
if (m->options().message_set_wire_format()) {
return MakeMessageSetMiniTable(m);
} else if (m->options().map_entry()) {
return MakeMapMiniTable(m);
} else {
return MakeRegularMiniTable(m);
}
}
upb_MiniTable* FilePlatformLayout::MakeMapMiniTable(
const protobuf::Descriptor* m) {
const auto key_type = static_cast<upb_FieldType>(m->map_key()->type());
const auto val_type = static_cast<upb_FieldType>(m->map_value()->type());
upb::MtDataEncoder e;
e.EncodeMap(key_type, val_type, GetFieldModifiers(m->map_key()),
GetFieldModifiers(m->map_value()));
const absl::string_view str = e.data();
upb::Status status;
upb_MiniTable* ret = _upb_MiniTable_Build(str.data(), str.size(), platform_,
arena_.ptr(), status.ptr());
if (!ret) {
fprintf(stderr, "Error building mini-table: %s\n", status.error_message());
}
assert(ret);
return ret;
}
upb_MiniTable* FilePlatformLayout::MakeMessageSetMiniTable(
const protobuf::Descriptor* m) {
upb::MtDataEncoder e;
e.EncodeMessageSet();
const absl::string_view str = e.data();
upb::Status status;
upb_MiniTable* ret = _upb_MiniTable_Build(str.data(), str.size(), platform_,
arena_.ptr(), status.ptr());
if (!ret) {
fprintf(stderr, "Error building mini-table: %s\n", status.error_message());
}
assert(ret);
return ret;
}
upb_MiniTable* FilePlatformLayout::MakeRegularMiniTable(
const protobuf::Descriptor* m) {
upb::MtDataEncoder e;
e.StartMessage(GetMessageModifiers(m));
for (const auto* f : FieldNumberOrder(m)) {
e.PutField(static_cast<upb_FieldType>(f->type()), f->number(),
GetFieldModifiers(f));
}
for (int i = 0; i < m->real_oneof_decl_count(); i++) {
const protobuf::OneofDescriptor* oneof = m->oneof_decl(i);
e.StartOneof();
for (int j = 0; j < oneof->field_count(); j++) {
const protobuf::FieldDescriptor* f = oneof->field(j);
e.PutOneofField(f->number());
}
}
absl::string_view str = e.data();
upb::Status status;
upb_MiniTable* ret = _upb_MiniTable_Build(str.data(), str.size(), platform_,
arena_.ptr(), status.ptr());
if (!ret) {
fprintf(stderr, "Error building mini-table: %s\n", status.error_message());
}
assert(ret);
return ret;
}
upb_MiniTableEnum* FilePlatformLayout::MakeMiniTableEnum(
const protobuf::EnumDescriptor* d) {
upb::Arena arena;
upb::MtDataEncoder e;
e.StartEnum();
for (uint32_t i : SortedUniqueEnumNumbers(d)) {
e.PutEnumValue(i);
}
e.EndEnum();
absl::string_view str = e.data();
upb::Status status;
upb_MiniTableEnum* ret = upb_MiniTableEnum_Build(str.data(), str.size(),
arena_.ptr(), status.ptr());
if (!ret) {
fprintf(stderr, "Error building mini-table: %s\n", status.error_message());
}
assert(ret);
return ret;
}
uint64_t FilePlatformLayout::GetMessageModifiers(
const protobuf::Descriptor* m) {
uint64_t ret = 0;
if (m->file()->syntax() == protobuf::FileDescriptor::SYNTAX_PROTO3) {
ret |= kUpb_MessageModifier_ValidateUtf8;
ret |= kUpb_MessageModifier_DefaultIsPacked;
}
if (m->extension_range_count() > 0) {
ret |= kUpb_MessageModifier_IsExtendable;
}
assert(!m->options().map_entry());
return ret;
}
uint64_t FilePlatformLayout::GetFieldModifiers(
const protobuf::FieldDescriptor* f) {
uint64_t ret = 0;
if (f->is_repeated()) ret |= kUpb_FieldModifier_IsRepeated;
if (f->is_required()) ret |= kUpb_FieldModifier_IsRequired;
if (f->is_packed()) ret |= kUpb_FieldModifier_IsPacked;
if (f->enum_type() && f->enum_type()->file()->syntax() ==
protobuf::FileDescriptor::SYNTAX_PROTO2) {
ret |= kUpb_FieldModifier_IsClosedEnum;
}
if (f->is_optional() && !f->has_presence()) {
ret |= kUpb_FieldModifier_IsProto3Singular;
}
return ret;
}
} // namespace upbc

@ -27,185 +27,97 @@
#include <string>
#include "google/protobuf/descriptor.pb.h"
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "net/proto2/proto/descriptor.upb.h"
// #else
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/descriptor.upb.h"
// end:github_only
#include "absl/container/flat_hash_map.h"
#include "absl/strings/substitute.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/encode_internal.hpp"
#include "upb/mini_table/extension_internal.h"
#include "upb/reflection/def.h"
#include "upb/reflection/def.hpp"
#include "upb/upb.hpp"
namespace upbc {
// Must be last
#include "upb/port/def.inc"
namespace protoc = ::google::protobuf::compiler;
namespace protobuf = ::google::protobuf;
namespace upbc {
std::vector<const protobuf::EnumDescriptor*> SortedEnums(
const protobuf::FileDescriptor* file);
std::vector<upb::EnumDefPtr> SortedEnums(upb::FileDefPtr file);
// Ordering must match upb/def.c!
//
// The ordering is significant because each upb_MessageDef* will point at the
// corresponding upb_MiniTable and we just iterate through the list without
// any search or lookup.
std::vector<const protobuf::Descriptor*> SortedMessages(
const protobuf::FileDescriptor* file);
std::vector<upb::MessageDefPtr> SortedMessages(upb::FileDefPtr file);
// Ordering must match upb/def.c!
//
// The ordering is significant because each upb_FieldDef* will point at the
// corresponding upb_MiniTableExtension and we just iterate through the list
// without any search or lookup.
std::vector<const protobuf::FieldDescriptor*> SortedExtensions(
const protobuf::FileDescriptor* file);
std::vector<const protobuf::FieldDescriptor*> FieldNumberOrder(
const protobuf::Descriptor* message);
////////////////////////////////////////////////////////////////////////////////
// FilePlatformLayout
////////////////////////////////////////////////////////////////////////////////
// FilePlatformLayout builds and vends upb MiniTables for a given platform (32
// or 64 bit).
class FilePlatformLayout {
public:
FilePlatformLayout(const protobuf::FileDescriptor* fd,
upb_MiniTablePlatform platform)
: platform_(platform) {
BuildMiniTables(fd);
BuildExtensions(fd);
}
// Retrieves a upb MiniTable or Extension given a protobuf descriptor. The
// descriptor must be from this layout's file.
upb_MiniTable* GetMiniTable(const protobuf::Descriptor* m) const;
upb_MiniTableEnum* GetEnumTable(const protobuf::EnumDescriptor* d) const;
const upb_MiniTableExtension* GetExtension(
const protobuf::FieldDescriptor* fd) const;
// Get the initializer for the given sub-message/sub-enum link.
static std::string GetSub(upb_MiniTableSub sub);
private:
// Functions to build mini-tables for this file's messages and extensions.
void BuildMiniTables(const protobuf::FileDescriptor* fd);
void BuildExtensions(const protobuf::FileDescriptor* fd);
upb_MiniTable* MakeMiniTable(const protobuf::Descriptor* m);
upb_MiniTable* MakeMapMiniTable(const protobuf::Descriptor* m);
upb_MiniTable* MakeMessageSetMiniTable(const protobuf::Descriptor* m);
upb_MiniTable* MakeRegularMiniTable(const protobuf::Descriptor* m);
upb_MiniTableEnum* MakeMiniTableEnum(const protobuf::EnumDescriptor* d);
uint64_t GetMessageModifiers(const protobuf::Descriptor* m);
uint64_t GetFieldModifiers(const protobuf::FieldDescriptor* f);
void ResolveIntraFileReferences();
// When we are generating code, tables are linked to sub-tables via name (ie.
// a string) rather than by pointer. We need to emit an initializer like
// `&foo_sub_table`. To do this, we store `const char*` strings in all the
// links that would normally be pointers:
// field -> sub-message
// field -> enum table (proto2 only)
// extension -> extendee
//
// This requires a bit of reinterpret_cast<>(), but it's confined to a few
// functions. We tag the pointer so we know which member of the union to
// initialize.
enum SubTag {
kNull = 0,
kMessage = 1,
kEnum = 2,
kMask = 3,
};
static upb_MiniTableSub PackSub(const char* data, SubTag tag);
static bool IsNull(upb_MiniTableSub sub);
void SetSubTableStrings();
upb_MiniTableSub PackSubForField(const protobuf::FieldDescriptor* f,
const upb_MiniTableField* mt_f);
const char* AllocStr(absl::string_view str);
std::vector<upb::FieldDefPtr> SortedExtensions(upb::FileDefPtr file);
private:
using TableMap =
absl::flat_hash_map<const protobuf::Descriptor*, upb_MiniTable*>;
using EnumMap =
absl::flat_hash_map<const protobuf::EnumDescriptor*, upb_MiniTableEnum*>;
using ExtensionMap = absl::flat_hash_map<const protobuf::FieldDescriptor*,
upb_MiniTableExtension>;
upb::Arena arena_;
TableMap table_map_;
EnumMap enum_map_;
ExtensionMap extension_map_;
upb_MiniTablePlatform platform_;
};
////////////////////////////////////////////////////////////////////////////////
// FileLayout
////////////////////////////////////////////////////////////////////////////////
std::vector<upb::FieldDefPtr> FieldNumberOrder(upb::MessageDefPtr message);
// FileLayout is a pair of platform layouts: one for 32-bit and one for 64-bit.
class FileLayout {
// DefPoolPair is a pair of DefPools: one for 32-bit and one for 64-bit.
class DefPoolPair {
public:
FileLayout(const protobuf::FileDescriptor* fd)
: descriptor_(fd),
layout32_(fd, kUpb_MiniTablePlatform_32Bit),
layout64_(fd, kUpb_MiniTablePlatform_64Bit) {}
const protobuf::FileDescriptor* descriptor() const { return descriptor_; }
const upb_MiniTable* GetMiniTable32(const protobuf::Descriptor* m) const {
return layout32_.GetMiniTable(m);
}
const upb_MiniTable* GetMiniTable64(const protobuf::Descriptor* m) const {
return layout64_.GetMiniTable(m);
}
const upb_MiniTableField* GetField32(
const protobuf::FieldDescriptor* f) const {
if (f->is_extension()) return &layout32_.GetExtension(f)->field;
return upb_MiniTable_FindFieldByNumber(GetMiniTable32(f->containing_type()),
f->number());
DefPoolPair() {
pool32_._SetPlatform(kUpb_MiniTablePlatform_32Bit);
pool64_._SetPlatform(kUpb_MiniTablePlatform_64Bit);
}
const upb_MiniTableField* GetField64(
const protobuf::FieldDescriptor* f) const {
if (f->is_extension()) return &layout64_.GetExtension(f)->field;
return upb_MiniTable_FindFieldByNumber(GetMiniTable64(f->containing_type()),
f->number());
upb::FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto,
upb::Status* status) {
upb::FileDefPtr file32 = pool32_.AddFile(file_proto, status);
upb::FileDefPtr file64 = pool64_.AddFile(file_proto, status);
if (!file32) return file32;
return file64;
}
const upb_MiniTableEnum* GetEnumTable(
const protobuf::EnumDescriptor* d) const {
return layout64_.GetEnumTable(d);
const upb_MiniTable* GetMiniTable32(upb::MessageDefPtr m) const {
return pool32_.FindMessageByName(m.full_name()).mini_table();
}
std::string GetMessageSize(const protobuf::Descriptor* d) const {
return UpbSize(GetMiniTable32(d)->size, GetMiniTable64(d)->size);
const upb_MiniTable* GetMiniTable64(upb::MessageDefPtr m) const {
return pool64_.FindMessageByName(m.full_name()).mini_table();
}
int GetHasbitIndex(const protobuf::FieldDescriptor* f) const {
const upb_MiniTableField* f_64 = upb_MiniTable_FindFieldByNumber(
GetMiniTable64(f->containing_type()), f->number());
return f_64->presence;
const upb_MiniTableField* GetField32(upb::FieldDefPtr f) const {
return GetFieldFromPool(&pool32_, f);
}
bool HasHasbit(const protobuf::FieldDescriptor* f) const {
return GetHasbitIndex(f) > 0;
const upb_MiniTableField* GetField64(upb::FieldDefPtr f) const {
return GetFieldFromPool(&pool64_, f);
}
template <class T>
static std::string UpbSize(T a, T b) {
if (a == b) return absl::Substitute("$0", a);
return absl::Substitute("UPB_SIZE($0, $1)", a, b);
private:
static const upb_MiniTableField* GetFieldFromPool(const upb::DefPool* pool,
upb::FieldDefPtr f) {
if (f.is_extension()) {
return pool->FindExtensionByName(f.full_name()).mini_table();
} else {
return pool->FindMessageByName(f.containing_type().full_name())
.FindFieldByNumber(f.number())
.mini_table();
}
}
private:
const protobuf::FileDescriptor* descriptor_;
FilePlatformLayout layout32_;
FilePlatformLayout layout64_;
upb::DefPool pool32_;
upb::DefPool pool64_;
};
} // namespace upbc
#include "upb/port/undef.inc"
#endif // UPBC_FILE_LAYOUT_H

@ -31,6 +31,7 @@
#include "absl/strings/match.h"
#include "google/protobuf/descriptor.h"
#include "upb/reflection/def.hpp"
namespace upbc {
@ -82,4 +83,36 @@ NameToFieldDescriptorMap CreateFieldNameMap(
return field_names;
}
NameToFieldDefMap CreateFieldNameMap(upb::MessageDefPtr message) {
NameToFieldDefMap field_names;
field_names.reserve(message.field_count());
for (const auto& field : message.fields()) {
field_names.emplace(field.name(), field);
}
return field_names;
}
std::string ResolveFieldName(upb::FieldDefPtr field,
const NameToFieldDefMap& field_names) {
absl::string_view field_name(field.name());
for (absl::string_view prefix : kAccessorPrefixes) {
// If field name starts with a prefix such as clear_ and the proto
// contains a field name with trailing end, depending on type of field
// (repeated, map, message) we have a conflict to resolve.
if (absl::StartsWith(field_name, prefix)) {
auto match = field_names.find(field_name.substr(prefix.size()));
if (match != field_names.end()) {
const auto candidate = match->second;
if (candidate.IsSequence() || candidate.IsMap() ||
(candidate.ctype() == kUpb_CType_String &&
prefix == kClearAccessor) ||
prefix == kSetAccessor) {
return absl::StrCat(field_name, "_");
}
}
}
}
return std::string(field_name);
}
} // namespace upbc

@ -32,6 +32,7 @@
#include "absl/container/flat_hash_map.h"
#include "google/protobuf/descriptor.h"
#include "upb/reflection/def.hpp"
namespace upbc {
@ -46,6 +47,17 @@ std::string ResolveFieldName(const google::protobuf::FieldDescriptor* field,
// Returns field map by name to use for conflict checks.
NameToFieldDescriptorMap CreateFieldNameMap(const google::protobuf::Descriptor* message);
using NameToFieldDefMap =
absl::flat_hash_map<absl::string_view, upb::FieldDefPtr>;
// Returns field name by resolving naming conflicts across
// proto field names (such as clear_ prefixes).
std::string ResolveFieldName(upb::FieldDefPtr field,
const NameToFieldDefMap& field_names);
// Returns field map by name to use for conflict checks.
NameToFieldDefMap CreateFieldNameMap(upb::MessageDefPtr message);
} // namespace upbc
#endif // UPB_PROTOS_GENERATOR_NAMES_H

@ -0,0 +1,198 @@
// Copyright (c) 2009-2021, Google LLC
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google LLC nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UPB_UPBC_PLUGIN_H_
#define UPB_UPBC_PLUGIN_H_
#include <string>
#include <vector>
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "net/proto2/compiler/proto/plugin.upb.h"
// #include "net/proto2/proto/descriptor.upb.h"
// #else
// #include "google/protobuf/compiler/plugin.upb.h"
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/compiler/plugin.upb.h"
#include "google/protobuf/descriptor.upb.h"
// end:github_only
#include "absl/container/flat_hash_set.h"
#include "absl/log/log.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "upb/reflection/def.hpp"
// Must be last.
#include "upb/port/def.inc"
namespace upbc {
inline std::vector<std::pair<std::string, std::string>> ParseGeneratorParameter(
const absl::string_view text) {
std::vector<std::pair<std::string, std::string>> ret;
for (absl::string_view sp : absl::StrSplit(text, ',', absl::SkipEmpty())) {
std::string::size_type equals_pos = sp.find_first_of('=');
std::pair<std::string, std::string> value;
if (equals_pos == std::string::npos) {
value.first = std::string(sp);
} else {
value.first = std::string(sp.substr(0, equals_pos));
value.second = std::string(sp.substr(equals_pos + 1));
}
ret.push_back(std::move(value));
}
return ret;
}
class Plugin {
public:
Plugin() { ReadRequest(); }
~Plugin() { WriteResponse(); }
absl::string_view parameter() const {
return ToStringView(
UPB_DESC(compiler_CodeGeneratorRequest_parameter)(request_));
}
template <class T>
void GenerateFilesRaw(T&& func) {
absl::flat_hash_set<absl::string_view> files_to_generate;
size_t size;
const upb_StringView* file_to_generate = UPB_DESC(
compiler_CodeGeneratorRequest_file_to_generate)(request_, &size);
for (size_t i = 0; i < size; i++) {
files_to_generate.insert(
{file_to_generate[i].data, file_to_generate[i].size});
}
const UPB_DESC(FileDescriptorProto)* const* files =
UPB_DESC(compiler_CodeGeneratorRequest_proto_file)(request_, &size);
for (size_t i = 0; i < size; i++) {
upb::Status status;
absl::string_view name =
ToStringView(UPB_DESC(FileDescriptorProto_name)(files[i]));
func(files[i], files_to_generate.contains(name));
}
}
template <class T>
void GenerateFiles(T&& func) {
GenerateFilesRaw(
[this, &func](const UPB_DESC(FileDescriptorProto) * file_proto,
bool generate) {
upb::Status status;
upb::FileDefPtr file = pool_.AddFile(file_proto, &status);
if (!file) {
absl::string_view name =
ToStringView(UPB_DESC(FileDescriptorProto_name)(file_proto));
LOG(FATAL) << "Couldn't add file " << name
<< " to DefPool: " << status.error_message();
}
if (generate) func(file);
});
}
void SetError(absl::string_view error) {
char* data =
static_cast<char*>(upb_Arena_Malloc(arena_.ptr(), error.size()));
memcpy(data, error.data(), error.size());
UPB_DESC(compiler_CodeGeneratorResponse_set_error)
(response_, upb_StringView_FromDataAndSize(data, error.size()));
}
void AddOutputFile(absl::string_view filename, absl::string_view content) {
UPB_DESC(compiler_CodeGeneratorResponse_File)* file = UPB_DESC(
compiler_CodeGeneratorResponse_add_file)(response_, arena_.ptr());
UPB_DESC(compiler_CodeGeneratorResponse_File_set_name)
(file, StringDup(filename));
UPB_DESC(compiler_CodeGeneratorResponse_File_set_content)
(file, StringDup(content));
}
private:
upb::Arena arena_;
upb::DefPool pool_;
UPB_DESC(compiler_CodeGeneratorRequest) * request_;
UPB_DESC(compiler_CodeGeneratorResponse) * response_;
static absl::string_view ToStringView(upb_StringView sv) {
return absl::string_view(sv.data, sv.size);
}
upb_StringView StringDup(absl::string_view s) {
char* data =
reinterpret_cast<char*>(upb_Arena_Malloc(arena_.ptr(), s.size()));
memcpy(data, s.data(), s.size());
return upb_StringView_FromDataAndSize(data, s.size());
}
std::string ReadAllStdinBinary() {
std::string data;
stdin = freopen(nullptr, "rb", stdin);
char buf[4096];
while (size_t len = fread(buf, 1, sizeof(buf), stdin)) {
data.append(buf, len);
}
return data;
}
void ReadRequest() {
std::string data = ReadAllStdinBinary();
request_ = UPB_DESC(compiler_CodeGeneratorRequest_parse)(
data.data(), data.size(), arena_.ptr());
if (!request_) {
LOG(FATAL) << "Failed to parse CodeGeneratorRequest";
}
response_ = UPB_DESC(compiler_CodeGeneratorResponse_new)(arena_.ptr());
UPB_DESC(compiler_CodeGeneratorResponse_set_supported_features)
(response_,
UPB_DESC(compiler_CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL));
}
void WriteResponse() {
size_t size;
char* serialized = UPB_DESC(compiler_CodeGeneratorResponse_serialize)(
response_, arena_.ptr(), &size);
if (!serialized) {
LOG(FATAL) << "Failed to serialize CodeGeneratorResponse";
}
if (fwrite(serialized, 1, size, stdout) != size) {
LOG(FATAL) << "Failed to write response to stdout";
}
}
};
} // namespace upbc
#include "upb/port/undef.inc"
#endif // UPB_UPBC_PLUGIN_H_

File diff suppressed because it is too large Load Diff

@ -25,46 +25,39 @@
#include <memory>
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/plugin.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.upb.h"
#include "upb/reflection/def.hpp"
#include "upb/util/def_to_proto.h"
#include "upbc/common.h"
#include "upbc/file_layout.h"
#include "upbc/plugin.h"
namespace upbc {
namespace {
namespace protoc = ::google::protobuf::compiler;
namespace protobuf = ::google::protobuf;
std::string DefInitSymbol(const protobuf::FileDescriptor* file) {
return ToCIdent(file->name()) + "_upbdefinit";
std::string DefInitSymbol(upb::FileDefPtr file) {
return ToCIdent(file.name()) + "_upbdefinit";
}
static std::string DefHeaderFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.h";
static std::string DefHeaderFilename(upb::FileDefPtr file) {
return StripExtension(file.name()) + ".upbdefs.h";
}
static std::string DefSourceFilename(std::string proto_filename) {
return StripExtension(proto_filename) + ".upbdefs.c";
static std::string DefSourceFilename(upb::FileDefPtr file) {
return StripExtension(file.name()) + ".upbdefs.c";
}
void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) {
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_DefPool_LoadDefInit(s, &$0);\n", DefInitSymbol(d->file()));
output(" return upb_DefPool_FindMessageByName(s, \"$0\");\n",
d->full_name());
ToCIdent(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");
output("\n");
for (int i = 0; i < d->nested_type_count(); i++) {
GenerateMessageDefAccessor(d->nested_type(i), output);
}
}
void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
void WriteDefHeader(upb::FileDefPtr file, Output& output) {
EmitFileWarning(file.name(), output);
output(
"#ifndef $0_UPBDEFS_H_\n"
@ -75,7 +68,7 @@ void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n\n",
ToPreproc(file->name()));
ToPreproc(file.name()));
output("#include \"upb/reflection/def.h\"\n");
output("\n");
@ -85,8 +78,8 @@ void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
output("extern _upb_DefPool_Init $0;\n", DefInitSymbol(file));
output("\n");
for (int i = 0; i < file->message_type_count(); i++) {
GenerateMessageDefAccessor(file->message_type(i), output);
for (auto msg : SortedMessages(file)) {
GenerateMessageDefAccessor(msg, output);
}
output(
@ -97,35 +90,37 @@ void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) {
"#include \"upb/port/undef.inc\"\n"
"\n"
"#endif /* $0_UPBDEFS_H_ */\n",
ToPreproc(file->name()));
ToPreproc(file.name()));
}
void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
EmitFileWarning(file, output);
void WriteDefSource(upb::FileDefPtr file, Output& output) {
EmitFileWarning(file.name(), output);
output("#include \"upb/reflection/def.h\"\n");
output("#include \"$0\"\n", DefHeaderFilename(file->name()));
output("#include \"$0\"\n", DefHeaderFilename(file));
output("#include \"$0\"\n", HeaderFilename(file));
output("\n");
for (int i = 0; i < file->dependency_count(); i++) {
output("extern _upb_DefPool_Init $0;\n",
DefInitSymbol(file->dependency(i)));
for (int i = 0; i < file.dependency_count(); i++) {
output("extern _upb_DefPool_Init $0;\n", DefInitSymbol(file.dependency(i)));
}
protobuf::FileDescriptorProto file_proto;
file->CopyTo(&file_proto);
std::string file_data;
file_proto.SerializeToString(&file_data);
upb::Arena arena;
google_protobuf_FileDescriptorProto* file_proto =
upb_FileDef_ToProto(file.ptr(), arena.ptr());
size_t serialized_size;
const char* serialized = google_protobuf_FileDescriptorProto_serialize(
file_proto, arena.ptr(), &serialized_size);
absl::string_view file_data(serialized, serialized_size);
output("static const char descriptor[$0] = {", file_data.size());
output("static const char descriptor[$0] = {", serialized_size);
// C90 only guarantees that strings can be up to 509 characters, and some
// implementations have limits here (for example, MSVC only allows 64k:
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1091.
// So we always emit an array instead of a string.
for (size_t i = 0; i < file_data.size();) {
for (size_t j = 0; j < 25 && i < file_data.size(); ++i, ++j) {
for (size_t i = 0; i < serialized_size;) {
for (size_t j = 0; j < 25 && i < serialized_size; ++i, ++j) {
output("'$0', ", absl::CEscape(file_data.substr(i, 1)));
}
output("\n");
@ -133,9 +128,9 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
output("};\n\n");
output("static _upb_DefPool_Init *deps[$0] = {\n",
file->dependency_count() + 1);
for (int i = 0; i < file->dependency_count(); i++) {
output(" &$0,\n", DefInitSymbol(file->dependency(i)));
file.dependency_count() + 1);
for (int i = 0; i < file.dependency_count(); i++) {
output(" &$0,\n", DefInitSymbol(file.dependency(i)));
}
output(" NULL\n");
output("};\n");
@ -144,51 +139,32 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
output("_upb_DefPool_Init $0 = {\n", DefInitSymbol(file));
output(" deps,\n");
output(" &$0,\n", FileLayoutName(file));
output(" \"$0\",\n", file->name());
output(" \"$0\",\n", file.name());
output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size());
output("};\n");
}
class Generator : public protoc::CodeGenerator {
~Generator() override {}
bool Generate(const protobuf::FileDescriptor* file,
const std::string& parameter, protoc::GeneratorContext* context,
std::string* error) const override;
uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
}
};
bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& parameter,
protoc::GeneratorContext* context,
std::string* error) const {
std::vector<std::pair<std::string, std::string>> params;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
for (const auto& pair : params) {
*error = "Unknown parameter: " + pair.first;
return false;
}
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> h_output_stream(
context->Open(DefHeaderFilename(file->name())));
Output h_def_output(h_output_stream.get());
void GenerateFile(upb::FileDefPtr file, Plugin* plugin) {
Output h_def_output;
WriteDefHeader(file, h_def_output);
plugin->AddOutputFile(DefHeaderFilename(file), h_def_output.output());
std::unique_ptr<protobuf::io::ZeroCopyOutputStream> c_output_stream(
context->Open(DefSourceFilename(file->name())));
Output c_def_output(c_output_stream.get());
Output c_def_output;
WriteDefSource(file, c_def_output);
return true;
plugin->AddOutputFile(DefSourceFilename(file), c_def_output.output());
}
} // namespace
} // namespace upbc
int main(int argc, char** argv) {
std::unique_ptr<google::protobuf::compiler::CodeGenerator> generator(
new upbc::Generator());
return google::protobuf::compiler::PluginMain(argc, argv, generator.get());
upbc::Plugin plugin;
if (!plugin.parameter().empty()) {
plugin.SetError(
absl::StrCat("Expected no parameters, got: ", plugin.parameter()));
return 0;
}
plugin.GenerateFiles(
[&](upb::FileDefPtr file) { upbc::GenerateFile(file, &plugin); });
return 0;
}

@ -0,0 +1,68 @@
#include <stddef.h>
#include "upb/collections/array_internal.h"
#include "upb/message/internal.h"
#include "upb/mini_table/decode.h"
#include "upb/mini_table/enum_internal.h"
#include "google/protobuf/compiler/plugin.upb.h"
#include "google/protobuf/descriptor.upb.h"
static upb_Arena* upb_BootstrapArena() {
static upb_Arena* arena = NULL;
if (!arena) arena = upb_Arena_New();
return arena;
}
const upb_MiniTable* google_protobuf_compiler_Version_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$(((1";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}
const upb_MiniTable* google_protobuf_compiler_CodeGeneratorRequest_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$E13kG";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 15), google_protobuf_FileDescriptorProto_msg_init());
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 3), google_protobuf_compiler_Version_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_compiler_CodeGeneratorResponse_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$1,lG";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 15), google_protobuf_compiler_CodeGeneratorResponse_File_msg_init());
return mini_table;
}
const upb_MiniTable* google_protobuf_compiler_CodeGeneratorResponse_File_msg_init() {
static upb_MiniTable* mini_table = NULL;
static const char* mini_descriptor = "$11l13";
if (mini_table) return mini_table;
mini_table =
upb_MiniTable_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
upb_MiniTable_SetSubMessage(mini_table, (upb_MiniTableField*)upb_MiniTable_FindFieldByNumber(mini_table, 16), google_protobuf_GeneratedCodeInfo_msg_init());
return mini_table;
}
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;
mini_table =
upb_MiniTableEnum_Build(mini_descriptor, strlen(mini_descriptor),
upb_BootstrapArena(), NULL);
return mini_table;
}

@ -0,0 +1,585 @@
/* This file was generated by upbc (the upb compiler) from the input
* file:
*
* google/protobuf/compiler/plugin.proto
*
* Do not edit -- your changes will be discarded when the file is
* regenerated. */
#ifndef GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_
#define GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_
#include "upb/collections/array_internal.h"
#include "upb/collections/map_gencode_util.h"
#include "upb/message/accessors.h"
#include "upb/message/internal.h"
#include "upb/mini_table/enum_internal.h"
#include "upb/wire/decode.h"
#include "upb/wire/decode_fast.h"
#include "upb/wire/encode.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct google_protobuf_compiler_Version google_protobuf_compiler_Version;
typedef struct google_protobuf_compiler_CodeGeneratorRequest google_protobuf_compiler_CodeGeneratorRequest;
typedef struct google_protobuf_compiler_CodeGeneratorResponse google_protobuf_compiler_CodeGeneratorResponse;
typedef struct google_protobuf_compiler_CodeGeneratorResponse_File google_protobuf_compiler_CodeGeneratorResponse_File;
extern const upb_MiniTable* google_protobuf_compiler_Version_msg_init();
extern const upb_MiniTable* google_protobuf_compiler_CodeGeneratorRequest_msg_init();
extern const upb_MiniTable* google_protobuf_compiler_CodeGeneratorResponse_msg_init();
extern const upb_MiniTable* google_protobuf_compiler_CodeGeneratorResponse_File_msg_init();
struct google_protobuf_FileDescriptorProto;
struct google_protobuf_GeneratedCodeInfo;
extern const upb_MiniTable* google_protobuf_FileDescriptorProto_msg_init();
extern const upb_MiniTable* google_protobuf_GeneratedCodeInfo_msg_init();
typedef enum {
google_protobuf_compiler_CodeGeneratorResponse_FEATURE_NONE = 0,
google_protobuf_compiler_CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL = 1
} google_protobuf_compiler_CodeGeneratorResponse_Feature;
extern const upb_MiniTableEnum* google_protobuf_compiler_CodeGeneratorResponse_Feature_enum_init();
/* google.protobuf.compiler.Version */
UPB_INLINE google_protobuf_compiler_Version* google_protobuf_compiler_Version_new(upb_Arena* arena) {
return (google_protobuf_compiler_Version*)_upb_Message_New(google_protobuf_compiler_Version_msg_init(), arena);
}
UPB_INLINE google_protobuf_compiler_Version* google_protobuf_compiler_Version_parse(const char* buf, size_t size, upb_Arena* arena) {
google_protobuf_compiler_Version* ret = google_protobuf_compiler_Version_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_Version_msg_init(), NULL, 0, arena) != kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE google_protobuf_compiler_Version* google_protobuf_compiler_Version_parse_ex(const char* buf, size_t size,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
google_protobuf_compiler_Version* ret = google_protobuf_compiler_Version_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_Version_msg_init(), extreg, options, arena) !=
kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE char* google_protobuf_compiler_Version_serialize(const google_protobuf_compiler_Version* msg, upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_Version_msg_init(), 0, arena, &ptr, len);
return ptr;
}
UPB_INLINE char* google_protobuf_compiler_Version_serialize_ex(const google_protobuf_compiler_Version* msg, int options,
upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_Version_msg_init(), options, arena, &ptr, len);
return ptr;
}
UPB_INLINE void google_protobuf_compiler_Version_clear_major(google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 1);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE int32_t google_protobuf_compiler_Version_major(const google_protobuf_compiler_Version* msg) {
int32_t default_val = (int32_t)0;
int32_t ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 1);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_Version_has_major(const google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 1);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_Version_clear_minor(google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 2);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE int32_t google_protobuf_compiler_Version_minor(const google_protobuf_compiler_Version* msg) {
int32_t default_val = (int32_t)0;
int32_t ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 2);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_Version_has_minor(const google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 2);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_Version_clear_patch(google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 3);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE int32_t google_protobuf_compiler_Version_patch(const google_protobuf_compiler_Version* msg) {
int32_t default_val = (int32_t)0;
int32_t ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 3);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_Version_has_patch(const google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 3);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_Version_clear_suffix(google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 4);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_Version_suffix(const google_protobuf_compiler_Version* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 4);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_Version_has_suffix(const google_protobuf_compiler_Version* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 4);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_Version_set_major(google_protobuf_compiler_Version *msg, int32_t value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 1);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_Version_set_minor(google_protobuf_compiler_Version *msg, int32_t value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 2);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_Version_set_patch(google_protobuf_compiler_Version *msg, int32_t value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 3);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_Version_set_suffix(google_protobuf_compiler_Version *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_Version_msg_init(), 4);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
/* google.protobuf.compiler.CodeGeneratorRequest */
UPB_INLINE google_protobuf_compiler_CodeGeneratorRequest* google_protobuf_compiler_CodeGeneratorRequest_new(upb_Arena* arena) {
return (google_protobuf_compiler_CodeGeneratorRequest*)_upb_Message_New(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), arena);
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorRequest* google_protobuf_compiler_CodeGeneratorRequest_parse(const char* buf, size_t size, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorRequest* ret = google_protobuf_compiler_CodeGeneratorRequest_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorRequest_msg_init(), NULL, 0, arena) != kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorRequest* google_protobuf_compiler_CodeGeneratorRequest_parse_ex(const char* buf, size_t size,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorRequest* ret = google_protobuf_compiler_CodeGeneratorRequest_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorRequest_msg_init(), extreg, options, arena) !=
kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorRequest_serialize(const google_protobuf_compiler_CodeGeneratorRequest* msg, upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 0, arena, &ptr, len);
return ptr;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorRequest_serialize_ex(const google_protobuf_compiler_CodeGeneratorRequest* msg, int options,
upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorRequest_msg_init(), options, arena, &ptr, len);
return ptr;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_clear_file_to_generate(google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 1);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView const* google_protobuf_compiler_CodeGeneratorRequest_file_to_generate(const google_protobuf_compiler_CodeGeneratorRequest* msg, size_t* size) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 1);
const upb_Array* arr = upb_Message_GetArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (upb_StringView const*)_upb_array_constptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorRequest_has_file_to_generate(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
size_t size;
google_protobuf_compiler_CodeGeneratorRequest_file_to_generate(msg, &size);
return size != 0;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_clear_parameter(google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 2);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_CodeGeneratorRequest_parameter(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 2);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorRequest_has_parameter(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 2);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_clear_compiler_version(google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 3);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE const google_protobuf_compiler_Version* google_protobuf_compiler_CodeGeneratorRequest_compiler_version(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
const google_protobuf_compiler_Version* default_val = NULL;
const google_protobuf_compiler_Version* ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 3);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorRequest_has_compiler_version(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 3);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_clear_proto_file(google_protobuf_compiler_CodeGeneratorRequest* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 15);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE const struct google_protobuf_FileDescriptorProto* const* google_protobuf_compiler_CodeGeneratorRequest_proto_file(const google_protobuf_compiler_CodeGeneratorRequest* msg, size_t* size) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 15);
const upb_Array* arr = upb_Message_GetArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (const struct google_protobuf_FileDescriptorProto* const*)_upb_array_constptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorRequest_has_proto_file(const google_protobuf_compiler_CodeGeneratorRequest* msg) {
size_t size;
google_protobuf_compiler_CodeGeneratorRequest_proto_file(msg, &size);
return size != 0;
}
UPB_INLINE upb_StringView* google_protobuf_compiler_CodeGeneratorRequest_mutable_file_to_generate(google_protobuf_compiler_CodeGeneratorRequest* msg, size_t* size) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 1);
upb_Array* arr = upb_Message_GetMutableArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (upb_StringView*)_upb_array_ptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE upb_StringView* google_protobuf_compiler_CodeGeneratorRequest_resize_file_to_generate(google_protobuf_compiler_CodeGeneratorRequest* msg, size_t size, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 1);
return (upb_StringView*)upb_Message_ResizeArray(msg, &field, size, arena);
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorRequest_add_file_to_generate(google_protobuf_compiler_CodeGeneratorRequest* msg, upb_StringView val, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 1);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, &field, arena);
if (!arr || !_upb_Array_ResizeUninitialized(arr, arr->size + 1, arena)) {
return false;
}
_upb_Array_Set(arr, arr->size - 1, &val, sizeof(val));
return true;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_set_parameter(google_protobuf_compiler_CodeGeneratorRequest *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 2);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorRequest_set_compiler_version(google_protobuf_compiler_CodeGeneratorRequest *msg, google_protobuf_compiler_Version* value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 3);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE struct google_protobuf_compiler_Version* google_protobuf_compiler_CodeGeneratorRequest_mutable_compiler_version(google_protobuf_compiler_CodeGeneratorRequest* msg, upb_Arena* arena) {
struct google_protobuf_compiler_Version* sub = (struct google_protobuf_compiler_Version*)google_protobuf_compiler_CodeGeneratorRequest_compiler_version(msg);
if (sub == NULL) {
sub = (struct google_protobuf_compiler_Version*)_upb_Message_New(google_protobuf_compiler_Version_msg_init(), arena);
if (sub) google_protobuf_compiler_CodeGeneratorRequest_set_compiler_version(msg, sub);
}
return sub;
}
UPB_INLINE struct google_protobuf_FileDescriptorProto** google_protobuf_compiler_CodeGeneratorRequest_mutable_proto_file(google_protobuf_compiler_CodeGeneratorRequest* msg, size_t* size) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 15);
upb_Array* arr = upb_Message_GetMutableArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (struct google_protobuf_FileDescriptorProto**)_upb_array_ptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE struct google_protobuf_FileDescriptorProto** google_protobuf_compiler_CodeGeneratorRequest_resize_proto_file(google_protobuf_compiler_CodeGeneratorRequest* msg, size_t size, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 15);
return (struct google_protobuf_FileDescriptorProto**)upb_Message_ResizeArray(msg, &field, size, arena);
}
UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_compiler_CodeGeneratorRequest_add_proto_file(google_protobuf_compiler_CodeGeneratorRequest* msg, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorRequest_msg_init(), 15);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, &field, arena);
if (!arr || !_upb_Array_ResizeUninitialized(arr, arr->size + 1, arena)) {
return NULL;
}
struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)_upb_Message_New(google_protobuf_FileDescriptorProto_msg_init(), arena);
if (!arr || !sub) return NULL;
_upb_Array_Set(arr, arr->size - 1, &sub, sizeof(sub));
return sub;
}
/* google.protobuf.compiler.CodeGeneratorResponse */
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse* google_protobuf_compiler_CodeGeneratorResponse_new(upb_Arena* arena) {
return (google_protobuf_compiler_CodeGeneratorResponse*)_upb_Message_New(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), arena);
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse* google_protobuf_compiler_CodeGeneratorResponse_parse(const char* buf, size_t size, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorResponse* ret = google_protobuf_compiler_CodeGeneratorResponse_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorResponse_msg_init(), NULL, 0, arena) != kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse* google_protobuf_compiler_CodeGeneratorResponse_parse_ex(const char* buf, size_t size,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorResponse* ret = google_protobuf_compiler_CodeGeneratorResponse_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorResponse_msg_init(), extreg, options, arena) !=
kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorResponse_serialize(const google_protobuf_compiler_CodeGeneratorResponse* msg, upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 0, arena, &ptr, len);
return ptr;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorResponse_serialize_ex(const google_protobuf_compiler_CodeGeneratorResponse* msg, int options,
upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorResponse_msg_init(), options, arena, &ptr, len);
return ptr;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_clear_error(google_protobuf_compiler_CodeGeneratorResponse* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 1);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_CodeGeneratorResponse_error(const google_protobuf_compiler_CodeGeneratorResponse* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 1);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_has_error(const google_protobuf_compiler_CodeGeneratorResponse* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 1);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_clear_supported_features(google_protobuf_compiler_CodeGeneratorResponse* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 2);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE uint64_t google_protobuf_compiler_CodeGeneratorResponse_supported_features(const google_protobuf_compiler_CodeGeneratorResponse* msg) {
uint64_t default_val = (uint64_t)0ull;
uint64_t ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 2);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_has_supported_features(const google_protobuf_compiler_CodeGeneratorResponse* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 2);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_clear_file(google_protobuf_compiler_CodeGeneratorResponse* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 15);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE const google_protobuf_compiler_CodeGeneratorResponse_File* const* google_protobuf_compiler_CodeGeneratorResponse_file(const google_protobuf_compiler_CodeGeneratorResponse* msg, size_t* size) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 15);
const upb_Array* arr = upb_Message_GetArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (const google_protobuf_compiler_CodeGeneratorResponse_File* const*)_upb_array_constptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_has_file(const google_protobuf_compiler_CodeGeneratorResponse* msg) {
size_t size;
google_protobuf_compiler_CodeGeneratorResponse_file(msg, &size);
return size != 0;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_set_error(google_protobuf_compiler_CodeGeneratorResponse *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 1);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_set_supported_features(google_protobuf_compiler_CodeGeneratorResponse *msg, uint64_t value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 2);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse_File** google_protobuf_compiler_CodeGeneratorResponse_mutable_file(google_protobuf_compiler_CodeGeneratorResponse* msg, size_t* size) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 15);
upb_Array* arr = upb_Message_GetMutableArray(msg, &field);
if (arr) {
if (size) *size = arr->size;
return (google_protobuf_compiler_CodeGeneratorResponse_File**)_upb_array_ptr(arr);
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse_File** google_protobuf_compiler_CodeGeneratorResponse_resize_file(google_protobuf_compiler_CodeGeneratorResponse* msg, size_t size, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 15);
return (google_protobuf_compiler_CodeGeneratorResponse_File**)upb_Message_ResizeArray(msg, &field, size, arena);
}
UPB_INLINE struct google_protobuf_compiler_CodeGeneratorResponse_File* google_protobuf_compiler_CodeGeneratorResponse_add_file(google_protobuf_compiler_CodeGeneratorResponse* msg, upb_Arena* arena) {
upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_msg_init(), 15);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, &field, arena);
if (!arr || !_upb_Array_ResizeUninitialized(arr, arr->size + 1, arena)) {
return NULL;
}
struct google_protobuf_compiler_CodeGeneratorResponse_File* sub = (struct google_protobuf_compiler_CodeGeneratorResponse_File*)_upb_Message_New(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), arena);
if (!arr || !sub) return NULL;
_upb_Array_Set(arr, arr->size - 1, &sub, sizeof(sub));
return sub;
}
/* google.protobuf.compiler.CodeGeneratorResponse.File */
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse_File* google_protobuf_compiler_CodeGeneratorResponse_File_new(upb_Arena* arena) {
return (google_protobuf_compiler_CodeGeneratorResponse_File*)_upb_Message_New(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), arena);
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse_File* google_protobuf_compiler_CodeGeneratorResponse_File_parse(const char* buf, size_t size, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorResponse_File* ret = google_protobuf_compiler_CodeGeneratorResponse_File_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), NULL, 0, arena) != kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE google_protobuf_compiler_CodeGeneratorResponse_File* google_protobuf_compiler_CodeGeneratorResponse_File_parse_ex(const char* buf, size_t size,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
google_protobuf_compiler_CodeGeneratorResponse_File* ret = google_protobuf_compiler_CodeGeneratorResponse_File_new(arena);
if (!ret) return NULL;
if (upb_Decode(buf, size, ret, google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), extreg, options, arena) !=
kUpb_DecodeStatus_Ok) {
return NULL;
}
return ret;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorResponse_File_serialize(const google_protobuf_compiler_CodeGeneratorResponse_File* msg, upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 0, arena, &ptr, len);
return ptr;
}
UPB_INLINE char* google_protobuf_compiler_CodeGeneratorResponse_File_serialize_ex(const google_protobuf_compiler_CodeGeneratorResponse_File* msg, int options,
upb_Arena* arena, size_t* len) {
char* ptr;
(void)upb_Encode(msg, google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), options, arena, &ptr, len);
return ptr;
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_clear_name(google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 1);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_CodeGeneratorResponse_File_name(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 1);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_File_has_name(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 1);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_clear_insertion_point(google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 2);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_CodeGeneratorResponse_File_insertion_point(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 2);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_File_has_insertion_point(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 2);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_clear_content(google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 15);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE upb_StringView google_protobuf_compiler_CodeGeneratorResponse_File_content(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
upb_StringView default_val = upb_StringView_FromString("");
upb_StringView ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 15);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_File_has_content(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 15);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_clear_generated_code_info(google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 16);
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE const struct google_protobuf_GeneratedCodeInfo* google_protobuf_compiler_CodeGeneratorResponse_File_generated_code_info(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const struct google_protobuf_GeneratedCodeInfo* default_val = NULL;
const struct google_protobuf_GeneratedCodeInfo* ret;
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 16);
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_compiler_CodeGeneratorResponse_File_has_generated_code_info(const google_protobuf_compiler_CodeGeneratorResponse_File* msg) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 16);
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_set_name(google_protobuf_compiler_CodeGeneratorResponse_File *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 1);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_set_insertion_point(google_protobuf_compiler_CodeGeneratorResponse_File *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 2);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_set_content(google_protobuf_compiler_CodeGeneratorResponse_File *msg, upb_StringView value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 15);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_compiler_CodeGeneratorResponse_File_set_generated_code_info(google_protobuf_compiler_CodeGeneratorResponse_File *msg, struct google_protobuf_GeneratedCodeInfo* value) {
const upb_MiniTableField field = *upb_MiniTable_FindFieldByNumber(google_protobuf_compiler_CodeGeneratorResponse_File_msg_init(), 16);
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE struct google_protobuf_GeneratedCodeInfo* google_protobuf_compiler_CodeGeneratorResponse_File_mutable_generated_code_info(google_protobuf_compiler_CodeGeneratorResponse_File* msg, upb_Arena* arena) {
struct google_protobuf_GeneratedCodeInfo* sub = (struct google_protobuf_GeneratedCodeInfo*)google_protobuf_compiler_CodeGeneratorResponse_File_generated_code_info(msg);
if (sub == NULL) {
sub = (struct google_protobuf_GeneratedCodeInfo*)_upb_Message_New(google_protobuf_GeneratedCodeInfo_msg_init(), arena);
if (sub) google_protobuf_compiler_CodeGeneratorResponse_File_set_generated_code_info(msg, sub);
}
return sub;
}
extern const upb_MiniTableFile google_protobuf_compiler_plugin_proto_upb_file_layout;
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* GOOGLE_PROTOBUF_COMPILER_PLUGIN_PROTO_UPB_H_ */
Loading…
Cancel
Save