|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "upb/hash/int_table.h"
|
|
|
|
#include "upb/hash/str_table.h"
|
|
|
|
#include "upb/reflection/def_builder_internal.h"
|
|
|
|
#include "upb/reflection/def_pool_internal.h"
|
|
|
|
#include "upb/reflection/def_type.h"
|
|
|
|
#include "upb/reflection/enum_def_internal.h"
|
|
|
|
#include "upb/reflection/enum_value_def_internal.h"
|
|
|
|
#include "upb/reflection/field_def_internal.h"
|
|
|
|
#include "upb/reflection/file_def_internal.h"
|
|
|
|
#include "upb/reflection/message_def_internal.h"
|
|
|
|
#include "upb/reflection/service_def_internal.h"
|
|
|
|
|
|
|
|
// Must be last.
|
|
|
|
#include "upb/port/def.inc"
|
|
|
|
|
|
|
|
struct upb_DefPool {
|
|
|
|
upb_Arena* arena;
|
|
|
|
upb_strtable syms; // full_name -> packed def ptr
|
|
|
|
upb_strtable files; // file_name -> (upb_FileDef*)
|
|
|
|
upb_inttable exts; // (upb_MiniTableExtension*) -> (upb_FieldDef*)
|
|
|
|
upb_ExtensionRegistry* extreg;
|
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
2 years ago
|
|
|
upb_MiniTablePlatform platform;
|
|
|
|
void* scratch_data;
|
|
|
|
size_t scratch_size;
|
|
|
|
size_t bytes_loaded;
|
|
|
|
};
|
|
|
|
|
|
|
|
void upb_DefPool_Free(upb_DefPool* s) {
|
|
|
|
upb_Arena_Free(s->arena);
|
|
|
|
upb_gfree(s->scratch_data);
|
|
|
|
upb_gfree(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_DefPool* upb_DefPool_New(void) {
|
|
|
|
upb_DefPool* s = upb_gmalloc(sizeof(*s));
|
|
|
|
if (!s) return NULL;
|
|
|
|
|
|
|
|
s->arena = upb_Arena_New();
|
|
|
|
s->bytes_loaded = 0;
|
|
|
|
|
|
|
|
s->scratch_size = 240;
|
|
|
|
s->scratch_data = upb_gmalloc(s->scratch_size);
|
|
|
|
if (!s->scratch_data) goto err;
|
|
|
|
|
|
|
|
if (!upb_strtable_init(&s->syms, 32, s->arena)) goto err;
|
|
|
|
if (!upb_strtable_init(&s->files, 4, s->arena)) goto err;
|
|
|
|
if (!upb_inttable_init(&s->exts, s->arena)) goto err;
|
|
|
|
|
|
|
|
s->extreg = upb_ExtensionRegistry_New(s->arena);
|
|
|
|
if (!s->extreg) goto err;
|
|
|
|
|
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
2 years ago
|
|
|
s->platform = kUpb_MiniTablePlatform_Native;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
err:
|
|
|
|
upb_DefPool_Free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_DefPool_InsertExt(upb_DefPool* s, const upb_MiniTableExtension* ext,
|
|
|
|
const upb_FieldDef* f) {
|
|
|
|
return upb_inttable_insert(&s->exts, (uintptr_t)ext, upb_value_constptr(f),
|
|
|
|
s->arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_DefPool_InsertSym(upb_DefPool* s, upb_StringView sym, upb_value v,
|
|
|
|
upb_Status* status) {
|
|
|
|
// TODO: table should support an operation "tryinsert" to avoid the double
|
|
|
|
// lookup.
|
|
|
|
if (upb_strtable_lookup2(&s->syms, sym.data, sym.size, NULL)) {
|
|
|
|
upb_Status_SetErrorFormat(status, "duplicate symbol '%s'", sym.data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!upb_strtable_insert(&s->syms, sym.data, sym.size, v, s->arena)) {
|
|
|
|
upb_Status_SetErrorMessage(status, "out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const void* _upb_DefPool_Unpack(const upb_DefPool* s, const char* sym,
|
|
|
|
size_t size, upb_deftype_t type) {
|
|
|
|
upb_value v;
|
|
|
|
return upb_strtable_lookup2(&s->syms, sym, size, &v)
|
|
|
|
? _upb_DefType_Unpack(v, type)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_DefPool_LookupSym(const upb_DefPool* s, const char* sym, size_t size,
|
|
|
|
upb_value* v) {
|
|
|
|
return upb_strtable_lookup2(&s->syms, sym, size, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_ExtensionRegistry* _upb_DefPool_ExtReg(const upb_DefPool* s) {
|
|
|
|
return s->extreg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void** _upb_DefPool_ScratchData(const upb_DefPool* s) {
|
|
|
|
return (void**)&s->scratch_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t* _upb_DefPool_ScratchSize(const upb_DefPool* s) {
|
|
|
|
return (size_t*)&s->scratch_size;
|
|
|
|
}
|
|
|
|
|
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
2 years ago
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_MessageDef* upb_DefPool_FindMessageByNameWithSize(
|
|
|
|
const upb_DefPool* s, const char* sym, size_t len) {
|
|
|
|
return _upb_DefPool_Unpack(s, sym, len, UPB_DEFTYPE_MSG);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_EnumDef* upb_DefPool_FindEnumByName(const upb_DefPool* s,
|
|
|
|
const char* sym) {
|
|
|
|
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_ENUM);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_EnumValueDef* upb_DefPool_FindEnumByNameval(const upb_DefPool* s,
|
|
|
|
const char* sym) {
|
|
|
|
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_ENUMVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FileDef* upb_DefPool_FindFileByName(const upb_DefPool* s,
|
|
|
|
const char* name) {
|
|
|
|
upb_value v;
|
|
|
|
return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FileDef* upb_DefPool_FindFileByNameWithSize(const upb_DefPool* s,
|
|
|
|
const char* name,
|
|
|
|
size_t len) {
|
|
|
|
upb_value v;
|
|
|
|
return upb_strtable_lookup2(&s->files, name, len, &v)
|
|
|
|
? upb_value_getconstptr(v)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FieldDef* upb_DefPool_FindExtensionByNameWithSize(
|
|
|
|
const upb_DefPool* s, const char* name, size_t size) {
|
|
|
|
upb_value v;
|
|
|
|
if (!upb_strtable_lookup2(&s->syms, name, size, &v)) return NULL;
|
|
|
|
|
|
|
|
switch (_upb_DefType_Type(v)) {
|
|
|
|
case UPB_DEFTYPE_FIELD:
|
|
|
|
return _upb_DefType_Unpack(v, UPB_DEFTYPE_FIELD);
|
|
|
|
case UPB_DEFTYPE_MSG: {
|
|
|
|
const upb_MessageDef* m = _upb_DefType_Unpack(v, UPB_DEFTYPE_MSG);
|
|
|
|
return _upb_MessageDef_InMessageSet(m)
|
|
|
|
? upb_MessageDef_NestedExtension(m, 0)
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FieldDef* upb_DefPool_FindExtensionByName(const upb_DefPool* s,
|
|
|
|
const char* sym) {
|
|
|
|
return upb_DefPool_FindExtensionByNameWithSize(s, sym, strlen(sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_ServiceDef* upb_DefPool_FindServiceByName(const upb_DefPool* s,
|
|
|
|
const char* name) {
|
|
|
|
return _upb_DefPool_Unpack(s, name, strlen(name), UPB_DEFTYPE_SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize(
|
|
|
|
const upb_DefPool* s, const char* name, size_t size) {
|
|
|
|
return _upb_DefPool_Unpack(s, name, size, UPB_DEFTYPE_SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s,
|
|
|
|
const char* name) {
|
|
|
|
upb_value v;
|
|
|
|
// TODO(haberman): non-extension fields and oneofs.
|
|
|
|
if (upb_strtable_lookup(&s->syms, name, &v)) {
|
|
|
|
switch (_upb_DefType_Type(v)) {
|
|
|
|
case UPB_DEFTYPE_EXT: {
|
|
|
|
const upb_FieldDef* f = _upb_DefType_Unpack(v, UPB_DEFTYPE_EXT);
|
|
|
|
return upb_FieldDef_File(f);
|
|
|
|
}
|
|
|
|
case UPB_DEFTYPE_MSG: {
|
|
|
|
const upb_MessageDef* m = _upb_DefType_Unpack(v, UPB_DEFTYPE_MSG);
|
|
|
|
return upb_MessageDef_File(m);
|
|
|
|
}
|
|
|
|
case UPB_DEFTYPE_ENUM: {
|
|
|
|
const upb_EnumDef* e = _upb_DefType_Unpack(v, UPB_DEFTYPE_ENUM);
|
|
|
|
return upb_EnumDef_File(e);
|
|
|
|
}
|
|
|
|
case UPB_DEFTYPE_ENUMVAL: {
|
|
|
|
const upb_EnumValueDef* ev =
|
|
|
|
_upb_DefType_Unpack(v, UPB_DEFTYPE_ENUMVAL);
|
|
|
|
return upb_EnumDef_File(upb_EnumValueDef_Enum(ev));
|
|
|
|
}
|
|
|
|
case UPB_DEFTYPE_SERVICE: {
|
|
|
|
const upb_ServiceDef* service =
|
|
|
|
_upb_DefType_Unpack(v, UPB_DEFTYPE_SERVICE);
|
|
|
|
return upb_ServiceDef_File(service);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UPB_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* last_dot = strrchr(name, '.');
|
|
|
|
if (last_dot) {
|
|
|
|
const upb_MessageDef* parent =
|
|
|
|
upb_DefPool_FindMessageByNameWithSize(s, name, last_dot - name);
|
|
|
|
if (parent) {
|
|
|
|
const char* shortname = last_dot + 1;
|
|
|
|
if (upb_MessageDef_FindByNameWithSize(parent, shortname,
|
|
|
|
strlen(shortname), NULL, NULL)) {
|
|
|
|
return upb_MessageDef_File(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_filedef(upb_DefPool* s, upb_FileDef* file) {
|
|
|
|
intptr_t iter = UPB_INTTABLE_BEGIN;
|
|
|
|
upb_StringView key;
|
|
|
|
upb_value val;
|
|
|
|
while (upb_strtable_next2(&s->syms, &key, &val, &iter)) {
|
|
|
|
const upb_FileDef* f;
|
|
|
|
switch (_upb_DefType_Type(val)) {
|
|
|
|
case UPB_DEFTYPE_EXT:
|
|
|
|
f = upb_FieldDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_EXT));
|
|
|
|
break;
|
|
|
|
case UPB_DEFTYPE_MSG:
|
|
|
|
f = upb_MessageDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_MSG));
|
|
|
|
break;
|
|
|
|
case UPB_DEFTYPE_ENUM:
|
|
|
|
f = upb_EnumDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_ENUM));
|
|
|
|
break;
|
|
|
|
case UPB_DEFTYPE_ENUMVAL:
|
|
|
|
f = upb_EnumDef_File(upb_EnumValueDef_Enum(
|
|
|
|
_upb_DefType_Unpack(val, UPB_DEFTYPE_ENUMVAL)));
|
|
|
|
break;
|
|
|
|
case UPB_DEFTYPE_SERVICE:
|
|
|
|
f = upb_ServiceDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_SERVICE));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UPB_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f == file) upb_strtable_removeiter(&s->syms, &iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const upb_FileDef* upb_DefBuilder_AddFileToPool(
|
|
|
|
upb_DefBuilder* const builder, upb_DefPool* const s,
|
|
|
|
const UPB_DESC(FileDescriptorProto) * const file_proto,
|
|
|
|
const upb_StringView name, upb_Status* const status) {
|
|
|
|
if (UPB_SETJMP(builder->err) != 0) {
|
|
|
|
UPB_ASSERT(!upb_Status_IsOk(status));
|
|
|
|
if (builder->file) {
|
|
|
|
remove_filedef(s, builder->file);
|
|
|
|
builder->file = NULL;
|
|
|
|
}
|
|
|
|
} else if (!builder->arena || !builder->tmp_arena) {
|
|
|
|
_upb_DefBuilder_OomErr(builder);
|
|
|
|
} else {
|
|
|
|
_upb_FileDef_Create(builder, file_proto);
|
|
|
|
upb_strtable_insert(&s->files, name.data, name.size,
|
|
|
|
upb_value_constptr(builder->file), builder->arena);
|
|
|
|
UPB_ASSERT(upb_Status_IsOk(status));
|
|
|
|
upb_Arena_Fuse(s->arena, builder->arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (builder->arena) upb_Arena_Free(builder->arena);
|
|
|
|
if (builder->tmp_arena) upb_Arena_Free(builder->tmp_arena);
|
|
|
|
return builder->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const upb_FileDef* _upb_DefPool_AddFile(
|
|
|
|
upb_DefPool* s, const UPB_DESC(FileDescriptorProto) * file_proto,
|
|
|
|
const upb_MiniTableFile* layout, upb_Status* status) {
|
|
|
|
const upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
|
|
|
|
|
|
|
|
// Determine whether we already know about this file.
|
|
|
|
{
|
|
|
|
upb_value v;
|
|
|
|
if (upb_strtable_lookup2(&s->files, name.data, name.size, &v)) {
|
|
|
|
upb_Status_SetErrorFormat(status,
|
|
|
|
"duplicate file name " UPB_STRINGVIEW_FORMAT,
|
|
|
|
UPB_STRINGVIEW_ARGS(name));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_DefBuilder ctx = {
|
|
|
|
.symtab = s,
|
|
|
|
.layout = layout,
|
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
2 years ago
|
|
|
.platform = s->platform,
|
|
|
|
.msg_count = 0,
|
|
|
|
.enum_count = 0,
|
|
|
|
.ext_count = 0,
|
|
|
|
.status = status,
|
|
|
|
.file = NULL,
|
|
|
|
.arena = upb_Arena_New(),
|
|
|
|
.tmp_arena = upb_Arena_New(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return upb_DefBuilder_AddFileToPool(&ctx, s, file_proto, name, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FileDef* upb_DefPool_AddFile(upb_DefPool* s,
|
|
|
|
const UPB_DESC(FileDescriptorProto) *
|
|
|
|
file_proto,
|
|
|
|
upb_Status* status) {
|
|
|
|
return _upb_DefPool_AddFile(s, file_proto, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_DefPool_LoadDefInitEx(upb_DefPool* s, const _upb_DefPool_Init* init,
|
|
|
|
bool rebuild_minitable) {
|
|
|
|
/* Since this function should never fail (it would indicate a bug in upb) we
|
|
|
|
* print errors to stderr instead of returning error status to the user. */
|
|
|
|
_upb_DefPool_Init** deps = init->deps;
|
|
|
|
UPB_DESC(FileDescriptorProto) * file;
|
|
|
|
upb_Arena* arena;
|
|
|
|
upb_Status status;
|
|
|
|
|
|
|
|
upb_Status_Clear(&status);
|
|
|
|
|
|
|
|
if (upb_DefPool_FindFileByName(s, init->filename)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
arena = upb_Arena_New();
|
|
|
|
|
|
|
|
for (; *deps; deps++) {
|
|
|
|
if (!_upb_DefPool_LoadDefInitEx(s, *deps, rebuild_minitable)) goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = UPB_DESC(FileDescriptorProto_parse_ex)(
|
|
|
|
init->descriptor.data, init->descriptor.size, NULL,
|
|
|
|
kUpb_DecodeOption_AliasString, arena);
|
|
|
|
s->bytes_loaded += init->descriptor.size;
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
upb_Status_SetErrorFormat(
|
|
|
|
&status,
|
|
|
|
"Failed to parse compiled-in descriptor for file '%s'. This should "
|
|
|
|
"never happen.",
|
|
|
|
init->filename);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_MiniTableFile* mt = rebuild_minitable ? NULL : init->layout;
|
|
|
|
if (!_upb_DefPool_AddFile(s, file, mt, &status)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_Arena_Free(arena);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
err:
|
|
|
|
fprintf(stderr,
|
|
|
|
"Error loading compiled-in descriptor for file '%s' (this should "
|
|
|
|
"never happen): %s\n",
|
|
|
|
init->filename, upb_Status_ErrorMessage(&status));
|
|
|
|
upb_Arena_Free(arena);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t _upb_DefPool_BytesLoaded(const upb_DefPool* s) {
|
|
|
|
return s->bytes_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
upb_Arena* _upb_DefPool_Arena(const upb_DefPool* s) { return s->arena; }
|
|
|
|
|
|
|
|
const upb_FieldDef* upb_DefPool_FindExtensionByMiniTable(
|
|
|
|
const upb_DefPool* s, const upb_MiniTableExtension* ext) {
|
|
|
|
upb_value v;
|
|
|
|
bool ok = upb_inttable_lookup(&s->exts, (uintptr_t)ext, &v);
|
|
|
|
UPB_ASSERT(ok);
|
|
|
|
return upb_value_getconstptr(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s,
|
|
|
|
const upb_MessageDef* m,
|
|
|
|
int32_t fieldnum) {
|
|
|
|
const upb_MiniTable* t = upb_MessageDef_MiniTable(m);
|
|
|
|
const upb_MiniTableExtension* ext =
|
|
|
|
upb_ExtensionRegistry_Lookup(s->extreg, t, fieldnum);
|
|
|
|
return ext ? upb_DefPool_FindExtensionByMiniTable(s, ext) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_ExtensionRegistry* upb_DefPool_ExtensionRegistry(
|
|
|
|
const upb_DefPool* s) {
|
|
|
|
return s->extreg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s,
|
|
|
|
const upb_MessageDef* m,
|
|
|
|
size_t* count) {
|
|
|
|
size_t n = 0;
|
|
|
|
intptr_t iter = UPB_INTTABLE_BEGIN;
|
|
|
|
uintptr_t key;
|
|
|
|
upb_value val;
|
|
|
|
// This is O(all exts) instead of O(exts for m). If we need this to be
|
|
|
|
// efficient we may need to make extreg into a two-level table, or have a
|
|
|
|
// second per-message index.
|
|
|
|
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {
|
|
|
|
const upb_FieldDef* f = upb_value_getconstptr(val);
|
|
|
|
if (upb_FieldDef_ContainingType(f) == m) n++;
|
|
|
|
}
|
|
|
|
const upb_FieldDef** exts = malloc(n * sizeof(*exts));
|
|
|
|
iter = UPB_INTTABLE_BEGIN;
|
|
|
|
size_t i = 0;
|
|
|
|
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {
|
|
|
|
const upb_FieldDef* f = upb_value_getconstptr(val);
|
|
|
|
if (upb_FieldDef_ContainingType(f) == m) exts[i++] = f;
|
|
|
|
}
|
|
|
|
*count = n;
|
|
|
|
return exts;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _upb_DefPool_LoadDefInit(upb_DefPool* s, const _upb_DefPool_Init* init) {
|
|
|
|
return _upb_DefPool_LoadDefInitEx(s, init, false);
|
|
|
|
}
|